本帖最后由 ddllxxrr 于 2014-10-21 08:17 编辑
本例是通过查询方式来实现循环显示,while循环检测定时/计数器中断标志寄存器TIFR的最低位TOV0是否为1,在溢出时TOV0被置位,通过写1可将其清零.
PROTEUS仿真:
Studio6.2截图:
程序:
- /*
- * GccApplication22.c
- *
- * Created: 2014-10-20 21:27:55
- * Author: Administrator
- */
- #define F_CPU 4000000UL
- #include <avr/io.h>
- #include <stdint.h>
- uint16_t Pattern = 0xFFFE;
- int main(void)
- {
- DDRC = 0xFF;DDRD = 0xFF;
- PORTC = 0xFF;PORTD = 0xFF;
- TCCR0 = 0x05; //预分频1024
- TCNT0 = 256 - F_CPU/1024.0*0.05; //晶振4MHz,0.05 s
-
- while(1)
- {
- while(!(TIFR&_BV(TOV0)));
- TIFR = _BV(TOV0); //通过对TOV0写1实现软件清零
-
- TCNT0 = 256 - F_CPU/1024.0*0.05;
-
- PORTC = (uint8_t)Pattern;
- PORTD = (uint8_t)(Pattern>>8);
-
- Pattern = Pattern << 1 | 0x001;
- if(Pattern == 0xFFFF) Pattern = 0xFFFE;
- }
- }
|