Menu

HPWM on AVR 328p

wn
2018-02-25
2018-05-07
  • wn

    wn - 2018-02-25

    I have been working with the HPWM mode on a 328p. I would like to use this in 10bit mode if possible. It looks like the library is set for 8 bit resolution. I am using OCA1 and OCB1 channels 3 / 4. I need the extra resolution to more accurately control some dc motors. I am using Timer 1 which is 16 bit so I think there is a way to do this.

    I think I found the answer looking at the pwm.h file.

    Passing in a WORD variable for the Duty cycle will set it to 10 bit resolution.

    Correction, that was for the PIC controllers.

     

    Last edit: wn 2018-02-25
  • kent_twt4

    kent_twt4 - 2018-02-25

    Yes, you will have to set up the AVR registers manually to work with the 10 bit mode PWM. I would reccomend using Mode3, or the phase correct 10 bit mode. That means setting the bits individually.

    'set the Mode
    WGM13 = 0:WGM12 = 0:WGM11 = 1:WGM10 = 1
    

    Next up would be deciding on what frequency to apply by selecting a prescale for Timer1, and applying the data sheet formula. Set the CS12, CS11, and CS10 bits. By default, I think a 7.8kHz frequency is presented for a 16MHz external oscillator?

    EDIT:

    'Set prescale to 1, should get 7.8kHz frequency with 16MHz osc
    Set CS12 Off
    Set CS11 Off
    Set CS10 On
    

    Now set the compare output mode for channels OCR1A and OCR1B for non inverted PWM.

    COM1A1 = 1
    COM1A0 = 0
    COM1B1 = 1
    COM1B0 = 0
    

    Try for a 50% duty cycle for each channel

    OCR1AH = 1
    OCR1AL = 255
    OCR1BH = 1
    OCR1BL = 255
    

    Let us know if that works/helps.

    Edit: My bad memory kicks in again. Need to set the prescale as default value of "0" is no clock source or TC1 stopped.

     

    Last edit: kent_twt4 2018-02-25
  • kent_twt4

    kent_twt4 - 2018-02-25

    So here is a demo on enabling the phase correct 10bit mode PWM. It ramps an led brightness up and down. No doubt timing parameters would need to be adjusted, minimum duty cycle on start, and possibly the frequency for a DC motor.

    'demo 10bit phase correct PWM mode with led on OC1A channel
    #chip mega328, 16
    
    'setup OC1A channel of TC1
    #define PWM1A PortB.1
    dir PWM1A Out
    
    'set to MODE 3, or phase correct 10bit PWM
    Set WGM13 Off
    Set WGM12 Off
    Set WGM11 On
    Set WGM10 On
    
    'Set prescale to 1, should get 7.8kHz frequency with 16MHz osc
    Set CS12 Off
    Set CS11 Off
    Set CS10 On
    
    'enable OC1A pin in phase correct PWM mode
    Set COM1A1 On
    Set COM1A0 Off
    
    Dim steps as Word
    
    Main:
    
      For steps = 0 to 1023
        SetDuty (steps)
        wait 3 ms
      Next
    
      For steps = 1023 to 0
        SetDuty (steps)
        wait 3 ms
      Next
    
    goto Main
    
    sub SetDuty (steps)
      OCR1AH = steps_H
      OCR1AL = steps
    end sub
    
     
  • wn

    wn - 2018-05-06

    I am working with the demo code. I think I need Fast PWM. The HPWM seems to work and does work in 8 bit mode. I am using a Frequency of 44 in HWPM. The board I am using is a custom board and has a 328pb chip. I have successfully modified the DAT file to leverage some of the extra features of the PB chip. Thanks for the help!

     
    • Anobium

      Anobium - 2018-05-06

      Hi. What mode have you made? I can incorporate these changes but I need to know what is missing/need to be corrected.

      :-)

       
  • kent_twt4

    kent_twt4 - 2018-05-06

    What Anobiums says :-)

    It would be helpful to understand what type or model of motor is involved for future reference and how it is being driven. Also if you could post code, it would be easier to follow along on how the registers have been set. 44Hz seems really low, have you actually tried to measure the frequency?

    Data sheet recommends using the phase correct for motors, but I have no deeper knowledge on why.

     
  • wn

    wn - 2018-05-07

    I was able to get the demo code to work. I needed to remove the defines for AVRTC1, etc. and of course enable the motors :). It is working great, I really appreciate the help.

     

Log in to post a comment.