Problem with the UART in Atmel Studio 7 with ATMega 328P on the Arduino Board

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

Hi everyone, again I found some problem when I tried to upload the program that will take my character input and display the ASCII value on the LEDs based on the "Make: AVR Programming" book and I did the modification to make it work on my UNO clone board (ATMega 328p SMD Based) that runs on default at 16 MHz clock speed but I change my MCU to run at 1MHz clock speed, my modification code looks as follows:

 

/*
A simple test of serial-port functionality.
Takes in a character at a time and sends it right back out,
displaying the ASCII value on the LEDs.
*/

 

#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/power.h>
#include "pinDefines.h"
#include "uart.h"

int main(void) {
    clock_prescale_set(clock_div_16);  //run your 16MHz hardware at 1MHz
    char serialCharacter;
    // -------- Inits --------- //
    LED_DDR = 0xff; /* set up LEDs for output */
    initUSART();
    printString("Hello World!\r\n"); /* to test */
    // ------ Event loop ------ //
    while (1) {
        serialCharacter = receiveByte();
        transmitByte(serialCharacter);
        LED_PORT = serialCharacter;
        /* display ascii/numeric value of character */
        } /* End event loop */
        return (0);
    }

 

 

but when I tried to upload the program, there was this error and I believe that my code is not being uploaded properly to the board:

1. there is so-called like a "not in sync" error code as shown in the below picture

 

 

Is it possible for everyone here to check what was my issue here and help me to solve it?  because I have done these following steps:

 

1. include all the required .h file and .c file in my project 

2. setting my baud rate become 9600 in my USART.h file as shown in the following picture

 

2. and the USART.c file that I include has a following code:

 

 

 

 

Embedded Lover and would like to pursue my future in embedded system

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

Note that my setbaud.h needs BAUD to be defined before you include the setbaud.h file. The manual:

 

https://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html

 

shows the example:

#define F_CPU 11059200
#define BAUD 38400
#include <util/setbaud.h>

in your case I guess this would be:

#define F_CPU 1000000
#define BAUD 9600
#include <util/setbaud.h>

I have no idea why you would choose to run the CPU at 1MHz if your plan is to use 9600 baud??

 

The inability of avrdude to do ISP has nothing whatever to do with the code you are trying to program in. So the fact that it is UART code has no bearing on whether it can be programmed. That is likely because of some other problem (usually ISp wiring or maybe decoupling caps or something).

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


One should be aware of all the things that are affected by a change in the cpu clock freq. before making the change.   It's ok to change it, just know what your doing.

As an example, changing the cpu clock to 1MHz, and then wanting to do async USART comms, means your limited to only slower baud rates, see data sheet:

Note, only baud rates 4800 or below will give a low error rate, even turning on the U2X bit, only increases to 9600 are possible, all others are either too large an error, or out of range altogether!

 

Jim

 

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

share.robinhood.com/jamesc3274
stack gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

clawson wrote:

Note that my setbaud.h needs BAUD to be defined before you include the setbaud.h file. The manual:

 

https://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html

 

shows the example:

#define F_CPU 11059200
#define BAUD 38400
#include <util/setbaud.h>

in your case I guess this would be:

#define F_CPU 1000000
#define BAUD 9600
#include <util/setbaud.h>

I have no idea why you would choose to run the CPU at 1MHz if your plan is to use 9600 baud??

 

The inability of avrdude to do ISP has nothing whatever to do with the code you are trying to program in. So the fact that it is UART code has no bearing on whether it can be programmed. That is likely because of some other problem (usually ISp wiring or maybe decoupling caps or something).

 

Appreciate for your help but I think that it doesn't help my error code as I still get the same thing

avrdude.exe : Version 6.3, compiled on Jan 17 2017 at 12:00:53
              Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
              Copyright (c) 2007-2014 Joerg Wunsch

              System wide configuration file is "C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf"

              Using Port                    : COM3
              Using Programmer              : arduino
              Overriding Baud Rate          : 115200
avrdude.exe : stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 2 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 3 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 4 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 5 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 6 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 7 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 8 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 9 of 10: not in sync: resp=0x00
avrdude.exe : stk500_getsync() attempt 10 of 10: not in sync: resp=0x00

avrdude.exe  done.  Thank you.

 

ki0bk wrote:

 

 

One should be aware of all the things that are affected by a change in the cpu clock freq. before making the change.   It's ok to change it, just know what your doing.

As an example, changing the cpu clock to 1MHz, and then wanting to do async USART comms, means your limited to only slower baud rates, see data sheet:

Note, only baud rates 4800 or below will give a low error rate, even turning on the U2X bit, only increases to 9600 are possible, all others are either too large an error, or out of range altogether!

 

Jim

 

 

 

Meaning I must lower my baud rate or increase my cpu clock frequency because what I believe and the datasheet mentioned that the arduino uno have a clock speed of 16MHz, so let's say I use the clock speed at 1MHz , how to define or choose my baud rate and is it available in the datasheet?

 

But even when I changed my baud rate become 4800 I still get the same error here

Embedded Lover and would like to pursue my future in embedded system

Last Edited: Mon. Jul 15, 2019 - 03:38 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

rizkifajarnugroho wrote:
Appreciate for your help but I think that it doesn't help my error code as I still get the same thing
I already told you that:

clawson wrote:
The inability of avrdude to do ISP has nothing whatever to do with the code you are trying to program in.

The inability to use avrdude to program the thing has nothing to do with your UART code. What kind of programmer are you trying to use anyway and what is the avrdude command line invocation?

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

clawson wrote:

rizkifajarnugroho wrote:
Appreciate for your help but I think that it doesn't help my error code as I still get the same thing
I already told you that:

clawson wrote:
The inability of avrdude to do ISP has nothing whatever to do with the code you are trying to program in.

The inability to use avrdude to program the thing has nothing to do with your UART code. What kind of programmer are you trying to use anyway and what is the avrdude command line invocation?

 

I used the Arduino UNO board that has the ATMega 328P SMD package on it

on my external tools section in the Atmel Studio 7

my command line is C:\Program Files (x86)\Arduino\hardware\tools\avr/bin/avrdude.exe 

my Arguments line is -C"C:\Program Files (x86)\Arduino\hardware\tools\avr/etc/avrdude.conf" -v -patmega328p -carduino -PCOM3 -b115200 -D -Uflash:w:"$(ProjectDir)Debug\$(TargetName).hex":i 

Embedded Lover and would like to pursue my future in embedded system

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

OK so the Arduino to AVR connection looks like it's not working. Are the wires connected correctly? Has Arduino-ISP programming EER worked with this setup? Does the target AVR have a clean PSU and power supply decoupling?

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

clawson wrote:

OK so the Arduino to AVR connection looks like it's not working. Are the wires connected correctly? Has Arduino-ISP programming EER worked with this setup? Does the target AVR have a clean PSU and power supply decoupling?

 

Yes I connected my FTDI cable to arduino uno as following

TX (FTDI) to RX (Arduino UNO)

RX (FTDI) to TX (Arduino UNO)

GND (FTDI) to GND of arduino uno

 

No I didn't use a separate AVR microcontroller, I used the one that attached to the Arduino UNO board (ATMega 328P SMD Package)

 

Embedded Lover and would like to pursue my future in embedded system