ant17 发表于 2009-4-27 11:23:44

AT88利用T0输出PWM波,时间不对,是编译器问题吗

设置如下:
//TIMER0 initialize - prescale:1
// WGM: PWM Fast
// desired value: 32uSec
// actual value: 32.000uSec
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x01; //set count
TCCR0A = 0x23;
TCCR0B = 0x01; //start timer
}

#pragma interrupt_handler timer0_compb_isr:16
void timer0_compb_isr(void)
{
//compare TCNT0=OCR0B
OCR0B=0x7F;
PORTD ^= 0x04;//pd2

}
内部时钟8M,无分频,ICC编译器
问题1:
使用application builder时,TCCR0A = 0x20; 但是我选择的是快速PWM模式,这里是bug吗?
问题2:
PD2脚按设置应该32us翻转一次,但实际值是520us,(此时TCCR0B = 0x01;);当TCCR0B = 0x02;PD2脚翻转时间还是520us,这里是怎么回事???
TCCR0B = 0x03时测得时间是对的2048us

下面是测试程序:
//ICC-AVR application builder : 2009-4-27 上午 10:31:21
// Target : M88
// Crystal: 8.0000Mhz

#include <iom88v.h>
#include <macros.h>

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

//TIMER0 initialize - prescale:1
// WGM: PWM Fast
// desired value: 32uSec
// actual value: 32.000uSec
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x01; //set count
TCCR0A = 0x23;
TCCR0B = 0x01; //start timer
}

#pragma interrupt_handler timer0_compb_isr:16
void timer0_compb_isr(void)
{
//compare TCNT0=OCR0B
OCR0B=0x7F;
PORTD ^= 0x04;//pd2

}

//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;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;

TIMSK0 = 0x04; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources

PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

void main(void)
{



init_devices();

while(1)
{

PORTD ^= 0x08;//pd3
}

}

、、、、、、、、、、、、、、、
糊涂了,麻烦马老师指点,寄存器的设置我反复对照过,不明白为什么就是不对

machao 发表于 2009-4-27 20:29:16

使用使用application builder生成代码后,再根据手册核对,ICC和CVAVR还是有BUG的,尤其是早的版本。
页: [1]
查看完整版本: AT88利用T0输出PWM波,时间不对,是编译器问题吗