PIC单片机定时器的使用(16F877)

2019-04-15 11:49发布

/*******************************************/
//功能:60倒计时,蜂鸣器以及数码管的使用
//开发板:PIC16F877
/*******************************************/

include

define uchar unsigned char

define uint unsigned int

void delay(uchar a,uchar x);
uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x00};//数组
uchar LED[3];
void init();//初始化函数
void interrupt TMRO();//TMRO中断函数
uint counter;
uchar tmro_counter;
void LED_DISP();//数码管的显示函数
main()
{
init();
counter=60;
while(1)
{
//counter=60;
LED[1]=(uchar)(counter/10);
LED[2]=(uchar)(counter%10);
LED_DISP();
//counter–;
//if(counter==0)
//RE0=1;
}
} void init()
{
TRISC=0X00;
TRISA=0X00;
TRISD=0X00;
PORTD=0XFF;
TRISE=0x00;
RC5=0;
OPTION_REG=0X07;//256分频
TMR0=61;
INTCON=0Xa0;
}
void interrupt TMRO()
{
TMR0IF=0;
//TMR0=158;
tmro_counter++;
if(tmro_counter==20)
{
tmro_counter=0;
tmro_counter++;
counter–;
if(counter==0)
RE0=1;
}
TMR0=61;
}
void LED_DISP()
{
uchar timer=0;
uchar temp=0x80;
RA3=0;
//for(k=0;k<=50;k++)
for(timer=1;timer<=2;timer++)
{
RC4=1;
PORTD=temp;
RC4=0;
RC3=1;
PORTD=table[LED[timer]];
RC3=0;
temp>>=1;
delay(10,10);
RC4=1;
PORTD=0X00;
RC4=0;
delay(1,10);
}
}
/***********************
/*延迟函数***
/************************
void delay(uchar a,uchar x)
{
while(–a)
while(–x);
}