SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on the software and hardware for Atmel's STK standard.
By Ibrahim Hussiny
#173783
i am working on project with ATMEGA8 and Bluetooth module HC-05 . I want to send char 'a' to the Bluetooth module and then make PORTB in the micro controller high but it never gets high, i tasted the bluetooth module and the program that sends the char on arduino and it worked fine but it is not working with the micro controller. any help please?

the micro controller code:

#include <stdint.h>
#include <avr/io.h>

//#include <util/delay.h>
// Define baud rate
#define F_CPU 8000000
#define USART_BAUDRATE 9600
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)

void USART_vInit(void)
{

// Set baud rate
UBRRH = (uint8_t)(51>>8);
UBRRL = (uint8_t)51;


// Set frame format to 8 data bits, no parity, 1 stop bit
UCSRC = (0<<USBS)|(3<<UCSZ0);

// Enable receiver and transmitter
UCSRB = (1<<RXEN)|(1<<TXEN);

}



uint8_t USART_vReceiveByte()

{

// Wait until a byte has been received

while((UCSRA&(1<<RXC)) == 0);

// Return received data

return UDR;

}


int main(void)

{
DDRB = 0xFF ;
uint8_t u8Data;


// Initialize USART
USART_vInit();





// Repeat indefinitely
for(;;) //forever

{

// Echo received characters
u8Data = USART_vReceiveByte();


switch (u8Data) { // decode what char just came in
case 'a' :
PORTB =0xff;
break;
case 'b' :
PORTB =0x00;
break;

}

}

}
By Valen
#173837
I suspect that the initialization of UCSRC is not going well in your code. It shares the memory location with UBRRH according to the Atmega8(l) datasheet. A special bit (URSEL) needs to be set to signify you want to alter UCSRC instead of UBRRH. Which you don't:

See page 146 of: http://www.atmel.com/images/atmel-2486- ... asheet.pdf
Accessing UBRRH/UCSRC Registers

The UBRRH Register shares the same I/O location as the UCSRC Register. Therefore some
special consideration must be taken when accessing this I/O location.
Write Access When doing a write access of this I/O location, the high bit of the value written, the USART Reg-
ister Select (URSEL) bit, controls which one of the two registers that will be written. If URSEL is
zero during a write operation, the UBRRH value will be updated. If URSEL is one, the UCSRC
setting will be updated.

...