Phase and Frequency Correct PWM setup

Discussion in 'Embedded Systems and Microcontrollers' started by LaurenceR, Sep 26, 2016.

  1. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    I am using an Attiny 261 mcu. I have an analog to digital input with a variable 0-5 vdc pot. The result is left adjusted. Secondly I am using Timer1 for PWM (phase and frequency correct). COM1D1 and COM1D0 is set to 01 (clear on compare). OCR1C (Top count) is set to ob11111111. The analog ADCH is continuously being read into OCR1D. Also the clock is 8M with a prescaler of 8.
    It seems to work by adjusting the pot causing the duty cycle to increase and decrease. In this case I am lighting an led. However as you increase from 0 to 5vdc the light gets brighter then dimmer. Then it seems to reverse its direction. There seems to be a consistent middle region where it works but I can't understand why it jumps around and seems to reverse direction. Some times as you decrease it it will jump to full on.
    Not sure what is going on.

    Thanks for any help.
     
  2. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    388
    You're gonna have to supply the source code to get any answers on this.
     
  3. AlbertHall

    Well-Known Member

    Jun 4, 2014
    1,969
    388
    That's horrible. Can you try it again but putting it between code tags. Put the word CODE in square brackets before the code and /CODE in square brackets after it.
     
  4. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    Sorry about that. Is the attached file better.

    Thanks
     
  5. DickCappels

    Moderator

    Aug 21, 2008
    2,664
    634
    Or...if you did not put a resistor in series with the LED it might be that the LED is overheating which can cause it to dim, and eventually be destroyed.
     
  6. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    There are two LEDs. One on the mcu board that is driven by the 5vdc output signal. It has a 330 ohm resistor. The drive board for the larger LED lamp which is switched on and off with an N channel mosfet has another resistor for the larger LED. Even if you disconnect the driver board. the small LED behaves the same way. I really think its something in the timing or the program.

    One question I have is on the compare value. with phase and frequency correct pwm there is a top value (OCR1C) and there is a compare vale (OCR1D) that determines when the signal changes on the up count and I assume on the down count as well. My question is; is there another register to set for the down count. Didn't think so.
     
  7. DickCappels

    Moderator

    Aug 21, 2008
    2,664
    634
    The AVR that I am fighting right now uses single register that counts down after reaching the Top value. Bottom is zero.

    Check that your compare register value does not exceed your Top value. Don't know for sure what that would result in, but I can imagine that it could cause the phenomenon that you reported.
     
  8. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    Feeding in the results from the ADC (ADCH). variable duty cycle.
     
  9. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,132
    267
    Sounds like it's a bit / register alignment problem.

    If you see the value going up and down within the range, it's usually because the low order bits are working, but the high bit of the PWM register is mapped to a low order input bit.
     
  10. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    This sounds like the exact problem. Not sure what to do about it. I am reading the high register of the ADC into the OCR1D (8 bit compare value for PWM). The Top value for PWM is set at 0b11111111 (8 bit value). TCNT1 is a 10 bit counter.
    Thanks
     
  11. Sensacell

    Well-Known Member

    Jun 19, 2012
    1,132
    267
    You need to:

    1) read the data sheet carefully, make sure you understand how the data is formatted in the registers.

    2) check the PWM function independent of the ADC, set the registers manually and make sure it does what you expect.

    3) check the ADC, input some voltages and make sure the resulting data looks like you expect it to.

    Troubleshooting 101: Divide and conquer, break the system down into smaller chunks- test the small chunks.
     
  12. DickCappels

    Moderator

    Aug 21, 2008
    2,664
    634
    Don't know whether this applies to your case. I am running an ATTINY2313 16 bit counter in mode 10 -PWM Phase Correct, with the Top value set for 14 bits ($3FFF). Whenever the value I plug into the Compare register associate with the PWM output that is 1 count above the Top value the PWM output falls to 0%.
     
  13. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2

    Quite right, the devil is in the details.

    Thanks
     
  14. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    Yes, I think this is where the problem is. I have to match up the ADC output with the compare value input so the input value doesn't exceed the top value.
    Thanks for your help.
     
  15. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    It must be something in the way I am matching up the ADC output with the PWM compare input.
    First I am loading 0xFF into OCR1C (TOP) and 0x03 into TC1H for a constant frequency.
    In 10 bit mode I'm loading the low reg from ADC then the high reg into r22 and r23, then loading the high reg TC1H and low register to OCR1D which is supposed to be the compare value. still getting up and down results.
    Thanks again.
     
  16. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    In cases like this, try to debug each module separately. For example, force feed a series of known values to the pwm module to see if it behaves as expected.

    After that, link in the ADC module.
     
  17. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2
    I put fixed values into the compare register (OCR1D) and viewing on a scope found 99% at 0b00000011 and 50% on at 0b11111111. Inverting the output left it full on all the time. I suspect I am not setting the TOP value correctly. I am programming OCR1D as compare value.
    LDI PWa, ADCL
    LDI PWb, ADCH
    OUT TC1H, PWb
    OUT OCR1D, PWa

    The chip is atmel attiny261A. I have scoured the data sheet all I can see is that OCR1C is used as the (TOP) value I assume for all the registers.
    Thanks in advance.
     
  18. jayanthd

    Member

    Jul 4, 2015
    274
    29
    Check the PM.
     
  19. LaurenceR

    Thread Starter Member

    Feb 7, 2013
    97
    2

    What is PM?
     
  20. DickCappels

    Moderator

    Aug 21, 2008
    2,664
    634
    PM = Private Message
     
Loading...