0

I cannot get my serial connection running to test my AVR chip. I currently assume it is because of the F_CPU value, but I am not 100% sure. The manual error i defined for #error Systematischer Fehler der Baudrate größer 1% appears when loading the program onto the chip. I don't know where to start right now finding the error. The console of my serial terminal does not show any output when connecting.

I created a config file I load in initially:

configGlobal.h

#ifndef F_CPU
#warning "F_CPU not yet defined, and will be set to 1MHz"
#define F_CPU 1000000UL
#endif

USART.c

#include "configGlobal.h"
#include <avr/io.h>
#include "USART.h"

#define BAUD 9600UL
#define BAUDRATE ((F_CPU) / (BAUD * 8UL) - 1) // set baud rate value for UBRR

void initUSART(void) {
    // Requires BAUD
    UBRR0H = (BAUDRATE >> 8) // Shift regisgter right by 8 bits to get upper 8 bits
    UBRR0L = BAUDRATE;
    UCSR0A |= (1 << U2X0);

    // Enable USART
    UCSR0B = (1 << TXEN0) | (1 << RXEN0); // Activate TX RX
    UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // 8 data bits, 1 stop bit
}

main.c

#include "configGlobal.h"
#include <avr/io.h>
#include <util/delay.h>
#include "USART.h"

int main(void) {

  char serialCharacter;

  // --- INITS --- //
  DDRB = 0xff;

  // Set up LED for output
  initUSART();
  printString("Hello World!\r\n");

  return 0;
}

OUTPUT OF AVRDUDE

AVR Development Stack
Get input main.c file...
In file included from main.c:1:0:
configGlobal.h:2:2: warning: #warning "F_CPU not yet defined, and will be set to 1MHz" [-Wcpp]
 #warning "F_CPU not yet defined, and will be set to 1MHz"
  ^
In file included from USART.c:1:0:
configGlobal.h:2:2: warning: #warning "F_CPU not yet defined, and will be set to 1MHz" [-Wcpp]
 #warning "F_CPU not yet defined, and will be set to 1MHz"
  ^
In file included from /usr/local/CrossPack-AVR-20131216/avr/include/avr/io.h:99:0,
                 from USART.c:2:
USART.c: In function 'initUSART':
USART.c:10:2: error: called object is not a function or function pointer
  UBRR0L = BAUDRATE;
  ^
Convert elf file to hex...
Uploading data to microcontroller...

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: Expected signature for ATmega328 is 1E 95 14
         Double check chip, or use -F to override this check.

avrdude done.  Thank you.
sesc360
  • 3,155
  • 10
  • 44
  • 86
  • `In file included from USART.c:2:0: configUSART.h:10:3: error: #error Systematic Error of BAUD Rate > 1% ` => your code doesn't compile. You're probably using an old .elf version that worked. Compute your baud values manually. – Jean-François Fabre Nov 18 '17 at 17:01
  • @Jean-FrançoisFabre Sorry, didnt update the file. It is actually the correct one. – sesc360 Nov 18 '17 at 17:03
  • @Jean-FrançoisFabre I changed the code above to manual calculation and get the error that my BAUDRATE cannot be read – sesc360 Nov 18 '17 at 17:09

1 Answers1

0

The compiler error is due to a missing a ";" in USART.c (line 10). If you are unsure about usable baudrates, you can check for baudrate compatibility at e.g. http://wormfood.net/avrbaudcalc.php

Please keep in mind that using the AVRs internal RC-oscillator as a clock source might lead to unstable UART, as the frequency is not exactly at 8MHz (or 1MHz with CKDIV8=1).

The bottom part of the avrdude-log suggests that the device settings are not correct. Are you sure that an ATmega328 is connect and not an ATmega328P?

M2C8
  • 1