亚洲春色中文字幕久久久-三上亚,一吻二脱三床四吻胸,国产真实伦对白视频全集,在线毛片观看,精品成品入口黄网,国产毛aⅴ片久久久,亚洲AV色香蕉一区二区三区老师,萧皇后A级艳片,色情日本视频更新,99久久亚洲精品日本无码
標(biāo)題:
51單片機(jī)串口接收程序問(wèn)題
[打印本頁(yè)]
作者:
Henzie0226
時(shí)間:
2021-4-9 10:20
標(biāo)題:
51單片機(jī)串口接收程序問(wèn)題
下面的程序接收到一個(gè)16進(jìn)制數(shù),我想將其顯示到LCD上,但是只能顯示出部分位。(如串口接收到0xEB,計(jì)劃顯示23.5,但是只能顯示23,大佬們知道這是為啥嗎?)
單片機(jī)源程序如下:
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="The temperature:";
uchar table1[]="";
uchar flag,temp;
uchar j=0;
sbit wela=P2^7;
sbit dula=P2^6;
sbit lcden=P3^4;
sbit lcdrs=P3^5;
void DelayMS(uint xms) //延時(shí)大約1ms
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0; //寫(xiě)命令
P0=com;
DelayMS(5);
lcden=1;
DelayMS(5);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1; //寫(xiě)數(shù)據(jù)
P0=date;
DelayMS(5);
lcden=1;
DelayMS(5);
lcden=0;
}
void send(uchar dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void uartinit()//串口初始化函數(shù)
{
SCON=0x50;//串口工作方式為模式1
TMOD=0x20;//定時(shí)器T1工作方式2
PCON=0x00;//SMOD=0
TH1=0xfd;//波特率為9600
TL1=0xfd;
EA=1;
ES=1;
TR1=1;//啟動(dòng)定時(shí)器
}
void init() //初始化函數(shù)
{
dula=0;
wela=0;
lcden=0;
write_com(0x38); //設(shè)置16*2顯示、5*7點(diǎn)陣、8位數(shù)據(jù)接口
write_com(0x0c); //設(shè)置開(kāi)顯示,不顯示光標(biāo)
write_com(0x06); //寫(xiě)一個(gè)字符后,地址指針自動(dòng)加一
write_com(0x01); //顯示清零,數(shù)據(jù)指針清零
uartinit();
}
void main()
{
uchar num=0;
uchar b=0;
init();
write_com(0x80);
for(b=0;b<16;b++)
{
write_data(table[b]);
DelayMS(5);
}
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
if(temp>=100)
{
table1[1]=temp/100+48; //ASCII碼轉(zhuǎn)換
table1[2]=temp%100/10+48;
table1[3]='.';
table1[4]=temp%10+48;
}
else if(temp>=10) //兩位數(shù),百位的零不顯示
{
table1[1]=' '; //ASCII碼轉(zhuǎn)換
table1[2]=temp%100/10+48;
table1[3]='.';
table1[4]=temp%10+48;
}
else //一位數(shù),前面的兩個(gè)零都不顯示
{
table1[1]=' '; //ASCII碼轉(zhuǎn)換
table1[2]='0';
table1[3]='.';
table1[4]=temp%10+48;
}
write_com(0x80+0x40);
for(b=1;b<5;b++)
{
write_data(table1[b]);
DelayMS(5);
}
for(b=1;b<5;b++)
{
send(table1[b]);
}
ES=1;
}
}
}
void ser() interrupt 4
{
if(RI==1)
{
RI=0;
temp=SBUF;
flag=1;
}
}
復(fù)制代碼
作者:
she
時(shí)間:
2021-4-9 14:51
如果你直接讓他顯示一個(gè)定值,能正常顯示嗎?
作者:
cn_zhx
時(shí)間:
2021-4-9 15:28
直接送個(gè)"23.5"字符串,看能不能正常顯示
作者:
zjy525
時(shí)間:
2021-4-9 15:38
因?yàn)槟愕馁x值是從table1[1]開(kāi)始的,table1[0]沒(méi)有被賦值
作者:
wulin
時(shí)間:
2021-4-9 21:31
改這樣
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="The temperature:";
uchar table1[4];
uchar flag,temp;
uchar j=0;
sbit wela=P2^7;
sbit dula=P2^6;
sbit lcden=P3^4;
sbit lcdrs=P3^5;
void DelayMS(uint xms) //延時(shí)大約1ms
{
uint x,y;
for(x=xms;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
lcdrs=0; //寫(xiě)命令
P0=com;
DelayMS(1);
lcden=1;
DelayMS(1);
lcden=0;
}
void write_data(uchar date)
{
lcdrs=1; //寫(xiě)數(shù)據(jù)
P0=date;
DelayMS(1);
lcden=1;
DelayMS(1);
lcden=0;
}
void send(uchar dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void uartinit()//串口初始化函數(shù)
{
SCON=0x50;//串口工作方式為模式1
TMOD=0x20;//定時(shí)器T1工作方式2
PCON=0x00;//SMOD=0
TH1=0xfd;//波特率為9600
TL1=0xfd;
EA=1;
ES=1;
TR1=1;//啟動(dòng)定時(shí)器
}
void init() //初始化函數(shù)
{
dula=0;
wela=0;
lcden=0;
write_com(0x38); //設(shè)置16*2顯示、5*7點(diǎn)陣、8位數(shù)據(jù)接口
write_com(0x0c); //設(shè)置開(kāi)顯示,不顯示光標(biāo)
write_com(0x06); //寫(xiě)一個(gè)字符后,地址指針自動(dòng)加一
write_com(0x01); //顯示清零,數(shù)據(jù)指針清零
}
void main()
{
uchar num=0;
uchar b=0;
uartinit();
init();
write_com(0x80);
for(b=0;b<16;b++)
{
write_data(table[b]);
DelayMS(5);
}
while(1)
{
if(flag==1)
{
ES=0;
flag=0;
if(temp>=100)
{
table1[0]=temp/100%10+'0'; //ASCII碼轉(zhuǎn)換
table1[1]=temp/10%10+'0';
table1[2]='.';
table1[3]=temp%10+'0';
}
if(temp<100&&temp>=10) //兩位數(shù),百位的零不顯示
{
table1[0]=' '; //ASCII碼轉(zhuǎn)換
table1[1]=temp/10%10+'0';
table1[2]='.';
table1[3]=temp%10+'0';
}
if(temp<10) //一位數(shù),前面的兩個(gè)零都不顯示
{
table1[0]=' '; //ASCII碼轉(zhuǎn)換
table1[1]='0';
table1[2]='.';
table1[3]=temp%10+'0';
}
write_com(0x80+0x40);
for(b=0;b<4;b++)
{
write_data(table1[b]);
DelayMS(5);
}
for(b=0;b<4;b++)
{
send(table1[b]);
}
ES=1;
}
}
}
void ser() interrupt 4
{
RI=0;
temp=SBUF;
flag=1;
}
復(fù)制代碼
作者:
Henzie0226
時(shí)間:
2021-4-10 20:33
wulin 發(fā)表于 2021-4-9 21:31
改這樣
您好,謝謝您的回復(fù),您的代碼我驗(yàn)證了一下可以實(shí)現(xiàn),請(qǐng)問(wèn)我的不能實(shí)現(xiàn)的原因是什么呢?
歡迎光臨 (http://www.denmoz.com/bbs/)
Powered by Discuz! X3.1