Pwm On Mikrobus Header Of Avr Iot Wg

About the project

Atmel Start only support PWM functionality for ERR (Red), DATA (Yellow) and CONN (green) of AVR iot WG board. Here we will learn to configure TCA timer in split mode to enable PWM on mikroBUS header of this board.

Project info

Difficulty: Easy

Platforms: AtmelMicrochip

Estimated time: 1 hour

License: MIT license (MIT)

Items used in this project

Hardware components

Avr-iot Wg Evaluation Board Avr-iot Wg Evaluation Board x 1
LED - Basic Red 3mm LED - Basic Red 3mm any type of led can be used as indicator x 1
Resistor 1K Resistor 1K resistor value depends on choice of led x 1

Software apps and online services

Atmel START Atmel START
Atmel Studio 7.0 Atmel Studio 7.0

Story

Atmel Start is very useful tool to configure and initialize all the peripherals but in AVR iot WG case it does not auto generate the PWM functions instead only generate initialization function for TCA0 in Split Mode. When TCA0 is used in Single Mode, it worked as 16-bit timer and can provide upto 3 wave outputs. These upto 3 possible wave out pins doesn't include the pwm labeled pin of mikroBUS header. 

In order to use the PWM pin of mikroBUS header of AVR iot WG we need to configure the TCA0 in Split Mode. In Split Mode we can get upto 6 wave outputs but only half of the timer bits for each output to manipulate the wave form.

To make our life easier we will use easy_pwm library which contains all the necessary functions for easy TCA0 Split Mode configuration and pwm generation.

STEP 1:

Create new Atmel Start project for AVR iot WG development board. (HELP)

STEP 2:

Goto "CLKCTRL" settings and change the "PDIV: Prescaler division" value to 2 under "Main Clock Configuration" as shown below.

STEP 3:

Now download the project without adding any software component.

STEP 4:

Also download the easy_pwm library from HERE.

Open the downloaded Atmel Start project in ATMEL STUDIO 7 and add the easyPWM library folder in PWM project directory. After adding the library in to our project we can see the source and header files of easy_pwm in "solution explorer". Now add the location of library files in C compiler settings for this projects by following the steps shown in image below.

STEP 5:

Modify the main.c file by adding the following code before main function

  1. #include "easy_pwm.h"
  2.  
  3. #define PD4 4 // pwm pin on mikroBUS header

After including easy pwm library and initializing the AVR in main function, we will configure the PD4 IO pin as pwm out.

  1. PORTD_set_pin_dir(PD4, PORT_DIR_OUT); // PD4 as output
  2. PORTD_set_pin_level(PD4, false); // PD4 set as low level output
  3. PORTMUX.TCAROUTEA |= (PORTMUX_TCA00_bm | PORTMUX_TCA01_bm); // Configure PD4 as PWM out pin

When IO pin is set as pwm out, we can initialize and enable the TCA0 in Split Mode. And set 50% duty cycle to generate square wave.

  1. easyPWM_init(SYSCLK_DIV_64); // initialize the TCA0 in split mode with clock = sysclk/64
  2. easyPWM_load_duty_cycle_ch4(0x80); // set pwm duty cycle at 50% for PD4
  3. easyPWM_enable_output_ch4(); // enable the PWM out at PD4

Now we can build and load on to AVR iot WG to test the pwm out by connecting the LED at pwm (PD4) pin of mikroBUS header.

For easy pwm testing, we can change the duty cycle in while loop.

Demo:

LED with series resistor of 1K is connected at PWM (PD4) pin of mikroBUS header and PWM is used to controlled the brightness of LED. 

Changing the brightness of 1 Watt LED using pwm.

List of all the register used in easyPWM library for Split Mode configuration and operation of TCA0.

source: Page 218 ATmega4808 Reference Manual (DC40002015A)

Code

Atmel Start configuration file of PWM project

Easy PWM library to get PWM out at PWM pin of mikroBUS header of AVR iot WG

Atmel START support only SPLIT mode initialization of TCA. And this easy to use PWM library provide all the necessary functions for easy configuration and enabling the PWM at PWM pin of mikroBUS header of AVR iot WG and upto 5 other IO pins.

Credits

Photo of mahmood_ul_hassan

mahmood_ul_hassan

Electronics Engineer with specialization in Robotics Engineering, seven years of experience in electronics research and development. I worked mostly on designing custom labview based automatic test and measurement equipment. Hobbyist, ARM enthusiast, DIY maker

   

Leave your feedback...