Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Post History

83%
+8 −0
Q&A 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 w...

2 answers  ·  posted 6mo ago by Carl‭  ·  last activity 6mo ago by Olin Lathrop‭

Question c boolean stdbool.h
#3: Post edited by user avatar Lundin‭ · 2026-03-12T09:43:56Z (6 months ago)
Embedded systems questions should use https://electrical.codidact.com/, please do not use the embedded tag.
#2: Post edited by user avatar Carl‭ · 2026-03-11T17:47:53Z (6 months ago)
  • 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.
  • ```c
  • //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
  • ```
  • ```c
  • //usart.c
  • #include <stdint.h>
  • #include <stdbool.h>
  • #include "usart.hh"
  • 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?
  • 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.
  • ```c
  • //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
  • ```
  • ```c
  • //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?
#1: Initial revision by user avatar Carl‭ · 2026-03-11T13:35:59Z (6 months ago)
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.

```c
//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
```

```c
//usart.c
#include <stdint.h>
#include <stdbool.h>
#include "usart.hh"
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?