定时器初始定时为700us,按键使能定时器溢出中断后,在前300ms时间内的第次中断触发时间间隔都是0.7ms使得DoorBell()可以输出1000/(0.7*2)=714Hz的频率,在后500ms内的每次中断触发时间间隔为1ms(初值为-1000),DoorBell()输出1000/(1.0*2)=500Hz的频率。
Proteus 仿真的波形:咚
Proteus仿真的波形:叮
Studio6.2编译通过截图:
程序清单:
/*
* GccApplication3.c
*
* Created: 2014-10-27 19:50:51
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdint.h>
#define DoorBell()(PORTD^=0x01)
#define Key_DOWN()((PINB & 0x80) == 0x00)
volatile uint16_t soundDelay;
int main(void)
{
DDRB = 0x00;PORTB = 0xFF;
DDRD = 0xFF;
TCCR1B = 0x01;
TCNT1 = -700;
sei();
while(1)
{
if(Key_DOWN())
{
TIMSK = _BV(TOIE1); //允许T1定时器溢出中断
soundDelay = -700;
_delay_ms(400);
soundDelay = -1000;
_delay_ms(600);
TIMSK = 0x00;
}
}
}
ISR(TIMER1_OVF_vect)
{
DoorBell();
TCNT1 = soundDelay;
}
|