// Target : ATMEGA8
// Crystal: 2.0000Mhz
#include <iom8v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB = 0xFF;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//TIMER1 initialize - prescale:1
// WGM: 15) PWM fast, TOP=OCRn
// desired value: 40KHz
// actual value: 40.000KHz
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0xCF;
OCR1AH = 0x00;
OCR1AL = 0x31;
OCR1BH = 0x00;
OCR1BL = 0x31;
ICR1H = 0x00;
ICR1L = 0x31;
TCCR1A = 0x53;
TCCR1B = 0x19; //start Timer
}
//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 = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
init_devices();
//insert your functional code here
while(1);
}
用的快速PWM,波形产生模式方式15,COM1A1,COM1A0=01,为什么只有20KZ?按公式foc1apwm=Fclc_i/o除以N(1+TOP)算出来TOP等于0X31啊? |