Selecting ATMega4808 Timer PWM Pins

Status
Not open for further replies.

riccardo

Member
Hello,

I am trying to set up an adjustable PWM signal on the ATMega4808. It's fairly new and the register config and features are a little unfamiliar. I have followed the "Getting started with TCA" app note, and I can see the output on the pin in the example. I am a little unsure how to use other pins..

The datasheet has "20.3.3.4.5 Port Override for Waveform Generation" and in the app note example it sas to set the pin as output where the wave should appear. The code below is the example code, but I also set a few more pins as outputs. I see nothing on these outputs however.

I think I am not quite understanding the description of how to use it. For example, if I wanted PA3 as a normal output for an LED, but was using the WGM, it sounds like PA3 will end up with the waveform on it, but I don't think this is the case. What am i missing?

C++:
#define PERIOD_EXAMPLE_VALUE (0x01A0)
#define DUTY_CYCLE_EXAMPLE_VALUE (0x00D0)
#include <avr/io.h>
/*Using default clock 3.33MHz */  //AFAIK my chip is running at 20MHz

void TCA0_init(void);
void PORT_init(void);

void TCA0_init(void)
{
    /* set waveform output on PORT A */
    PORTMUX.TCAROUTEA = PORTMUX_TCA0_PORTA_gc;

    TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm /* enable compare
    channel 0 */
    | TCA_SINGLE_WGMODE_DSBOTTOM_gc; /* set dual-slope PWM
    mode */

    /* disable event counting */
    TCA0.SINGLE.EVCTRL &= ~(TCA_SINGLE_CNTEI_bm);

    /* set PWM frequency and duty cycle (50%) */
    TCA0.SINGLE.PERBUF = PERIOD_EXAMPLE_VALUE;
    TCA0.SINGLE.CMP0BUF = DUTY_CYCLE_EXAMPLE_VALUE;

    TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV4_gc /* set clock source
    (sys_clk/4) */
    | TCA_SINGLE_ENABLE_bm; /* start timer */
}
void PORT_init(void)
{
    /* set pin 0 of PORT A as output */
    PORTA.DIR |= PIN0_bm;    // I see a 1kHz signal on this pin
    
    PORTA.DIR |= PIN1_bm;    // Nothing on this pin
    PORTA.DIR |= PIN2_bm;    // Nothing on this pin
    PORTA.DIR |= PIN3_bm;    // Nothing on this pin
}
int main(void)
{
    PORT_init();

    TCA0_init();

    /* Replace with your application code */
    while (1)
    {

    }
}
 
Thank you. I see that, but I can't find which pins...
"The compare channels can be used for waveform generation on the corresponding port pins" Where does it tell me which are the corresponding pins to the three compare channels?

p18 - 4.1 Multiplexed signals shows 0WOn (0 to 5) on PA0-5 So It looks like there is 5 possible pins in PORTA that can have the waveform output.

p139 15.3.5 PORTMUX Control for TCA - This shows how I can choose an alternate PORT for the output signal.

I'm still not seeing how I select just a single pin. For example, What would change in that example code to output the signal on PA2, or PA3 instead of PA0?
 
Have a look at the pinouts list on page 19 of the data sheet.

Under the TCA0 column, that shows bits 0-5 of the selected port corresponding to timer output WO0 to WO5
So it depends which timer compare/PWM output(s) you use or enable.

I'm guessing the "tca.single" stuff only enables or configures the first PWM with one output, so bit 0.

For a different pin, you presumably need to configure another / different PWM unit.
 
Use a modern PIC instead, and use PPS to allocate the pins to where you want

I bought a few of the Nano 4809 boards from MicroChip, because they were cheap - but haven't really done anything with them as there's little in the way of examples, and I just can't be bothered working that hard.
 
I think I got it

Canging the 0 to 2 here "TCA_SINGLE_CMP2EN_bm" and "CMP2BUF", selecting PIN2 as output worked. The other three output pins only work in dual PWM mode as far as I can tell so using WO2 in dual mode will mean WO5 is the other active pin. The frequency is 5x lower than what I calculated it should be, but I guess that's just some other config I need to look at elsewhere in my code.

RE: Modern PIC: I've not used a PIC in about 10 years, but I will have a look at that next time as that PPS does look handy.
 

It's REALLY, REALLY handy - but you do need to be aware that you can't use PPS on all the pins - and you need to check for the particular one you're using, as different PIC's have different PPS facilities (been there, changed PIC - oops - PPS doesn't work any more).
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…