1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

atmega + HC05

Discussion in 'AVR' started by devjeetmandal, May 26, 2017.

  1. devjeetmandal

    devjeetmandal New Member

    Joined:
    May 26, 2017
    Messages:
    1
    Likes:
    0
    hello everyone..
    i was trying to interface HC-05 bluetooth module with atmega8. F_CPU=1MHz and baud rate for communication is 9600. i have a led at PORTB pin 0 and i will use any bluetooth app from play store to turn the led on or off.

    usart.h

    void usart_init(uint16_t ubrr_value)
    {
    UBRRL = ubrr_value;
    UBRRH = (ubrr_value>>8);
    UCSRB|= (1<<RXEN)|(1<<TXEN);
    UCSRC |= (1 << URSEL)|(3<<UCSZ0);
    }


    unsigned char usart_data_receive( void )
    {
    while ( !(UCSRA & (1<<RXC)) )
    ;
    return UDR;
    }



    hc-05.h

    char hc_05_bluetooth_receive_byte(void)
    {
    return usart_data_receive();
    }


    main.c

    int main(void)
    {
    DDRB=0x01;
    char received_data;
    usart_init(6);
    while(1)
    {
    received_data=hc_05_bluetooth_receive_byte();
    if(received_data == '1')
    {
    PORTB=0x01;
    }
    else if(received_data == '2')
    {
    PORTB=0x00;
    }
    else
    {

    }
    }
    }



    any guess why its not working?
     
  2. wkrug

    wkrug Active Member

    Joined:
    May 30, 2010
    Messages:
    280
    Likes:
    29
    Location:
    Germany
    I guess the problem ist here.
    while ( !(UCSRA & (1<<RXC)) )

    When one byte was received the controller hangs into this loop until an other byte was received.

    I would suggest You to make the receiving routine in an interrupt routine.
    Then create a small ring buffer.
    The interrupt routine fill this buffer and set a pointer.
    The main routine read that pointer and compare it with an readout pointer.
    When both are uneque the routine handles the new bytes and increment the readout pointer an all is fine.
     

Share This Page