當前位置:首頁 » 編程語言 » 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; //十位和個位
}
}

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

熱點內容
sql寫數據 發布:2025-07-10 15:01:29 瀏覽:161
php入門手冊 發布:2025-07-10 14:42:24 瀏覽:791
手機如何設密碼鎖屏 發布:2025-07-10 14:17:06 瀏覽:803
java求絕對值 發布:2025-07-10 14:10:55 瀏覽:653
usb調試開關在哪裡安卓 發布:2025-07-10 13:59:55 瀏覽:78
資料庫維度 發布:2025-07-10 13:54:31 瀏覽:799
c語言位域的賦值 發布:2025-07-10 13:54:30 瀏覽:583
查成績密碼忘了怎麼辦 發布:2025-07-10 13:52:21 瀏覽:819
java死 發布:2025-07-10 13:51:30 瀏覽:996
車輛設施配置有哪些 發布:2025-07-10 13:42:28 瀏覽:820