Difference between revisions of "Accelerometer data over bluetooth"
Line 156: | Line 156: | ||
==See Also== | ==See Also== | ||
[[Btm-112 | Btm-112 ]] | [[Btm-112 | Btm-112 ]] | ||
+ | [[Category: Projects]] |
Revision as of 13:11, 4 March 2010
Introduction
The story begins in one nice suuny morning when i came across project where the guy took a glove from some old nintendo console and remade it with accelerometer and bluetooth module. I watched his youtube movie without even breathing. A few months later came the time when it was necessary to pick up a bachelors project so i can get a degree. Searching for a right topic for me i came along "Data transmission over bluetooth" topic and i said myself, "this would be great to use bachelors project to make something i want". And that's, how it began :)
Schematic
As you can see, i chosed the Atmega8 microprocessor. The reason was that i was playing a little bit with it before starting bachelors project, so i used my little base knowledge i already gained ;) What the Atmega does is that it reads and converts axes from accelerometer and send then over UART into bluetooth module.
Parts
Accelerometer Motorola/Freescale MMA7260Q
Microprocessor Atmel Atmega8
BT module Rayson BTM-112
Movie
<youtube width="330" height="220" align="left">YZGwKe6Cvhw</youtube>
May the source be with you
<enscript lang=c>
- define F_CPU 8000000UL
- define BAUD_RATE 19200
- include <util/delay.h>
- include <avr/io.h>
void init_adc(void) { ADCSRA = (1<<ADEN) | (0<<ADFR) | (0<<ADIE) | (1<<ADPS2) | (0<<ADPS1) | (0<<ADPS0);
ADMUX = (0<<REFS1) | (1<<REFS0) | (1<<ADLAR) | (0<<MUX3) | (0<<MUX2) | (0<<MUX1) | (0<<MUX0);
}
void init_uart(void)
{
UCSRA = (0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<PE) | (0<<U2X) | (0<<MPCM);
UCSRB = (0<<RXCIE) | (0<<TXCIE) | (0<<UDRIE) | (0<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC = (1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
//UBRRH = 0b00000000;
//UBRRL = 0x02;
UBRRH = (((F_CPU/BAUD_RATE)/16)-1)>>8; // set baud rate
UBRRL = (((F_CPU/BAUD_RATE)/16)-1);
}
unsigned char get_adc(unsigned char adc)
{
unsigned char value=0;
ADMUX = adc; ADCSRA |= (1<<ADSC);
while(!(ADCSRA & (1<<ADIF))) ;
value = ADCH;
ADCSRA &= ~(1<<ADIF);
return value; }
void transmit( unsigned char data )
{
while( !(UCSRA & ( 1 << UDRE ) ) )
;
UDR = data;
while (!(UCSRA & (1 << TXC ) ) ) // TXC == 0;
;
if((UCSRA & (1<<TXC))) // TXC != 0; UCSRA = (1 << TXC);
}
void decompose( unsigned char i )
{
unsigned char j;
unsigned int k = 1000;
while(i>0) { j=i/k; i -= k*j; j += 48; k /= 10; transmit(j); } }
int main(void)
{
unsigned char i; unsigned char adc=0x20; unsigned int value=0,data[10];
init_adc(); init_uart();
while(1) {
for(i=0;i<10;i++) { data[i] = get_adc(adc); }
value = 0; for(i=0;i<10;i++) value += (unsigned int) data[i]; value /= 10;
switch(adc)
{
case 0x20:
transmit('x');
transmit(':');
decompose(value);
transmit(' ');
break;
case 0x21:
transmit('y');
transmit(':');
decompose(value);
transmit(' ');
break;
case 0x22:
transmit('z');
transmit(':');
decompose(value);
transmit('\n');
break;
default: transmit('0'); break;
}
++adc;
if(adc == 0x23)
adc=0x20;
_delay_ms(30);
}
return 0;
}
</enscript>