Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on Alternative to boolean type for embedded C (ATmega2560)
Post
Alternative to boolean type for embedded C (ATmega2560)
I'm working on writing drivers for the peripherals in my ATmega2560 microcontroller. One of these is a USART which has the option of being double speed or normal speed. If this was C for the PC I would use <stdbool.h> to create a boolean member of the USART struct and write my header and source file as shown below.
//usart.h
#ifndef USART_H
#define USART_H
#include <stdbool.h>
typedef enum { USART_PARITY_NONE, USART_PARITY_EVEN, USART_PARITY_ODD } usart_parity_t;
typedef enum { USART_STOP_1, USART_STOP_2 } usart_stopbits_t;
typedef enum { USART_BITS_5, USART_BITS_6, USART_BITS_7, USART_BITS_8} usart_databits_t;
typedef struct
{
usart_parity_t parity;
usart_stopbits_t stopbits;
usart_databits_t databits;
bool doublespeed;
} usart_t;
//Prototype functions below
#endif
//usart.c
#include <stdint.h>
#include <stdbool.h>
#include "usart.h"
static void set_baud(uint32_t baud, bool doublespeed)
{
if(doublespeed)
{
//Setup usart for double speed
}
else
{
//Setup usart for normal speed
}
//do more stuff
}
However, <stdbool.h> is not provided by the C library implementation available for the ATmega2560.
Question: How do I handle this in my code? Should I use uint8_t doublespeed instead? Are there any consequences of doing this?

2 comment threads