本例用了2个定时器中断和1个外部中断,T0控制灯旋转,T1定时器溢出中断控制着声音输出。
proteus仿真:
Studio6.2编译截图:
程序清单:
- /*
- * GccApplication4.c
- *
- * Created: 2014-10-28 20:22:56
- * Author: Administrator
- */
- #define F_CPU 1000000UL
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <stdint.h>
- #define SPK()(PORTD^= _BV(PD7))
- volatile uint8_t FRQ = 0x00;
- uint8_t ON_OFF = 0;
- uint8_t Pattern = 0xE0;
- int main(void)
- {
- DDRC = 0xFF;
- DDRD = ~_BV(PD2);PORTD=_BV(PD2);
- TCCR0 = 0x05;
- TCNT0 = 256 - F_CPU/1024.0*0.1;
- TCCR1B = 0x01;
- MCUCR = 0x02;
- GICR = 0x40;
- sei();
- while(1)
- {
- FRQ++;
- _delay_ms(1);
- }
- }
-
- ISR(INT0_vect)
- {
- ON_OFF = !ON_OFF;
- if(ON_OFF)
- {
- TIMSK |=0x05;
- Pattern = 0x80;
- }
- else
- {
- TIMSK = 0x00;
- PORTC = 0x00;
- PORTD &=~_BV(PD7);
- }
- }
- ISR(TIMER0_OVF_vect)
- {
- TCNT0 = 256-F_CPU/1024.0*0.1;
- if(Pattern&0x80) Pattern=(Pattern<<1)|0x01;
- else Pattern<<= 1;
- PORTC= Pattern;
- }
- ISR(TIMER1_OVF_vect)
- {
- TCNT1 = 0xFE00+FRQ;
- SPK();
- }
|