Issue with Interfacing atmega328p with Bluetooth

596 Views Asked by At

I’m trying to interface atmega328p with Blutooth HC-0. I was following the example in Atmega328P datasheet in USART section. The code is simply trying to send a letter 'b' to Bluetooth terminal on mobile phone and receive a letter. If the received letter is 'a', an LED on PORTB0 will turn on, if the letter is 'c', the LED will turn off. But unfortunately nothing is working.

Connection between atmega328P and HC-05 is as follows:

HC-05 -> Atmega328P 
RXD -> pin3
TXD -> pin2
GND -> pin8
VCC -> pin7

Bluetooth light is turning on and off, and is connected successfully to mobile phone but no data is received, and when the letters 'a' and 'c' are sent nothing happens with LED connected to PORTB0.

The code is shown here. Thank you for any help!

#define F_CPU 16000000UL // Clock Speed
#define BAUD 9600
#define MYUBRR F_CPU/16/BAUD-1

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

char data;
char data2 = 'b';

void USART_Init(unsigned int ubrr)
{
    /* Set baud rate */
    UBRR0H = (unsigned char)(ubrr>>8);
    UBRR0L = (unsigned char)(ubrr);
    
    /* Enable receiver and transmitter */
    UCSR0B = (1<<RXEN0)|(1<<TXEN0);
    
    /* Set frame format: 8data, 2stop bit */
    UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}

char USART_Receive(void)
{
    /* Wait for data to be received */
    while ( !(UCSR0A & (1<<RXC0)) );
    /* Get and return received data from buffer */
    return UDR0;
}

void USART_Transmit(char data)
{
    /* Wait for empty transmit buffer */
    while ( !( UCSR0A & (1<<UDRE0)) );
    /* Put data into buffer, sends the data */
    UDR0 = data;
}

int main(void)
{
    DDRB  = 0b00000001;
    PORTB = 0b00000000;
    USART_Init(MYUBRR);
    while (1) {
        data = USART_Receive();
        USART_Transmit(data2);
        if (data == 'a') {
            PORTB = 0b00000001;
        } else if (data == 'c') {
            PORTB = 0b00000000;
        }
    }
}
0

There are 0 best solutions below