###學單片機還沒半個月,程序多多少會有問題,歡迎指點
##仿真圖
##源碼.c
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit a = P1^0;
sbit b = P1^1;
sbit c = P1^2;
sbit d = P1^3;
uchar code table[]= {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共陽極0-916進制編碼
uint num=0;
uchar shi=0;
void display(uint num)
{
d=1; //共陽極數碼管高電平有效
P0=table[num%10]; //循環顯示0-9
delayms(5); //刷新
P0=0XFF;
d=0;
c=1; //共陽極數碼管高電平有效
P0=table[(num/10)%10]; //循環顯示0-9
delayms(5); //刷新
P0=0XFF;
c=0;
}
void jishu() interrupt 1
{
if(shi==10)
{ //10*50MS定時時間爲0.5秒
shi=0;
num++;
}
TH0=(65536-50000)/256; //重新賦值
TL0=(65536-50000)%256;
shi++; //50毫秒加1,加20次爲一秒
}
void delayms(uint xms) //延時程序
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
void main()
{
TMOD=0X01; /*中斷初始化*/
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; //中斷總開關
ET0=1; //請求中斷
TR0=1; //允許中斷
while(1)
{
a=0;//關閉其他位
b=0;
display(num);//調用函數
}
}