0

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;
        }
    }
}
ib.
  • 27,830
  • 11
  • 80
  • 100
  • 1. Check if LED toggle works (with simple delay for example) 2. Check if `data = USART_Receive();` is receiving anything at all 3. Check if you can send data (send letter every second or something) 4. If all that works, check if data is correct etc. – Rev Sep 09 '20 at 06:43
  • I think it's receiving data .. I made the LED on at the beginning at turns off when I send any data. and it turns off. But i can't receive any data from Bluetooth. also I don't know how the data received by the Bluetooth. – Ismaiel Ahmed Sep 10 '20 at 14:32
  • you have received data on your avr side but it's a wrong data that is good indication ... that is mean that the problem will be in the communication speed (baudrate )or communication setting (how many bit you will resceiv , **parity bit** , how many stop bits) make sure that your program and bluetooth module has the **same setting** and and **same budrate** .... (some time the bluetooth module defulte is one stop bit or the baudrate maybe 115200) if you have a **usb to TTL** module try to connect (bluetooth > TTL > pc terminal) and see what you send in a terminal screen on your pc – Ibram Reda Sep 10 '20 at 17:10

0 Answers0