ELECTRONICS DESIGN

Exploring microcontroller architectures and embedded programming techniques



Assignment Checklist

Status Task Evidence
Use test equipment to observe microcontroller operation View
Document group work and individual reflections View
Design a development board with EDA software View
Demonstrate circuit board design workflows View
Interact and communicate with embedded microcontroller View

3D Scanning Process


Embedded C Programming

Variable Declaration

The typical declaration of variables in C follows the format:

const unsigned char var = 64;

Declares an unsigned char constant with the value 64.

Data Types
Type Description Size
int Integer values 2-4 bytes
float Decimal numbers 4 bytes
char Single characters 1 byte
void Absence of data -
Functions

A function defines reusable, organized code blocks:

int addNumbers(int a, int b) {
  return a + b;
}

This function takes two integers and returns their sum.

Pointers

A pointer stores a variable's address:

int var = 10;
int* ptr = &var;
*ptr = 20; // Changes var to 20

Pointers are essential for hardware register access.

Structures and Classes
Structures (struct)

Structures group related data into one type:

struct ClimateSensor {
  float temperature;
  float humidity;
  unsigned long timestamp;
};
Classes (C++)

A class defines a data type with attributes and methods:

class Circle {
  private:
    float radius;
  public:
    Circle(float r) : radius(r) {}
    float area() { return 3.14 * radius * radius; }
};

Embedded C Programming

Variables
Functions
Structures
Pointers
1
const unsigned char ledPin = 13; // Pin number for LED
2
volatile int sensorValue = 0; // Stores ADC reading
3
float temperature = 25.5; // Current temperature
Variable Declaration in Embedded C

Variables in embedded systems should be declared with careful consideration of their type and scope. Use const for values that won't change, volatile for variables that may be modified outside the program flow (like by interrupts), and choose the smallest data type that can hold your values to save memory.

Memory allocation for different variable types

1
void setup() { // Runs once at startup
2
  pinMode(ledPin, OUTPUT); // Set pin as output
3
}
4
5
int readTemperature(int sensorPin) { // Custom function
6
  int raw = analogRead(sensorPin);
7
  return raw * 0.488; // Convert to °C
8
}
Functions in Embedded Systems

Functions help organize code into reusable blocks. In embedded systems, keep functions small and focused on a single task. Avoid recursion due to limited stack space. For time-critical functions, consider declaring them as inline to eliminate function call overhead.

1
typedef struct { // Sensor data structure
2
  float temperature;
3
  float humidity;
4
  unsigned long timestamp;
5
} SensorData;
6
7
SensorData readings[10]; // Array of sensor data
Structures for Data Organization

Structures allow you to group related data together. In embedded systems, they're useful for organizing sensor readings, configuration parameters, and state machines. Use typedef to create cleaner type names. For memory-constrained systems, consider using bitfields within structures to pack data efficiently.

1
int sensorValue = 0; // Regular variable
2
int* sensorPtr = &sensorValue; // Pointer to variable
3
4
void updateSensor(int* value) { // Pointer parameter
5
  *value = analogRead(A0); // Dereference pointer
6
}
7
8
volatile uint8_t* portB = (uint8_t*)0x25; // Hardware register
Pointers in Embedded Systems

Pointers are essential for direct memory access, hardware register manipulation, and efficient data passing. In embedded C:

  • Use pointers to access specific memory addresses (like hardware registers)
  • Pass large data structures by pointer to avoid copying
  • Be cautious with pointer arithmetic and always initialize pointers
  • Use const with pointers to protect data when appropriate

3D Scanning Process

Final Reflections

Key Takeaways
What I Learned
  • Different microcontroller architectures and their trade-offs
  • Proper use of test equipment for circuit analysis
  • Efficient embedded C programming techniques
  • PCB design workflows from schematic to manufacturing
  • Importance of signal integrity in circuit design
Challenges Faced
  • Debugging timing issues in embedded systems
  • Managing limited resources on small MCUs
  • Ensuring reliable communication between components
  • Optimizing code for power efficiency
  • Designing for manufacturability

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.

The experience of designing a complete development board from scratch has given me a much deeper appreciation for the complexity and artistry involved in electronics design. I now feel more confident in my ability to create custom solutions for embedded systems.