AVR单片机(学习ing)—(四)、ATMEGA16的定时/计数器—05
扫描二维码
随时随地手机看文章
四、ATMEGA16的定时/计数器
四—(05)、T/C1的输入捕获实验
还是果断贴图片吧:
好了,今天就把第四章给结束了~~
ATMEGA16的定时/计数器也就这么多啊~~
会用就哦了~~
竟然忘记贴出程序来:擦擦擦
//------------------------------------------------------------------------------
//定时/计数器1的输入捕获实验
//外部按键开关来模拟脉冲的输入,脉冲信号输入到T/C1的输入捕获口PD6(ICP)。
//当输入捕获口发生脉冲信号的变化时,T/C1的值被传送到ICR1,在输入捕获中断函数中,可以将该值取出并显示在LCD中。
#include"ioavr.h"
#include"intrinsics.h"
#include"delayics.h"
#include"bitics.h"
#include"lcdinit.h"
//------------------------------------------------------------------------------
uchar __flash title[]={"ICP text:"};
uchar __flash test[]={"ICP:"};
uintvalue;
uchar flag=0x00;
//------------------------------------------------------------------------------
//端口定义函数
voidport_init()
{
DDRA=0XFF;
PORTA=0X00;
DDRB=0XFF;
PORTB=0X00;
DDRD=0X00;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//T/C1初始化函数
voidtimer1_init()
{
TCNT1H=0X00;
TCNT1L=0X00;
ICR1H=0X00;
ICR1L=0X00;
TCCR1A=0X00;
TCCR1B=0X05;//启动T/C1,输入脉冲从PD6口输入,下降沿触发,频率选择为1024分频
}
//------------------------------------------------------------------------------
//芯片初始化函数
voiddevice_init()
{
__disable_interrupt();
port_init();
timer1_init();
TIMSK=0X24;//T/C1输入捕获中断使能、溢出中断使能
__enable_interrupt();
}
//------------------------------------------------------------------------------
//main
voidmain()
{
device_init();
lcd_init();
lcd_display_chars(0x80,title,9);
lcd_display_chars(0x80+0x40,test,4);
delay_s(1);
while(1)
{
lcd_display_onechar(0x80+0x40+4,(value/10000)+0x30);
lcd_display_onechar(0x80+0x40+5,(value000/1000)+0x30);
lcd_display_onechar(0x80+0x40+6,(value00/100)+0x30);
lcd_display_onechar(0x80+0x40+7,(value0/10)+0x30);
lcd_display_onechar(0x80+0x40+8,(value)+0x30);
}
}
//------------------------------------------------------------------------------
//TIMER1_CAPT
#pragma vector=TIMER1_CAPT_vect
__interrupt voidtimer1_capt()
{
value=(uint)ICR1L;
value|=(uint)ICR1H<<8;
}
//------------------------------------------------------------------------------
//TIMER1_OVF
#pragma vector=TIMER1_OVF_vect
__interrupt voidtimer1_ovf()
{
__enable_interrupt();
TCNT1H=0X00;
TCNT1L=0X00;
}