使用Keil语言的嵌入式C编程教程(下)
使用Keil語(yǔ)言的嵌入式C編程教程(下)
用8051單片機(jī)進(jìn)行定時(shí)器/計(jì)數(shù)器的計(jì)算與編程
延遲是應(yīng)用軟件開發(fā)中的重要因素之一。然而,在實(shí)現(xiàn)定時(shí)延遲的過程中,正常的延遲并不能給出克服這一問題的寶貴結(jié)果。定時(shí)器和計(jì)數(shù)器是微控制器的硬件組成部分,在許多應(yīng)用中使用它來提供具有計(jì)數(shù)的寶貴時(shí)間延遲脈沖兩個(gè)任務(wù)都是通過軟件技術(shù)實(shí)現(xiàn)的。
定時(shí)器延遲
WAP使用T1M2(timer1和mode2)生成500us延時(shí)?
#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單片機(jī)的串行通信計(jì)算與編程
串行通信通常用于發(fā)送和接收信號(hào)。8051微控制器包括由Rx和Tx引腳發(fā)送和接收的信號(hào)的UART串行通信。UART接收字節(jié)的數(shù)據(jù)并按順序發(fā)送各個(gè)位。寄存器是一種在存儲(chǔ)器中收集和存儲(chǔ)數(shù)據(jù)的方法。UART是一種半雙工協(xié)議。半雙工是指?jìng)鬏敽徒邮諗?shù)據(jù),但不能同時(shí)進(jìn)行。
- WAP將字符“S”傳輸?shù)酱写翱谑褂?600作為波特率?
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從超級(jí)終端接收數(shù)據(jù)并使用9600波特將數(shù)據(jù)發(fā)送到微控制器的端口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單片機(jī)中斷程序
中斷是強(qiáng)制停止當(dāng)前程序并立即執(zhí)行其他程序的信號(hào)。8051微控制器提供6個(gè)內(nèi)部和外部中斷源。當(dāng)中斷發(fā)生時(shí),微控制器暫停當(dāng)前任務(wù)并通過執(zhí)行ISR處理中斷,然后微控制器返回到最近的任務(wù)。
WAP在定時(shí)器0中斷時(shí)執(zhí)行左移操作,然后在主功能中執(zhí)行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單片機(jī)進(jìn)行鍵盤編程
矩陣鍵盤是一種模擬開關(guān)設(shè)備,在許多嵌入式應(yīng)用中使用,允許用戶執(zhí)行必要的任務(wù)。矩陣鍵盤由行和列中矩陣格式的開關(guān)排列組成。行和列連接到微控制器,使得開關(guān)行連接到一個(gè)管腳,并且每列中的開關(guān)連接到另一個(gè)管腳,然后執(zhí)行操作。
- 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單片機(jī)進(jìn)行液晶顯示編程
LCD顯示器是一種電子設(shè)備,在許多應(yīng)用中經(jīng)常用于以文本或圖像格式顯示信息。液晶顯示器是一種可以在屏幕上輕松顯示字符的顯示器。液晶顯示器由8條數(shù)據(jù)線和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++);
}
總結(jié)
以上是生活随笔為你收集整理的使用Keil语言的嵌入式C编程教程(下)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用Keil语言的嵌入式C编程教程(上)
- 下一篇: AI芯片体系结构目标图形处理