atmega 32 with hc-05

Go To Last Post
28 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i don't understand why my avr does not receive anything although my code runs on proteus good , i send characters from mobile app to hc-05 and then check these characters and do action base on them but avr doesn't respond at all , here is the code

 

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>

void forward()
{
    PORTA=0b00000101;
}
void backward()
{
    PORTA=0b00001010;
}
void right()
{
    PORTA=0b00001001;
}
void left()
{
    PORTA=0b00000110;
}
void stop()
{
    PORTA=0;
}
int main(void)
{
    DDRA = 0b00001111;
    uint16_t UBRRValue = 103;
    UBRRL = (uint8_t)UBRRValue;
    UBRRH = (uint8_t)(UBRRValue>>8);
    UCSRB = (1<<RXEN)|(1<<TXEN);
    UCSRC|= (3<<UCSZ0);
    while (1)
    {
    while(!(UCSRA&(1<<RXC)));
        
    
        
    switch(UDR)
        {
            case 'W':forward(); break;
        case 'S': backward(); break;
        case 'D':right(); break;
        case'A': left();  break;
        default : stop(); break;
        }

            
    }
return 0;
}

 

 

should i do something to hc-05 or add some code?

 

* Posted in the wrong forum. *

 

Last Edited: Thu. Mar 8, 2018 - 11:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

amr_pk wrote:
i don't understand why my avr does not receive anything

So how do you know that your AVR does not receive anything?

 

Quote:
although my code runs on proteus good
 

How is your Proteus connected to the HC-05 ?

 

i send characters from mobile app to hc-05 and then check these characters and do action base on them but avr doesn't respond at all

Again, how do you know that the characters actually reach the AVR?

 

How do you know that the AVR "doesn't respond at all" ?

 

Debugging Serial Comms:

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

first when avr receives the character i send, dc motor should rotate by using h bridge but there is nothing happens

 

second in proteus i use virtual terminal to send characters and the avr responds correctly

 

third i don't know whether the characters reach avr or not but as i said the dc motor doesn't rotate

 

Last Edited: Fri. Mar 9, 2018 - 12:16 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

amr_pk wrote:
i don't know whether the characters reach avr or not 

So don't you think that's the first thing you need to check?!

 

If nothing reaches the AVR, then the AVR will obviously do nothing - won't it?!

 

Similarly if what reaches the AVR is junk.

 

in proteus i use virtual terminal to send characters and the avr responds 

So how do you know that what you're sending is actually the same as what the HC-05 sends?

 

What happens if you use a terminal to send characters to the real AVR?

 

 

when avr receives the character i send, dc motor should rotate 

Only if the character is 'W' or 'S' or 'D' or 'A'

 

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From Post#1:

#define F_CPU 16000000

 

How do you know that the uC is running at 16MHz?

 

Did you change the fuses in the uC?

If so, to what values?

 

To verify that the uC is running at 16MHz, write a test program

that blinks a led once per second and then time the blinks.

(This is common practice for every new project.)

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So how to check that avr receive something , how can i do that?

And also how can i use terminal to send characters to real avr?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use 16mhz crystal capacitor but i didn't change anything in the fuses , should i do that ? And if so what should i do?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

amr_pk wrote:
So how to check that avr receive something , how can i do that?

And also how can i use terminal to send characters to real avr?

Did you not look at the links in #2 - under, "Debugging Serial Comms" ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Mar 9, 2018 - 09:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh i didn't see that , i will look at them and see , thanks!!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I Don't understand how can i implement this chart using tera term and i use hc 05 not esp , is this gonna work?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Go on! It's just a really simple block diagram.

 

Did you read the description on that page?

 

At the top, you have your AVR connected to your HC-05 (was "ESP" in the original):

 

+------+                                        +-------+
|      |                                        |       |
|    TX+--------------------------------------->+RX     |
|AVR   |                                        |  HC-05|
|    RX+<---------------------------------------+TX     |
|      |                                        |       |
+------+                                        +-------|

 

You want to be able to monitor what is being sent by the AVR from its TX to the HC-05's RX;

 

You also want to be able to monitor what is being sent by the HC-05 from its TX to the AVR's RX.

 

So you add connections to monitor those lines:

+------+                                        +-------+
|      |                                        |       |
|    TX+-------+------------------------------->+RX     |
|AVR   |       |                                |   ESP |
|    RX+<--------------------------+------------+TX     |
|      |       |                   |            |       |
+------+       |                   |            +-------|
               V                   V
            Monitor             Monitor
            AVR TX              HC-05 TX
           to HC-05              to AVR

 

Those lines are UART signals at logic levels - 3V or 5V (or whatever - depending on what voltage your system is running at).

 

