1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

ATMega328P generating PWM with timer2

Discussion in 'AVR' started by Cantafford, Feb 19, 2017.

  1. Cantafford

    Cantafford Member

    Joined:
    Jul 5, 2015
    Messages:
    251
    Likes:
    2
    Hello,

    I'm trying to generate a PWM singlal with timer2 module of ATMega328P. Say I want to generate a 50% duty cycle signal. I have attached a DC motor at the output OOC2A(RB3) to see the value of the signal.

    I wrote this code: Initialised TMR2 in fast pwm non inverting mode. My freq is 1Mhz and I used /8 prescaler option.
    From my understanding OCR2A is the duty cycle when TMR2 is in PWM mode. Now since TMR2 is an 8-bit timer from my understanding writing 255 in OCR2A means 100% duty cycle. And 127 means ~50% duty cycle. So I wrote 127 in the OCR2A and expected a 50% duty cycle. Please correct me if I'm wrong here.
    WHen I run the code the motor runs full speed no matter the value which I put in the OCR2A register :(. Am I missing something here?

    Code (text):

    /*
     * AVRGCC1.c
     *
     * Created: 2/19/2017 4:28:32 PM
     *  Author: Jarvis
     */

    #include <avr/io.h>
    #include <util/delay.h>
    #define F_CPU 1000000L

    void pwm_init()
    {
        // initialize TCCR2 as: fast pwm mode, non inverting
        TCCR2A |= (1<<COM2A1) | (1<<COM2A0) | (1<<WGM21) | (1<<WGM20);
        TCCR2B |= (1<<CS21); // clkT2S/8 prescale
        // OC2RA pin made as output
        DDRB |= (1<<PB3);
       
    }


    int main(void)
    {
       
        pwm_init();
       
        while(1)
        {
         OCR2A = 127;    
        }
    }
     
     
  2. wkrug

    wkrug Active Member

    Joined:
    May 30, 2010
    Messages:
    280
    Likes:
    29
    Location:
    Germany
    The code seem's to be right.
    Have You disconnected the motor and measured the signal with an oscilloscope?
    You can simulate the code with AVR Studio 7.

    You have attached a free wheeling diode to the Motor?
     
  3. Cantafford

    Cantafford Member

    Joined:
    Jul 5, 2015
    Messages:
    251
    Likes:
    2
    I'm simulating everything in proteus. :D I'm using an L293D driver and sending pwm signal into it's enable pin.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Cantafford

    Cantafford Member

    Joined:
    Jul 5, 2015
    Messages:
    251
    Likes:
    2

    I've edited some stuff in the code and it runs fine now for small duty cycles. If I give it a high duty cycle the motor does not start.
     
  6. wkrug

    wkrug Active Member

    Joined:
    May 30, 2010
    Messages:
    280
    Likes:
    29
    Location:
    Germany
    I think the PWM generation will not be the problem.
    In the L293 datasheet i've seen that the t/on time ist about 700ns for the half amplitude of output signal.
    I think that ist the time from input signal to output signal.
    For the enable pin is explicid not time given in the datasheet.

    I would suggest You to test the time from rising the enable pin to the output signal with an 2 channel oscilloscope.
    Possibly at one point the pulses from the PWM are to short for the L293, and so the Motor wouldn't start.
     
  7. Cantafford

    Cantafford Member

    Joined:
    Jul 5, 2015
    Messages:
    251
    Likes:
    2
    Yeah will do that once the components arrive. I have ordered them online. Will let you know.
     

Share This Page