使用Keil語言的嵌入式C編程教程(下)
用8051單片機進行定時器/計數器的計算與編程
延遲是應用軟件開發中的重要因素之一。然而,在實現定時延遲的過程中,正常的延遲並不能給出克服這一問題的寶貴結果。定時器和計數器是微控制器的硬件組成部分,在許多應用中使用它來提供具有計數的寶貴時間延遲脈衝兩個任務都是通過軟件技術實現的。
定時器延遲
WAP使用T1M2(timer1和mode2)生成500us延時?
#include<reg51.h>
void main()
{
unsigned char i;
TMOD=0x20; //set the timer mode//
for(i=0i<2;i++) //double the time daly//
{
TL1=0x19; //set the time delay//
TH1=0x00;
TR1=1; //timer oN//
While(TF1==0); //check the flag bit//
TF1=0;
}
TR1=0; //timer off//
}
Normal Loop Delay
void delay()
{
unsignedint k;
for(k=0;k<30000;k++);
}
基於8051單片機的串行通信計算與編程
串行通信通常用於發送和接收信號。8051微控制器包括由Rx和Tx引腳發送和接收的信號的UART串行通信。UART接收字節的數據並按順序發送各個位。寄存器是一種在存儲器中收集和存儲數據的方法。UART是一種半雙工協議。半雙工是指傳輸和接收數據,但不能同時進行。
- WAP將字符“S”傳輸到串行窗口使用9600作爲波特率?
28800是8051微控制器的最大波特率
28800/9600= 3
That baud rate ‘3’ is stored in the timers
#include<reg51.h>
void main()
{
SCON=0x50; //start the serial communication//
TNOD=0x20; //selected the timer mode//
TH1=3; // load the baud rate//
TR1=1; //Timer ON//
SBUF=’S’; //store the character in the register//
while(TI==0); //check the interrupt register//
TI=0;
TR1=0; //OFF the timer//
while(1); //continuous loop//
}
- WAP從超級終端接收數據並使用9600波特將數據發送到微控制器的端口0?
- 28800是8051微控制器的最大波特率
28800/9600=3
That baud rate ‘3’ is stored in the timers
#include<reg51.h>
void main()
{
SCON=0x50; //start the serial communication//
TMOD=0x20; //selected the timer mode//
TH1=3; // load the baud rate//
TR1=1; //Timer ON//
PORT0=SBUF; //send the data from SBUF to port0//
while(RI==0); //check the interrupt register//
RI=0;
TR1=0; //OFF the timer//
while(1); //stop the program when character is received//
}
用8051單片機中斷程序
中斷是強制停止當前程序並立即執行其他程序的信號。8051微控制器提供6個內部和外部中斷源。當中斷髮生時,微控制器暫停當前任務並通過執行ISR處理中斷,然後微控制器返回到最近的任務。
WAP在定時器0中斷時執行左移操作,然後在主功能中執行P0的中斷操作?
#include<reg51.h>
unsigned char b;
void timer0() interrupt 2 //selected timer0 interrupt//
{
b=0x10;
P1=b<<2;
}
void main()
{
unsigned char a,i;
IE=0x82 //enable the timer0 interrupt//
TMOD=0x01;
TLo=0xFC; //interrupt timer//
TH1=0xFB;
TR0=1;
a=0x00;
while(1)
{
for(i=0;i<255;i++)
{
a++;
Po=a;
}
}
}
用8051單片機進行鍵盤編程
矩陣鍵盤是一種模擬開關設備,在許多嵌入式應用中使用,允許用戶執行必要的任務。矩陣鍵盤由行和列中矩陣格式的開關排列組成。行和列連接到微控制器,使得開關行連接到一個管腳,並且每列中的開關連接到另一個管腳,然後執行操作。
- WAP to toggle the LED by pressing the switch
#include<reg51.h>
sbit a=P3^0;
sbit b=P3^1;
sbit c=P3^2;
sbit d=P3^3;
void delay();
void main()
{
while(1)
{
a=0;
b=1;
c=1;
d=1;
delay();
a=1;
b=0;
c=1;
d=1;
void delay()
{
unsigned char i;
TMOD=0x20; //set the timer mode//
for(i=0i<2;i++) //double the time daly//
{
TL1=0x19; //set the time delay//
TH1=0x00;
TR1=1; //timer oN//
While(TF1==0); //check the flag bit//
TF1=0;
}
TR1=0; //timer off//
}
- WAP to Switch ON the LED by pressing the key ‘1’ on the
keypad?
#include<reg51.h>
sbit r1=P2^0;
sbit c1=P3^0;
sbit LED=P0^1;
void main()
{
r1=0;
if(c1==0)
{
LED=0xff;
}
}
- WAP to display the number 0,1,2,3,4,5 on the seven segment by
pressing the respective key on the keypad?
#include<reg51.h>
sbit r1=P2^0;
sbit c1=P3^0;
sbit r2=P2^0;
sbit c2=P3^0;
sbit a=P0^1;
void main()
{
r1=0; a=1;
if(c1==0)
{
a=0xFC;
}
If(c2==0)
{
a=0x60;
}
if(c3==0)
{
a=0xDA;
}
If(c4==0)
{
a=0xF2;
}
}
用8051單片機進行液晶顯示編程
LCD顯示器是一種電子設備,在許多應用中經常用於以文本或圖像格式顯示信息。液晶顯示器是一種可以在屏幕上輕鬆顯示字符的顯示器。液晶顯示器由8條數據線和3條控制線組成,用於與微控制器接口。
WAP to display the “EDGEFX KITS” on LED display ?
#include<reg51.h>
#define kam P0
voidlcd_initi();
voidlcd_dat(unsigned char );
voidlcd_cmd(unsigned char );
void delay();
void display(unsigned char *s, unsigned char r)
sbitrs=P2^0;
sbitrw=P2^1;
sbit en=P2^2;
void main()
{
lcd_initi();
lcd_cmd(0x80);
delay(100);
lcd_cmd(0xc0);
display(“edgefx kits”,11);
while(1);
}
void display(unsigned char *s, unsigned char r)
{
unsignedint w;
for(w=0;w<r;w++)
{
lcd_data(s[w]);
}
}
voidlcd_initi()
{
lcd_cmd(0×01);
delay(100);
lcd_cmd(0×38);
delay(100);
lcd_cmd(0×06);
delay(100);
lcd_cmd(0x0c);
delay(100);
}
voidlcd_dat(unsigned char dat)
{
kam = dat;
rs=1;
rw=0;
en=1;
delay(100);
en=0;
}
}
voidlcd_cmd(unsigned char cmd)
{
kam=cmd;
rs=0;
rw=0;
en=1;
delay(100);
en=0;
}
void delay( unsigned int n)
{
unsignedint a;
for(a=0;a<n;a++);
}