Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] atmega8+HC-05 not working

Status
Not open for further replies.

devjeetmandal

Newbie level 2
Joined
May 26, 2017
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
23
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

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

Code C - [expand]
1
2
3
4
char hc_05_bluetooth_receive_byte(void)
{
return usart_data_receive();
}



main.c

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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?
 
Last edited by a moderator:

nothing is showing in terminal.. i tried all the bauds. En pin was kept high and even tried with en pin low. nothings working. but HC-05 led blinks and i can connect too using a phone.
 

You are not configuring U2X register anywhere; moreover, adding numeral argument to UBRR instead of compiler's acronym to the bits or even a baud rate formula turns more difficult to check if something is wrong. Another point is that you are shifting 3 times UCSZ0 yielding a reserved word to the Atmega8 register UCSRC.

Why don't you take a look to some of the many codes available on the Web to give you some reference of what is usually made ? There are also a program "avrcalc" pretty useful to make the init code for you, not only for the UART peripheral but also for all others.
 

in your code UART intialisation use ubrr value is 6. USE 51 for 9600 baud rate

Unless me and him both we have made wrong calculations, for the above specified Oscillator 1MHz the baud rate of 9.600 is obtained with UBRR~6 (U2X=0) and with UBRR~12 (U2X=1):

baud.png
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top