weird out put waveform of pwm

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0



#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...

 

Attachment(s): 

Rjchoudhary

Last Edited: Tue. Jul 5, 2022 - 12:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
	PORTB &=~(1<<PINB1);

	DDRC |= (1<<PINC0);
	DDRD|= (1<<PIND5);

	EIMSK |= (1<<INT0);
	EICRA |= /*(1 << ISC00)|*/(1 << ISC01);
	PORTD &=~ (1 << PIND2);

These are all wrong...you use equals (=) when setting up & initializing registers, not or. (|=)  Who told you to do this?

 

looks like you are stopping with inversion from what you want (you are sopping with high)...check your CSx bit settings for options

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. Jul 5, 2022 - 04:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

yes i have made the changes as you just told me. but the out puts are still same as it was earlier.

  

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 I am just trying to make a inverted and a non inverted pwm out put with that i can drive my igbt. Is my pwm init method is worth it for my desired result or I am making any mistake.

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Why not be simple and use timer hardware PWM?

 

Also use of floats, slow for some applications. 

Dave in TN

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is it safe for high frequency, like IGBTS because I will be dealing with 30KHZ to 66KH Frequency, If it is then can you suggest me any sample Code.

Rjchoudhary

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Using attiny85, it is possible to clock PWM at 64 mHz, and achieve 250kHz PWM at 8 bits.  Settings for that are in user manual, clock settings section.

 

Code that feeds PWM register, needs to be efficient. Not use of float vars. :)

Dave in TN

Last Edited: Sun. Jul 10, 2022 - 02:21 AM