4. Embedded Programming¶
Group Assignment¶
We started by exploring the available microcontrollers in our local lab. We listed them to better organize our work. Then, we read a book about microcontrollers.
1.1 Understanding Microcontrollers¶
What is a Microcontroller?¶
A microcontroller is a compact integrated circuit that contains a processor, memory, and input/output (I/O) peripherals. It allows us to store and process data efficiently in a single chip.
Feature | Microcontroller | Microprocessor |
---|---|---|
Components | CPU, memory, and I/O in one chip | CPU only, needs external parts |
Usage | Used in embedded systems | Used in computers |
Bits and Bytes¶
A bit is the smallest unit of data in computing, representing either 0 or 1. A byte consists of 8 bits grouped together.
Understanding Number Systems¶
The importance of different number systems used in microcontrollers: - Decimal (Base-10): The system we commonly use. - Binary (Base-2): Used in digital electronics, consisting of only 0s and 1s. - Hexadecimal (Base-16): Commonly used in microcontrollers to simplify binary representations. - BCD (Binary-Coded Decimal): A system where each decimal digit is represented by its binary equivalent.
Registers¶
A register (or memory cell) is a small storage unit in a microcontroller that holds data temporarily.
Special Function Registers (SFR)¶
These are registers with predefined functions set by the manufacturer.
Memory Types¶
- Read-Only Memory (ROM): Stores permanent data, such as firmware.
- Masked ROM (MROM): Pre-programmed by the manufacturer.
- One-Time Programmable ROM (OTP ROM): Can be programmed once; if an error occurs, a new chip is needed.
- UV Erasable Programmable ROM (UV EPROM): Data can be erased using UV light, allowing reprogramming.
- Random Access Memory (RAM): Stores temporary data that is lost when power is off.
- Electrically Erasable Programmable ROM (EEPROM): Can be reprogrammed electronically while retaining data even after power loss.
Central Processing Unit (CPU)¶
The CPU is the core of the microcontroller, responsible for processing instructions and controlling all operations. It consists of:
- Instruction Decoder: Deciphers program instructions.
- Arithmetic Logic Unit (ALU): Performs mathematical and logical operations.
- Accumulator: Temporarily stores data for processing.
Bus System¶
- Address Bus: Transfers memory addresses from the CPU to memory.
- Data Bus: Connects different components inside the microcontroller.
Serial Communication¶
For short distances, microcontrollers communicate via parallel connections. For longer distances, serial communication is used.
- Baud Rate: The number of bits transmitted per second (bps).
Common Serial Communication Protocols¶
- I2C (Inter-Integrated Circuit): Used for short-distance data exchange between multiple devices.
- SPI (Serial Peripheral Interface): A fast communication protocol for short distances.
- UART (Universal Asynchronous Receiver/Transmitter): Uses a single communication line for data exchange.
Feature | I2C | SPI | UART |
---|---|---|---|
Communication Type | Multi-master, multi-slave | Single master, multiple slaves | Full-duplex (one-to-one) |
Wires | 2 (SDA, SCL) | 4 (MOSI, MISO, SCLK, SS) | 2 (TX, RX) |
Speed | Medium | Fast | Slow |
Devices | Many | Few | Two |
Complexity | High | Medium | Low |
Analog-to-Digital Conversion (ADC)¶
Converts analog signals into digital data that the CPU can process.
Internal Architecture¶
Von Neumann Architecture¶
Uses a single memory for both instructions and data, making it slower because the CPU can only access one at a time.
Harvard Architecture¶
Has separate memory for instructions and data, allowing simultaneous access for faster processing.
RISC (Reduced Instruction Set Computer)¶
Executes only simple operations (e.g., addition, subtraction) for higher efficiency.
CISC (Complex Instruction Set Computer)¶
Supports a large number of instructions, enabling more complex operations at high speed.
1.2 How to Choose the Right Microcontroller?¶
Selecting the right microcontroller can be challenging due to the vast number of options. Consider the following factors:
- Number of input/output pins needed.
- Complexity of operations (simple or advanced).
- Requirement for serial communication.
- and more..
Once you define your needs, narrow down the options and compare prices.
Steps to Get Started¶
- Select a manufacturer: Choose a microcontroller family that is easy to find.
- Study one model: Focus on learning the essentials without getting lost in details.
- Solve a practical problem: Applying knowledge to real-world tasks makes it easier to work with other models in the same family.
1.3 PIC Microcontroller Architecture¶
We explored the architecture of 8-bit PIC microcontrollers. With the knowledge gained from the previous sections, understanding different microcontroller architectures became easier.
2. Seeed Studio RP2040 - Getting Started¶
RP2040 Technical Information¶
Overview¶
The RP2040 is a dual-core ARM Cortex-M0+ microcontroller designed by Raspberry Pi. It features flexible I/O options, low power consumption, and excellent performance for embedded systems and IoT applications.
Key Features:¶
- Cores: Dual-core ARM Cortex-M0+ (up to 133 MHz)
- Memory: 264 KB SRAM
- I/O Pins: 30 GPIO (26 usable), with support for SPI, I²C, UART, and PWM
- Power: 1.8V to 3.3V, with low-power sleep modes
- Maximum Clock Speed: 133 MHz (Dual-core ARM Cortex-M0+)
Step 1: Configure Arduino IDE¶
- Open Arduino IDE and go to Preferences.
- In the Additional Board Manager URLs, paste the following link:
https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json
- Click OK.
Step 2: Install RP2040 Board¶
- Go to Tools > Board > Boards Manager.
- Search for “RP2040”.
- Select and install the appropriate board package.
Step 3: Connect and Upload Code¶
-
Press and hold button B before connecting the board to your laptop.
-
Check the port in Tools > Port.
Then upload your code.
Blinking LED¶
I uploaded this code
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(3000);
digitalWrite(LED_BUILTIN, LOW);
delay(3000);
}
Fading Two LEDs¶
3. Seeed Studio ESP32 C3 - Getting Started¶
After working with Arduino IDE, We moved on to MicroPython following this tutorial.
š§ Step 1: Install esptool
on Windows¶
-
Open Command Prompt
Press
Windows + R
, typecmd
, and hit Enter. -
Install
esptool
using pipRun one of the following commands:
pip install esptool
or
python -m pip install esptool
-
Verify Installation
After the installation completes, check that
esptool
is working by running:python -m esptool
š„ Step 2: Download MicroPython Firmware¶
-
Go to the official MicroPython page for the ESP32C3:
-
Download the firmware specifically for Seeed Studio XIAO ESP32C3.
-
Extract the firmware
.bin
file to a known location on your Windows PC.(Example used:
esp32c3-usb-20230426-v1.20.0.bin
)
š Step 3: Flash MicroPython Firmware¶
-
Connect the Board
Plug in your XIAO ESP32C3 using a USB-C cable.
-
Navigate to Firmware Folder
In Command Prompt, navigate to the folder where the
.bin
file is saved:cd C:\path\to\your\firmware
-
Erase Existing Flash
Run this command (replace
COM13
with your actual COM port):python -m esptool --chip esp32c3 --port COM8 erase_flash
-
Flash the New Firmware
Then, flash the
.bin
firmware with:python -m esptool --chip esp32c3 --port COM8 --baud 115200 write_flash -z 0x0 esp32c3-usb-20230426-v1.20.0.bin
š¹ Writing and Uploading the Blink Program in Thonny IDE¶
- Download and open Thonny IDE.
- Under Tools > Options > Interpreter, choose:
- Interpreter: MicroPython (ESP32)
- Port: Your actual COM port (e.g.
COM8
)
ā Wrote the LED blink program and executed it.
Code Used in MicroPython (Python)¶
from machine import Pin
from time import sleep
myLED = Pin(10,Pin.OUT)
while 1:
myLED.on()
sleep(0.5)
myLED.off()
sleep(0.5)
ā The LED blinked successfully, confirming that MicroPython was set up correctly.
šÆ Learning Outcome¶
Gained foundational knowledge of microcontrollers, logic, memory, and communication. Learned how microcontrollers power automation and embedded systems with efficient and responsive design.
š Reference¶
Microcontroller Programming in C ā eBook