VI XỬ LÝ 8051 chap8 c programming

50 0 0
VI XỬ LÝ 8051 chap8 c programming

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

ĐH Bách Khoa TP.HCM Lê Chí Thơng The 8051 Microcontroller Chapter 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 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 sites.google.com/site/chithong ĐH Bách Khoa TP.HCM Lê Chí Thơng References • Cx51 Compiler User Guide (pdf) http://www.neuromorphs.net\nm\raw-attachment\wiki\2010\usb10\C51.PDF • Matthew Kramer, Writing C Code for the 8051 (pdf) http://www.kel.ftn.uns.ac.rs/predmeti/3/mpe/razno/writing_c_code_8051.pdf • M Mazidi, J Mazidi, and R McKinlay, The 8051 Microcontroller and Embedded Systems Using Assembly and C (2nd ed.) • Chung-Ping Young, 8051 Programming in C (pdf) • Ibrahim Kamal, C programming for 8051 using KEIL IDE, http://www.ikalogic.com/part-2-c-programming-for-8051-usingkeil-ide/ Lê Chí Thơng Blinky Program (Schematic) sites.google.com/site/chithong Lê Chí Thơng ĐH Bách Khoa TP.HCM Lê Chí Thông Blinky Program void Delayms (unsigned int ms) { //Documentatons unsigned int i; /************************** while (ms) { Blinky program i = 115; Date: 05-Apr-2012 while (i> 0) i ; Version: 1.0 ms ; ***************************/ } //Preprocessor Statements } #include // Main Program #define ON main (void) { #define OFF while (1) { // Global declarations LED1 = ON; // Turn on LED1 sbit LED1=P1^0; Delayms (500); // Delay 500 ms // Subprograms LED1 = OFF; // Turn off LED1 /* Delay in milliseconds for a 11.0592 MHz crystal */ Delayms (500); // Delay 500 ms } Lê Chí Thơng } source From the C program to the machine language Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/ Lê Chí Thơng sites.google.com/site/chithong ĐH Bách Khoa TP.HCM 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 Source: Chung-Ping Young, 8051 Programming in C Lê Chí Thơng Structure of a C program Documentations Preprocessor Statements Global Declarations Subprograms (User defined functions) Main Program (Main function) Lê Chí Thơng sites.google.com/site/chithong ĐH Bách Khoa TP.HCM Lê Chí Thơng Structure of a C program //Documentatons //Name of the program, programmer, //and other details //Preprocessor Statements #include //header files #include #define TRUE //symbolic constants #define FALSE //Global Declarations unsigned char x,y; int z; long n=0; //Subprograms Void function1 (int x) { //Statements } //Main function void main(void) { int sum = 0; //Local Declarations int x; float y; //Statements } Lê Chí Thơng Keil Product Downloads http://www.keil.com/download/product/ Lê Chí Thơng sites.google.com/site/chithong 10 ĐH Bách Khoa TP.HCM Lê Chí Thơng Using Keil • Open Keil and start a new project • Chose a name for your new project, create a separate folder where all the files of your project will be stored, chose a name and click save Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/ 11 Using Keil • Select a device for Target ‘Target 1′: seek for ATMEL, then select AT89C51, AT89S52, … • You will be asked whether to ‘copy standard 8051 startup code‘ click No Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/ sites.google.com/site/chithong 12 Đ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: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/ 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: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/ sites.google.com/site/chithong 14 ĐH Bách Khoa TP.HCM Lê Chí Thơng Using Keil • Compile your source code, and correct eventual syntax errors: ‘rebuild all targets’ •Debug: Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/ 15 Using Keil • Run: • Stop: • Reset: • Step: Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/ sites.google.com/site/chithong 16 ĐH Bách Khoa TP.HCM Lê Chí Thơng Keil Variable Extensions • Declaration of the types of memory data idata bdata xdata internal RAM (direct addressing) internal RAM (indirect addressing) bit addressable internal RAM 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 sites.google.com/site/chithong 18 ĐH Bách Khoa TP.HCM Lê Chí Thơng bdata Description: The variable will be stored in bit addressable memory of controller Example: unsigned char bdata x; //each bit of the variable x can be accessed //as follows x ^ = 1; //1st bit of variable x is set x ^ = 0; //0th bit of variable x is cleared Lê Chí Thơng 19 xdata Description: The variable will be stored in external RAM memory of controller Example: unsigned char xdata x; Lê Chí Thơng sites.google.com/site/chithong 20 10 ĐH Bách Khoa TP.HCM Lê Chí Thơng I/O Programming – Example Source: Chung-Ping Young Lê Chí Thơng 71 I/O Programming – Example Source: Chung-Ping Young Lê Chí Thơng sites.google.com/site/chithong 72 36 Đ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 0 1 1 Character ‘0’ ‘1’ ‘2’ ‘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 main() { unsigned char in1; in1 = P1; in1 = in1 & 0x3; switch (in1) { case(0): { P0='0'; break; } case(1): { P0='1'; break; } case(2): { P0='2'; break; } case(3): { P0='3'; break; } } //switch } //main Lê Chí Thơng sites.google.com/site/chithong 74 37 ĐH Bách Khoa TP.HCM Lê Chí Thơng Your Turn! (Packed-BCD to ASCII) • Write an 8051 C program to convert packed BCD 0x29 to ASCII and output the bytes to Port and Port Lê Chí Thơng 75 Solution (Packed-BCD to ASCII) • Write an 8051 C program to convert packed BCD 0x29 to ASCII and output the bytes to Port and Port #include main() { unsigned char x,y; unsigned char mybyte=0x29; x=mybyte&0x0F; P1=x|0x30; y=mybyte&0xF0; y=y>>4; P2=y|0x30; } Lê Chí Thơng sites.google.com/site/chithong 76 38 Đ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 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 #include main() { unsigned char bcdbyte; unsigned char w = '4'; unsigned char z = '7'; w=w&0x0F; w=w

Ngày đăng: 13/04/2023, 08:09

Tài liệu cùng người dùng

Tài liệu liên quan