timer1.c

来自「AVR单片机定时器1使用源程序,使用人员使用 ICCV7 for AVR编译后」· C语言 代码 · 共 88 行

C
88
字号
//ICC-AVR application builder : 2008-7-8 13:21:17
// Target : M16
// Crystal: 11.059Mhz

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

void port_init(void)
{
 PORTA=0x00;
 DDRA=0x01;
 
 PORTB=0x01;
 DDRB=0x00;
 
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 
 PORTD = 0x00;
 DDRD  = 0x00;
}

//TIMER1 initialize - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1Hz
// actual value: Out of range
void timer1_init(void)
{
 TCCR1B = 0x00; //stop
 //TCNT1H = 0x00 /*INVALID SETTING*/; //setup
 TCNT1 = 0xFB /*INVALID SETTING*/;
 
 //OCR1AH = 0x00 /*INVALID SETTING*/;
 OCR1A = 0xFD /*INVALID SETTING*/;
 
 OCR1BH = 0x00 /*INVALID SETTING*/;
 OCR1BL = 0x00 /*INVALID SETTING*/;
 
 ICR1H  = 0x00 /*INVALID SETTING*/;
 ICR1L  = 0x00 /*INVALID SETTING*/;
 
 TCCR1A = 0x00;
 TCCR1B = 0x0B; //start Timer  T/C1工作于普通模式,64分频
  
}

#pragma interrupt_handler timer1_compa_isr:iv_TIM1_COMPA
void timer1_compa_isr(void)
{
 //compare occured TCNT1=OCR1A
 PORTA = ~PORTA;		// PA0取反输出
}

#pragma interrupt_handler timer1_ovf_isr:iv_TIM1_OVF
void timer1_ovf_isr(void)
{
 
 TCNT1=0xFB;			// 重新设置TCNT0的初值
 PORTA = ~PORTA;		// PA0取反输出
}

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

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x14; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//
void main(void)
{
 init_devices();
 //insert your functional code here...
  while (1)
    {
    	// Place your code here
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?