ATtiny – Willekeurige piepjes generator
Getest en geschikt voor: ATtiny13
Hieronder vind je een script om piepjes te genereren met een willekeurige tijdinterval, leuk als je iemand wil plagen, verstop het ding!
Sluit het volgende aan via onderstaand schema (in dit schema is ook de bedrading voor het programmeren via de ISP meegenomen):
De gegevens zijn in te stellen met:
1 2 3 |
unsigned int secMax = 600; // Maximum number of seconds until beep unsigned int freq = 6000; // Frequency of beep in Hz unsigned int msDuration = 1000; // Duration of beep in milliseconds (1000 = 1 sec) |
De code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
/* Random Beep Firmware for use with ATtiny13 Sam Mirsky 20-Aug-2008 To get the timer settings: This was created from slm.c by Mitch Altman, 26-July-07 Distributed under Creative Commons 2.5 -- Attib & Share Alike */ #include <avr/io.h> // this contains all the IO port definitions #include <avr/interrupt.h> // definitions for interrupts #include <util/delay.h> #include <avr/sleep.h> // definitions for power-down modes #include <avr/pgmspace.h> // definitions or keeping constants in program memory // This function basically wastes time void delay_sec(long int sec) { unsigned long int timer; while (sec != 0) { // this number is dependant on the clock frequency for (timer=0; timer <= 4200; timer++); _delay_ms(1000); sec--; } } int main(void) { unsigned int secMin = 180; // Minimum number of seconds until beep unsigned int secMax = 600; // Maximum number of seconds until beep unsigned int freq = 6000; // Frequency of beep in Hz unsigned int msDuration = 1000; // Duration of beep in milliseconds (1000 = 1 sec) unsigned long int sec; unsigned long int counts; DDRB = 0xFF; // set all PortB pins as outputs PORTB = 0x00; // all PORTB output pins Off // Setup OC0A (using Timer0) // 8-bit Timer0 OC0A (PB0, pin 5) is set up for CTC mode, toggling output on each compare // Fclk = Clock = 1.2MHz // Prescale = 1 // F = Fclk / (2 * Prescale * (1 + OCR0A) ) = Hz . Therfore OCR0A = Fclk/F/2/Prescale - 1 counts = 1.2e6/freq/2/1 - 1; TCCR0A = 0b01000010; // COM0A1:0=01 to toggle OC0A on Compare Match // COM0B1:0=00 to disconnect OC0B // bits 3:2 are unused // WGM01:00=10 for CTC Mode (WGM02=0 in TCCR0B) TCCR0B = 0b00000001; // FOC0A=0 (no force compare) // F0C0B=0 (no force compare) // bits 5:4 are unused // WGM2=0 for CTC Mode (WGM01:00=10 in TCCR0A) // CS02:00=001 for prescaler = 1 for(;;){ /* main event loop */ TCCR0A = 0b01000010; // COM0A1:0=01 to toggle OC0A on Compare Match TCCR0B = 0b00000001; // FOC0A=0 (no force compare) OCR0A = counts; // to output on OC0A (PB0, pin 5) _delay_ms(msDuration); TCCR0B &= 0b11111000; // CS02:CS00=000 to stop Timer0 (turn off audio in Right ear speaker) //scale = 32727/(secMax - secMin); sec = secMin + rand()/(32727/(secMax - secMin)); //_delay_ms(1000); //delay_sec(1); delay_sec(sec); } } |