大家好,下面這個程序可以用串口助手發(fā)送十六進(jìn)制數(shù)并在數(shù)碼管上顯示.因?yàn)榇谕ㄓ嵻浖荒馨l(fā)2位數(shù),所以顯示也只能是2位,請教大家:如和實(shí)現(xiàn)三位數(shù)字的發(fā)送與顯示,舉例說明:比如我想發(fā)送123并在單片機(jī)的數(shù)碼管上顯示123,該如何發(fā)送與分離數(shù)字.程序如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar ss,sg;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void Init_Uart(void);
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uint i)
{
uchar ss,sg;
ss=i/16;
sg=i%16;
P0=table[ss];
P2=0x7f;
delay(2);
P0=table[sg];
P2=0xbf;
delay(2);
}
void main(void)
{
uint i = 0;
Init_Uart();
while(1)
{
if(RI==1)
{
RI = 0;
i = SBUF;
}
else display(i);
}
}
void Init_Uart(void)
{
TMOD = 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
SCON = 0x50;
PCON = 0;
EA = 1;
}
/********************* (C) COPYRIGHT 2010 **************
* 文件名稱: 串口.c
* 程序作者: whhjdz(QQ:595279970 ,Email:whhjdz@163.com)
* 程序版本: V1.0
* 編制日期: 02-17-2010
* 功能描述: PC串口與單片機(jī)通訊實(shí)驗(yàn)
* 編譯器:Keil C uVision3
* 芯片:STC89C52,12M晶振
* 注意: P0 -- 數(shù)碼管段顯示 P2 -- 數(shù)碼管位選
*
*********************************************************/
#include<reg52.h> //頭文件
#define uchar unsigned char //定義變量
#define uint unsigned int
uchar ss,sg;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e}; //0~F字模
void delay(uint z) //延時子函數(shù)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uint i) //顯示子函數(shù)
{
uchar ss,sg;
ss=i/16; //分離數(shù)的十位
sg=i%16; //分離數(shù)的個位
P0=table[ss];
P2=0x7f; //第一個數(shù)碼管點(diǎn)亮并顯示數(shù)的十位
delay(2);
P0=table[sg];
P2=0xbf; //第二個數(shù)碼管點(diǎn)亮并顯示數(shù)的個位
delay(2);
}
void Init_Uart(void) //定時器初始化(設(shè)置波特率)
{
TMOD = 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
SCON = 0x50;
PCON = 0;
EA = 1;
}
void main(void)
{
uint i = 0;
Init_Uart();
while(1)
{
if(RI==1)
{
RI = 0;
i = SBUF;
}
else display(i);
}
}
將顯示程序改了一下,能顯示三位了,但最大只能顯示到255,對應(yīng)的十六進(jìn)制是FF
void display(uint i) //顯示子函數(shù)
{
uchar sb,ss,sg;
sb=i/100; //分離數(shù)的百位
ss=i%100/10; //分離數(shù)的十位
sg=i%10; //分離數(shù)的個位
P0=table[sb];
P2=0x7f; //第一個數(shù)碼管點(diǎn)亮并顯示數(shù)的百位
delay(2);
P0=table[ss];
P2=0xbf; //第二個數(shù)碼管點(diǎn)亮并顯示數(shù)的十位
delay(2);
P0=table[sg];
P2=0xdf; //第二個數(shù)碼管點(diǎn)亮并顯示數(shù)的個位
delay(2);
}
歡迎光臨 (http://www.torrancerestoration.com/bbs/) | Powered by Discuz! X3.1 |