To connect them to a PC, you need UART-to-USB converters - to plug into the PC's USB ports:

+------+                                        +-------+
|      |                                        |       |
|    TX+-------+------------------------------->+RX     |
|AVR   |       |                                |   ESP |
|    RX+<--------------------------+------------+TX     |
|      |       |                   |            |       |
+------+       |                   |            +-------|
               V                   V
          +----+-----+----+   +----+-----+----+
          |    RX   TX    |   |    RX   TX    |
          | USB-to-Serial |   | USB-to-Serial |
          |       1       |   |       2       |
          +-------+-------+   +-------+-------+
                  |                   |
               USB|                   |USB
                  |                   |
                  V                   V
          +-------+-------------------+-------+
          |   TeraTerm            TeraTerm    |
          |   Instance            Instance    |
          |       1                   2       |
          |                 PC                |
          +-----------------------------------+

 

The UART-to-USB converters each appear as a COM port on the PC - so you can connect a terminal app to each.

 

Thus one terminal will show you what is being sent by the AVR from its TX to the HC-05's RX;

The other terminal will show you what is being sent by the HC-05 from its TX to the AVR's RX.

 

 

The use of USB-to-UART converters has been discussed at length recently; eg,

 

https://www.avrfreaks.net/forum/connecting-atmega-notebook

 

 

https://www.avrfreaks.net/forum/mcu-usb-and-c-gui

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Mar 9, 2018 - 12:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you mean by(hc 05 sends to avr) that what i send from mobile app to hc 05 and then it sends it to avr?
Also i don't have uart to usb converter , is there another way to debug serial communication ?

Can i do that using usbasp ?

And also when i know that nothing reaches avr , what will i do then?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fill in your member profile with your location, there may be a freak near by that can loan you a USB/TTL serial adapter, although they are very cheap, you should have one or more.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

amr_pk wrote:

I use 16mhz crystal capacitor but i didn't change anything in the fuses , should i do that ? And if so what should i do?

 

By "16mhz crystal capacitor" I'm guessing crystal and capacitors.

 

 

Yes, if you want to use a crystal with the avr, you need to change the fuses.

 

From the ATmega32 datasheet (document 2503Q–AVR–02/11):

The device is shipped with CKSEL = “0001” and SUT = “10”.

The default clock source setting is therefore the 1MHz Internal RC Oscillator

with longest startup time.

 

You can change the fuses using your ISP programmer.

 

You can determine the proper fuse values using the online

Engbedded Atmel AVR Fuse Calculator (LINK).

 

Be careful in changing the fuses since it is possible to

"Brick" the uC if you use the wrong values.

("Brick": a condition where the uC doesn't work and won't

  respond to the ISP programmer.)

 

Edit:

While you're changing the fuses and since you are using an ISP programmer,

disable the  JTAG Interface which is enabled on new chips (default condition).

 

 

 

Last Edited: Fri. Mar 9, 2018 - 11:10 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Below is an example of fuse settings for ATmega32 with 16MHz crystal:

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

amr_pk wrote:
Do you mean by(hc 05 sends to avr) that what i send from mobile app to hc 05 and then it sends it to avr?

Not really.

 

The AVR neither knows nor cares where the data actually originates - all it sees is bytes arrving at its UART input.

 

You need to see exactly what those bytes are.

 

Also i don't have uart to usb converter

Then you will need to get some.

 

The linked threads show where you can obtain them very cheaply.

 

 

 is there another way to debug serial communication ?

Not without investing significantly more money in more advanced test gear.

 

This really is the cheapest, most basic approach to it!

 

 

And also when (sic) i know that nothing reaches avr , what will i do then?

You mean, "if" ?

 

If that is the case, then you will have to find out why nothing is reaching the AVR.

 

It does seem that you are tackling a project which is rather beyond your capabilities here.

 

Perhaps you should take a few steps back to the basics:

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So now i will get usb/ttl and connect it with the line that goes from hc 05 tx to avr rx and open tera trim and send to hc 05 from my app so what next?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

amr_pk wrote:
So now i will get usb/ttl

Do not get just one!

 

Get at least two.

 

In general, you need to be able to see both what is being sent and what is being received.

 

 

and connect it with the line that goes from hc 05 tx to avr rx and open tera trim and send to hc 05 from my app so what next?

Actually, before you do anything with the AVR, you need to just look at the HC-05 - is it actually sending what you expect?

 

 

 

Note that the name is TeraTerm.

 

 

EDIT

 

When you get your USB-to-TTL (aka USB-to-UART) converters, be sure that they are for the appropriate voltage - 3V or 5V

 

