Download as pdf or txt
Download as pdf or txt
You are on page 1of 33

BED25403:

FUNDAMENTALS OF MICROPROCESSOR &


EMBEDDED SYSTEM
Hazizulden Abdulaziz (HZAA)
©2023 Hazizulden AbdulAziz

PROGRAMMING THE MCU


Programming the STM32 microcontroller using C programming language
©2023 Hazizulden AbdulAziz

Learning Outcomes
1. Declare type variables according to bit-size of data
2. Use bitwise operators to manipulate data at bit level
3. Read codes written in C for embedded system
4. Explain the flow of instructions written for embedded system
©2023 Hazizulden AbdulAziz

Microcontroller as a Binary Machine


Microcontroller is an implementation of a digital system. Digital electronics,
being the fundamental of digital systems, dictate the use of binary numbers to
represent the data and signals being processed and stored.

In digital electronics, binary 0 represent an OFF state or a LOW signal or a


ZERO voltage while binary 1 represent an ON state or a HIGH signal or a non-
ZERO voltage. Combination of binary digits (bits) forms the coded instructions
(opcodes) executed by microcontrollers.
©2023 Hazizulden AbdulAziz

Programming the microcontroller


Traditionally Assembly Language is used to program microprocessor and
microcontroller devices. Assembly Language uses processor dependent
Instruction Set as mnemonic to represent opcodes for programming the
microcontroller.

Advances in compiler technology increases the use of high-level programming


language to program microcontrollers. C programming language is the
language of choice to program these devices.
©2023 Hazizulden AbdulAziz

ARM Processor
Instruction Set
The ARM architecture includes
two instruction sets for executing
code on processors that support
the Thumb mode: Thumb and
Thumb-2.

Thumb is a 16-bit instruction set


designed to reduce code size and
improve performance on memory-
constrained systems.

Thumb-2 is an extension of the


Thumb instruction set that
supports both 16-bit and 32-bit
instructions
©2023 Hazizulden AbdulAziz

Code written in
Assembly Language
©2023 Hazizulden AbdulAziz

Easier to write codes in C which can be disassembled and


converted into efficient assembly codes instead of to write
efficient codes in assembly from scratch
©2023 Hazizulden AbdulAziz

Embedded C
An ANSI/ISO C with some extensions and adaptations to suit the specific
microcontroller architecture
©2023 Hazizulden AbdulAziz

Using C for Low-Level Programming


In order to write efficient C codes for embedded system, it is important to have
good knowledge about:
the data types native support
the capability of tools for programming & debugging
the availability of standard libraries
the processor architecture characteristics

C needs processor specific compiler (cross compiler) to translate codes in C


to machine code
©2023 Hazizulden AbdulAziz

Structure of a
Program written in
Embedded C
©2023 Hazizulden AbdulAziz

Data Types
Data types Description Range

char (signed char) 8-bit ASCII character ± 127

unsigned char 8-bit ASCII character 0 to 255

int (signed int) 16-bit integer ± 32767

unsigned int 16-bit integer 0 to 65535

float 4 bytes single precision floating point number

double 8 bytes double precision floating point number


©2023 Hazizulden AbdulAziz

Unit of binary number: bit, nibble, byte and word


Bit is the smallest unit of binary number. A bit only has 1 digit, either 1 or 0.

String of binary digits (bits) are usually grouped into a string of 4 or 8 bits to
simplified writing, reading and processing the binary numbers. A nibble is a unit
of binary number consisting 4 bits of 1’s and/or 0’s and, a byte is a unit of
binary number consisting of 8 bits of 1’s and/or 0’s.

A word is a unit of binary number consisting of 2-4 bytes of 1’s and/or 0’s

Most digital systems process and store binary data in a group of 8-bit or a byte
©2023 Hazizulden AbdulAziz

C type stdint.h type Bits Range

The stdint.h type char uint8_t 8 0…255


