#define F_CPU 12000000UL #include <avr/io.h> #include <avr/interrupt.h> #include <string.h> #include <stdlib.h> #include <math.h> #include <avr/wdt.h> volatile uint32_t a=0; volatile uint32_t alpha = 0; volatile int b=0; volatile float comp=0.50,comp2=0.40; void PWM_INIT2() { DDRB|=(1<<PINB1) | (1<<PINB2); TCCR1B= (1<<WGM13) | (1<<CS10);//| (1<<CS10);// | (1<<CS20); TCCR1A=(1<<COM1B0) | (1<<COM1B1) |(1<<COM1A1);// | (1<<WGM11); //ICR1=90; } void T0_init() { TCCR0B |=(1<<CS00)|(1<<CS02); TCNT0 = 255; } void PWM_SET(uint16_t PWM_VALUE2) { double x0,fractpart0,intpart0; double x1,fractpart1,intpart1; double A1=0,B1=0; ICR1 =PWM_VALUE2; x0=((PWM_VALUE2*40.5)/100); fractpart0 = modf(x0, &intpart0); if (fractpart0 > comp) { A1=intpart0+1; } else if (fractpart0 <= comp) { A1=intpart0; } OCR1A =A1; x1=((PWM_VALUE2*59.5)/100); fractpart1 = modf(x1, &intpart1); if (fractpart1 > comp2) { B1=intpart1+1; } else if (fractpart1 <= comp2) { B1=intpart1; } OCR1B =B1; } void setWDT() { WDTCSR |= 0b00011000; WDTCSR = (1<<WDIE)|(1<<WDE)|(1<<WDP0)|(1<<WDP1)|(1<<WDP2);//sWDT | WDTO_2S; //Set WDT based user setting and for 2 second interval wdt_reset(); } int main(void) { wdt_disable(); DDRB |= (1<<DDB2)|(1<<DDB1); PORTB &=~(1<<PINB1); DDRC |= (1<<PINC0); DDRD|= (1<<PIND5); EIMSK |= (1<<INT0); EICRA |= /*(1 << ISC00)|*/(1 << ISC01); PORTD &=~ (1 << PIND2); PWM_INIT2(); T0_init(); TIMSK0 |= (1<<TOIE0); sei(); setWDT(); while (1) { wdt_reset(); if (b==1) { if ((a>=15)&&(a<=108)) { PWM_SET(90); }else if (a>108){ PWM_SET(0); b=0; a=0; } } } } ISR(INT0_vect) { b=1; } ISR(TIMER0_OVF_vect){ if (b==1) { a++; } TCNT0=255; //reseting the tcnt value to count from 255 to get continuous 100 micro sec of interrupt } ISR(WDT_vect){ }
The wave form I am expecting from this code is once ZCD is detected start the PWM and just before ZCD will happen again turn off PWM.
but the out put is very weird, it stays on even after I turn it off. Can anyone help me reach out the solution. I am attaching the wave form samples with this query.
thanks in advanced.
Moderator: the attached image...