-2

So I have this DC motor which I want to reduce it's speed to 25% so obviously I used phase correct pwm to do so via the motor driver , I was able to do it through timer1 but my assistant professor wants me to do it with the 8-bit timer0 so I wrote the code and it ran but in full so my question is there are some calculation that must be done before writing the code and if there are , what are these calculations ?

Note : motor frequency is 100-250 Hz I am working with internal frequency 1 MHz and prescaler 1024

 #define F_CPU 1000000UL

 #include <avr/io.h>

 #include <util/delay.h>

int main(void)
{

DDRB  = DDRB | (1<<PB3); //set OC0 as output pin --> pin where the PWM signal is generated from MC

/*set CS02:0 to 101 to work with 1024 prescaler
set WGM0[1:0] to 01 to work with phase correct,pwm
set COM01:0 to 11 to Set OC0 on compare match when up-counting. Clear OC0 on compare match
when downcounting*/

    TCCR0 = 0b00111101;
   OCR0 = 64; // 25% DUTY CYCLE
    while (1)
{
    ;
}
      }

1 Answers1

0

Your question actually forces us to guess a bit - You're not telling enough facts to really help you. So, I'll guess you're using fast PWM, and guess you're controlling the motor speed with the PWM duty cycle.

Motor frequency and prescaler values are actually not so interesting - If you want to have speed reduction, you want to change the duty cycle, I assume.

A duty cycle of 25% of a 16-bit timer ist $10000/4 = $4000 (I guess that's what you set the Output compare register of your 16 bit timer to)

Obviously, on an 8-bit timer, a duty cycle of 25% is $100/4 = $40.

Also note what you need to write into TCCR0 to achieve the same thing from timer 0 is entirely different from what you write to TCCR1 for pretty much the same action - The bit positions are 100% different. Consult the data sheet, I think you got that wrong.

tofro
  • 5,640
  • 14
  • 31
  • Then next add an oscilloscope view on how your output voltage looks like. – tofro Jun 11 '17 at 13:01
  • Unfortunately I can't reach an oscilloscope , look my main problem is when I connected it to timer0 it worked but in full speed but with timer1 it worked as I hoped so what I think there's something wrong with my code @tofro – Evram Karam Jun 11 '17 at 13:05
  • TCCR0 is nothing more than the timer register which you fill to your selected mode and prescaler in another meaning for initializing the timer – Evram Karam Jun 11 '17 at 14:15