Preferably, get ones that work with both.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Sat. Mar 10, 2018 - 10:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Debugging is a key skill in any form of development - hardware, software, whatever.

 

You really need to learn how to think about a problem, analyse what's going on, and come up with solutions.

 

The key questions to ask when debugging are:

 

  1. What should be happening?
  2. What actually is happening?
  3. How can I explain the difference?

 

Once you have explained the difference between what should be happening and what actually is happening - you know what to fix to get the desired behaviour!

 

Note that a common reason why what should be happening is different from what actually is happening is that there is a mistake in the code, on in the hardware design; or some other false assumption about the system.

So always check your assumptions!

 

Also check your expectations: If your expectation of what should happen is incorrect - then it won't happen!

 

 

See also: https://www.avrfreaks.net/commen...

 

Some general articles on debugging:

 

http://www.8052mcu.com/faqs/120313

 

http://www.ganssle.com/articles/...

 

#HowToDebug

 

 

EDIT

 

Added expectations.

 

EDIT 2

 

Changed 8052.com link to 8052mcu.com - see http://www.8052mcu.com/forum/rea... for details

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Tue. Mar 20, 2018 - 11:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks man , i really appreciate your help and i'll try all what you told me to do , thanks again!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

While you're waiting for the converters to arrive, you could think about the possible differences between running in a simulator, and running in real hardware ...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I use usbasp not isp programmer , is there somoething will change?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

amr_pk wrote:

I use usbasp not isp programmer , is there somoething will change?

Your question is vague and does not make much sense, can you explain what you mean?

 

Also note that a USBASP is an ISP programmer.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Sat. Mar 10, 2018 - 05:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

larryvc wrote:
Your question is vague and does not make much sense

Indeed.

 

I can't see that the question has anything to do with any part of the discussion in this thread.

 

If it's a separate question, then start a separate thread.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No i just want to check that my fuses is well adjusted , maybe this is the problem , you understand me?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is there another way to debug serial communication ?

Well, in a pinch you can use a scope, though often not as convenient...send something like 0x55 which has alternating 0101010101 pattern, or something else easy to spot.  Some better/newer scopes have decoding built in, a great help.  A logic analyzer works even better, since it is more suited for such work.  The scope is the best to really see noise, glitches, etc & to perhaps determine what prevents the terminal from working.  When things are running smoothly, the terminal is best to see what is going back & forth & the responses.

 

A few years ago, I programmed up a two chan serial monitor that combined & caps-coded the streams into one output to make it somewhat easier to track sends & replies:  HELLOyesMOT?stoVOLTS?pped12.6vVALrVEpmstONall  

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You asked:

amr_pk wrote:

I use usbasp not isp programmer , is there somoething will change?

amr_pk wrote:

No i just want to check that my fuses is well adjusted , maybe this is the problem , you understand me?

Perfectly, but how would we know what post you are referencing with the question you posed?  Magic?  A crystal ball?  Get in the habit of quoting a little bit of the post that you are responding to, it will help us understand you even better! smiley

 

So now we can assume that you were asking Chuck99 that question, yes?  His posts are the only posts that referred to fuses.

 

You will need to use avrdude, or avrdudess, to read the fuse values from the chip.  Then compare them to the values shown for your chip at the Engbedded Atmel AVR Fuse Calculator (LINK).

 

If the fuse values you read from the chip are different than the fuse values shown at the Enbedded fuse calculator,  you can enter the chip fuse values into the calculator to see what features the chip fuse values have set.

 

EDIT: Or you can use a scope as avrcandies wrote above.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Sat. Mar 10, 2018 - 10:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

amr_pk wrote:

I use usbasp not isp programmer , is there somoething will change?

 

As larryvc stated, the USBASP is an ISP programmer.

What software are you using with the USBASP to program the ATmega32?

 

amr_pk in Post#7 wrote:

I use 16mhz crystal capacitor but i didn't change anything in the fuses ...

If the ATmega32 is new, then it is running at 1MHz not 16MHz.

If this is the case then the uC is running at 600 Baud not 9600 Baud.

 

In Post#5 I mentioned blinking a LED to test the uC clock frequency.

If you have a LED (and resistor), please do that - it is simple, fast,

and will tell you if the uC fuses need to be changed.

 

Here is a program to blink a LED.

#define F_CPU 16000000
#include <avr/io.h>
#include <util/delay.h>

 

int main(void) {

 

   // blink LED once per second

   while (1) {

      // insert code here to switch LED on

       _delay_ms (150);

 

      // insert code here to switch LED off

       _delay_ms (850);

   }

}

 

 

 

Last Edited: Sun. Mar 11, 2018 - 03:03 AM