AVR ATMEGA328p BAUD Rate Error / No Serial Connection - c

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.

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?

Related

simple arduino code without using avr headers in avr-libc

I have this simple code
That I am trying to compile for arduino microcontroller and without using avr headers. I just defined all macros in my source program file
but my gcc-avr says
led.c:15:8: error: lvalue required as left operand of assignment
DDRB |= 0B100000; // PORTB5 1010
^
Now I can expect this error on some cpu that io area is not virtual memory space of this process but I am running my code on mocrocontroller that must have execution bit. how to get rid of this message and compile it and able to run on arduino
But the gcc-avr throws error that
#define F_CPU 16000000
#define BLINK_DELAY_MS 5000
#include <util/delay.h>
#define __SFR_OFFSET 0x20
#define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET)
#define DDRB _SFR_IO8(0x04)
#define PORTB _SFR_IO8(0x05)
int main (void)
{
// Arduino digital pin 13 (pin 5 of PORTB) for output
DDRB |= 0B100000; // PORTB5 1010
while(1) {
// turn LED on
PORTB |= 0B100000; // PORTB5
// _delay_ms(BLINK_DELAY_MS);
int x=0;
while(x<25)
{
x++;
}
x=0;
// turn LED off
PORTB &= ~ 0B100000; // PORTB5
//hello
while(x<25)
{
x++;
}
//_delay_ms(BLINK_DELAY_MS);
}
}
The problems are the macros, you are defining the register as an integer, not as an address to an integer.
DDRB expands to 0x04 + 0x20 so you end up with code like (0x04 + 0x20) |= 0B100000;. You should be able to fix this with a cast and then de-reference:
#define _SFR_IO8(io_addr) ( *(volatile uint8_t*) ((io_addr) + __SFR_OFFSET) )
For details see How to access a hardware register from firmware?
Please also note that macros starting with double underscore __ are reserved for the compiler, so we should never use that or we might end up with naming collisions.

Embedded C, and AVR GCC compilation issue with multiple definitions

