Date post: | 10-Nov-2015 |
Category: |
Documents |
Upload: | vishal-gudla-nagraj |
View: | 355 times |
Download: | 57 times |
8051 PROGRAMMING IN C
C V.S. Assembly language
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
C has become the standard for embedded system programming Object oriented language (C++, Java, C#) are usually not as efficient as C C is flexible One of the main concerns in C programming for embedded system is to generate a hex file that is small in size We need to pay attention to the size of variables during programming
DATA TYPE Following different Data typesunsigned 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 8-bit. Most popular data type, matches most registers. Represent ASCII code or integers in the range of 0 ~ 255. Example: write a C program to send the ASCII code of 0, 1, 2, A, B, C to port 1 #include // the definition of registersvoid main (void){unsigned char mynum[ ]= {1,2,3,'a','b','c'};unsigned char z;for (z = 0; z
signed char An 8-bit number with the most significant bit representing sign (+ or -) Range: -128 ~ 127 Example: write a C program to send -3 to 3 to port 1#include void main(void){char mynum[] = {-3,-2,-1,0,1,2,3}; // signed charunsigned char z;for (z = 0; z
Signed integerSigned 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 automaticallysfr 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 calculationFor the same C code, different compilers usually will generate different assembly codes Thus the same C codes with different compilers might generate different delaysThe only way to know the exact delay is through oscilloscope.
I/O Programming :-Byte size 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
Bit addressable I/O programmingExample: write a program to monitor bit P1.5. If it is high, send 55H to P0;otherwise send AAH to P2
SFR RegistserExample: read P0, send the result to P1; read in the value of P2.6
Logic operationsLogic operatorsAnd :- &&Or :- ||Not :- ! Example: if (var1 < 3 && var2 == 1)if (!(var > 5))
Bitwise logic operators
- Bit-by-bit logical operations: & (and), | (or), ^ (xor), ~ (not) Shifting: > (shift to right)
Example #include void main(void){P0=0x35 & 0x0F;P1=0x04 | 0x68;P2=0x04 ^ 0x78;P3=~0x55;P0 = 0x35
write a C program to bring in a byte of data serially one bit at a timevia P1.0. LSB should come first #include sbit P1b0 = P1^0;sbin ACCMSB = ACC^7;void main(void){unsigned char x;for (x=0; x> 1;}}
Data Conversion Example: Write a C program to convert packed BCD 0x29 to ASCII, send the result to P1 and P2#include void main(void){unsigned char x, y, z;unsigned char packedBCD = 0x29;x = packedBCD & 0x0F; // extract low nibbleP1 = x | 0x30; // unpacked BCDASCIIx = packedBCD & 0xF0; //extract high nibbley = x>>4; // shift it to low nibbleP2 = y | 0x30; //unpacked BCD ASCII}
Timer program in c Write a program to toggle P1.5 every 250 ms. Use timer 0, mode 2 (8-bit auto reload)#include 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 2TH0 = -23; // count 23 times,overflow.23*1.085=25 usTR0 = 1; //start timerwhile (TF0 = = 0); //wait till overflow not occursTR0 = 0;TF0=0;}
Timer -counter#include void main( ){T0 = 1; //(make T0 an input)TMOD = 0x05; // 0000 0101 (C/T = 1, mode 1)TL0 = 0;TH0 = 0; //clear counterswhile(1){do{TR0=1; //start timerP1 = TL0;P2 = TH0;}while(TF0 = = 0);TR0 = 0; //stop timerTF0 = 0; //clear TF}}
Assume a 1 Hz external clock is being fed into pin T0 (P3.4). Write a C program for counter T0 in mode 1 (16-bit) to display TH0 and TL0 on P2 and P1, respectively.
Write an 8051 C program to receive a byte of data from serial port 0, then send it back to serial port 0. Do this continuously.
#include void SerTx(unsigned char);void SerRx(unsigned char *);void main(void){char byteBuf;TMOD = 0x20; // timer 1, 8-bit auto-reloadTH1 = 0XFD; // or: TH1 = -3, 9600 baudSCON = 0x50;TR1 = 1; // start timerwhile(1){SerRx(&byteBuf); // read byte from serial portSerTx(byteBuf); // send byte back to serial port}}
void SerTx(unsigned char x){SBUF = x; // put the char in SBUF registerwhile(TI = =0); // wait until transmittedTI = 0;}void SerRx(unsigned char * pX){while(RI = =0); // wait until receivedRI = 0;*pX = SBUF; // copy the data in SBUF to (pX)}