如何在 Atmega32u4 微控制器上获得 timer/counter4 的 64Mhz 时钟?

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

所以目前我需要从 timer/counter4 生成频率为 64Mhz 的 PWM。我当前的频率是系统时钟的 16Mhz,我需要将其乘以 4 somehow.The 数据 sheet 声称使用 PLL 时钟可以降低到该频率,但似乎弄乱 PLL 寄存器会干扰带USB通讯。如果有人知道如何解决或规避问题,我已附上我的代码。

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:

}

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

那不会发生。 timer/counter 的最大时钟为 64 MHz;这将导致最大输出频率为 32 MHz。

也就是说,您需要的寄存器值是:

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

这组:

  • PLL 输入定标器为 ÷2(使输入频率为 8 MHz,这是必需的)
  • PLL 输出频率达到 96 MHz
  • PLL USB 除数为 ÷2(使 USB 频率为 48 MHz,这是必需的)
  • PLL 计时器除数为 ÷1.5(使计时器频率为 64 MHz)