The 8051 Microcontroller - Chapter 8: C Programming for 8051 - Lê Chí Thông

C Programming for the 8051
• C programming is less time consuming, but has
larger hex file size
• The reasons for writing programs in C
– It is easier and less time consuming to write in C than
Assembly
– C is easier to modify and update
– You can use code available in function libraries
– C code is portable to other microcontroller with little
of no modification 
pdf 50 trang thamphan 28/12/2022 3000
Bạn đang xem 20 trang mẫu của tài liệu "The 8051 Microcontroller - Chapter 8: C Programming for 8051 - Lê Chí Thông", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdfthe_8051_microcontroller_chapter_8_c_programming_for_8051_le.pdf

Nội dung text: The 8051 Microcontroller - Chapter 8: C Programming for 8051 - Lê Chí Thông

  1. ĐH Bách Khoa TP.HCM Lê Chí Thông The 8051 Microcontroller Chapter 8 C Programming for 8051 TS. Lê Chí Thông chithong@hcmut.edu.vn sites.google.com/site/chithong Ho Chi Minh City University of Technology Lê Chí Thông 1 Outline • Structure of a C program • Using Keil • Declaration of the types of memory • Variable types • Keywords in Keil C51 • Functions • Operations – Statements • Time delay • I/O programming Lê Chí Thông 2 sites.google.com/site/chithong 1
  2. ĐH Bách Khoa TP.HCM Lê Chí Thông Blinky Program //Documentatons void Delayms (unsigned int ms) { / unsigned int i; Blinky program while (ms) { Date: 05-Apr-2012 i = 115; Version: 1.0 while (i> 0) i ; / ms ; //Preprocessor Statements } #include } #define ON 1 // Main Program #define OFF 0 main (void) { // Global declarations while (1) { sbit LED1=P1^0; LED1 = ON; // Turn on LED1 // Subprograms Delayms (500); // Delay 500 ms /* Delay in milliseconds for a 11.0592 MHz crystal */ LED1 = OFF; // Turn off LED1 Delayms (500); // Delay 500 ms } source Lê Chí Thông} 5 From the C program to the machine language Source: Lê Chí Thông 6 sites.google.com/site/chithong 3
  3. ĐH Bách Khoa TP.HCM Lê Chí Thông Structure of a C program // Documentatons //Subprograms //Name of the program, programmer, Void function1 (int x) { //and other details // Statements } // Preprocessor Statements #include //header files //Main function #include void main(void) { #define TRUE 1 //symbolic constants int sum = 0; //Local Declarations #define FALSE 0 int x; float y; // Global Declarations //Statements unsigned char x,y; } int z; long n=0; Lê Chí Thông 9 Keil Product Downloads Lê Chí Thông 10 sites.google.com/site/chithong 5
  4. ĐH Bách Khoa TP.HCM Lê Chí Thông Using Keil • Click File, New • Click ‘ File, Save as ’ and chose a file name for your source code ending with the letter ‘ .c ’, ‘code.c’ for example, and click save. Source: 13 Using Keil Add file to project: • Right-click on ‘source group 1 ‘, click on ‘ Add files to group ‘, •Right-click on Target 1 , click Options for target ‘Target 1′ , then under the ‘Output ‘ tab, check the box ‘ crate HEX file ‘. Source: 14 sites.google.com/site/chithong 7
  5. ĐH Bách Khoa TP.HCM Lê Chí Thông Keil Variable Extensions • Declaration of the types of memory data internal RAM (direct addressing) idata internal RAM (indirect addressing) bdata bit addressable internal RAM xdata external data memory code code memory (program memory) Lê Chí Thông 17 data/idata Description: The variable will be stored in internal data memory of controller. Example: unsigned char data x ; //or unsigned char idata y; Lê Chí Thông 18 sites.google.com/site/chithong 9
  6. ĐH Bách Khoa TP.HCM Lê Chí Thông code Description: This keyword is used to store a constant variable in code memory . Lets say you have a big string which is not going to change anywhere in program. Wasting ram for such string will be foolish thing. So instead we will make use of the keyword "code" as shown in example below. Example: unsigned char code str ="this is a constant string" ; Lê Chí Thông 21 Variable Types • The Keil C compiler supports most C variable types and adds several of its own: – Standard Types – Keil Types • A good understanding of C data types for 8051 can help programmers to create smaller hex files Lê Chí Thông 22 sites.google.com/site/chithong 11
  7. ĐH Bách Khoa TP.HCM Lê Chí Thông bit Description: This keyword is used to define a bit from bit-addressable on-chip RAM . Example: bit = 0x80 ; // Lê Chí Thông 25 sbit Description: This keyword is used to define a special bit from SFR (special function register) memory. Example: sbit Port0_0 = 0x80 ; // Special bit with name Port0_0 is defined // at address 0x80 Lê Chí Thông 26 sites.google.com/site/chithong 13
  8. ĐH Bách Khoa TP.HCM Lê Chí Thông unsigned char – Example 1 Source: Chung-Ping Young Lê Chí Thông 29 unsigned char – Example 2 Source: Chung-Ping Young Lê Chí Thông 30 sites.google.com/site/chithong 15
  9. ĐH Bách Khoa TP.HCM Lê Chí Thông sbit, signed int – Example Source: Chung-Ping Young Lê Chí Thông 33 Keywords in Keil C51 • Keil C51 compiler adds few more keywords to the scope C Language: _at_ using interrupt small large compact far alien reentrant _priority_ _task_ Lê Chí Thông 34 sites.google.com/site/chithong 17
  10. ĐH Bách Khoa TP.HCM Lê Chí Thông interrupt Description: This keyword will tells the compiler that function described is an interrupt service routine. C51 compiler supports interrupt functions for 32 interrupts (0-31). Use the interrupt vector address in the following table to determine the interrupt number. Interrupt number Address Interrupt source 0 0003H External0 1 000BH Timer0 2 0013H External1 3 001BH Timer 1 4 0023H Serial Lê Chí Thông 37 interrupt Example: void External_Int0 () interrupt 0{ //code } Lê Chí Thông 38 sites.google.com/site/chithong 19
  11. ĐH Bách Khoa TP.HCM Lê Chí Thông An Example of Functions // Examples of functions sum4 () { #include result4 = a+b; int a,b,result2; } int result3,result4; main () { int sum (int x, int y) { a=2; int result; b=3; result = x+y; sum(a,b); return (result); sum2(a,b); } sum3(a,b); void sum2 (int x, int y) { sum4(); result2 = x+y; } } sum3 (int x, int y) { result3 = x+y; } Lê Chí Thông 41 Constant int const a=12; int const b[]={2,4,15,0,155}; unsigned char const c=5; unsigned char const c=’5’; unsigned char const x[]="ABCDEF"; Lê Chí Thông 42 sites.google.com/site/chithong 21
  12. ĐH Bách Khoa TP.HCM Lê Chí Thông Operators (3) >= Right shift assignment, x>>=y, is the same as x=x>>y >> Right shift operator -> Structure Pointer operation -= Subtraction assignment operator - Subtraction operator sizeof Determines size in bytes of operand Lê Chí Thông 46 sites.google.com/site/chithong 23
  13. ĐH Bách Khoa TP.HCM Lê Chí Thông Comparison Operations Operator Description == Equal to Smaller than, bigger than. Smaller than or equal to, bigger than or = equal to. != Not equal to Example: Lê Chí Thông 49 Logical and Shift Operations – Example 1 Source: Chung-Ping Young Lê Chí Thông 50 sites.google.com/site/chithong 25
  14. ĐH Bách Khoa TP.HCM Lê Chí Thông Statements (1) if (expr) stmt ; [else stmt ;] while (expr) stmt ; do stmt while (expr); for (expr1;expr2;expr3) stmt; switch (expr) { case cexpr 1: stmt; case expr2: stmt; [default: stmt ] } Lê Chí Thông 53 Statements (2) return [stmt ] terminates the execution of a function and returns the value to the calling function Ex: return (5); return (a+b); goto label performs an unconditional jump to the named label Ex: goto loop; label: stmt; Ex: loop: a=x+y; Lê Chí Thông 54 sites.google.com/site/chithong 27
  15. ĐH Bách Khoa TP.HCM Lê Chí Thông for for(start;condition;step){ Statement1; Statement2; } start : the start value assigned to the count value before the loop begins condition : the expression that is to remain true for the loop to continue step : the increase or decrease of the counting variable, Ex: for(i=0;i<10;i++){ P0 = i; } Lê Chí Thông 57 while while(condition){ Statement1; Statement2; } Ex: i = 0; while(i < 10){ P0 = i; i = i +1; } Lê Chí Thông 58 sites.google.com/site/chithong 29
  16. ĐH Bách Khoa TP.HCM Lê Chí Thông Time Delay • There are two ways to create a time delay in 8051 C – Using the 8051 timer – Using a simple for loop 3 factors that can affect the accuracy of the delay • The 8051 design – The number of machine cycle – The number of clock periods per machine cycle • The crystal frequency connected to the X1 – X2 input pins • Compiler choice – C compiler converts the C statements and functions to Assembly language instructions – Different compilersLê Chíproduce Thông different code 61 Time Delay – Example 1 Source: Chung-Ping Young Lê Chí Thông 62 sites.google.com/site/chithong 31
  17. ĐH Bách Khoa TP.HCM Lê Chí Thông I/O Programming – Example 2 Source: Chung-Ping Young Lê Chí Thông 65 I/O Programming – Example 3 Source: Chung-Ping Young Lê Chí Thông 66 sites.google.com/site/chithong 33
  18. ĐH Bách Khoa TP.HCM Lê Chí Thông I/O Programming – Example 6 Source: Chung-Ping Young Lê Chí Thông 69 I/O Programming – Example 7 Source: Chung-Ping Young Lê Chí Thông 70 sites.google.com/site/chithong 35
  19. ĐH Bách Khoa TP.HCM Lê Chí Thông Your turn! (switch case) Write an 8051 C program to read the P1.0 and P1.1 bits and send an ASCII character to P0 according to the following table. P1.0 P1.1 Character 0 0 ‘0’ 0 1 ‘1’ 1 0 ‘2’ 1 1 ‘3’ Lê Chí Thông 73 Solution (switch case) Write an 8051 C program to read the P1.0 and P1.1 bits and send an ASCII character to P0 according to the following table. #include case(2): { main() { P0='2'; unsigned char in1; break; } in1 = P1; case(3): { in1 = in1 & 0x3; P0='3'; switch (in1) { break; } case(0): { } //switch P0='0'; } //main break; } case(1): { P0='1'; break; } Lê Chí Thông 74 sites.google.com/site/chithong 37
  20. ĐH Bách Khoa TP.HCM Lê Chí Thông Your Turn! (ASCII to Packed-BCD) • Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and send them to Port 1. Lê Chí Thông 77 Solution (ASCII to Packed-BCD) • Write an 8051 C program to convert ASCII digits of ‘4’ and ‘7’ to packed BCD and send them to Port 1. #include main() { unsigned char bcdbyte; unsigned char w = '4'; unsigned char z = '7'; w=w&0x0F; w=w<<4; z=z&0x0F; bcdbyte=w|z; P1=bcdbyte; } Lê Chí Thông 78 sites.google.com/site/chithong 39
  21. ĐH Bách Khoa TP.HCM Lê Chí Thông RAM space (1) • Compile and single-step the following program on Keil. Examine the contents of the internal RAM space to locate the ASCII values. #include void main(void) { unsigned char mynum[]="ABCDEF"; //RAM space unsigned char z; for (z=0;z void main(void) { unsigned char mydata[100]; //RAM space unsigned char x,z=0; for (x=0;x<100;x++) { z ; mydata[x]=z; P1=z; } } Lê Chí Thông 82 sites.google.com/site/chithong 41
  22. ĐH Bách Khoa TP.HCM Lê Chí Thông Lê Chí Thông 85 Lê Chí Thông 86 sites.google.com/site/chithong 43
  23. ĐH Bách Khoa TP.HCM Lê Chí Thông Solution (Data Serialization) • Write a C program to send out the value 44H serially one bit at a time via P1.0. The MSB should go out first. #include sbit P1b0=P1^0; sbit regAMSB=ACC^7; void main(void) { unsigned char conbyte=0x44; unsigned char x; ACC=conbyte; for (x=0;x sbit P1b0=P1^0; sbit ACCMSB=ACC^7; bit membit; void main(void) { unsigned char x; for (x=0;x >1; ACCMSB=membit; } P2=ACC; } Lê Chí Thông 90 sites.google.com/site/chithong 45
  24. ĐH Bách Khoa TP.HCM Lê Chí Thông Timer - Assembly language Write a program using Timer 0 to create a 10 Hz square wave on P1.0 MOV TMOD, #01H ; Timer 0, mode 1 (16-bit timer mode) LOOP: MOV TH0, #HIGH(-50000); high byte of -50,000 MOV TL0, #LOW(-50000) ; low byte of -50,000 SETB TR0 ; start timer WAIT: JNB TF0, WAIT ; wait for overflow CLR TR0 ; stop timer CLR TF0 ; clear timer overflow flag CPL P1.0 ; toggle port bit SJMP LOOP ; repeat Delay 50,000 μs (source ) P1.0  NOT (P1.0) Lê Chí Thông 93 Timer - C language Write a program using Timer 0 to create a 10 Hz square wave on P1.0 #include sbit LED1 = P1^0; (source ) int count; main() { TMOD = 0x01; loop: count = -50000; TL0 = count & 0x00FF; TH0 = count >> 8; TR0 = 1; while(TF0 == 0) {} Delay 50,000 μs TR0 = 0; TF0 = 0; LED1 = !LED1; P1.0  NOT (P1.0) goto loop; Lê Chí Thông 94 } sites.google.com/site/chithong 47
  25. ĐH Bách Khoa TP.HCM Lê Chí Thông Transmission – C Language Assume a 10-byte string of data is stored in the internal RAM from the location 30H. Write a program that sends this string to the 8051 serial port (1200 baud, crystal 11.0592 MHz) Lê Chí Thông 97 LED Chaser • Write a C program to bring in a byte of data serially one bit at a time via P1.0. The MSB should come in first. #include sbit P1b0=P1^0; sbit regALSB=ACC^0; bit membit; void main(void) { unsigned char x; for (x=0;x<8;x++) { membit=P1b0; ACC=ACC<<1; regALSB=membit; } P2=ACC; } Lê Chí Thông 98 sites.google.com/site/chithong 49