- Thu Jul 09, 2009 10:15 am
#76663
Hello All,
I am a newbie so I apologize in advance if these questions are ridiculously obvious.
I am trying to make a bluetooth door lock that is controlled from my cell phone. The basic idea would be that I could send a password to a micro controller (via bluetooth) and if it was correct the microcontroller would tell a soleniod to flick open a door lock.
I am using the blueSMIRF and a atmega168 (with an 16 external crystal)
I now have the SMIRF connected to the atmega and can connect to the smirf with a bluetooth dongle from my PC. I am able to recieve information from the bluetooth when I use a feed back loop, but when I connect to the atmega I get garbage.
Any ideas why this is?
Here is the code I am using. Much of it is from the sparkfun tutorials:
I am a newbie so I apologize in advance if these questions are ridiculously obvious.
I am trying to make a bluetooth door lock that is controlled from my cell phone. The basic idea would be that I could send a password to a micro controller (via bluetooth) and if it was correct the microcontroller would tell a soleniod to flick open a door lock.
I am using the blueSMIRF and a atmega168 (with an 16 external crystal)
I now have the SMIRF connected to the atmega and can connect to the smirf with a bluetooth dongle from my PC. I am able to recieve information from the bluetooth when I use a feed back loop, but when I connect to the atmega I get garbage.
Any ideas why this is?
Here is the code I am using. Much of it is from the sparkfun tutorials:
Code: Select all
Any Help would be greatly appreciated.
#include <stdio.h>
#include <avr/io.h>
#define FOSC 16000000
#define BAUD 9600
#define MYUBRR (((((FOSC * 10) / (16L * BAUD)) + 5) / 10) - 1)
#define sbi(var, mask) ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask) ((var) &= (uint8_t)~(1 << mask))
#define STATUS_LED 0
//Define functions
//======================
void ioinit(void); // initializes IO
static int uart_putchar(char c, FILE *stream);
uint8_t uart_getchar(void);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void delay_ms(uint16_t x); // general purpose delay
//======================
int main (void)
{
uint8_t key_press;
ioinit(); //Setup IO pins and defaults
while(1)
{
key_press = uart_getchar();
if(key_press == 'g') sbi(PORTC, STATUS_LED);
if(key_press == 'X') cbi(PORTC, STATUS_LED);
}
return(0);
}
void ioinit (void)
{
//1 = output, 0 = input
DDRB = 0b11101111; //PB4 = MISO
DDRC = 0b11111111; //
DDRD = 0b11111110; //PORTD (RX on PD0)
//USART Baud rate: 9600
UBRR0H = MYUBRR >> 8;
UBRR0L = MYUBRR;
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
stdout = &mystdout; //Required for printf init
}
static int uart_putchar(char c, FILE *stream)
{
if (c == '\n') uart_putchar('\r', stream);
loop_until_bit_is_set(UCSR0A, UDRE0);
UDR0 = c;
return 0;
}
uint8_t uart_getchar(void)
{
while( !(UCSR0A & (1<<RXC0)) );
return(UDR0);
}
//General short delays
void delay_ms(uint16_t x)
{
uint8_t y, z;
for ( ; x > 0 ; x--){
for ( y = 0 ; y < 80 ; y++){
for ( z = 0 ; z < 40 ; z++){
asm volatile ("nop");
}
}
}
}