單片機(jī)C語言基礎(chǔ)編程源碼八則
4.利用庫函數(shù)_getkey編寫一函數(shù),實(shí)現(xiàn)從單片機(jī)串行口接收數(shù)據(jù)的C51程序,把接收的數(shù)據(jù)存放在片內(nèi)數(shù)據(jù)存儲(chǔ)器從0x40開始的區(qū)域,遇到回車符CR(ASCII碼是0x0d)結(jié)束。
include stdio.h>
void main()
{
unsigned char data *dpt;
unsigned char i=0,temp;
dpt=0x40;
while(1)
{
temp=_getchar();
if(temp==0x0d)
break;
*(dpt+i)=temp;
i++;
}
}
5.利用庫函數(shù)scanf編寫一函數(shù),實(shí)現(xiàn)從單片機(jī)串行口接收數(shù)據(jù)的C51程序,把接收的數(shù)據(jù)存放在片外數(shù)據(jù)存儲(chǔ)器從0x240開始的區(qū)域,遇到回車符CR(ASCII碼是0x0d)結(jié)束。
include stdio.h>
void main()
{
unsigned char xdata *xdpt;
unsigned char i=0,temp;
xdpt=0x240;
while(1)
{
scanf(“%c”,temp);
if(temp==0x0d)
break;
*(xdpt+i)=temp;
i++;
}
}
6.利用庫函數(shù)putchar編寫一函數(shù),實(shí)現(xiàn)從單片機(jī)串行口發(fā)送數(shù)據(jù)的C51程序,發(fā)送的數(shù)據(jù)存放在片內(nèi)數(shù)據(jù)存儲(chǔ)器從0x50開始的區(qū)域,遇到回車符CR(ASCII碼是0x0d)結(jié)束。
include stdio.h>
void main()
{
unsigned char data *dpt;
unsigned char i=0;
dpt=0x50;
while(*(dpt+i)!=0x0d)
{
putchar(*(dpt+i));
i++;
}
}
7.利用庫函數(shù)printf編寫一函數(shù),實(shí)現(xiàn)從單片機(jī)串行口發(fā)送數(shù)據(jù)的C51程序,發(fā)送的數(shù)據(jù)存放在片外數(shù)據(jù)存儲(chǔ)器從0x100開始的區(qū)域,遇到回車符CR(ASCII碼是0x0d)結(jié)束。
include stdio.h>
void main()
{
unsigned char xdata *xdpt;
unsigned char i=0;
xdpt=0x100;
while(*(xdpt+i)!=0x0d)
{
printf(“%c”,*(xdpt+i));
i++;
}
}
8.編寫一定時(shí)器/計(jì)數(shù)器0定時(shí)100ms中斷處理函數(shù),使函數(shù)實(shí)現(xiàn)時(shí)鐘的功能,即產(chǎn)生時(shí)鐘的秒、分鐘和小時(shí)。設(shè)定時(shí)初值的高、低字節(jié)分別為TIMER_H和TIMER_L。
設(shè)fosc=6mhz
#include reg52.h>
sfr TIMER_H=0x8b;
sfr TIMER_L=0x8a;
unsigned char data shi,fen,miao,bfm;
void main()
{
TMOD=0x01;
TIMER_H=15536/16;
TIMER_L=15536%16;
ET0=1;
EA=1;
TR0=1;
while(1);
}
void time0_int() interrupt 1
{
TIMER_H=15536/16;
TIMER_L=15536%16;
bfm++;
if(bfm==10)
{
bfm=0;
miao++;
}
if(miao==60)
{
miao=0;
fen++;
}
if(fen==60)
{
fen=0;
shi++;
}
if(shi==24)
{
shi=0;
}
}
評(píng)論