|
主要實(shí)驗(yàn)功能:1.按下P3^0,串口發(fā)送1,2,3,4,5,6,7,8共8個(gè)數(shù)據(jù)到串口助手 2.串口助手傳來的數(shù)據(jù)再LCD1602顯示
問題1:發(fā)送到串口助手的數(shù)據(jù)錯(cuò)亂
問題2:LCD1602顯示1的話,需要在串口助手傳回11
#include <reg52.h>
#include <LCD1602.h>
typedef unsigned char u8;
void delayms(u8 ms){
u8 i;
while(ms--)
for(i=0;i<113;i++);
}
u8 scan(void){
static bit kp=0;
if((P3&0x0f)!=0x0f)
{
kp=1;
if((P3&0x0f)==0x0e) return 1;
}
else kp=0;
return 0;
}
void Init()
{
SCON=0X50; //設(shè)置為工作方式1
TMOD=0X20; //設(shè)置計(jì)數(shù)器工作方式2
PCON=0; //波特率不加倍
TH1=0XFD; //計(jì)數(shù)器初始值設(shè)置,注意波特率是9600的
TL1=0XFD;
ES=1; //打開接收中斷
EA=1; //打開總中斷
TR1=1; //打開計(jì)數(shù)器
}
void main() {
Init(); //串口初始化
LCD_Init(); //LCD1602初始化
while(1) {
}
}
void Usart() interrupt 4
{
u8 i = 0;
u8 receiveData;
if(scan() == 1)
{
SBUF = 0x01;
while(TI == 0);
TI = 0;
delayms(100);
SBUF = 0x02;
while(TI == 0);
TI = 0;
delayms(100);
SBUF = 3;
while(TI == 0);
TI = 0;
delayms(100);
SBUF = 4;
while(TI == 0);
TI = 0;
delayms(100);
SBUF = 5;
while(TI == 0);
TI = 0;
delayms(100);
SBUF = 6;
while(TI == 0);
TI = 0;
delayms(100);
SBUF = 7;
while(TI == 0);
TI = 0;
delayms(100);
SBUF = 8;
while(TI == 0);
TI = 0;
}
if(RI)
{
receiveData = SBUF; //接收到的數(shù)據(jù)
RI = 0; //清除接收中斷標(biāo)志位
LCD_ShowChar(1,1,receiveData);
}
}
|
-
|