-5
\$\begingroup\$

I am trying to build staircase wave using atmega16.i have generated pwm signal that also can be controlled width automatically.i have used low pass filter with oc0 output pin of microcontroller.as a result a change is noticed in the wave form.the code and response curve are below; avr code:

MY code:

volatile uint16_t second=0;

volatile unsigned int minute=0;

volatile unsigned int val=0;

void init_timer1(void);

void init_timer0(void);

int main(void)

{

DDRB=0xFF;

init_timer1();

sei();

init_timer0();

while (1) {

val=minute*36.4;

OCR0=val;


 }
 }

void init_timer0(void)

{

TCCR0 |=(1<<CS00) | (1<<CS02) | (1<<WGM00) | 1<<WGM01 | 1<<COM01;

TCCR0 &=(~1<<CS01) | (~1<<COM00);


  }

void init_timer1(void)

   {  TCCR1B |=(1<<CS12);
      TCCR1B |=(1<<WGM12);
      TCNT1=0;
      OCR1A=430; 
     TIMSK |=(1<<OCIE1A);     

}

ISR(TIMER1_COMPA_vect) {

 second++;



if(second<7)
{ minute=second;
       }
else
 { minute--;

  }
  if(second==12)
  {
    second=0;}
     }

End the code ,

i have tried in different way but i have not gained my desired out put,please help anyone;

circuit diagram:

enter image description here

the output response is being shown in proteus oscilloscope bellow:

enter image description here

but when R=15 ohm C=4.7u then the output bellow;enter image description here since the OCR0 resister value is set to minute from code " val=minute*36.4; OCR0=val;"

so the width of pwm is being changed but i need staircase wave,

\$\endgroup\$
  • 2
    \$\begingroup\$ You might want to clean up your code a little bit. There's no need for you to be sloppy. Also, why is there an image inside the code? \$\endgroup\$ – Harry Svensson Nov 23 '17 at 12:58
  • 1
    \$\begingroup\$ A proper staircase function needs to contain many high-frequency harmonics. Which are filtered out by your filter. You need the filter for cleaning up the PWM signal. Since the PWM signal frequency from an ATMega is low frequency the only proper staircase function you can make will be at extremely low frequencies, think a couple of Hz. \$\endgroup\$ – Bimpelrekkie Nov 23 '17 at 13:18
  • 1
    \$\begingroup\$ Not only is the code formatted to be difficult to read due to excessive blank lines, but there isn't a single comment in the whole file! It's just plain rude to ask others to look at code this irresponsibly written. \$\endgroup\$ – Olin Lathrop Nov 23 '17 at 15:28
  • \$\begingroup\$ i have edited my code .some problem was occured.when the value of resistance and capacitance of my lowpass is scaled in ohm and micron respectively then that output is shown in proteus oscilloscope.but when those value is increased the output is like pwm wave. \$\endgroup\$ – Muzahid Karim Nov 23 '17 at 17:46
  • \$\begingroup\$ I will repeat myself, there's no need for you to be sloppy. Which you are. This is very similar to this scenario: You want us to taste some pie you've made, normally you cut off a part of a pie with a knife, put it on a plate together with a spoon and say "Hope it tastes good". What you've done is this:"Do you want some pie?", which I answer "Uhm.. sure", to which you throw it in my face. I got pie covering my entire face and get some in my mouth. You cannot grasp the problem, because I am indeed tasting the pie. - Your presentation is off the charts. I hope this analogy will reach you. \$\endgroup\$ – Harry Svensson Nov 24 '17 at 17:35
1
\$\begingroup\$

As you have learned, using PWM on a Mega to create a staircase is problematic. You have all sorts of filtering issues due to the default frequency being ~ 1KHz. As Bimpelrekkie points out in the comments, this is only designed for really low frequency stuff like the level on an LED. You can fiddle with the prescaling to change the PWM frequency, but not by vast amounts like orders of magnitude. And you'll still have low pass filtering issues. The reason PWM seems to work for humans, is either our persistence of vision (for LED levels) or the natural filtering that occurs due to thermal or mass inertia (incandescent lamp, electric motor).

There are two practical solution that will work.

  1. Use an off the shelf digital to analogue converter. You can get really simple ones that feature I2C or SPI interfaces. There are lots of internet examples of this.
  2. If you're adventurous, you can build an ersatz DAC with a resistance ladder. That looks like below and it's draw is that it's very very fast, but you need good (1% or better) resistors. However, you can only build the number of steps that you need. So you might only go for 4 bits of resolution leading to 16 output levels. You might still need a little filtering, but in this case it's the correct application for it and a simple RC or LC combination will work well. Better details at Analogue Devices.

ladder

2½. Long shot - Are you really sure that your scope output is showing the true waveform? Your picture looks like software on a PC. It's not a Keysight scope. Some PC style scopes have poor frequency response and a symptom of this is showing rectangular wave edges as curvey and /or slopey as all tends to a sine wave. Using the on board PC audio feature is notorious for this.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.