Widely used data types for 8051 C
Data Type
|
Size in Bits
|
Data range/Usage
|
Unsigned char
|
8 – bit
|
0 – 255
|
Signed char
|
8 – bit
|
-128 - +127
|
Unsigned int
|
16 – bit
|
0 – 65535
|
Signed int
|
16 – bit
|
-32768 - +32768
|
sbit
|
1 – bit
|
SFR bit addressable only
|
bit
|
1 – bit
|
RAM bit addressable only
|
sfr
|
8 – bit
|
RAM addressable 80 – FF only
|
Some program examples using above data types.
#include<reg51.h>
/*
* This function
* SEND VALUES 00 - FF TO PORT P1
*
*/
void main()
{
usigned
char z;
for(z
= 0; z<=255; z++)
P1
= z;
}//end of program
|
#include<reg51.h>
/*
* This function
* SENDS ASCII CHARS A,B,C,D,1,2,3 TO PORT 1
*
*/
void main()
{
unsigned
char ascii[] = "ABCD123";
unsigned
char z;
for(z=
0; z<=7; z++)
P1
= ascii[z];
}//en of program
|
#include<reg51.h>
/*
* This function
* TOGGLES BIT D0 OF PORT 1
* P1.0
*/
sbit P10bit = P1^0;
void main()
{
unsigned
int z;
for(z
= 0; z<=someValue; z++)
{
P10bit
= 0;
P10bit
= 1;
}
}//end of program
|
Points to note in above examples:
1)
Try using smallest possible size of a variable
to save chip space. Like for looping I used a char variable instead of an
integer saving a whole byte of storage space.
2)
Sbit is used for sfr bit addressable only. It includes
all sfr registers including ports and register A and B too.
3)
Including reg51.h is of course must. As it
contains well in built definition for embedded type data types. This file is
different for different chip, controllers and processors.
Time delay in C
There are 2 ways to create time delay in 8051 C.
1)
Using a simple for loop.
2)
Using 8051 timers.
Point to note:
When we program in assembly language we can control exact
instructions and their sequences used in delay subroutine. In case of C
programs, it is the C compiler that converts the C statements and functions to
assembly language instruction. As a result different compliers produce
different codes.
Programs related to time delay using loop:
#include<reg51.h>
/*
* Toggle P1 with some delay in between
* on and off.
*
*/
void main()
{
unsigned
int x;
while(1)
// Repeat forever
{
P1
= 0x55;
for(x
= 0; x<80000; x++);
//some
delay. delay size unknown
P1
= 0xAA;
for(x
= 0; x<80000; x++);
}//end
of while loop
}// end of main
//end of program
|
#include<reg51.h>
/*
* Create a delay of 250 ms
*
*/
void msdelay(unsigned int x)
{
unsigned
int i,j; //loop variables
for(i
= 0; i<x; i++)
for(j
= 0;j<1275; j++);
}//end of msdelay()
void main()
{
while(1)//Repeat
foreever
{
P1
= 0x55;
msdelay(250);
P1
= 0xAA;
msdelay(250);
}//end
of while
}//end of main
//end of program
|
Points to note in loop delay:
1)
Though not accurate method for time delay. Loop method
does come handy for display programming. For different chips its need to be
tested with an oscilloscope. Value of 1275 works well enough for a millisecond
delay. So all delay will be created using value 1275.
2)
Though I will mostly use loop delay. But it is
not considered a good practice to use loop delay. It is easy to understand. So I
will say try using timers. As it will be shown in coming posts.
Really your content is so informative. So please share some more content ..
ReplyDeleteMicrocontroller course in Noida