Hi,
I’m doing a balancing robot project using bluesmirf and bluetooth to rc my robot around but am having problems sending/receiving signals to/from the bluesmirf.
I’ve set up the bluetooth and windows xp is recognising it and is connected to com10.
If connected the tx-0 and rx-1 pins on the bluesmirf to the avr board and connected pins cts-1 and rts-0 together.
I can get hyperterminal to connect to the device and i can write commands to it. baud rate in the AVR and on the bluetooth are 9600bps. I’m not sure if this is right but i’m using the ATMD command to start sending data to the bluesmirf but getting no reply from the avr atmega64.
I basically want to continuously recieve a command from the hyperterminal without having to push enter after every command and have the avr board read the signals.
I’m using USART to enable the transmitter and receiver and have basic function that puts the lights on at PORTC once i recieve a interrupt (USART1_RX_vect) but it never enters that function. I’ll paste the code below. You’re help would be appreciated
*****this is my main function
#include “usart.h”
#include <avr/io.h>
int main(void){
DDRC = 0xFF;
//char temp;
usart_init(USART_BAUDRATE(28800,1));
while(1) {
//temp=usart_getc();
//usart_putc(‘a’)
//usart_puts(“blah”);
}
return 0;
}
***actual program
#include <avr/io.h>
#include <avr/interrupt.h>
#include <string.h>
#include <stdlib.h>
#include “usart.h”
char usart_buffer[USART_BUFFER_SIZE];
volatile unsigned char usart_buffer_pos_first = 0, usart_buffer_pos_last = 0;
volatile unsigned char usart_buffer_overflow = 0;
void usart_init(unsigned char baud_divider) {
// Baud rate selection
UBRR0H = 0x00;
UBRR0L = 3;//baud_divider;
// USART setup
UCSR0A = 0x02; // 0000 0010
// U2X enabled
UCSR0C = 0x86; // 1000 0110
// Access UCSRC, Asyncronous 8N1
UCSR0B = 0x98; // 0001 1000
// Receiver enabled, Transmitter enabled
// Enable interrupts globally
//USART1 is used for the Bluesmirf
// Baud rate selection
UBRR1H = 0x00;
UBRR1L = 95;//baud_divider 47;
// USART setup
UCSR1A = (1<<U2X); //0x02;// 0000 0010
// U2X enabled
UCSR1B = (1<<RXCIE) | (1<<RXEN) | (1<<TXEN); //0x98; // 1001 1000
// Receiver enabled, Transmitter enabled
// RX Complete interrupt enabled
UCSR1C = (1<<UCSZ1) | (1<<UCSZ0); //0x06; // 0000 0110 0x86;
// Access UCSRC, Asyncronous 8N1
sei(); // Enable interrupts globally
}
void usart_putc(char data) {
while (!(UCSR0A & (1<<RXC)))//(1<<UDRE))); // Wait until USART data register is empty
// Transmit data USB
UDR0 = data;
}
void usart_putc2(char data) {
while (!(UCSR1A & (1<<RXC)))//(1<<UDRE))); // Wait until USART data register is empty
// Transmit data Bluetooth
UDR1 = data;
}
void usart_puts(char *data) {
int len, count;
len = strlen(data);
for (count = 0; count < len; count++)
usart_putc(*(data+count));
}
void usart_puti(int number, int radix) {
char buffer[32];
usart_puts(itoa(number, buffer, radix));
}
char usart_getc(void) {
// Wait untill unread data in ring buffer
if (!usart_buffer_overflow)
while(usart_buffer_pos_first == usart_buffer_pos_last);
usart_buffer_overflow = 0;
// Increase first pointer
if (++usart_buffer_pos_first >= USART_BUFFER_SIZE)
usart_buffer_pos_first = 0;
// Get data from the buffer
return usart_buffer[usart_buffer_pos_first];
}
unsigned char usart_unread_data(void) {
if (usart_buffer_overflow)
return USART_BUFFER_SIZE;
if (usart_buffer_pos_last > usart_buffer_pos_first)
return usart_buffer_pos_last - usart_buffer_pos_first;
if (usart_buffer_pos_last < usart_buffer_pos_first)
return USART_BUFFER_SIZE-usart_buffer_pos_first
- usart_buffer_pos_last;
return 0;
}
SIGNAL(USART1_RX_vect) {
// Increase last buffer
//if (++usart_buffer_pos_last >= USART_BUFFER_SIZE)
// usart_buffer_pos_last = 0;
//if (usart_buffer_pos_first == usart_buffer_pos_last)
// usart_buffer_overflow++;
// Put data to the buffer
//usart_buffer[usart_buffer_pos_last] = UDR1;
PORTC = 0xFF; //turn lights on when it receives a signal. Just for testing.
unsigned char c;
c = UDR1;
usart_putc(c);
}