AVR USART Problem with Module CP2102

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

I am trying to send data from AVR to PC.I use Code vision 3.12
And select USART Interface with module CP2102.
When I read data received (In Matlab and also code vision terminal).
There is just €€ €  €€ € €€€ €€  €  €€ €€ €  €€ €€€€€€  
.I Almost Sure Hardware Implement is true.

Can you help me with where I'm wrong?

Below is my code:

 

    

 #include <mega16.h>
      #include <alcd.h>
      #include <delay.h>
      #include <stdio.h>

      void usart_init(){

      UCSRB=0x08;
      UCSRC=0x86;
      UBRRL=0x33;
      }

      void usart_send(unsigned char ch){

      while(!(UCSRA &(1<<UDRE)));
      UDR=ch;
      }


      void main(void)
      {
       unsigned char str[8]="Bashid";
       unsigned char strlentgh =8;
       unsigned char i=0;

      usart_init();

      while(1){
      usart_send(str[i++]);
      delay_ms(100);
      if(i>=strlentgh)
      i=0;
      }


      }

 

This topic has a solution.

 

 

 

Last Edited: Wed. Jul 29, 2015 - 10:29 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You don't give us many details.  What speed is your AVR running at?  How have you proven that?  What baud rate are you using?

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

What is your clock source, xtal or internal? 

The internal source is the root cause of many failures here on AVRFreaks forums, most were solved using an external xtal.

 

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

I use atmega16a at internal clock 1Mhz.
My baud rate is 9600-8bit-1stop-No parity.
And about internal & external clock source,I don't think change something.If it was a clock problem certainly show diffrent sign on output not just one sign either I am going to change my clock.I say the Result in future post.
Best Regards.
Bashid

 

 

 

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

You can NOT get 9600 baud with 1MHz. (unless you use U2X)
Either learn how to use U2X or choose 4800 baud.

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

I use atmega16a at internal clock 1Mhz.
My baud rate is 9600-8bit-1stop-No parity.

Yet you set UBRR to 51.  What does the datasheet chart Examples of Baud Rate Scaling say about that?  51 would be for 9600 with an AVR clock of 8MHz.  >>Within +/- 2%.  The datasheet says that the internal oscillator, without further tuning with OSCCAL, is +/- 3%.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Basic UART testing :

a) First loop-back the CP2102, to confirm you do have working Drivers and have opened the correct COM port etc

 

b) UARTS are LSB first so send 0xFF, then 0xFE, adding one more low each time,

Check the RX values in HEX on a terminal.

0xFF has the highest tolerance to clock differences, and will confirm  you are at least in the right ballpark.

0xFE etc place increasing demands on the clock, so you can get an idea of clock error issues by how many zeroes fail.

A system that uses only the lower nibble of each byte, has more clock tolerance than a full byte, & you need two stop bits on the host end.

 

c) A CP2012 can output any 12MHz/N Baud rate, so you can seek at match to a given poor RC Osc, if your system can tolerate the time.  OR you can AutoBAUD at the MCU end, by sending a known BYTE and timing that.

 

 

Last Edited: Tue. Jul 28, 2015 - 08:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello

1-I loop-back the module it's work correctly Means everything I write in terminal receive itself.

 

2-Then I use a 16MHZ external clock And Now I have this answer in matlab(fgets): þýðøüôýüþýøøñþý and in code vision terminal: ‎ü‏‎ًّ‎ü‏‎ًّ‎ü‏‎ًّ‎ü‏‎ًّ‎ü‏‎ًّ‎ü‏‎ًّ‎ü‏‎ًّ‎ü‏‎ًّ

According to talk above,With external crystal must be Right everything.

Attach my circuit,You think I must use two stop bits also with 16MHZ Clock?

Do you think I must Change My Atmega16a?

Is my circuit right?

 

 

 

Attachment(s): 

 

 

 

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

Your breadboard has not got any 100nF capacitors on the VCC/GND pins.

Your crystal has no 22pF capacitors.

 

How do you program the AVR?

What value are the fuses?

 

Just use the Codevision Wizard to make a "Hello World" program @ 4800 baud and 1MHz clock.

Then buy two 22pF and ten 100nF capacitors.   Program the fuses for "16MHz XTAL Oscillator".

Write a new Wizard program for 9600 baud and 16MHz clock.

 

You can do all the testing with the Codevision IDE Terminal.    If it does not show the correct text,   get it to display in Hexadecimal.

 

Incidentally,   the Wizard will write the correct initialisation for 9600 baud @ 1MHz.   So you can always run at 1MHz until you can afford to buy those 22pF capacitors.    You can run without a crystal.    It is always wise to use 100nF capacitors with every integrated circuit chip.    Ten 100nF capacitors are not expensive.

 

David.

 

 

Last Edited: Wed. Jul 29, 2015 - 07:25 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi

I don't thought this precision required.

 

Now I put to 22nF Cap for crystal And also 100nF for source.

 

Received data in code vision terminal in hex value is this:

 

And I also test in Hyper terminal And saw this:

I think they are show one thing: Data send to computer correctly But Don't Show Correct.

 

Although In Matlab and Code vision terminal(ASCII) Don't Show Anything Correct.

 

Can you show me the way?

 

 

 

 

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have you set the fuses to enable the 16MHz crystal? Also have you changed the value you are putting into UBRRL? For 9600 @ 16MHz the value is 103 (0x67).

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well you clearly can not have a crystal that could EVER oscillate with 22nF capacitors.
Just tell CV that you are running at 1MHz and 4800 baud.

You can see from the hex values that you are running very SLOWLY.
Have you read the current fuses? What type of programmer do you have?

David.

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

Thanks man.

It's work correctly after i set UBBRL Correctly.(According to my clock and desire baud rate)

 

Best Regards

Bashid

 

 

 

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

Have you tried with real 22 picoFarad capacitors. i.e. enable the 16MHz crystal oscillator with the fuses.

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

Yes, I use two 22pf with a 16MHz crystal And program Fuse For this crystal

And Now work correctly.

 

 

 

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

Well Done !!!

 

Sorry to be pedantic about component values.   I would certainly notice the difference between $1000 and $1000000.   So do crystals with capacitors.

 

I use the CV Wizard for an Xmega but seldom use it for a Mega.    However it is always worth trying when you come across a problem.    You can verify that the hardware is working correctly and then compare your code with the Wizard code to see where you went wrong.

 

David.

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

Thanks so much David. You Teach me some great notes.

 

smiley

Best Regards

Bashid

 

 

 

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

Your "font" renders reasonably on my PC running Firefox.

On a Android Tablet it is horrible !!!

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

laugh

Good Luck