Tuesday, January 3, 2012

The data serialization using 8051 C.

There are 2 ways to transfer a byte serially.
1)      Using serial port.
2)      To transfer data 1 bit at a time and control the sequence of data and spaces in between them.

The data serialization is very easy in C. seriously.
#include<reg51.h>
/*
 * send some data through port
 * say P2.1
 * lsb should go first
 *
 */
sbit p21 = P2^1;
sbit lsbA = ACC^0;

void main()
{
                unsigned char sendMePleasebyte = 0x55h;
                unsigned char i;
                ACC = sendMePleasebyte;
                for(i = 0;i<8;i++)
                {
                                p21 = lsbA;
                                ACC = ACC<<1;
                }//end of for loop
}// end of main

#include<reg51.h>
/*
 * recieve some data through port
 * say P2.1
 * lsb should come first
 *
 */
sbit p21 = P2^1;
sbit lsbA = ACC^7;
void main()
{
                unsigned char i;
                ACC = sendMePleasebyte;
                for(i = 0;i<8;i++)
                {
                                lsbA = p21;
                                ACC = ACC>>1;
                }//end of for loop
}// end of main

The data space.

In 8051 there are 3 spaces in which one can store data.

1)      The 128 bytes of RAM space with address 00 to 7Fh. We can read and write directly or indirectly into this space using R0 and R1 registers.
2)      The 64k bytes of code space or generally called program space with address 0000 – FFFFh. This 64k of memory is used for storing opcodes and is directly under control of program counter (PC) register. We can use MOVC command to refer it. There are 2 major problems with this usage. First being that since its ROM it can be burned only by predefined data. And we are unable to write during execution. Second problem is that more code space we use for lookup table less the space we have for our program code. For this purpose for storing lookup tables we use external memory.
3)      The 64k bytes of external memory which can be used both for RAM and ROM purpose. We access it using MOVX instruction. So this gave us a 64k on chip ROM plus a 64k off chip ROM. Increasing memory to 128k.

So how to differentiate this in c??
For that lets see an excerpt for examples.
Unsigned char xyz[] = ‘abcdefghijk’;
Unsigned char I;
Both variable xyz and I uses RAM space for storage. So imagine RAM space used when we write this..
Unsigned char xyz[100];
Int array[21];
As you must have already counted that the char xyz[100] uses 100 bytes of RAM space whereas the int array[21] uses 42 bytes of RAM space overloading our existing RAM space. So in cases like these we need to use code space.

To make C complier use code space instead of RAM space we need to use keyword code in front of the variable declaration.
code unsigned char xyz[] = “abcdef”;
code int array[12];
The keyword code helps complier recongnize where to store the data in RAM or in ROM.

Checksum Byte in ROM

To ensure integrity of contents of ROM, every system must perform checksum calculation. The process of checksum is carried out to detect any corruption in contents of ROM. ROM can get corrupted in many ways major one is the current surge during on and off procedure. The checksum process uses a byte known as checksum byte. The checksum byte is an extra byte that is tagged at the end of a series of bytes of data.
The process of checksum is as follows:
1)      Add the bytes together and drop the carries.
2)      Take the 2’s complement of the sum. This 2’s compliment is our checksum byte, which becomes last byte of the series.

Let us take an example.
Consider size of ROM being 6 bytes.  Having hex values as follows.
32h – 45h – 67h – 12h – 11h – 32h

Now we wish to find first the checksum byte.
For that purpose lets add them up.
32h + 45h + 67h + 12h + 11h + 32h = 133h
So dropping the carries we get 33h
Now 2’s compliment of 33h = CDh
So CDh is our checksum byte.

Now we wish to ensure data integrity.
For this purpose we are going to add 0Ch to previous all bytes.
32h + 45h + 67h + 12h + 11h + 32h + CDh = 200h
Dropping the carries we get 00h ensuring that the data is not corrupted.
If we get any other value except 00h it means that the data in the ROM is corrupted.

This is how we perform checksum calculation.

A program based on this method.
#include<reg51.h>
void main()
{
                unsigned char mydata[] = {0x32,0x11,0x43,0x65,0x67,0x22};
                unsigned char checksum;
                unsigned char sum=0;
                unsigned char i;
                for(i = 0; i<mydata.length(); i++)
                {
                                sum = sum + mydata[x];
                }//end of for loop
                checksum = ~sum + 1; //2's complement
                /*
                 * do whatever you want with the checksum byte
                 */
               
}// end of main
// end of program