|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
×
弄了好几天,发现定时器不工作。然后遍了这么个程序来看看它是否真的不工作,下载到片子上,发现灯一直都是亮的,没有产生循环效果。由于是刚接触128这个片子,所以有的寄存器不是很熟悉。
希望哪位好心人能帮我看看,问题到底出在哪里???
//ICC-AVR application builder : 2005-5-10 16:21:30
// Target : M128
// Crystal: 4.0000Mhz
#include <iom128v.h>
#include <macros.h>
unsigned char an_time_count=0;
unsigned char an_time_1s=0;
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 = 0x64; //reload counter value
an_time_count++;
if (an_time_count>100) {an_time_count=0;an_time_1s++;}
}
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0xFF;
PORTB = 0xFF;
DDRB = 0xF8;
PORTC = 0xFF; //m103 output only
DDRC = 0xFF;
PORTD = 0xFF;
DDRD = 0xF7;
PORTE = 0xFF;
DDRE = 0x00;
PORTF = 0xFF;
DDRF = 0xFF;
PORTG = 0x1F;
DDRG = 0x06;
}
//TIMER0 initialize - prescale:256
// WGM: Normal
// desired value: 10mSec
// actual value: 9.984mSec (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT0 = 0x64; //set count
OCR0 = 0x9C;
TCCR0 = 0x06; //start timer
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
XDIV = 0x00; //xtal divider
XMCRA = 0x00; //external memory
port_init();
timer0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EICRB = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK = 0x01; //timer interrupt sources
ETIMSK = 0x00; //extended timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main (void)
{
init_devices();
while(1)
{
PORTD=0xff; PORTB=0xff;
if(an_time_1s>=3)
{PORTD=0x00; PORTB=0x00;
an_time_1s=0;}
}
} |
|