|
//用LED數(shù)碼管顯示計數(shù)器T0的計數(shù)值
#include<reg52.h> //包含51單片機寄存器定義的頭文件
sbit S=P3^2 ; //將S位定義為P3.2引腳
unsigned char Tab[ ]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //段碼表
unsigned char x;
/******************************************************************
函數(shù)功能: 延時約0.6ms
********************************************************************/
void delay(void)
{
unsigned char j;
for(j=0;j<200;j++)
;
}
/******************************************************************
函數(shù)功能:顯示計數(shù)次數(shù)的子程序
入口參數(shù):x
********************************************************************/
void Display(unsigned char x)
{
P2=0xbf; //P3.6引腳輸出低電平,DS6點亮
P0=Tab[x/10]; //顯示十位
delay();
delay();
delay();
delay();
P2=0x7f; //P3.7引腳輸出低電平,DS7點亮
P0=Tab[x%10]; //顯示個位
delay();
delay();
delay();
delay();
P2=0xff;
P0=0xff;
delay();
delay();
delay();
delay();
}
/*******************************************
函數(shù)功能:主函數(shù)
******************************************/
void main(void)
{
EA=1; //開放總中斷
EX0=1; //允許使用外中斷
IT0=1; //選擇負跳變來觸發(fā)外中斷
x=0;
while(1)
Display(x);
}
/**************************************************************
函數(shù)功能:外中斷T0的中斷服務程序
**************************************************************/
void int0(void) interrupt 0 using 0 //外中斷0的中斷編號為0
{
x++;
if(x==100)
x=0;
}
|
|