Microprocessing Systems - Chapter 8: C Programming for 8051 - 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

pdf50 trang | Chuyên mục: Vi Xử Lý – Vi Điều Khiển | Chia sẻ: tuando | Lượt xem: 768 | Lượt tải: 0download
Tóm tắt nội dung Microprocessing Systems - Chapter 8: C Programming for 8051 - Lê Chí Thông, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
hông
sites.google.com/site/chithong 33
I/O Programming – Example 2
Lê Chí Thông 65
Source: Chung-Ping Young
I/O Programming – Example 3
66
Source: Chung-Ping Young
Lê Chí Thông
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 34
I/O Programming – Example 4
67
Source: Chung-Ping Young
Lê Chí Thông
I/O Programming – Example 5
68
Source: Chung-Ping Young
Lê Chí Thông
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 35
I/O Programming – Example 6
69
Source: Chung-Ping Young
Lê Chí Thông
I/O Programming – Example 7
70
Source: Chung-Ping Young
Lê Chí Thông
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 36
I/O Programming – Example 8
71
Source: Chung-Ping Young
Lê Chí Thông
I/O Programming – Example 9
72
Source: Chung-Ping Young
Lê Chí Thông
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 37
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.
Lê Chí Thông 73
P1.0 P1.1 Character
0 0 ‘0’
0 1 ‘1’
1 0 ‘2’
1 1 ‘3’
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.
Lê Chí Thông 74
#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
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 38
Your Turn! (Packed-BCD to ASCII)
• Write an 8051 C program to convert packed BCD 0x29 to ASCII 
and output the bytes to Port 1 and Port 2.
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 1 and Port 2.
Lê Chí Thông 76
#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;
}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 39
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.
Lê Chí Thông 78
#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;
}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 40
Your Turn! (Binary to Decimal)
• Write an 8051 C program to convert 11111101B to decimal and 
send the digits to Port 0, Port 1 and Port 2.
Lê Chí Thông 79
Solution (Binary to Decimal)
• Write an 8051 C program to convert 11111101B to decimal and 
send the digits to Port 0, Port 1 and Port 2.
Lê Chí Thông 80
#include 
main()
{
unsigned char x,bin,d1,d2,d3;
bin=0xFD;
x=bin/10;
d1=bin%10;
d2=x%10;
d3=x/10;
P0=d1;
P1=d2;
P2=d3;
}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 41
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.
Lê Chí Thông 81
#include 
void main(void)
{
unsigned char mynum[]="ABCDEF"; //RAM space
unsigned char z;
for (z=0;z<=6;z++)
P1=mynum[z];
}
RAM space (2)
• Write, compile and single-step the following program on Keil. 
Examine the contents of the internal RAM space to locate the 
values.
Lê Chí Thông 82
#include 
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;
}
}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 42
Code space
• Compile and single-step the following program on Keil. Examine 
the contents of the code space to locate the ASCII values.
Lê Chí Thông 83
#include 
void main(void)
{
code unsigned char mynum[]="ABCDEF";
unsigned char z;
for (z=0;z<=6;z++)
P1=mynum[z];
}
Compare and contrast the following programs and 
discuss the advantages and disadvantages of each one.
Lê Chí Thông 84
(a)
#include 
void main(void)
{
P1=‘H’;
P1=‘E’;
P1=‘L’;
P1=‘L’;
P1=‘O’;
}
(b)
#include 
void main(void){
unsigned char mydata[]=“HELLO”;
unsigned char z;
for (z=0;z<=5;z++)
P1=mydata[z];}
(c)
#include 
void main(void){
code unsigned char 
mydata[]=“HELLO”;
unsigned char z;
for (z=0;z<=5;z++)
P1=mydata[z];}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 43
Lê Chí Thông 85
Lê Chí Thông 86
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 44
Data Serialization
• Write a C program to send out the value 44H serially one bit at a 
time via P1.0. The LSB should go out first.
Lê Chí Thông 87
#include 
sbit P1b0=P1^0;
sbit regALSB=ACC^0;
void main(void)
{
unsigned char conbyte=0x44;
unsigned char x;
ACC=conbyte;
for (x=0;x<8;x++)
{
P1b0=regALSB;
ACC=ACC>>1;
}
}
Your Turn! (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.
Lê Chí Thông 88
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 45
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.
Lê Chí Thông 89
#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<8;x++)
{
P1b0=regAMSB;
ACC=ACC<<1;
}
}
Data Serialization (2)
• Write a C program to bring in a byte of data serially one bit at a 
time via P1.0. The LSB should come in first.
Lê Chí Thông 90
#include 
sbit P1b0=P1^0;
sbit ACCMSB=ACC^7;
bit membit;
void main(void)
{
unsigned char x;
for (x=0;x<8;x++)
{
membit=P1b0;
ACC=ACC>>1;
ACCMSB=membit;
}
P2=ACC;
}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 46
Your Turn! (Data Serialization 2)
• 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.
Lê Chí Thông 91
Solution (Data Serialization 2)
• 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.
Lê Chí Thông 92
#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;
}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 47
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
93Lê Chí Thông
Timer - Assembly language
Delay 50,000 μs
P1.0  NOT (P1.0)
(source)
Write a program using Timer 0 to create a 10 Hz square wave on P1.0
#include 
sbit LED1 = P1^0; 
int count;
main() { 
TMOD = 0x01; 
loop: 
count = -50000;
TL0 = count & 0x00FF; 
TH0 = count >> 8; 
TR0 = 1; 
while(TF0 == 0) {} 
TR0 = 0;
TF0 = 0;
LED1 = !LED1; 
goto loop; 
}
94Lê Chí Thông
Timer - C language
Delay 50,000 μs
P1.0  NOT (P1.0)
(source)
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 48
Ex: Write a program that sets up the on-chip serial port, sets the 
baud rate, and waits for a character. When a character is received, it 
is transmitted using the putchar function. 
#include ”reg51.h”
#include ”stdio.h”
void main (void){
SCON = 0x50; //SCON: mode 1, 8-bit UART, enable receiver
TMOD |= 0x20;//TMOD: timer 1, mode 2, 8-bit reload 
TH1 = -13 ; //TH1: reload value for 2400 baud
TR1 = 1; //TR1: timer 1 run 
TI = 1; //TI: set TI to send first char of UART
while(1) {
char aaa;
aaa = _getkey(); //waits for a character
putchar(aaa); //transmit
}
}
95Lê Chí Thông
Serial Port
(source)
Transmission – Assembly Language
ORG 0000H
MOV SCON,#01010010B ;Serial port mode 1
MOV TMOD,#00100000B ;Timer 1 mode 2
MOV TH1,#-24 ;reload count for 1200 baud
SETB TR1 ;start Timer 1
MOV R2,#10 ;number of loops
MOV R0,#30H ;starting address
LOOP: MOV A,@R0 ;get data
ACALL SEND ;send data
INC R0 ;increase pointer
DJNZ R2,LOOP ;loop 10 times
SJMP DONE
SEND: JNB TI,$ ;transmit buffer empty? No:check again
CLR TI ;yes: clear flag and 
MOV SBUF,A ; send data
RET ;return
DONE: NOP
END
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)
96Lê Chí Thông
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 49
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)
97Lê Chí Thông
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.
Lê Chí Thông 98
#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;
}
ĐH Bách Khoa TP.HCM Lê Chí Thông
sites.google.com/site/chithong 50
Lê Chí Thông 99
100
References 
Lê Chí Thông
• Chung-Ping Young, 8051 Programming in C, Dept. of 
Computer Science and Information Engineering, 
National Cheng Kung University, TAIWAN
• Mazidi, Mazidi and McKinlay, The 8051 Microcontroller 
and Embedded Systems: Using Assembly and C

File đính kèm:

  • pdfmicroprocessing_systems_chapter_8_c_programming_for_8051_le.pdf