본문 바로가기

만들기 / making/sensing workshop

audio sensor MCU code

audio sensor MCU code

#include <avr/io.h>
#include <util/delay.h>


#define FOSC 16000000
#define BAUD 9600
#define UBRR FOSC/16/BAUD-1


// Initialize USART0
void init_USART0(unsigned int baud)
{
UBRR0 = baud; // Set Baudrate
UCSR0C = (3<<UCSZ00); // Character Size 8 bit
UCSR0B |= _BV(RXEN0) | _BV(TXEN0); // Receiver and Transmitter Enable
}




// Set Receive Interrupt Enable
void setRXCIE_USART0()
{
UCSR0B |= _BV(RXCIE0);
}


// Receive 1 byte Data
unsigned char receive_1byte_USART0(void)
{
loop_until_bit_is_set(UCSR0A, RXC0);
return UDR0;
}


// Transmit 1 byte Data
void transmit_1byte_USART0(unsigned char data)
{
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = data;
}


// Transmit String Data
void transmit_str_USART0(char *str)
{
while (*str != 0) {
transmit_1byte_USART0(*str);
*str++;
}
}

// Transmit Four-Digit Integer
void transmit_4digit_USART0(int num)
{
unsigned char temp;
int digit = 1000;

while (digit != 0) {
temp = num / digit;
transmit_1byte_USART0('0'+temp);
num -= (digit*temp);
digit /= 10;
}
}


int main()
{
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz

ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading

// No MUX values needed to be changed to use ADC0

ADCSRA |= (1 << ADATE); // Set ADC to Free-Running Mode

ADCSRA |= (1 << ADEN); // Enable ADC
ADCSRA |= (1 << ADSC); // Start A2D Conversions

init_USART0(UBRR); // initialize USART0

int adcvalue;

while (1) {


if ('a' == receive_1byte_USART0()) {
adcvalue = (unsigned int)ADCH;
transmit_1byte_USART0((char)adcvalue);


}



}

return 0;
}

'만들기 / making > sensing workshop' 카테고리의 다른 글

Audio sensor module on Breadboard  (0) 2009.08.31
audio sensor processing code  (0) 2009.08.31
Day 7: Audio Sensor  (0) 2009.08.31
Day 5: Ultrasonic Sensor  (0) 2009.08.31
ATmega168 USART  (0) 2009.08.31