5

I am trying to get the pwm working on the Atmel Xmega256A3BU. I am not getting the desired PWM on the port pin. Here's my initialization for the PWM generation in the code.

void pwm_init()
{
 PORTC_DIR = 0x01;             //Set PC.0 as the output port 
 TCC1_PER = 0xFFFF;            //Set the period of the waveform 
 TCC1_CTRLB |= 0x03;           //Single slope mode     
 TCC1_CTRLB |= 0x10;           //channel selection CCAEN
 TCC1_CTRLA |= 0x02;           //clock selection clk/2
}
void main( void )
{
  pwm_init();
  TCC1_CCABUF = 0x7FFF;                      //set the duty cycle as 50%
  while((TCC1_INTFLAGS & 0x01) == 0);     //wait for the new compare value to be loaded 
  TCC1_INTFLAGS = 0x00;                   //clear the interrupt flag
  while(1);
 }

Here, I have just provided the snippet of my code which is running at 32MHz. So what else am I missing here ? Any help would be much appreciated. I am doing this in the AVR IAR workbench. So if anyone can share your PWM code that would be helpful too.

Jimit
  • 423
  • 5
  • 14

1 Answers1

3

Here's the code which solved my problem of PWM. Its a standalone code for PWM generation for ATXmega256A3BU. It uses timer-0 for PWM generation on PC.0 pin.

#include "ioxm256a3bu.h"
#include <stdint.h>
#include <stdio.h>

void pwm_init(void);

int main( void )
{
 pwm_init();
 TCC0_CCABUF = 0x7FFF;                      //set the duty cycle as 50%
 while((TCC0_INTFLAGS & 0x01) == 0);     //wait for the new compare value to be loaded 
 TCC0_INTFLAGS = 0x00;                   //clear the interrupt flag
 while(1);
}

 void pwm_init()
{
 PORTC_DIR = 0x01;             //Set PC.0 as the output port 
 TCC0_PER = 0xFFFF;            //Set the period of the waveform 
 TCC0_CTRLB |= 0x03;           //Single slope mode     
 TCC0_CTRLB |= 0x10;           //channel selection CCAEN
 TCC0_CTRLA |= 0x02;           //clock selection clk/2
}
Jimit
  • 423
  • 5
  • 14