10月9日
定时器我们在中断里面也讲过了,就是在定时一个时间周期的时候进入中断来处理中断程序,可以作为计时、计数、扫描等用途。
同样的,在今天的这一讲里面,我们要做的是,还是采用设置断点的方式对
LED0进行处理,因为我们的
硬件平台还没有做好,所以目前只能这么将就着,但是原理都是一样的。
首先,我们还是老规矩,建立一个工程。
1、 打开iccavr
软件,新建一个工程。
如图所示:
工程建好以后,下面我们就开始选择目标
器件,在option里面选择mega16。
如图所示:
然后点击OK,选择图标开始配置
器件。
出现下图所示:
我们按照如上图配置好以后,选择PORTS,就出现了如下的界面:
这次我们选择TIMER0进行操作,则会出现如下的界面:
按照上图设置好以后,我们按下ok就会出现如下的程序:
//ICC-AVR application builder : 2008-10-11 7:38:57
// Target : M16
// Crystal: 7.3728Mhz
// 作者:南京华岳电子 练祥华
// 功能:学习定时中断0的程序
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0x40;
DDRA = 0x40;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 9.861mSec (1.4%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xB9; //set count
OCR0 = 0x47; //set compare
TCCR0 = 0x05; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0xB9; //reload counter value
}
//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 = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
}
将程序按照我们的要求进行修改,那么就会出现以下的程序:
//ICC-AVR application builder : 2008-10-11 7:38:57
// Target : M16
// Crystal: 7.3728Mhz
// 作者:南京华岳电子 练祥华
// 功能:学习定时中断0的程序
#include <iom16v.h>
#include <macros.h>
#define
LED0_CPL PORTA ^= (1 << PA6)
void port_init(void)
{
PORTA = 0x40;
DDRA = 0x40;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
//m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER0 initialize - prescale:1024
// WGM: Normal
// desired value: 10mSec
// actual value: 9.861mSec (1.4%)
void timer0_init(void)
{
TCCR0 = 0x00;
//stop
TCNT0 = 0xB9;
//set count
OCR0 = 0x47;
//set compare
TCCR0 = 0x05;
//start timer
}
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0xB9;
//reload counter value
LED0_CPL;
}
//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 = 0x01;
//timer interrupt sources
SEI();
//re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here...
while(1)
{
;
}
}
在avrstdio里面设置断点,如图所示:
此时 我们按下F5,全速运行,那么,我们就会看到在断点处如图所示:
然后再按下F5,就会继续出现如下图:
此时,我们可以看出定时中断的功能就完全实现了。
此程序仅仅是用来调试使用,并不能用来进行
LED0的点亮,因为眼睛有个视觉暂留的特性,所以使用这个程序的
LED0会一直亮的。