當前位置:首頁 » 編程語言 » C語言52

C語言52

發布時間: 2023-06-12 09:01:51

❶ 用52單片機C語言編寫程序,實現00~59計時。要求用T0,工作方式1。

#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit la=P2^6; //段選
sbit wela=P2^7; //位選
uchar shi,ge,num1,num2; //變數
uchar code table[]={0x3f,0x06,0x5b,0x4f, //共陰極字型碼
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,
0x39,0x5e,0x79,0x71};
void delay(uint x) //延時子程序
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<120;j++);
}
void init()
{
TMOD=0x01; //設置定時器0的工作方式
TH0=(65536-50000)/256; //裝初值
TL0=(65536-50000)%256;
EA=1; //開總中斷
ET0=1; //開定時器0中斷
TR0=1; //啟動定時器0
}
void display() //顯示子程序
{
la=1;
P0=table[ge]; //送段選數據
la=0;
P0=0xff; //送位選數據前關閉所有顯示
wela=1;
P0=0xfe; //送位選數據
wela=0;
delay(5); //延時

la=1;
P0=table[shi];
la=0;
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
delay(5);
}
void main()
{
init();
while(1)
{
display();
}
}
void timer() interrupt 1
{
TH0=(65536-50000)/256; //重裝初值
TL0=(65536-50000)%256;
num1++;
if(num1==20) //到了20次,1秒時間到
{
num1=0; //清0重新計數
num2++;
if(num2==60)
num2=0;
P1=0x00;
delay(500);
P1=0xff;
shi=num2/10; //把一個2位數分離後分別送數碼管顯示
ge=num2%10; //十位和個位
}
}

試了的,可以放心,採納吧!

熱點內容
搭建自己的伺服器直播間 發布:2025-02-13 11:27:34 瀏覽:986
酷狗音樂試聽緩存刪了會怎樣 發布:2025-02-13 11:02:12 瀏覽:267
python游戲服務端 發布:2025-02-13 11:00:19 瀏覽:927
雲原生伺服器 發布:2025-02-13 10:55:34 瀏覽:827
linuxip命令查看ip 發布:2025-02-13 10:49:45 瀏覽:421
java基礎應用 發布:2025-02-13 10:44:53 瀏覽:711
linux內核搶占 發布:2025-02-13 10:36:32 瀏覽:890
家裝公司源碼 發布:2025-02-13 10:35:35 瀏覽:49
aspnet更新資料庫 發布:2025-02-13 10:35:34 瀏覽:385
海爾壓縮機不工作 發布:2025-02-13 10:15:32 瀏覽:224