signed char int8_t 8 -128…127
unsigned short uint16_t 16 0…65535
The types defined in stdint.h is
used for storing and working with short (int) int16_t 16 -32768…32767
data explicitly at the bit level.
unsigned int uint32_t 32 0…4,294,967,295
The stdint.h types declare a
variable as fix length variables int (signed int) int32_t 32 -2,147,483,648
regardless of the OS.
…2,147,483,647
Preprocessor instruction unsigned long uint64_t 64 0…
#include <stdint.h> long (int) int64_t 64
must be declared as
preprocessor directive
©2023 Hazizulden AbdulAziz

32-bit Data
Processing
A 32 bits long binary number can
by divided into 8 groups of 4-bit
binary.
4-bit binary can easily be written
as an hexadecimal number.

Example:
100101101010010111000011000
01111 in binary can be written as
0x96A5C30F in hexadecimal
©2023 Hazizulden AbdulAziz

4-bit binary Hexadecimal


Hexadecimal numbers 0000 0
0001 1
0010 2
Hexadecimal number system 0011 3
has 16 digits (composed of digits 0100 4
0 to 9 and letters A-F) and has a 0101 5
base of 16 (base-16). 0110 6
0111 7
Hexadecimal number is used to 1000 8
represent or to write a 4-bit 1001 9
binary number (nibble) 1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
©2023 Hazizulden AbdulAziz

Binary-Coded-Decimal (BCD)
Binary-Coded-Decimal (BCD) is used to Decimal Binary
represent each decimal digit as a binary 0 0000
number. 1 0001
Also known as 8-4-2-1 code 2 0010
3 0011
4 0100
5 0101
6 0110
7 0111
8 1000
9 1001
©2023 Hazizulden AbdulAziz

Alphanumeric
characters
representation
ASCII (American
Standard Code for
Information Interchange)
code is used to represent
alphanumeric characters
in binary format
©2023 Hazizulden AbdulAziz

Bitwise Operators
Category Operators
Bitwise NOT ~
Bitwise AND &
Bitwise OR |
Bitwise XOR ^
Bitwise left-shift << n
Bitwise right-shift >> n

Bitwise AND assignment &= A &= B; // A = A & B


Bitwise OR assignment |=
Bitwise XOR assignment ^=
Bitwise left-shift assignment <<= A <<= n; // A = A << n
Bitwise right-shift assignment >>=
©2023 Hazizulden AbdulAziz

Example 1:
Bitwise shift
operation
©2023 Hazizulden AbdulAziz

Bits Masking
Bits-masking is method of applying a mask to a value. Bits-masking filters a bit
or a multiple bit in a byte of data.
Using a bitmask, a bit in a byte can be set either to 1 (masked ON) or to 0
(masked OFF) using bitwise operation.
©2023 Hazizulden AbdulAziz

Example 2:
Bit Masking
©2023 Hazizulden AbdulAziz

Structure Data Type


Structure is a composite data type that allows you to group together variables of
different data types under a single name. A structure can contain any number of
variables of different data types, including other structures, arrays, and pointers.
©2023 Hazizulden AbdulAziz

Example 3:
Structure datatype
Each member of a structure is
accessed using member/dot (.)
operator
©2023 Hazizulden AbdulAziz

Example 4:
Structure datatype
Using arrow operator (->) to
access member of structure data
type pointed by a pointer variable
©2023 Hazizulden AbdulAziz

Pointer
A pointer is a variable that stores the memory address of another variable.
Pointers allow you to manipulate the data stored in memory, which is useful for
tasks such as dynamic memory allocation, passing parameters to functions by
reference, and creating complex data structures like linked lists and trees.
©2023 Hazizulden AbdulAziz

Example 5:
Pointer
©2023 Hazizulden AbdulAziz

Using structure data type in embedded programming


Structure data type simplify access to peripheral registers of a memory-mapped
microcontroller peripherals
©2023 Hazizulden AbdulAziz

Example 6:
©2023 Hazizulden AbdulAziz

Example 7:
©2023 Hazizulden AbdulAziz

Learning Outcomes (Review)


1. Declare type variables according to bit-size of data
2. Use bitwise operators to manipulate data at bit level
3. Read codes written in C for embedded system
4. Explain the flow of instructions written for embedded system
©2023 Hazizulden AbdulAziz

When writing a program, the program should be clear,


concise, correct & commented
©2023 Hazizulden AbdulAziz

NEXT
Getting started with STM32 IDE

You might also like