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

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

/*******************************************/

//功能: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);

}


相关文章
蓝桥杯之单片机学习(十八)——555定时器与频率测量
蓝桥杯之单片机学习(十八)——555定时器与频率测量
620 0
蓝桥杯之单片机学习(十八)——555定时器与频率测量
|
1月前
|
传感器
单片机定时器中断
单片机定时器中断
|
8月前
【单片机期中测试】9.定时器实现简单的秒表程序
【单片机期中测试】9.定时器实现简单的秒表程序
123 0
|
8月前
【单片机期中测试】10.利用定时器实现pwm呼吸灯
【单片机期中测试】10.利用定时器实现pwm呼吸灯
99 0
|
9月前
|
C语言 Windows
C语言课设项目-51单片机-定时器和计数器
C语言课设项目-51单片机-定时器和计数器
166 0
|
9月前
|
存储 调度
51单片机--定时器与按键控制流水灯模式
51单片机--定时器与按键控制流水灯模式
292 0
|
10月前
|
C语言 芯片
51单片机学习--定时器--中断--串口通信
51单片机学习--定时器--中断--串口通信
190 0
|
11月前
【51单片机】使用STC烧录软件生成定时器的代码以及注意事项
使用STC烧录软件生成定时器的代码以及注意事项
164 0
【51单片机】使用STC烧录软件生成定时器的代码以及注意事项
|
11月前
【STC15单片机】定时器0+静态数码管显示0~9
【STC15单片机】定时器0+静态数码管显示0~9
171 0
|
11月前
|
存储
【51单片机】用定时器扫描矩阵键盘
【51单片机】用定时器扫描矩阵键盘
213 0