ATTINY816 - Enabling WO4 on Pin PA4

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Hello all,

 

I am fairly new to programming with AVR, so I apologize if this is a very simple question.

I am trying to generate a PWM signal (WO4) on PA4 with Timer TCA0.

 

I thought this could be done by changing the PORTA.DIR to the appropriate pin and setting the PORTMUX.CTRLC to the default pin for but I am still unable to get the PWM signal out that specific pin.

I have a feeling I may be setting up the compare registers incorrectly, but I'm a bit to new to this to understand where the issue is.

 

Thank you!

 

Relevant Code:

// PWM test using timer A
#define F_CPU 2660000 // 2.66 MHz
#include <avr/io.h>
#include <util/delay.h>

#define PWM_DUTY_CYCLE	0
#define PWM_PERIOD 256

//------------------------------------------------
void timer_Init(void)
{
	// Set PA4 as output (waveform output)
	PORTA.DIRSET = PIN4_bm;
	
	// Port Multiplexer TCA0 Output 4 select
	PORTMUX.CTRLC = PORTMUX_TCA04_DEFAULT_gc;
	
	// Frequency: Fpwm_ss = Fclk_per/(N(PER+1))
	// Max resolution: Rpwm_ss = (log(PER+1))/(log(2))
	TCA0.SINGLE.PER = PWM_PERIOD;

	// CMP sets the duty cycle of the PWM signal -> CT = CMP0 / PER
	// DUTY CYCLE is approximately 50% when CMP0 is PER / 2
	TCA0.SINGLE.CMP0 = PWM_DUTY_CYCLE;
	
	// Counter starts at 0
	TCA0.SINGLE.CNT = 0x00;

	// Configuring CTRLB register
	// Compare 0 Enabled: Output WO4 (PA4)
	// Single slope PWM mode is selected
	TCA0.SINGLE.CTRLB = TCA_SINGLE_CMP0EN_bm | TCA_SINGLE_WGMODE_SINGLESLOPE_gc;

	// Using system clock (no frequency division, the timer clock frequency is Fclk_per)
	// Enable the timer peripheral
	TCA0.SINGLE.CTRLA = TCA_SINGLE_CLKSEL_DIV1_gc | TCA_SINGLE_ENABLE_bm;
}

//------------------------------------------------
int main(void)
{
	timer_Init();
	
	while(1)
	{
		for(uint8_t brightness = 0; brightness < PWM_PERIOD; brightness++)
		{
			TCA0.SINGLE.CMP0 = brightness;
			// wait to see the LED brightness change
			_delay_ms(150);
			
		}
	}
}

This topic has a solution.
Last Edited: Sun. Sep 12, 2021 - 10:47 PM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


This pin only works in split mode.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you kabasan!!

 

Just that little piece of information fixed everything by enabling split mode.

 

Cheers!