本人用M16的TC1产生PWM信号,并且在产生信号的同时不断改变频率,但变频幅度很小,这时就出现一个很令人费解的现象,PWM信号会不时有断开的现象
PWM频率:28KHzOC1A、OC1B双路输出:想得到的信号是:
但是一旦改变频率,两路的信号会随即的中断6.5ms左右,不知其解,各位高手请指教
程序:
// Target : M16
// Crystal: 10.000Mhz
#include <iom16v.h>
#include <macros.h>
void port_init(
void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0b00110000;
}
//TIMER1 initialize - prescale:1
// WGM: 8) PWM phz freq correct, TOP=ICRn
// desired value: 28KHz
// actual value: 28.090KHz (0.3%)
void timer1_init(
void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x4E;
OCR1AH = 0x00;
OCR1AL = 0xB2-0x50;
OCR1BH = 0x00;
OCR1BL = 0x50;
ICR1H = 0x00;
ICR1L = 0xB2;
TCCR1A = 0xB0;
TCCR1B = 0x11; //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 Delay(
unsigned int t)
{
unsigned int i,j;
for(i=0;i<t;i++)
for(j=0;j<280;j++);
}
void main(
void)
{
unsigned char i;
unsigned char ICR1L_;
i=0;
ICR1L_=0xB2;
SPH=0;
SPL=220;
port_init();
init_devices();
timer1_init();
while(1)
{
i++;
if(i>9)i=0;
Delay(100); //延时
ICR1L=ICR1L_+i; //改变频率
}
}
此帖出自
小平头技术问答
一周热门 更多>