It seems that my compiler does not willing to build the firmware in stepper_motor.c and as you can see below, it throws this error, which I am unable to solve.
I have created a stepper_motor.c source where all my functions and variables are placed, and a stepper_motor.h header where all the #defines and function prototypes are. In main.c I just include the stepper_motor.h header and use the functions. As you can see from shell log data, it does not compile and tells that 4 x variables are defined multiple times. Those ones are used in ISR() routine for a timer, so they need to be also volatile.
Any information would be highly appreciated on this issue.
this is my main.c includes: --------
#include <avr/io.h>
#include <Hardware_Bay.h>
#include <util/delay.h>
#include <USART.h>
#include <string.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdbool.h>
#include <avr/interrupt.h>
#include <scale16.h>
#include <stepper_motor.h>
const uint8_t motor_Phases[] = {
(1 << COIL_B1), // full step
(1 << COIL_B1) | (1 << COIL_A2), // half step
(1 << COIL_A2), // full step
(1 << COIL_A2) | (1 << COIL_B2), // half step
(1 << COIL_B2), // full step
(1 << COIL_B2) | (1 << COIL_A1), // etc..
(1 << COIL_A1),
(1 << COIL_A1) | (1 << COIL_B1),
};
volatile uint8_t stepPhase = 0;
volatile uint16_t stepCounter = 0;
volatile int8_t direction = FORWARD;
ISR(TIMER0_COMPA_vect) { // Timer/Counter-0 Compare match interrupt vector enable
stepPhase += direction; // take step in right direction
stepPhase &= 0b00000111; // keep the stepPhase in range (0 - 7)
STEPPER_PORT = motor_Phases[stepPhase]; // write phase out to motor COIL-1 A/B
HALL_PORT = motor_Phases[stepPhase]; // write phase out to motor COIL-2 A/B
stepCounter ++;
}
------------------------------header file -----------------------------------------------
#ifndef STEPPER_MOTOR_H_INCLUDED
#define STEPPER_MOTOR_H_INCLUDED
#endif // STEPPER_MOTOR_H_INCLUDED
#define FORWARD 1
#define BACKWARD -1
#define TURN 200
#define MAX_DELAY 255
#define MIN_DELAY 10
#define ACCELERATION 16
#define RAMP_STEPS (MAX_DELAY - MIN_DELAY) / ACCELERATION
void stepperDrive(uint8_t number_of_steps, uint8_t delay);
void trapezoidDrive_Stepper(int16_t number_of_steps);
PS D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6> make flash
avr-gcc -Wl,-Map,Physalis_GEO_version_3.6.map -Wl,--gc-sections -mmcu=atmega1284p fuse.o main.o stepper_motor.o USART.o -o Physalis_GEO_version_3.6.elf
stepper_motor.o: In function `stepperDrive':
D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6/stepper_motor.c:50: multiple definition of `stepCounter'
main.o:(.bss.stepCounter+0x0): first defined here
stepper_motor.o:(.data.direction+0x0): multiple definition of `direction'
main.o:(.data.direction+0x0): first defined here
stepper_motor.o:(.rodata.motor_Phases+0x0): multiple definition of `motor_Phases'
main.o:(.rodata.motor_Phases+0x0): first defined here
stepper_motor.o: In function `stepperDrive':
D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6/stepper_motor.c:50: multiple definition of `stepPhase'
main.o:(.bss.stepPhase+0x0): first defined here
make: *** [Physalis_GEO_version_3.6.elf] Error 1
PS D:\Users\Arhitect\Documents\C_Programs\Physalis_Banshee\Physalis_GEO_version_3.6>
Really the error messages tell you all you need to know - you have defined these symbols in two places - main.c and setpper_motor.c.
You have defined stepCounter at line 50 of stepper_motor.c and also as shown in main.c. Similarly for stepPase and motor_Phases.
If they are independent and should not be visible externally then
both should be declared static so that they are visible only in
their respective translation units.
If it is intended that the symbol refers to the they are the same object, then one of them needs to be declared extern to indicate to the compiler its type and name, but that it is defined elsewhere..
If they are independent but "need" to be global (because you cannot
think of a better solution), then they cannot have the same name.
If you do not reference them in main.c, then they should in any event be removed from there. It seems odd that you would define them here when they clearly relate to stepper motor control and should probably be encapsulated in stepper_motor.c.
Avoiding the use of globals in the fist place is a much better solution see https://www.embedded.com/a-pox-on-globals/

Use #define to specify multiple paramaters [duplicate]

I want to be able to define a tuple which represents the arguments needed by other macros.
I think the best way to show what I want is to show an example:
#include <avr/io.h>
#define LED_PORT PORTB
#define LED_DDR DDRB
#define LED_PIN PB7
#define LED LED_PORT, LED_DDR, LED_PIN
#define OUTPUT(port, ddr, pin) ddr |= 1 << pin
void main(void) {
OUTPUT(LED);
}
I want OUTPUT(LED) to be then expanded into:
LED_DDR |= 1 << LED_PIN
The problem that I get is to do with the order of expansion, and results in the following error:
macro "OUTPUT" requires 3 arguments, but only 1 given
This is for use with an AVR project with custom built hardware where I have defined LED and other components with a respective LED_PORT LED_DDR and LED_PIN.
I then want to define more macros that can take this LED and use the appropriate arguments to map to the most succinct way possible.
Is this possible with the standard C-preprocessor?
You can add a level of indirection to the macro to achieve this:
#define OUTPUT_I(port, ddr, pin) ddr |= 1 << pin
#define OUTPUT(spec) OUTPUT_I(spec)
During rescanning, spec is expanded before OUTPUT_I, so the OUTPUT_I macro sees three parameters.

UART5 on stm32F429-DISCOVERY - not initialized

