Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

AT89S51/52 ISP Programmer – Lock Bits Setting

By Himanshu Choudhary December 27, 2011

Lock bits are set of bits to enable or disable some special security features of a microcontroller. For example, in some cases you might want to disable the memory read functionality of the microcontroller, so that the code you have written cannot be stolen by others. The number of lock bits and their functionality is always given in the datasheet. The controller used here has three lock bits which can be set or reset depending on the feature.
 
The reader should know the basics of sending and receiving the single byte in programming mode. Refer to 8051 Programmer basics before reading this tutorial.
 
There are 3 lock bits in the controller 89S51 named as LB1, LB2 and LB3 respectively. The values of each of the lock bit can be either 0 or 1. If it is 0, it is said to be unprogrammed (U) and if it is 1, it is said to be programmed (P). The following table shows the results of programming and unprogramming the different lock bits.
 
ProgramLock Bits
Protection Type
 
LB1
LB2
LB3
1
U
U
U
 
No program lock features
 
 
2
 
 
P
 
 
U
 
 
U
MOVC instructions executed from external program memory are disabled from fetching code bytes from internal memory, EA is sampled and latched on reset, and further programming of the Flash memory is disabled
3
P
P
U
Same asmode 2, but verify is also disabled
4
P
P
P
Same asmode 3, but external execution isalso disabled
 
When the lock bit 1 is programmed the memory reading feature of the controller is disabled, i.e., the memory cannot be read. In case an attempt is made to read the memory after setting the lock bit 1, the buffer will always show the value FF in every memory location. However the microcontroller will carry out its normal operations.
 

Instructions to set lock bits

Table listing instructions to set lock bits for 8051 Programmer

Fig. 1: Table listing instructions to set lock bits for 8051 Programmer

The table above shows the instruction corresponding to setting the lock bits. We can either write the lock bits or read the status of lock bits. Reading lock bits is quite simple you have to send the instruction corresponding to the reading lock bit and the fourth byte gives the status of lock bits.
 
Setting the lock bit is bit tricky. The two bits B1 and B2 in the second byte of the instruction corresponding to write lock bit are used to program or unprogram the lock bits B1 B2 B3.
 
Image showing modes of locking bit for 8051 Programmer

Fig. 2: Image showing modes of locking bit for 8051 Programmer

The above block shows the bit pattern to program or unprogram the lock bits. The important thing to note here is that at one point of time only one lock bit can be programmed. Also the programming needs to be done sequentially i.e., in order to program the lock bit 3, the lock bit 1 and 2 must be programmed first and hence instruction corresponding to mode1, mode 2 and mode 3 needs to be sent before mode 4.
 

Circuit and Algorithm

Circuit Diagram

Circuit Diagram of 8051 Programmer

Fig. 3: Circuit Diagram of 8051 Programmer
Algorithm
Algorithm to write lock bits:
1.      Power on the circuit.
2.      Send the instruction for programming enable.
3.      Check the 4th byte on MISO pin. If we receive 0x69 this means serial programming mode is enabled.
4.      If the serial programming is enabled then send the instruction for setting lock bits to mode1.
5.      Send instruction for setting lock bits to mode2.
6.      Send instruction for setting lock bits to mode3.
7.      Send instruction for setting lock bits to mode4.
 
Lock bit read Algorithm
1.      Power on the circuit.
2.      Send the instruction for programming enable.
3.      Check the 4th byte on MISO pin. If we receive 0x69 this means serial programming mode is enabled.
4.      If the serial programming is enabled then send the instruction for reading the status of lock bits.
 

Code

Code
The following program is used to set the lock bits of microcontroller.

#include <REG51.h>
#include<intrins.h>
#define port P1

sbit sck=port^0;
sbit res=port^3;
sbit miso=port^1;
sbit mosi=port^2;

bit bit1;
unsigned int i,bitno=0;
unsigned char a,b,c,d;
void delay(unsigned int msec)   // Function for delay
{
	int i,j;
	for(i=0;i<msec;i++)
		for(j=0;j<1275;j++);
}

void sendbit()
{
	mosi=bit1;
	delay(1);
	_nop_();
	sck=1;
	delay(1);
	_nop_();
	sck=0;
}

void sendbyte(unsigned char m)
{
	for(bitno=0;bitno<8;bitno++)
	{
		bit1=m/128;
		m=m<<1;
		sendbit();
	}
}

void main()
{
	i=0;
	sck=0;
	res=1;
	delay(500);

//=======Program enable===========

	a=0xac;
	b=0x53;
	c=0x00;
	d=0x11;
	sendbyte(a);
	sendbyte(b);
	sendbyte(c);
	sendbyte(d);

//===========Lockbit==============

	sendbyte(0xac);
	sendbyte(0xE0);
	sendbyte(0x00);
	sendbyte(0x00);

	sendbyte(0xac);
	sendbyte(0xE1);
	sendbyte(0x00);
	sendbyte(0x00);

	sendbyte(0xac);
	sendbyte(0xE2);
	sendbyte(0x00);
	sendbyte(0x00);

	sendbyte(0xac);
	sendbyte(0xE3);
	sendbyte(0x00);
	sendbyte(0x00);

	res=1;
	while(1);
}


Filed Under: Tutorials
Tagged With: 8051, isp, lock bit setting circuit & code, microcontroller
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on Electro-Tech-Online.com and EDAboard.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Identification of a 6 pin smd chip (sto-23-6) marked E2
  • Dynacord enter protect
  • IGBTs without negative gate drive
  • Need suggestions in task NI6363 retrigger (analog trigger)
  • Monte-Carlo simulation error on ADE-XL

RSS Electro-Tech-Online.com Discussions

  • Fun with AI and swordfish basic
  • using a RTC in SF basic
  • Does US electric code allow branching ?
  • Faulty heat air gun (dc motor) - problem to locate fault due to Intermittent fault
  • Sump pit water alarm - Kicad 9

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • How IoT network topologies work
  • The top five AI startups to watch in 2025
  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe