Menu

Setting up a timer along with a PWM output

Winch
2022-05-12
2022-05-20
<< < 1 2 3 > >> (Page 2 of 3)
  • Winch

    Winch - 2022-05-14

    No not going to use the USB!

     
  • Winch

    Winch - 2022-05-14

    Because of this change of clock I now also see that this affects the regular "wait 500 ms" for example. This is no longer correct and I now have to divide by 8!
    Or can that be solved in another way?

     
  • kent_twt4

    kent_twt4 - 2022-05-14

    I seem to be at a loss here, so if someone has an idea, or can duplicate on a mega32u4, heh, heh. I have used a couple interrupts at the same time before but this one has me stumped so far.

    Using freq=64 and counter of 10500 does give me about a 1 sec on time for the LED_Red. I do not have the scope pulled out. So using freq=2 and counter of 5000 very roughly speaking 1 sec on time. You wouldn't think changing HPWM freq would change the counter value in the timeroverflow interrupt. That's the rub.. There is a lot of crosstalk or capacitance on my breadboard and plus using DVM :-(

    Sorry for some questions to verify your setup once again. Plus attach your asm file for a look thru.

    What is your osc setting in #chip mega32u4,???
    A 16MHz crystal is being used?
    Does the Tone freq measure correctly?

    Thanks, back to ZZZZZZZZZZZZZ

    EDIT: oops didn't see the last two posts, we are on page two now. You have to DIV8 for GCB to correctly use wait, Tone and other commands. At least with trying to use TC0 for interrupt.

     

    Last edit: kent_twt4 2022-05-14
  • Winch

    Winch - 2022-05-14

    Normally I set the fuses as the Arduino Leonardo has set them. So far I have always had the best results with it.
    So these are the following settings:
    Extended = FB
    High = D8
    Low = FF
    Now with the change of clock the Low are at 7F as you can see in the picture.
    This is the change I made this morning!
    And indeed just like the Arduino Leonardo I also run my design on 16Mhz.

     
  • Winch

    Winch - 2022-05-14

    This is one of my test files from today.

     
  • kent_twt4

    kent_twt4 - 2022-05-14

    Using TC2 on the UNO for the timeroverflow, instead of TC0, seems to work OK. I think this will transfer over to the TC4, which the mega328p does not have. Different PS and so forth because TC2 is 8 bit, and TC4 is 10 bit. For the UNO board: CKDIV8 fuse is checked, #chip mega328p,2, and counter is 244.

    Some HPWM gymnastics, change freq and duty cycle at will!!! Again Interrupt period is very rough on my end.

    All the best.

    #chip mega328p,2
    #option explicit
    
    #define AVRTC1    'Specifies AVR TC1 associated with channel 3, 4 and 5
    #define AVRCHAN3
    #define AVRCHAN4
    
    #define INTLED PortD.2
    dir PortD.2 Out
    Set INTLED Off
    #define RedLED PortB.1    'OCR1A
    dir PortB.1 Out
    Set RedLED Off
    #define BlueLED PortB.2   'OCR1B
    dir PortB.2 Out
    Set BlueLED Off
    
    Dim freq, as byte
    Dim Counter as Word
    InitTimer2 OSC, PS_64
    
    On Interrupt Timer2Overflow Call BlinkLED
    StartTimer 2  'not req'd for Timer0
    Counter = 0
    
    Main:
    
      freq = 2
        HPWM 3,freq,32
        HPWM 4,freq,250
        wait 3 s
      freq = 64
        HPWM 3,freq,250
        HPWM 4,freq,32
        wait 3 s
    
    goto Main
    
    Sub BlinkLED
      Counter ++
      If Counter = 244 Then
        INTLED = Not INTLED
        Counter = 0
       End If
    End Sub
    

    EDIT: Revised program and associated comments 5/15/22
    EDIT: Revised once again 5/15/22

     

    Last edit: kent_twt4 2022-05-15
    • stan cartwright

      stan cartwright - 2022-05-14

      Why's it ; ----- Configuration
      #chip mega328,2 please Will it work for 328p,16 or did it?

       
      • Anobium

        Anobium - 2022-05-15

        The oscillator for the 328p and most of the AVR is assummed to be 16 mHz.

        It is very simple to add the oscillator control code to the compiler. Then, the range of oscillator frequencies would be support.

        At the moment AVR familys list below have internal oscillator support for mutliple frequencies.

        -ChipFamily 120 Subtype: 121
         AVR core version AVR8L, also called AVRrc, reduced core class microcontrollers. ATTiny4-5-9-10 and ATTiny102-104.
        
        - ChipFamily 120 Subtype: 122
         LGT microcontrollers.
        

        So, today if you tried 328p, 2... the compiler will use 2 Mhz as the frequency and the user will have to set the oscillator.


        How it could work for bare chip (not the UNO as this as 16mHz external oscillator)

        The ATmega328p has two internal oscillators, one clocked at 8mHz and one at 128kHz. A bare chip ( NOT AN UNO) the fuses of the ATmega328p are configured that the internal 8MHz oscillator can be used. This allows the ATmega328p to be operated without any external components at a supply voltage anywhere between 2.7V and 5.5V. So, a bare chip would require the fuses to selected the 8mHz internal oscillator (LOW=62&HIGH=D9&EXTENDED=FF&LOCKBIT=FF) . Then, an update to INITSYS() to set the clock frequency. To avoid unintentional changes of clock frequency, a special write procedure must be followed to change the CLKPS bits:
        1. Write the clock prescaler change enable (CLKPCE) bit to one and all other bits in CLKPR to zero.
        2. Within four cycles, write the desired value to CLKPS while writing a zero to CLKPCE.

        Easy, just needs someone to sit down and do this.

         

        Last edit: Anobium 2022-05-15
  • kent_twt4

    kent_twt4 - 2022-05-15

    Winch, meant mega328p, my bad. UNO has 16MHz crystal like your Leonardo? board.

    So, yes was able to finally figure out values for 16MHz clock that worked for my UNO board. CKDIV8 is NOT checked!

    config -----#chip mega328p,16
    TC2-----InitTimer2 OSC, PS_64
    Interrupt counter-----1024

    I have edited the previous example code. Heh, cannot have different channel frequencies for the same Timer, at the same time!!!

    My guess for the mega32u4 Timer4, to at least start??? I am always bad at guessing.

    config -----#chip mega32u4,16
    TC4-----InitTimer4 OSC, PS_128
    Interrupt counter-----2048

     
  • Winch

    Winch - 2022-05-15

    Dear Kent,
    Do I now understand correctly that I not select the CKDIV8?
    The last few comments are very confusing to me and at this point I don't really understand what to do to get this problem right.
    I also wonder what is it now? Is it because GC can't handle it well or is it the microcontroller that can't handle it?

     
  • kent_twt4

    kent_twt4 - 2022-05-15

    Winch,well you have me doubting myself now about the CKDIV8 selection. Having a slower clock did seem at one time to provide more consistent results. I am going to change my program back again to selecting the CKDIV8 fuse, mega328p,2, and counter of 244, for the UNO board. Feel free to experiment.

    Not having a mega32u4, or a Leonardo board puts me at a disadvantage. The question was asked if one could use HPWM and TimerOverflow interrupt at the same time, and the answer should absolutely be yes. How that is accomplished can be a tortured road.

    GCB ca n handle it. Between the Libraries, GCB Help, Demo programs, and the Forum we can make it happen.

     
  • Winch

    Winch - 2022-05-15

    Together with Evan, another problem was previously tackled by sending an Arduino Leonardo to him for testing purposes.
    I am certainly willing to do this again if needed.
    If this helps in this process let me know?

     
    • kent_twt4

      kent_twt4 - 2022-05-15

      I appreciate the offer but I don't think it is required? I have been a bit inconsistent here lately on the forum. Hopefully, we get your project tackled.

       
  • Winch

    Winch - 2022-05-18

    With the change of the fuse to divide the clock by 8 I got the problem that the UART no longer works properly. At least at a Baud rate of 9600. Do you also have to divide this by 8?

     
  • Winch

    Winch - 2022-05-18

    Ok sorry about that. I just gave my answer myself.
    At 1200 Baud I do have my communication!

     
  • kent_twt4

    kent_twt4 - 2022-05-18

    How important is that 1 sec interval, or that your timer interrupts add up to exactly 1 sec? How about using the Watch Dog Timer WDT for interrupts?

     
  • Winch

    Winch - 2022-05-18

    What I want to achieve is an independent counter that counts for 1 sec in the background! I need this to realize certain time delays.
    I haven't worked with the watchdog timer before?
    To be honest, I don't really know what to do with it either.
    Sometimes I come across it in texts but I've never delved into it further

     
  • kent_twt4

    kent_twt4 - 2022-05-18

    See chapter 8.9 of data sheet. The few main advantages of the WDT is that:
    1. Independent 128kHz clock.
    2. Can provide much longer timer interrupt intervals 16ms to 8s.
    3. Will wake up device from sleep, to save power, which is important for battery operations.

    Not so important if project is on line power. Could be useful if timer resources are short, or code conflicts arise with shorter interrupt cycles, otherwise, it is just extra baggage to worry about :-)

     
  • Winch

    Winch - 2022-05-18

    Okay I'm going to study it.
    Should I conclude from this that the problem is not yet solved for the time being?

     
  • kent_twt4

    kent_twt4 - 2022-05-18

    Going back to the very first conversations, problem solved when HPWM and the TimerOverflow heartbeat were put on different Timers (Timer1 and Timer3). No, Yes?

    I am a feared that I led you down a stray path when trying to use the Timer Interrupt other than Timer3. The mega328p that i am using, does not have a Timer3. I think I will order a Leonardo for any further developments.

    Edit: Ordered, now I will have trio next to the UNO, and Mega boards :-)

     

    Last edit: kent_twt4 2022-05-18
  • Winch

    Winch - 2022-05-19

    Indeed now use timer0 with 16/8 2Mhz clock in the fuse settings.
    So I can use PWM channels 3, 4, and 5.
    That's nice that you have your own Leonardo, because I'm curious if any further conclusions arise from that?

    I will also see if I can use that watchdog timer. I actually hope that I can change back that internal clock to the normal 16 Mhz speed.
    I'm not happy that the "wait" times don't match with the reality.

     
  • Winch

    Winch - 2022-05-19

    It might be a bit stupid, but can you help me with the following?
    With the last update I don't see the top menu in GC anymore.
    How can I reactivate it? It must be a hotkey but I can't figure it out?

     
    • stan cartwright

      stan cartwright - 2022-05-19

      There's "continue without code" see image0 then you get tool menu, see image1.

       

      Last edit: stan cartwright 2022-05-19
  • Winch

    Winch - 2022-05-19

    This is what I get after I choose "continue without code" no menu!

     
    • stan cartwright

      stan cartwright - 2022-05-19

      Where is Angel? I am using English version... try English or get Dutch language pack?
      No idea how to help. sorry.
      And no easy way to paste copied code!

       
<< < 1 2 3 > >> (Page 2 of 3)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.