因此,当前我需要从定时器/计数器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:
}
答案 0 :(得分:2)
所以目前我需要有一个由定时器/计数器4生成的PWM,频率为64Mhz。
那不会发生。定时器/计数器的最大时钟为64 MHz;这将导致最大输出频率为32 MHz。
话虽如此,您需要的寄存器值为:
PLLFRQ = _BV(PLLUSB) | _BV(PLLTM1) | _BV(PDIV3) | _BV(PDIV1);
PLLCSR = _BV(PINDIV) | _BV(PLLE);
此设置: