Search code examples
carduinoatmega

How to get 64Mhz clock for timer/counter4 on Atmega32u4 microcontroller?


So currently I need to have a PWM generated from timer/counter4 with a frequency of 64Mhz. My current frequency is the system clock's 16Mhz, I need to multiply this by 4 somehow.The data sheet claims that using the PLL clock you can get down to that frequency but it seems that messing with the PLL registers interferes with USB communication. I have attached my code if anyone knows how to fix or circumvent the issue.

int D4 = 9;

//PWM Waveforms
byte arr1[] = {0x0C, 0x0C, 0x0F, 0x0F, 0x0C, 0x0C, 0x08, 0x08, 0x08, 0x08, 
0x04, 0x04, 0x0, 0x0, 0x04, 0x04, 0x08, 0x08, 0x08, 0x08};
byte arr2[] = {0x00,0x8,0xF,0x8};

int i = 0; 
int b = 0; 
int g = 0;

void setup() {
  // put your setup code here, to run once:
  noInterrupts (); // no interrrupts during setup

//PLL Clock registers (Disabled so code will upload)
//  PLLCSR = (1<<PLLE);
//  PLLFRQ = (1<<PLLTM1)|(0<<PLLTM0);

//Timer/Counter1 used to controll PWM update
  TCCR1A = (1<<WGM11)|(1<<WGM10);
  TCCR1B = (1<<WGM13)|(1<<WGM12)|(0<<CS12)|(0<<CS11)|(1<<CS10);
  OCR1A = 0xA0;

  TIMSK1 = (1<<TOIE1);

//Timer/Counter4 used to generate PWM
  DDRC = (1<<DDC7)|(1<<DDC6);
  TCCR4A = (0<<COM4A1)|(1<<COM4A0)|(1<<PWM4A);
  TCCR4B = (1<<PSR4);
  TCCR4B = (0<<DTPS41)|(0<<DTPS40)|(0<<CS43)|(0<<CS42)|(0<<CS41)|(1<<CS40); 
  TCCR4D = (0<<WGM41)|(0<<WGM40);
  pinMode(D4, OUTPUT);

 interrupts (); // enable global interrupts
//Setting 4 bit resolution
 OCR4C = 0xF;
}

ISR (TIMER1_OVF_vect) // Updating PWM on timer4 using timer1 interrupt 
{ 
 if (b == 8){
    OCR4A= 0x08;
    if (g == 400){
     b = 0;
     g = 0;
    }
    else{
      ++g;
    }
  }
 else{
    OCR4A= arr1[i];
    if (i < (sizeof(arr1)-1)){
    i++;
    }
    else{
      i = 0;
      ++b;
    }
  }
}

void loop() {
  // put your main code here, to run repeatedly:

}

Solution

  • So currently I need to have a PWM generated from timer/counter4 with a frequency of 64Mhz.

    That's not going to happen. The maximum clock for the timer/counter is 64 MHz; this will result in a maximum output frequency of 32 MHz.

    That being said, the register values you need are:

    PLLFRQ = _BV(PLLUSB) | _BV(PLLTM1) | _BV(PDIV3) | _BV(PDIV1);
    PLLCSR = _BV(PINDIV) | _BV(PLLE);
    

    This sets:

    • PLL input scaler to ÷2 (making the input frequency 8 MHz, which is required)
    • PLL output frequency to 96 MHz
    • PLL USB divisor to ÷2 (making the USB frequency 48 MHz, which is required)
    • PLL timer divisor to ÷1.5 (making the timer frequency 64 MHz)