#

Embedded Programming

This week we were assigned to use different types of software to program and see how efficient each of the programming languages is. For this week I thought about creating a basic code for my PCB to play a song. To accomplish this task, use my PCB from Week 4

On the group page you will find information to be able to program in C++, PHYTON and Microphiton. There is also information about some microcontrollers that are in stock, you can consult it here

List of architectures in FabLab Puebla

Architecture AVR (8-bit) ARM Cortex-M0+ RISC-V (32-bit) Xtensa (LX6/LX7) Tensilica L106 (32-bit) ARM Cortex-M4F
Pros Low power consumption, Simple to program, Good community support Energy efficient, Wide range of tools, High performance for 32-bit Open source architecture, Scalable, Flexible Highly customizable, Good for DSP applications, Integrated Wi-Fi and Bluetooth Low cost, Integrated Wi-Fi, Good SDK support High performance, Floating point unit, Energy efficient
contras Limited processing power, Fewer advanced peripherals More complex to program than AVR, Higher cost Less mature ecosystem, Fewer ready-to-use libraries Proprietary, Requires licensing for commercial use Single core, Less powerful than newer ESP32 models More expensive than simpler cores, Complex for beginners

Fundamentals of C Programming

Variable Declaration and Data Types

The typical declaration of variables in C follows the format:
Type-Qualifier Type-Modifier Data-Type Name = Initial_Value.
Example: const unsigned char var = 64;
declares an unsigned char constant with the value 64.

Key data types

Key data types Description
Integer for integer values.
Floating Point for decimal numbers.
Character for single characters.
Void absence of data.

Operators

Operator Type Description
Logical Used in conditions (&&, ||, !)
Bitwise Operates on bits (&, |, ^, ~)
Shift Shifts bits left or right (<<, >>)

Control Flow

Control Flow Type Description
If, Else, Switch Decision structures that execute code based on conditions.
Loops (For, While, Do-While) Repeat code blocks while certain conditions are met.

Functions
A function defines reusable, organized code blocks. Basic example:
This function takes two integers and returns their sum.
Pointers
A pointer stores a variable's address. Example:


Structures and Classes

Structures (struct)
Structures group related data into one type.
Example of a structure for climate monitoring:
Usage:
Enumerations (enum)
enum allows defining a set of constants. Example:

Classes (in C++)

A class defines a data type with attributes and methods.
Example of a Circle class:
Main program usage:

My own example


ATtiny LED RGB - 2 Buttons

Code 1
Download LED RGB - 2 Buttons

This program uses an ATtiny85 to control an RGB LED, allowing color combinations of red, blue, and green through two buttons. Pins 2, 1, and 0 are connected to each color output of the LED, while the control buttons are on pins 3 and 4.

ATtiny LED RGB with Counter - 2 Buttons

Code 2
Download LED RGB Counter - 2 Buttons

This program controls an RGB LED through a counter ranging from 0 to 7, adjusted with two buttons. Each counter value lights up the LED with a specific color combination: red, blue, green, pink, yellow, cyan, or white. A 500 ms delay ensures the button presses are registered without unwanted bouncing.

ATtiny Neopixel

Code 1
Download Neopixel

This code alternates the color of a Neopixel LED between red, green, and blue every second, demonstrating single-wire serial color control.

ATtiny Neopixel Strip with 8 LEDs - 2 Buttons

Code 3
Download Neopixel Strip x 5 - 2 Buttons

This code controls a strip of 5 Neopixel LEDs connected to pin 0 of the microcontroller. During each loop cycle, the LEDs progressively light up in red, starting from the first LED, with a 500 ms delay between each update.

Final refections

This lesson has been very helpful in advancing my programming skills. I’ve always thought of programming as the "soul" of a person, yet I’ve come to realize there were still many things I didn’t know, like the pros and cons of different languages and data types. At times, it’s been a bit stressful, especially when trying to upload code to my MCU, as it occasionally doesn’t work as expected.