I ran into some issues configuring my stm32f429-DISCO board for a UART transmission on UART5.
I used the example project provided by st. To be exact, the UART/UART_TwoBoards_ComDMA in version 1.7.0.
In this example the USART1 is used to circle one data package around.
When the DISCO-board is programmed with the original code, I can see the USART1 output message on my oscilloscope.
On the other hand, when I try the same thing with the UART5, because the pins for USART1 will be blocked in my final configuration it won't work.
I narrowed the problem down to the initialization process.
HAL_UART_MspInit(huart);
This function is not setting the TC and RXNE bit in UART1->SR, and therefore the UART5 is not configured.
I know for UART1 you need to enable the clock, because it can be a synchronous transmission.
__HAL_RCC_USART1_CLK_ENABLE();
I can't seem to find a similar function for UART5. Has somebody an idea or a hint for me?
In case a bigger problem is underlying this issue, here are the changed settings for the UART5 configuration of the example.
/* Definition for USARTx clock resources */
#define USARTx UART5
//#define USARTx_CLK_ENABLE() __HAL_RCC_USART1_CLK_ENABLE();
#define DMAx_CLK_ENABLE() __HAL_RCC_DMA1_CLK_ENABLE()
#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOD_CLK_ENABLE()
#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
//#define USARTx_FORCE_RESET() __HAL_RCC_USART1_FORCE_RESET()
//#define USARTx_RELEASE_RESET() __HAL_RCC_USART1_RELEASE_RESET()
/* Definition for USARTx Pins */
#define USARTx_TX_PIN GPIO_PIN_12
#define USARTx_TX_GPIO_PORT GPIOC
#define USARTx_TX_AF GPIO_AF8_UART5
#define USARTx_RX_PIN GPIO_PIN_2
#define USARTx_RX_GPIO_PORT GPIOD
#define USARTx_RX_AF GPIO_AF8_UART5
/* Definition for USARTx's DMA */
#define USARTx_TX_DMA_CHANNEL DMA_CHANNEL_4
#define USARTx_TX_DMA_STREAM DMA1_Stream7
#define USARTx_RX_DMA_CHANNEL DMA_CHANNEL_4
#define USARTx_RX_DMA_STREAM DMA1_Stream0
/* Definition for USARTx's NVIC */
#define USARTx_DMA_TX_IRQn DMA1_Stream7_IRQn
#define USARTx_DMA_RX_IRQn DMA1_Stream0_IRQn
#define USARTx_DMA_TX_IRQHandler DMA1_Stream7_IRQHandler
#define USARTx_DMA_RX_IRQHandler DMA1_Stream0_IRQHandler
#define USARTx_IRQn UART5_IRQn
#define USARTx_IRQHandler UART5_IRQHandler
I'm happy for any suggestion and help that guides me in the right direction.
Thank you for your time,
eimer
UART5 has no S, so the function to enable the clock is called
__HAL_RCC_UART5_CLK_ENABLE();, like e.g. there: Receiving data from 2 UARTs, STM32F4-Discovery, HAL drivers

#define a tuple in C

I want to be able to define a tuple which represents the arguments needed by other macros.
I think the best way to show what I want is to show an example:
#include <avr/io.h>
#define LED_PORT PORTB
#define LED_DDR DDRB
#define LED_PIN PB7
#define LED LED_PORT, LED_DDR, LED_PIN
#define OUTPUT(port, ddr, pin) ddr |= 1 << pin
void main(void) {
OUTPUT(LED);
}
I want OUTPUT(LED) to be then expanded into:
LED_DDR |= 1 << LED_PIN
The problem that I get is to do with the order of expansion, and results in the following error:
macro "OUTPUT" requires 3 arguments, but only 1 given
This is for use with an AVR project with custom built hardware where I have defined LED and other components with a respective LED_PORT LED_DDR and LED_PIN.
I then want to define more macros that can take this LED and use the appropriate arguments to map to the most succinct way possible.
Is this possible with the standard C-preprocessor?
You can add a level of indirection to the macro to achieve this:
#define OUTPUT_I(port, ddr, pin) ddr |= 1 << pin
#define OUTPUT(spec) OUTPUT_I(spec)
During rescanning, spec is expanded before OUTPUT_I, so the OUTPUT_I macro sees three parameters.

Resources