Menu

Mega328PB example request

2022-11-11
2022-11-16
  • CARLO ALBERTO PATERLINI

    Hi, I need to have 3 pwm 16bit (or only 3 timers 16bit + relative interrupts). One good micro found within AVR's is ATMEGA328PB. Unfortunately this controller is very few documented. Someone could enclose an example with declarations of the 3 PWM and relative 16bit timers just to start with? Thanks a lot!
    charlie-pater

     
  • Anobium

    Anobium - 2022-11-11

    I would recommend following the Help - that shows how to setup HPWM for the AVRs.

    Have look and then come back here.

    :=)

     
  • CARLO ALBERTO PATERLINI

    Hi, first of all thanks for all the links. Unfortunately, however, it is not what I was looking for. I simply wanted a PWM, but also a 16bit timer with interrupt. The problem is being able to choose the frequency continuously from 50 to 10000Hz. The pwm duty can be any value. I chose the Mega328PB because I need 3 equal 16bit PWMs.

    Thanks a lot

    PS. It is quite difficult to understand how the constant avrtc1 and avrchan3 interact in the assemby. For example the prescaler can be changed?

     
  • Anobium

    Anobium - 2022-11-15

    @kent. You able to help?

     
  • Anobium

    Anobium - 2022-11-15

    @kent. You able to help?

     
  • Anobium

    Anobium - 2022-11-15

    @kent. You able to help?

     
  • kent_twt4

    kent_twt4 - 2022-11-15

    I do not have the mega328PB. For those devices I did not have, I had to make some assumptions during development. My extended 16 bit TC library (TC3, TC4, TC5) was based off the mega2650 Mega board.

    The mega328PB has three 16bit PWM's available, but only two PWM channels per TC instead of the three in the library.

    Sub 1kHz values are probably not going to be valid if my recollection is correct. This is due to the effort to keep the library small and compact without using Word variables in preprocessing. In other words, compatible with the AVRtiny devices as well as the mega devices.

    I am thinking that 50-10,000 kHz signal will have to have the TC registers manually operated. If the duty cycle is not important then I wouldn't use the PWM mode. Instead I would use the CTC mode (clear timer on compare match) and toggle the output for a 50/50 duty cycle.

    The HPWM library automatically changes the prescale values as appropriate, but only for 1, 8, and 64 as noted in Help.

    I will see if the way back machine can help with a CTC frequency example. Might take a bit.

     
  • Anobium

    Anobium - 2022-11-16

    Why not just write to the registers and hand craft the solution ?

    Examine the datasheet and write the code. :-)

     
  • Chris Roper

    Chris Roper - 2022-11-16

    The latest version of PICSIMLAB has the atmega328p available. It may prove useful for testing before committing your code to hardware.

     
  • Anobium

    Anobium - 2022-11-16

    I found this on the web.. but, it may not work.

    DDRB |= 6; //Set pin PB1 & PB2 as output for OC1A & OC1B
    // Timer 1 setup pwm
    TCCR1A = 0xA2; // Set OC1A & OC1B; WMG1[1]. Table 18-8 in Datasheet. & Table 18-9.
    TCCR1B = 0x18;// Set WMG1[2] & WMG1[3]; tABLE 18-9TCNT1 = 0;// clear counter
    ICR1 = 40000; // count of 40000 for a 20ms period or 50 Hz cycle
    OCR1A = 2900; // start in the middle between 900 & 4900
    OCR1B = 2900;//
    TCCR1B |= 2;//set clock to divide by 8 and start TABLE
    

    In GCB,,,
    TCCR1B |= 2 would be TCCR1B = ( TCCR1B AND 0xF8) OR 2

    Something like:

    #chip mega328pb, 20
    #option Explicit
    
    //Set pin PB1 & PB2 as output for OC1A & OC1B
    DIR PORTB.1 OUT: PORTB.1 = 1
    DIR PORTB.2 OUT: PORTB.2 = 1
    
    Dim ICR1 AS WORD ALIAS ICR1H, ICR1L
    Dim OCR1A AS WORD ALIAS OCR1AH, OCR1AL
    Dim OCR1B AS WORD ALIAS OCR1BH, OCR1BL
    Dim TCNT1 AS WORD ALIAS TCNT1H, TCNT1L
    
    // Timer 1 setup pwm
    TCCR1A = 0xA2           // Set OC1A & OC1B; WMG1[1]
    TCCR1B = 0x18           // Set WMG1[2] & WMG1[3]
    TCNT1 = 0               // clear counter
    ICR1 = 220              // count of 200 
    
    TCCR1B = ( TCCR1B AND 0xF8) OR 2
    
    OCR1A = 0            
    OCR1B = 0   
    
    do
    
        OCR1AL = (OCR1AL+1) MOD 220
        wait 50 ms
    
    loop
    
     

Log in to post a comment.