⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer1.c

📁 AVR单片机定时器1使用源程序,使用人员使用 ICCV7 for AVR编译后可直接使用
💻 C
字号:
//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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -