0% found this document useful (0 votes)
21 views20 pages

Lecture 2 - C Lecture Notes

This document provides an overview of C programming basics specifically for embedded systems, focusing on concepts such as functions, pointers, loops, conditionals, and logic operations. It emphasizes the importance of C in programming STM32 microcontrollers and explains key elements like variable declaration, loops, and pointer usage with examples. The content is designed to prepare learners for effective microcontroller development by understanding how C interacts with hardware.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views20 pages

Lecture 2 - C Lecture Notes

This document provides an overview of C programming basics specifically for embedded systems, focusing on concepts such as functions, pointers, loops, conditionals, and logic operations. It emphasizes the importance of C in programming STM32 microcontrollers and explains key elements like variable declaration, loops, and pointer usage with examples. The content is designed to prepare learners for effective microcontroller development by understanding how C interacts with hardware.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

C Programming Basics for

Embedded Systems
Functions, Pointers, Function Pointers, Loops,
Conditionals, and Logic Operations

Prepared for STM32 Microcontroller Learners


Introduction to C
• C is the most widely used language in
embedded systems.
• STM32 microcontrollers are programmed
primarily in C.
• Learning these core concepts prepares us for
microcontroller development.
Introduction to C
• What is C Language?
• General-purpose programming language developed in the early
1970s.
• Known for being fast, efficient, and close to hardware.
• Forms the foundation of many modern languages (C++, Java, C#,
etc.).
• Widely used in operating systems, embedded systems, and device
drivers.
• Provides direct memory access via pointers → powerful for
hardware control.
• Why we learn it:
– Most microcontrollers (like STM32) are programmed in C.
– Gives us control over hardware while still being human-readable.
C Code → Machine Code (Binary) Concept
C code → Assembly → (binary 1001…) →
Executable.
Variables in C
• Definition: A variable is a named storage location in memory that
holds a value.
• Types:
– int → whole numbers (e.g., 10, -5)
– float → decimals (e.g., 3.14)
– char → single character (e.g., 'A')
• Example:
• int age = 20;
• float temperature = 36.5;
• char grade = 'A';
• Real-life analogy:
– Like labelled boxes in a cupboard → each box (variable) stores one
kind of item (value).
Variables in C
• Variable Declaration in C
• Definition: Tells the compiler the variable’s name and type.
• Syntax:
• type variableName;
• Examples:
• int age; // integer variable
• float salary; // decimal number
• char grade; // single character
• Initialization:
• int age = 20;
• float salary = 4500.50;
• char grade = 'A';
• Rules:
– Must start with a letter or underscore.
– Case-sensitive (Age ≠ age).
– Cannot use C keywords.
• Analogy: Declaring a variable is like labeling a box before putting something
inside.
Bad Variable Names in C
Bad Variable Names in C
• These are not allowed because they break C’s naming rules:
• Starting with a digit
• int 1value; // cannot start with a number
• Using symbols (other than underscore)
• int cost$; // $ not allowed
• int value-1; // - is an operator, not valid
• Containing spaces
• int my value; // space not allowed
• Using reserved keywords
• int for; // "for" is a keyword
• int int; // "int" is a keyword

• Remember:
• Variable names can use letters, digits, underscore.
• Must start with a letter or underscore.
• Cannot use C reserved words.
Loops: while and for
• while loop: Repeats while condition is true.
• for loop: Repeats for a fixed number of times.
• Embedded link: while(1) is the infinite main
loop in microcontroller programs.
Counter in C
int counter = 0;

while (counter <= 10) {


printf("%d\n", counter);
counter = counter + 1;
}
For loop in C
#include <stdio.h>

int main() {
int numbers[] = {2, 4, 6, 8, 10}; // array of integers
int length = sizeof(numbers) / sizeof(numbers[0]);

// Loop through the array


for (int i = 0; i < length; i++) {
printf("Element %d: %d\n", i, numbers[i]);
}

return 0;
}
Pointers
• Definition: A variable that stores the memory
address of another variable.
• Used for efficient memory access and
hardware register control.
• Real-life analogy: A home address that tells
you where a person lives.
Pointers
• Understanding Pointers in C
• Key Concepts:
• Pointer Declaration:
int *p;
Declares a pointer p that can store the address of an
integer.
• Assigning Address to Pointer:
p = &x;
Stores the address of variable x in pointer p.
• Dereferencing Pointer:
*p
Accesses the value stored at the address pointed to by p.
Pointers
• #include <stdio.h>
• int main() {

• int* pc, c;

• c = 22;
• printf("Address of c: %p\n", &c);
• printf("Value of c: %d\n\n", c); // 22

• pc = &c;
• printf("Address of pointer pc: %p\n", pc);
• printf("Content of pointer pc: %d\n\n", *pc); // 22

• c = 11;
• printf("Address of pointer pc: %p\n", pc);
• printf("Content of pointer pc: %d\n\n", *pc); // 11

• *pc = 2;
• printf("Address of c: %p\n", &c);
• printf("Value of c: %d\n\n", c); // 2
• return 0;
• }
Pointers
• More on pointers - https://www.programiz.com/c-programming/c-pointers?utm_source
Functions
• Definition: Reusable block of code.
• Takes input (parameters), may return output.
• Helps organize code into logical units.
• Real-life example: A calculator function that
adds two numbers.
Function Example
• Example:
• int add(int a, int b) {
• return a + b;
• }
• Main program calls add(3,4) → returns 7.
• Embedded link: A function can turn on an LED
or read a sensor.
Loop Example
• for (int i=0; i<5; i++) {
• printf("LED Blink %d\n", i);
• }
• Real-life analogy: A traffic light repeats red-
yellow-green cycle.
Function to swap two numbers using pointers
• #include <stdio.h>
• void swap(int *a, int *b) {
• int temp = *a; // store value of a
• *a = *b; // put value of b into a
• *b = temp; // put temp (original a) into b
• }

• int main() {
• int x = 10, y = 20;

• printf("Before swap: x = %d, y = %d\n", x, y);

• swap(&x, &y); // pass addresses of x and y

• printf("After swap: x = %d, y = %d\n", x, y);

• return 0;
• }
Conditionals: if statements
• Definition: Executes code only if condition is
true.
• Example: if (temperature > 30) { turnOnFan();
}
• Embedded link: Check if a button is pressed
before toggling an LED.
Next :Logic
Operations

• Operators: && (AND), || (OR), !


(NOT).
• Example: if (buttonPressed &&
systemOn) { startMotor(); }
• Real-life analogy: Car starts only if
brake is pressed AND key is
turned.

You might also like