AVR單片機(jī)(學(xué)習(xí)ing)—(四)、ATMEGA16的定時(shí)/計(jì)數(shù)器—05
掃描二維碼
隨時(shí)隨地手機(jī)看文章
四、ATMEGA16的定時(shí)/計(jì)數(shù)器
四—(05)、T/C1的輸入捕獲實(shí)驗(yàn)
還是果斷貼圖片吧:
好了,今天就把第四章給結(jié)束了~~
ATMEGA16的定時(shí)/計(jì)數(shù)器也就這么多啊~~
會用就哦了~~
竟然忘記貼出程序來:擦擦擦
//------------------------------------------------------------------------------
//定時(shí)/計(jì)數(shù)器1的輸入捕獲實(shí)驗(yàn)
//外部按鍵開關(guān)來模擬脈沖的輸入,脈沖信號輸入到T/C1的輸入捕獲口PD6(ICP)。
//當(dāng)輸入捕獲口發(fā)生脈沖信號的變化時(shí),T/C1的值被傳送到ICR1,在輸入捕獲中斷函數(shù)中,可以將該值取出并顯示在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;
//------------------------------------------------------------------------------
//端口定義函數(shù)
voidport_init()
{
DDRA=0XFF;
PORTA=0X00;
DDRB=0XFF;
PORTB=0X00;
DDRD=0X00;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//T/C1初始化函數(shù)
voidtimer1_init()
{
TCNT1H=0X00;
TCNT1L=0X00;
ICR1H=0X00;
ICR1L=0X00;
TCCR1A=0X00;
TCCR1B=0X05;//啟動T/C1,輸入脈沖從PD6口輸入,下降沿觸發(fā),頻率選擇為1024分頻
}
//------------------------------------------------------------------------------
//芯片初始化函數(shù)
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;
}