定时器定时1S,每秒数码管显示加一:
#include<reg52.h>
//定义数码管位选通
sbit P21=P2^1;
sbit P22=P2^2;
sbit P23=P2^3;
unsigned int counter;
static unsigned int i;
unsigned char code Seg_Dis[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF};
/**************************************************************************************
** Function Name: _Delay
** Descriptions: 延时
** Input Parameters: ii
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void _Delay(unsigned int ii)
{
unsigned int jj;
for(;ii>0;ii--)
for(jj=0;jj<125;jj++)
{
;
}
}
/**************************************************************************************
** Function Name: _Delay500
** Descriptions: 延时
** Input Parameters: iii
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void _Delay500(unsigned int iii)
{
unsigned int jjj;
for(;iii>0;iii--)
for(jjj=0;jjj<60;jjj++)
{
;
}
}
/**************************************************************************************
** Function Name: _Delay500
** Descriptions: 延时
** Input Parameters: iii
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void main()
{
/******************************* 定时器0初始化 ******************************************/
EA = 1; //允许CPU中断
ET0 = 1; //定时器0中断打开
TMOD = 0x1; //设定时器0为模式1,16位模式
TH0 = 0xB1;
TL0 = 0xDF; //设定时值为20000us(20ms)
TR0 = 1;
while(1)
{
for(i=0;i<=999;i++)
{
counter = i;
_Delay(500);
}
}
}
/**************************************************************************************
** Function Name: KeyAndDis_Time0
** Descriptions: 定时器0中断外理、数码管显示
** Input Parameters: void
** Output parameters: void
** Returned Value: void
**--------------------------------------------------------------------
**************************************************************************************/
void KeyAndDis_Time0(void) interrupt 1 using 2
{
TH0=0xB1;
TL0=0xDF; //设定时值为20000us(20ms)
P0=Seg_Dis[counter/100];
P21=0;
_Delay500(5);
P21=1;
P0=Seg_Dis[(counter-(counter/100)*100)/10];
P22=0;
_Delay500(5);
P22=1;
P0=Seg_Dis[(counter-(counter/100)*100)-((counter-(counter/100)*100)/10)*10];
P23=0;
_Delay500(5);
P23=1;
}
数码管真的很耗系统的资源,占用了太多的CPU时间...
文章评论(0条评论)
登录后参与讨论