新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 單片機(jī)C程序-優(yōu)化計(jì)時(shí)(只顯示有效位)

單片機(jī)C程序-優(yōu)化計(jì)時(shí)(只顯示有效位)

作者: 時(shí)間:2016-12-02 來(lái)源:網(wǎng)絡(luò) 收藏
本程序所用的原理圖下載:點(diǎn)這里,單片機(jī)芯片使用的stc89c52;找到相應(yīng)部分即可.這是一整個(gè)單片機(jī)開(kāi)發(fā)板的電路圖其他的忽略.

hex文件及其工程文件下載:http://www.51hei.com/f/youhuajs.rar以下是通過(guò)測(cè)試的源代碼:

本文引用地址:http://m.butianyuan.cn/article/201612/324639.htm

/**
*功能:實(shí)現(xiàn)一秒優(yōu)化計(jì)時(shí)(只顯示有效位),0~59;
*作者:徐冉
*日期:2013-05-02
**/
/************AT89C52-RC 單片機(jī)-51hei-5實(shí)驗(yàn)板***********/
/*******************51hei-開(kāi)發(fā)板**********************/

#include
typedef unsigned int uint;
typedef unsigned char uchar;
sbit wela = P2^7;
sbit dula = P2^6;
uchar counter,shi,ge;
uint num;
void init();
void display(uint num1);
void delay(uint xms);
//數(shù)碼管編碼
uchar code table[] = {
0x3F, //"0"
0x06, //"1"
0x5B, //"2"
0x4F, //"3"
0x66, //"4"
0x6D, //"5"
0x7D, //"6"
0x07, //"7"
0x7F, //"8"
0x6F //"9"
};
//主程序
void main()
{
init();
while(1)
{
display(num);
}
}
//定時(shí)器初始化程序
void init()
{
TMOD = 0x01;
TH0 = 0xB8;//精確定時(shí)20毫秒
TL0 = 0x00;
TR0 = 1;
EA = 1;
ET0 = 1;
}
//顯示子程序
void display(uint num1)
{
shi = num1 / 10 % 10;
ge = num1 % 10;

if(num1 < 10)
{
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xdf;
wela = 0;
delay(1);
}
else
{
dula = 1;
P0 = table[shi];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xef;
wela = 0;
delay(1);
dula = 1;
P0 = table[ge];
dula = 0;
P0 = 0xff;
wela = 1;
P0 = 0xdf;
wela = 0;
delay(1);
}
}
//延時(shí)程序
void delay(uint xms)
{
uint i,j;
for(i = xms; i > 0; i--)
for(j = 110; j > 0; j--);
}
//定時(shí)器0中斷服務(wù)子程序
void int_time0() interrupt 1
{
TH0 = 0xB8;
TL0 = 0x00;
counter++;
if(counter == 50)
{
counter = 0;
num++;
if(num == 60)
num = 0;

}
}



評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