سلام.
تولید موج مربعی 10% در مد fast pwm روی پایه oc؟نمی دونم کجای کد ایراد داره که شکل موج رو مربعی نشون نمی ده و شبیه سینوسیه.لطفا کمک کنید:
کد:
/*
2)PWM wave_pulse width %10 on pin PB7 or OC0(OCx)
compiler: iar avr 6.1 or later
Target : M2560
Crystal: 16.000Mhz
*/
#include <ioavr.h>
#include <intrinsics.h>
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x80; // PB7 (OC0A) output
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
PORTH = 0x00;
DDRH = 0x00;
PORTJ = 0x00;
DDRJ = 0x00;
PORTK = 0x00;
DDRK = 0x00;
PORTL = 0x00;
DDRL = 0x00;
}
//TIMER0 initialize - prescale:8
// WGM: PWM Fast-non invert
// desired value: 2MHz.... f=fcl/8 = 2MHz
void timer0_init(void)
{
TCCR0A = 0x00;
TCCR0B = 0x00; //stop
TCNT0 = 0x05; //set count
OCR0A = 0x19;
TCCR0A = 0x83; //start timer, f=fcl/8 = 2MHz, fast pwm, non invert
TCCR0B = 0x02; //start timer
}
#pragma vector = TIMER0_OVF_vect
__interrupt void timer0_ovf_isr(void)
{
TCNT0 = 0x05; //reload counter value
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
__disable_interrupt(); //disable all interrupts
XMCRA = 0x00; //external memory
XMCRB = 0x00; //external memory
port_init();
timer0_init();
MCUCR = 0x00;
EICRA = 0x00; //pin change int edge 0:3
EICRB = 0x00; //pin change int edge 4:7
PCICR = 0x00; //pin change int enable
PCMSK0 = 0x00; //pin change mask
PCMSK1 = 0x00; //pin change mask
PCMSK2 = 0x00; //pin change mask
EIMSK = 0x00;
TIMSK0 = 0x01; //timer0 interrupt sources
TIMSK1 = 0x00; //timer1 interrupt sources
TIMSK2 = 0x00; //timer2 interrupt sources
TIMSK3 = 0x00; //timer3 interrupt sources
TIMSK4 = 0x00; //timer4 interrupt sources
TIMSK5 = 0x00; //timer5 interrupt sources
__enable_interrupt(); //re-enable interrupts
//all peripherals are now initialized
}
int main(void)
{
init_devices();
while(1);
return 0;
}