8051 Programming in C
8051 Programming in C
Advantage of C
Its easier to program in C compared to assembly.
C code can be easily ported to other microcontroller, while
assembly language can usually be used for 1 type of
microcontroller
There are lots of function libraries written in C
Advantage of assembly language
The hex file generated by assembly is usually smaller
Code efficiency is higher (faster)
C programming
DATA TYPE
Following different Data types
unsigned char (8 bit :- Range 0-255)
signed char(8 bit:-Range -128 to +127)
unsigned int (16 bit Range= 0 to 65565)
signed int, (16 bit Range -32768 to to 32767)
sbit, ( 1bit)
bit ( 1 bit :-assigned address in bit addressable ram
space (20 to 2f)h)
sfr ( access special function registers in 51)
Time delay
Two methods to achieve time delay: Timer
Loop
When using loops in C, its difficult to determine the
exact time delay by means of calculation
For the same C code, different compilers usually will
generate different assembly codes
Thus the same C codes with different compilers might
generate different delays
The only way to know the exact delay is through
oscilloscope.
Unsigned char
signed
char
Integer
Unsigned int
16-bit (needs two registers), range: 0 ~ 65535 (FFFFH)
E.g.: unsigned int a;
Example: write a C program to toggle all bits of P1 continuously
#include <reg51.h>
void main(void)
{
unsigned int i;
P1 = 0x00;
// turn off bit
for (i =0;i<60000 ;i++ ) // delay loop
P1 = 0xFF;
// turn on
for (i =0;i<60000 ;i++ ) // delay loop
}
Signed integer
Signed
int
16-bit, MSB represents sign.
- Range: -32768 ~ 32767
E.g.: int a;
Since int requires twice as many memory
space as char, use unsigned or signed int
only when the number cannot be
represented with unsigned or signed char.
bit
Used to access single bit of bit-addressable RAM
(20H 2FH)
Example: bit mybit = 0; // the compiler will assign a
RAM space to automatically
sfr
Used to access special function registers
example: sfr ACC = 0xE0; // the address of reg. A
Time Delay
Two methods to achieve time delay:
Loop
When using loops in C, its difficult to determine the
exact time delay by means of calculation
For the same C code, different compilers usually will
generate different assembly codes
Thus the same C codes with different compilers
might generate different delays
The only way to know the exact delay is through
oscilloscope.
Example: write a program to get a byte of data from P0. If its less
than 100,send it to P1; otherwise send it to P2
SFR Registser
Example: read P0, send the result to P1; read in the value of P2.6
Logic operations
Logic
operators
And :- &&
Or :- ||
Not :- !
Example:
#include <reg51.h>
void main(void)
{
P0=0x35 & 0x0F;
P1=0x04 | 0x68;
P2=0x04 ^ 0x78;
P3=~0x55;
P0 = 0x35 << 3;
Data Conversion
Example: Write a C program to convert packed
BCD 0x29 to ASCII, send the result to P1 and P2
#include <reg51.h>
void main(void)
{
unsigned char x, y, z;
unsigned char packedBCD = 0x29;
x = packedBCD & 0x0F; // extract low nibble
P1 = x | 0x30; // unpacked BCDASCII
x = packedBCD & 0xF0; //extract high nibble
y = x>>4; // shift it to low nibble
P2 = y | 0x30; //unpacked BCD ASCII
}
Accessing ROM in C
To require the compiler store data in ROM, use the code keyword .
Without code keyword, all the data will be stored in RAM
The compiler will automatically allocate ROM space for the variables.
Example
#include <reg51.h>
void main(void)
{
code unsigned char mynum[] = ABCDEF;
unsigned char z;
for (z = 0; z<6; z++)
P1 = mynum[z];
}
Timer program in c
Write a program to toggle P1.5 every 250 ms. Use timer 0, mode 2 (8-bit auto
reload)
#include <reg51.h>
void T0M2delay25us(void);
sbit mybit = P1^5;
void main(void)
{
unsigned int x;
while(1)
{
mybit = ~mybit;
for (x = 0; x < 10000; x++)
T0M2delay25us();
}
}
void T0M2delay25us(void)
{
TMOD = 0x02; // timer 0, mode 2
TH0 = -23; // count 23 times,overflow.23*1.085=25 us
TR0 = 1; //start timer
while (TF0 = = 0); //wait till overflow not occurs
TR0 = 0;
TF0=0;
}
Timer -counter