我正在尝试使用Atmega644构建鼓机。鼓机将具有16个LED,这些LED将显示其所在节拍的第16个。用户应该能够设置BPM,因此对于不同的BPM,LED的更新将有所不同。
我在XTAL1和XTAL2设置了一个16 MHz的晶振。我使用FUSE计算器将FUSE位写入“全摆幅振荡器”并删除内部时钟分频8。然后设置TIMER0来执行此任务
#define F_CPU 16000000UL // 1MHz internal clock
unsigned int tempo = 250; // Default BPM of 120.
volatile unsigned int seq_timer = 0;
//set-up timer for 0.5 ms update
TIMSK0 = (1<<OCIE0A);
TCCR0A = (1 << WGM01) |(1<<WGM00);
TCCR0B = (0 << WGM02) | (0<< CS02) | (1 << CS01) | (1 << CS00); //divide by 64
OCR0A = 125;
ISR (TIMER0_COMPA_vect) {
if (seq_timer>0) {seq_timer--;}
}
//Update which 16th note we are at.
void step(void){
update_led();
step_number = step_number < 15 ? step_number +1 : 0;
}
//Shift the LED such that the LED corresponding to step_number is lit up.
void update_led(void) {
for (int j = 16; j>=0; j--)
{
if ((((1 << step_number))>>j) & 0x01) {
PORTD |= (1 << PD5); //Serial Input
}
else {
PORTD &= ~(1 << PD5);
}
PORTD |= (1 << PD4); //Read clock
PORTD &= ~(1<< PD4);
}
PORTD |= (1 << PD3); //Shift clock
_delay_ms(40);
PORTD &= ~(1 << PD3);
}
while (1)
{
if ((seq_timer == 0)) {
seq_timer = tempo;
step();
}
step函数调用“ update_led()”函数,该函数点亮与step_number相对应的LED。根据我的计算,这应该发生(16e6 /(64 * 250 * 125))= 8更新/秒。而是每秒更新4次。
我缺少一些重要的东西吗?