Skip to content

Instantly share code, notes, and snippets.

@loganwilliams
Created June 19, 2017 22:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganwilliams/d16d45ae2fec374e8300bbc17bd19338 to your computer and use it in GitHub Desktop.
Save loganwilliams/d16d45ae2fec374e8300bbc17bd19338 to your computer and use it in GitHub Desktop.
#define F_CPU 16000000UL
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
volatile unsigned int limit = 2;
volatile unsigned int count = 0;
int main(void)
{
//Setup the clock
cli(); //Disable global interrupts
TCCR1B |= 0<<CS11 | 1<<CS10 | 1<<CS12; //Divide by 1024
OCR1A = 15625; //Count 15624 cycles for 1 second interrupt
TCCR1B |= 1<<WGM12; //Put Timer/Counter1 in CTC mode
TIMSK1 |= 1<<OCIE1A; //enable timer compare interrupt
sei(); //Enable global interrupts
//Setup the I/O for the buzzer
DDRD |= 0xFF; //Set PortD Pin0 as an output
while(1) { } //Loop forever, interrupts do the rest
}
ISR(TIMER1_COMPA_vect) //Interrupt Service Routine
{
count++;
if (count > limit) {
count = 0;
// buzz once
PORTD = 0x01;
_delay_ms(200);
PORTD = 0x00;
_delay_ms(300);
// buzz again
PORTD = 0x01;
_delay_ms(200);
PORTD = 0x00;
limit = ((rand()) & 0x1F) + 5;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment