Bài giảng Microprocessing Systems - Chapter 8: C Programming for 8051 - Lê Chí Thông

ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
The 8051 Microcontroller  
Chapter 8  
C Programming for 8051  
TS. Lê Chí Thông  
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
Đ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)  
• 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,  
keil-ide/  
Lê Chí Thông  
3
Blinky Program  
(Schematic)  
Lê Chí Thông  
4
sites.google.com/site/chithong  
2
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
Blinky Program  
void Delayms (unsigned int ms) {  
//Documentatons  
unsigned int i;  
while (ms) {  
i = 115;  
/**************************  
Blinky program  
Date: 05-Apr-2012  
while (i> 0) i--;  
ms--;  
Version: 1.0  
***************************/  
//Preprocessor Statements  
#include <reg51.h>  
}
}
// Main Program  
main (void) {  
while (1) {  
#define ON 1  
#define OFF 0  
// Global declarations  
sbit LED1=P1^0;  
LED1 = ON; // Turn on LED1  
Delayms (500); // Delay 500 ms  
LED1 = OFF; // Turn off LED1  
Delayms (500); // Delay 500 ms  
}
// Subprograms  
/* Delay in milliseconds for a 11.0592 MHz crystal */  
source  
Lê Chí Th}ông  
5
From the C program  
to the machine language  
Lê Chí Thông  
6
sites.google.com/site/chithong  
3
Đ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  
7
Structure of a C program  
Documentations  
Preprocessor Statements  
Global Declarations  
Subprograms (User defined functions)  
Main Program (Main function)  
Lê Chí Thông  
8
sites.google.com/site/chithong  
4
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
Structure of a C program  
//Subprograms  
//Documentatons  
//Name of the program, programmer,  
//and other details  
Void function1 (int x) {  
//Statements  
}
//Preprocessor Statements  
#include <stdio.h> //header files  
#include <reg51.h>  
//Main function  
void main(void)  
{
#define TRUE 1 //symbolic constants  
#define FALSE 0  
int sum = 0; //Local Declarations  
int x;  
float y;  
//Statements  
}
//Global Declarations  
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
Đ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.  
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.  
12  
sites.google.com/site/chithong  
6
Đ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.  
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‘.  
sites.google.com/site/chithong  
7
Đ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:  
Using Keil  
• Run:  
• Stop:  
• Reset:  
• Step:  
sites.google.com/site/chithong  
8
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
Keil Variable Extensions  
• Declaration of the types of memory  
data  
internal RAM (direct addressing)  
internal RAM (indirect addressing)  
bit addressable internal RAM  
external data memory  
idata  
bdata  
xdata  
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
Đ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 = 1; //1st bit of variable x is set  
x ^ 0 = 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  
20  
sites.google.com/site/chithong  
10  
Đ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  
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
Standard Types  
Data type  
Bytes Range  
unsigned char  
signed char  
unsigned int  
signed int  
1
1
2
2
4
4
0 to 255  
-128 To +127  
0 to 65,535  
-32,768 To +32,767  
0 to 4,294,967,295  
-2,147,483,648 to 2,147,483,647  
± 1.175494E-38 to  
± 3.402823E +38  
unsigned long  
signed long  
float  
4
• Unsigned char is an 8-bit data type in the range of 0 –  
255 (00 – FFH) and is one of the most widely used data  
types for the 8051.  
Lê Chí Thông  
23  
Keil Types  
Data type  
bit  
Bits  
Bytes  
Range  
0 to 1  
1
sbit  
1
0 to 1  
sfr  
8
1
2
0 to 255  
0 to 65,535  
sfr16  
16  
Lê Chí Thông  
24  
sites.google.com/site/chithong  
12  
Đ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  
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
sfr  
Description: sfr is used to define an 8-bit special  
function register from SFR memory.  
Example:  
sfr Port1 = 0x90;  
// Special function register with name Port1  
// defined at addrress 0x90  
Lê Chí Thông  
27  
sfr16  
Description: This keyword is used to define a two  
sequential 8-bit registers in SFR memory.  
Example:  
sfr16 DPTR = 0x82;  
// 16-bit special function register starting  
at 0x82  
// DPL at 0x82, DPH at 0x83  
Lê Chí Thông  
28  
sites.google.com/site/chithong  
14  
Đ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  
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
unsigned char – Example 3  
Source: Chung-Ping Young  
Lê Chí Thông  
31  
signed char – Example  
Source: Chung-Ping Young  
Lê Chí Thông  
32  
sites.google.com/site/chithong  
16  
Đ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  
large  
alien  
interrupt  
compact  
reentrant  
small  
far  
_priority_  
_task_  
Lê Chí Thông  
34  
sites.google.com/site/chithong  
17  
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
_at_  
Description: This keyword is used to store a variable on  
a defined location in ram.  
Example:  
unsigned char idata x _at_ 0x30;  
// variable x will be stored at location  
// 0x30 in internal data memory  
Lê Chí Thông  
35  
using  
Description: This keyword is used to define register  
bank for a function. User can specify register bank 0 to  
3.  
Example:  
void function () using 2{  
// code  
}
// Function named "function" uses register  
bank 2 while executing its code  
Lê Chí Thông  
36  
sites.google.com/site/chithong  
18  
Đ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  
0003H  
000BH  
0013H  
001BH  
0023H  
Interrupt source  
External 0  
Timer 0  
0
1
2
3
4
External 1  
Timer 1  
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  
ĐH Bách Khoa TP.HCM  
Lê Chí Thông  
Examples of Data Types  
data char Var1;  
char code text [] = "Enter temperature:";  
unsigned long xdata MyArray [100];  
float idata x, y, z;  
unsigned int pdata Dimension;  
unsigned char xdata vector [10] [4] [4];  
char BDATA Indicator;  
Lê Chí Thông  
39  
Function  
• A general form of a C function looks like this:  
<return type> FunctionName (Argument1, Argument2, …)  
{
Statement1;  
Statement2;  
…
}
An example of function:  
int sum (int x, int y)  
{
int result;  
result = x + y;  
return (result);  
}
Lê Chí Thông  
40  
sites.google.com/site/chithong  
20  
Tải về để xem bản đầy đủ
pdf 50 trang myanh 23260 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng Microprocessing Systems - 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:

  • pdfbai_giang_microprocessing_systems_chapter_8_c_programming_fo.pdf