// // hello.SPU0414HR5H.c // // SPU0414HR5H microphone hello-world // 9600 baud FTDI interface // // Neil Gershenfeld 11/17/15 // (c) Massachusetts Institute of Technology 2015 // // This work may be reproduced, modified, distributed, // performed, and displayed for any purpose. Copyright is // retained and must be preserved. The work is provided // as is; no warranty is provided, and users accept all // liability. // #include #include #define output(directions,pin) (directions |= pin) // set port direction for output #define set(port,pin) (port |= pin) // set port pin #define clear(port,pin) (port &= (~pin)) // clear port pin #define pin_test(pins,pin) (pins & pin) // test for port pin #define bit_test(byte,bit) (byte & (1 << bit)) // test for bit set #define bit_delay_time 102 // bit delay for 9600 with overhead #define bit_delay() _delay_us(bit_delay_time) // RS232 bit delay #define half_bit_delay() _delay_us(bit_delay_time/2) // RS232 half bit delay #define char_delay() _delay_ms(10) // char delay #define serial_port PORTB #define serial_direction DDRB #define serial_pin_out (1 << PB2)//|(1 << PB3) //PB# is for clap detection #define serial_pin_out_clap (1 << PB3)//|(1 << PB2) #define NPTS 100 // points in buffer ; This was 100 before, but now with 125 it leads to 98% workload void put_char(volatile unsigned char *port, unsigned char pin, char txchar) { // // send character in txchar on port pin // assumes line driver (inverts bits) // // start bit // clear(*port,pin); bit_delay(); // // unrolled loop to write data bits // if bit_test(txchar,0) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,1) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,2) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,3) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,4) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,5) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,6) set(*port,pin); else clear(*port,pin); bit_delay(); if bit_test(txchar,7) set(*port,pin); else clear(*port,pin); bit_delay(); // // stop bit // set(*port,pin); bit_delay(); // // char delay // bit_delay(); } void delay_3s(void) { uint8_t i; for (i = 0; i < 255; i++) _delay_ms(1); //for whatever reason it corresponds to 3s instead of 0.3s } int main(void) { // // main // unsigned char currADCL,currADCH,onoroff,waitornot,clap_2,thr; //delay_3s(); //a short delay before we start messing with the clock rate _delay_ms(300); //This is exactly 3s // // set clock divider to /1 // CLKPR = (1 << CLKPCE); //here we enable the chane of CLKPS bits CLKPR = (0 << CLKPS3) | (1 << CLKPS2) | (0 << CLKPS1) | (1 << CLKPS0); // /32 clock divider // // initialize output pins // set(serial_port, serial_pin_out); output(serial_direction, serial_pin_out); //my code for LED set(serial_port, serial_pin_out_clap); //pull up for the LED and turn it on output(serial_direction, serial_pin_out_clap); //clear(serial_port, serial_pin_out_clap); //turn led off // // init A/D // ADMUX = (0 << REFS2) | (1 << REFS1) | (0 << REFS0) // 1.1V ref | (0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0); // PB4, which is from the microphone input ADCSRA = (1 << ADEN) // enable ADC | (1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0); // prescaler /64 - the division factor between the system clock frequency and the input clock to the ADC. // start counter // TCCR0B |= (1 << CS00)|(1 << CS02); // /1024 prescaling for timer, corr. to 1024*32*255/8e6=1.044s period //and 8.188e-3s timer step clap_2=0; onoroff=1; waitornot=0; thr = 0x7D;//0x8D; // // // main loop // while (1) { // initiate conversion // ADCSRA |= (1 << ADSC); //ADSC value returns to zero after conversion is complete // // wait for completion // while (ADCSRA & (1 << ADSC)) ; // // save result // currADCL = ADCL; //2 extra bits currADCH = ADCH; //8 bit precision*/ if ((currADCL>thr) & (waitornot==0)) { //here we detect the first clap TCNT0 = 0; //counter value is zeroed TIFR |= (1 << TOV0); //TOV0 is cleared clap_2=0; waitornot = 1; } if (((TCNT0>24) & (TCNT0<44)) & (currADCL>thr)) { //from 100ms to 180ms after first clap it should be quiet waitornot=0; } if (((TCNT0>44) & (TCNT0<73)) & ((waitornot==1) & (currADCL>thr))) { //from 180ms to 300ms from first clap we can start expecting the 2nd one clap_2=1; } if (((waitornot==1) & (TCNT0==73)) & (clap_2==0)) { //by 300ms from first clap we should have had the 2nd one waitornot=0; } if ((TCNT0>73) & (currADCL>thr)) { //after the second clap (300ms) until the end it should be quiet again waitornot=0; } if ((TCNT0==0xFF) & (waitornot==1)) { if (onoroff==0) set(serial_port, serial_pin_out_clap); else clear(serial_port, serial_pin_out_clap); onoroff=1-onoroff; waitornot=0; } } }