Doci 发表于 2016-3-1 21:00:17

定时器外部输入脉冲计数,进步了中断

本帖最后由 Doci 于 2016-3-1 21:01 编辑

#include <iom16v.h>
#include <macros.h>

const unsigned char seg7_data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00};//0~F and "shut"


void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0x01; //PB0,是TIMER0的外部时钟输入脚(T0),需要设为输入,并且使能内部上拉
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}

//TIMER0 initialize - prescale:Falling edge
// WGM: Normal
// desired value: 1KHz
// actual value: Out of range
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x00; //set count
OCR0 = 0x0A; //set compare十进制的十,十次按键后匹配,进入这里
TCCR0 = 0x06; //start timer 时钟由T0 引脚输入,下降沿触发
}

#pragma interrupt_handler timer0_comp_isr:20
void timer0_comp_isr(void)
{
//compare occured TCNT0=OCR0
//按下键OCR0次后,会进入本中断
TCNT0 = 0x00;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();

MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x02; //timer interrupt sources 允许定时器0,比较中断
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

void main(void)
{
init_devices();
while(1)
{
PORTA = seg7_data; //一直显示TCNT0的值
}
}
T0外部脉冲计数,低电平有效,按理说应该计数计到10然后再重新计数,结果一直累加,到10不归零。

shuipaopao 发表于 2016-3-5 11:29:51

你利用CTC模式,TCNT0会自动清零,用不着在中断里面清零。
页: [1]
查看完整版本: 定时器外部输入脉冲计数,进步了中断