Serial communication problem in atmega328

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

<

hi, to all the fellows

i am doing serial communication in atmega 328 using atmel studio 7 as IDE and usbasp for uploading program on my chip and ftdi rs232 USB to TTL converter for serial communication between laptop and atmega 328 and using herculus for serial monitor.

i am simply transmitting and receiving data through serial communication.

in this for debugging i have written portb command to check the error in code .

When i write UART_TxChar () it is going inside the  while loop as leds blink when i run this function it is not printing anything on the screen ...dont know why even though transmitting led on the ftdi chip blinks but nothing is printed on the screen

 

similarly when i use UART_RxChar() it is not goung inside the while loop as led dont blink when i run this function 

 

will be waiting for reply 

hoping to receive help as soon as possible

regards kunal

>

Attachment(s): 

<p>Kunal Gupta</p>

Last Edited: Tue. Nov 19, 2019 - 05:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kunalgupta wrote:
it is not printing anything

Maybe your AVR is sending "junk" - so the terminal just ignores it?

 

Have you tried any other terminal(s) ?

 

See Tips #2 & #3 regarding UARTs sending "junk" ...

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 have not tested other terminals .... can u suggest which one i can use

But previously it is working very fine this problem comes suddenly and i have not changed anything in the code..

u can check my code if want 

and also i baud rate is not the issue because it is working fine previously at 9600 

 

Attachment(s): 

<p>Kunal Gupta</p>

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

Please see Tip #1 for how to post source code.

 

Other terminals: TeraTerm, PuTTY.

 

Do you have an oscilloscope to see what's happening on the wires?

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 dont have oscilloscope but i think  can check with multimeter by seeing the voltage variations at the rx and tx pins of chip

according to u thier is no error in code???

 

<p>Kunal Gupta</p>

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

Very unlikely that a multimeter would tell you anything useful.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

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

Kunalgupta wrote:
i think  can check with multimeter by seeing the voltage variations at the rx and tx pins of chip

No; as Jim says, a multimeter won't show you anything useful there at all - the signals are far too quick!

 

according to u there is no error in code???

Waiting for you to post it properly.

 

 

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

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

void UART_Init();
char UART_RxChar();
void UART_TxChar(const char data);
//DDRB=0xff;
int main(void)
{DDRB=0xff;
    char myValue;
    
    UART_Init();
    
    while (1)
    {   //PORTB=0xff;
        UART_TxChar('k');
        myValue = UART_RxChar();
        UART_TxChar(myValue);
        
    }
}

void UART_Init()
{
    UBRR0L = 103;
    UCSR0B = (1<<TXEN0)|(1<<RXEN0);
    UCSR0C = (1<<UCSZ00)|(1<<UCSZ01);
}

void UART_TxChar(char data)
{PORTB=0xff;
    while(!(UCSR0A & (1<<UDRE0)));
    {
        UDR0 = data;
PORTB=0x00;
    }
}

char UART_RxChar()
{//PORTB=0xff;
    while(!(UCSR0A & (1<<RXC0)));
    {
        //PORTB=0x00;
        return UDR0;

}

}

<p>Kunal Gupta</p>

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

