标题: AVR单片机定时器做的门铃程序+电路 [打印本页]

作者: jinsheng7533967    时间: 2018-12-22 12:11
标题: AVR单片机定时器做的门铃程序+电路




#define F_CPU  1000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define INT8U   unsigned char
#define INT16U  unsigned int

#define DoorBell() (PORTD ^= 0x01)          /

#define Key_DOWN() ((PINB & 0x80) == 0x00)

volatile INT16U soundDelay;                 //两个不同取值分别对应于"叮","咚"

int main()
{
        DDRB = 0;      PORTB = 0xff;
        
        DDRD = 0xff;  
        
        TCCR1B = 0x01;

        TCNT1 = -700;
        
        sei();
        
        while(1)
        {
                if( Key_DOWN() )
                {
                        TIMSK = _BV(TOIE1);
                        
                        soundDelay = -700;
                        
                        _delay_ms(400);
                        
                        soundDelay = -1000;
                        
                        _delay_ms(600);
                        
                        TIMSK = 0x00;
                }
               
        }
        
}


ISR ( TIMER1_OVF_vect )
{
        
        DoorBell();
               
                TCNT1 = soundDelay;
               
}







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1