AVR定时器与中断编程详解
1. 定时器轮询代码
首先来看一段定时器轮询的C代码:
#include <avr/io.h>
#ifndef TIFR
#define TIFR TIFR0
#endif
int main(void)
{
DDRB = (1 << DDB4); // Pin PB4 as output pin
TCCR0A = (1 << WGM01); // CTC mode
OCR0A = 0xFF; // Output compare value
TCCR0B = (1 << CS02) | (1 << CS00); // Prescaler CLK/1024 and start timer
while(1) {
if (TIFR & (1 << OCF0A)) { // Check for timer compare match
PINB = (1 << PORTB4); // Toggle pin PB4
TIFR = (1 << OCF0A); // Clear OCF0A flag
}
}
}
这段代码的功能是让连接在PB4引脚的LED闪烁。与之前的汇编代码相比,写入
超级会员免费看
订阅专栏 解锁全文
63

被折叠的 条评论
为什么被折叠?



