【问题标题】:How to get 64Mhz clock for timer/counter4 on Atmega32u4 microcontroller?如何在 Atmega32u4 微控制器上为定时器/计数器 4 获得 64Mhz 时钟?
【发布时间】:2018-07-06 01:30:50
【问题描述】:

所以目前我需要从定时器/计数器 4 生成一个频率为 64Mhz 的 PWM。我当前的频率是系统时钟的 16Mhz,我需要以某种方式将其乘以 4。数据表声称使用 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:

}

【问题讨论】:

    标签: c arduino atmega


    【解决方案1】:

    所以目前我需要从定时器/计数器 4 生成一个频率为 64Mhz 的 PWM。

    这不会发生。定时器/计数器的最大时钟为 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)

    【讨论】:

    • 谢谢,效果很好。但是这个 _BV() 是什么?
    • @Cyborgshark 它代表bit value,它评估为带有索引位集的位掩码。基本上#define bv(x) (1u &lt;&lt; (x)).
    猜你喜欢
    • 1970-01-01
    • 2010-10-27
    • 1970-01-01
    • 1970-01-01
    • 2018-03-25
    • 1970-01-01
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多