This tutorial explains how to read the content of the microcontroller’s flash memory. The source microcontroller reads the content of the memory and displays it on the LEDs. The content is nothing but the program written in the memory of microcontroller. This step is often used to verify whether microcontroller has been correctly programmed or not. If the values which are read from a memory location are same as the one which needs to be written at this particular memory location, then the microcontroller has been programmed correctly. If there is any mismatch, it means microcontroller has not been programmed correctly.
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 two modes to read the values of a microcontroller. The table below shows the instruction corresponding to memory read operation.
Fig. 1: Table listing 8051 instructions for memory read operation
1. Byte Mode
In this mode the values are read one by one. You need to send the address of the memory location in every instruction whose value is to be fetched. The microcontroller in return sends the value which is stored on this location. The first byte of the instruction signifies memory read operation (byte mode). The second and third byte tells the address of the memory location. Some bits of the second byte are don’t care. This is because the maximum size of flash memory for this family is 8K and in order to address the last memory location a maximum of 13 bits is required. The target controller returns the value corresponding to the address during the fourth byte.
2. Page mode
The memory of the microcontroller is arranged in form of pages. Every page contains 256 bytes arranged in 16 rows and 16 columns. In this mode, a chunk of 256 bytes is read in one read instruction. The second byte of the instruction sends the address of the page to be read. The target microcontroller in return sends 256 consecutive bytes just after the second byte of the instruction. No instruction is accepted till 265 data bytes are read and if instruction is read it will be considered as data.
Circuit and Algorithm
Circuit Diagram
Fig. 2: Circuit Diagram of 8051 Programmer
Algorithm
Algorithm for Byte mode:
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 chip is enabled.
4. If chip is enabled then send the memory read instruction (byte mode) specifying the address of memory location whose value you want to read.
5. The required value is returned during the transmission of the fourth byte on the MISO pin.
6. Keep repeating step 5 till all the bytes are read from the target microcontroller.
Algorithm for Page read mode:
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 chip is enabled.
4. If chip is enabled then send first byte to enable page read mode followed by the address bytes.
5. After the address bytes the 256 data bytes will be received on MISO pin.
6. Keep repeating step 5 till all the pages you want to write are written.
Code
Code
The following code is used to read the bytes from the memory of the microcontroller and the output is displayed on LEDs. This code reads four bytes using the byte mode.
#include <REG51.h>
#include<intrins.h>
#define port P1
#define p2 P2
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,output[13], count;
void delay(unsigned int msec) // Function for delay
{
int i,j;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void read()
{
bit1=miso;
output[count]=output[count]<<1;
if(bit1==1)
output[count]++;
}
void sread()
{
count++;
output[count]=0x00;
for(bitno=0;bitno<8;bitno++)
{
_nop_();
delay(1);
_nop_();
sck=1;
delay(1);
_nop_();
read();
sck=0;
}
}
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;
p2=0;
count=-1;
delay(50);
//=======Program enable===========
a=0xac;
b=0x53;
c=0x00;
d=0x11;
sendbyte(a);
sendbyte(b);
sendbyte(c);
sendbyte(d);
//==========Read===================
sendbyte(0x20);
sendbyte(0x00);
sendbyte(0x00);
sread();
sendbyte(0x20);
sendbyte(0x00);
sendbyte(0x01);
sread();
sendbyte(0x20);
sendbyte(0x00);
sendbyte(0x02);
sread();
sendbyte(0x20);
sendbyte(0x00);
sendbyte(0x03);
sread();
//========Display=============
for(i=0;i<4;i++) ;
p2=output[i];
p2=0xff;
res=1;
while(1);
}
Filed Under: Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.