Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

VI XỬ LÝ 8051 chap8 c programming
Nội dung xem thử
Mô tả chi tiết
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 1
Chapter 8
C Programming for 8051
The 8051 Microcontroller
Lê Chí Thông 1
TS. Lê Chí Thông
sites.google.com/site/chithong
Ho Chi Minh City University of Technology
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
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 2
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 3
Blinky Program
Lê Chí Thông 4
(Schematic)
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 3
Blinky Program
//Documentatons
/**************************
Blinky program
Date: 05-Apr-2012
Version: 1.0
***************************/
//Preprocessor Statements
#include <reg51.h>
#define ON 1
#define OFF 0
// Global declarations
sbit LED1=P1^0;
// Subprograms
/* Delay in milliseconds for a 11.0592 MHz crystal */
Lê Chí Thông 5
void Delayms (unsigned int ms) {
unsigned int i;
while (ms) {
i = 115;
while (i> 0) i--;
ms--;
}
}
// Main Program
main (void) {
while (1) {
LED1 = ON; // Turn on LED1
Delayms (500); // Delay 500 ms
LED1 = OFF; // Turn off LED1
Delayms (500); // Delay 500 ms
}
source }
From the C program
to the machine language
Lê Chí Thông 6
Source: http://www.ikalogic.com/part-2-c-programming-for-8051-using-keil-ide/
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 4
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
Lê Chí Thông 7
Source: Chung-Ping Young, 8051 Programming in C
Lê Chí Thông 8
Documentations
Preprocessor Statements
Global Declarations
Subprograms (User defined functions)
Main Program (Main function)
Structure of a C program
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 5
//Documentatons
//Name of the program, programmer,
//and other details
//Preprocessor Statements
#include <stdio.h> //header files
#include <reg51.h>
#define TRUE 1 //symbolic constants
#define FALSE 0
//Global Declarations
unsigned char x,y;
int z;
long n=0;
Lê Chí Thông 9
//Subprograms
Void function1 (int x) {
//Statements
}
//Main function
void main(void)
{
int sum = 0; //Local Declarations
int x;
float y;
//Statements
}
Structure of a C program
Keil Product Downloads
http://www.keil.com/download/product/
Lê Chí Thông 10