You say it worked fine at 9600 baud. Your screen shot shows a UBBR0L = 103, which would be 9600 (assuming you're really clocking at 16mhz).  What did you change the UBBR0L to that makes it fail?

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

sry but i didnt get what u r saying

<p>Kunal Gupta</p>

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

Please read the instructions on how to post soiurce code properly!

 

Again, they are in Tip #1.

 

When done properly, it should look something like this:

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

void UART_Init();
char UART_RxChar();
void UART_TxChar(const char data);

//DDRB=0xff;

int main(void)
{
    DDRB=0xff;

    char myValue;
    
    UART_Init();
    
    while (1)
    {   
        //PORTB=0xff;

        UART_TxChar('k');
        myValue = UART_RxChar();
        UART_TxChar(myValue);
        
    }
}

 

 

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

Kunalgupta wrote:

u can check my code if want 

and also i baud rate is not the issue because it is working fine previously at 9600

 

Previously you said "u can check my code if want and also i baud rate is not the issue because it is working fine previously at 9600"

 

This implies you have since changed the baudrate to something else.  If you haven't change the code at all, but your setup no longer works, then I would suspect a hardware issue.  Loose wires, bad power supply, etc.

Can you post a schematic of your setup?  Or is this an Arduino board?

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

what do u mean by source code??? are asking me to upload the file of the code here ???

i didnt understand one thng that in uart txchar() it is going inside the while loop but not printing ???

and when i am sending data from serial monitor why it is not going inside the while loop of uart rxchar() function

 

i think i cant upload video here otherwise i would have shown the problem in video

<p>Kunal Gupta</p>

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

i changed the value of UBBR0l to 51 from 103 ...because i read on net that 103 is for 4800 and 51 is for 9600.

so after changing the baude rate it is showing me this on sending some character

Attachment(s): 

<p>Kunal Gupta</p>

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

You need to use the datasheet for the 328 and look at the baudrate charts.  The baudrate is based on the clock frequency you're using with your AVR.  Again assuming you clock is 16mhz, the a UBBR0L = 103 is 9600.  Changing it to 51 makes the baudrate 19200.  If you want to use 9600 then change it back to 103.

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

still it is not working ....

any more suggestions please?????????????????

<p>Kunal Gupta</p>

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

Did you by ANY chance change your Fuse settings to program the DIV8 fuse in the 328 which would alter your internal clock to 2Mhz by chance?

 

Did you change ANY fuse settings?

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Show us your fuse settings, and what is your clock source?  Internal RC or external xtal?

Serial comms require an accurate clock source, most people that come here asking for help with serial comms have not proven what their clock source is set to, or have forgotten to uncheck the clock /8 fuse setting.

Also your h/w connections need to be:

TX(chip pin -> RXD of ttl/usb interface

RX(chip pin <- TXD of ttl/usb interface

GND(chip pin) = GND of ttl/usb interface

 

It is common for beginners to connect Tx to TXD and RX to RXD which will not work, or forget to also connect the gnd.

 

Hope that helps.

Jim

 

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

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

lock and fuse bytes are

lock-3f

efuse-ff

hfuse -d9

lfuse -62

 

do i have to change them????

 

and my hardware connections are correct , i have double checked them

<p>Kunal Gupta</p>

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

Those settings mean that the internal DIV8 fuse is set, and you are running on the internal oscillator of 8Mhz divided by 8 ti 1Mhz internal CPU clock. 

 

Check the baud tables for 1Mhz clock and 9600

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

well thanks to u all on changing the fuse and lock bytes serial communication starts working and working fine

but now another new problem arises!!

when i am uploading sketches it is showing me as shown in screen shot

 

anyone who can suggest what should i do now??

 

i have also atteched the pic of the serial communication working

 

Attachment(s): 

<p>Kunal Gupta</p>

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

Post a clear picture of your setup please

jim

 

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

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

My problem is solved..i am new to avr freaks community and i want to ask another question since last week .i have searched avr freaks website many times but can't figure out how to post new discussion .
Under my discussion it is not showing me any clickable icon to add new discussion .this question doesn't suit the content of this forum but i have many new douts and can't ask because of this.
So anybody who can tell me how to add question under my discussion .
Will be waiting for reply and hoping to get reply as soon as possible

Attachment(s): 

<p>Kunal Gupta</p>

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

Simply go to the forum you want to start a new discussion, and click on the button in the top left corner that says "Start a New Topic"

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

On going to forum it is not showing up any thing like start an new topic

Attachment(s): 

<p>Kunal Gupta</p>

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

Kunalgupta wrote:
On going to forum it is not showing up any thing like start an new topic

 

Good Grief!

 

You have to CLICK on the forum you want to start a discussion in.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Thanks a lot .. problem solved

<p>Kunal Gupta</p>