Hej Troels ...
Her er noget kode til P/F korrekt PWM jeg har fusket med ...
Ideen er at lave en tæller som tæller i savtand ... og skifte niveau når tælleren er nulstillet på denne måde fjerner man glitches osv...
M.v.h. Lasse Madsen
// Timer 2 overflow interrupt service routine interrupt [TIM2_OVF] void timer2_ovf_isr(void) { static unsigned char pwm_temp=0,direction=1,channel_1=1,phase=0; static unsigned char channel_2=0,channel_3=0,channel_4=0,channel_5=0,channel_6=0; if ( pwm_temp == channel_1 ) OUT_AA = ~OUT_AA; if ( pwm_temp == channel_2 ) OUT_BA = ~OUT_BA; if ( pwm_temp == channel_3 ) OUT_CA = ~OUT_CA; if ( pwm_temp == channel_4 ) OUT_AB = ~OUT_AB; if ( pwm_temp == channel_5 ) OUT_BB = ~OUT_BB; if ( pwm_temp == channel_6 ) OUT_CB = ~OUT_CB;
// Sørg for at PWM tælleren kører i Savtands modus if ( pwm_temp == 100 ) { direction = 0; }
// Find Bund Positionen if ( pwm_temp == 0 ) { direction = 1;
if ( phase == 3 ) { channel_1 = pwm0; channel_2 = pwm1; channel_3 = pwm2; channel_4 = pwm3; channel_5 = pwm4; channel_6 = pwm5; phase = 0; } else phase++; }
// Skift Retning på tæller if ( direction == 0 ) pwm_temp--; else pwm_temp++;
TCNT2=0x9E; }