ECE 521: Microprocessor System: CHAPTER 3: Microprocessor Programming in C
ECE 521: Microprocessor System: CHAPTER 3: Microprocessor Programming in C
Microprocessor System
CHAPTER 3: Microprocessor Programming in C
What we will learn in this section:
❑ Data types
❑ Bit-wise Operations in C
❑ Logic operation
❑ Data conversion
❑ Simple I/O programming in C language
Why Use C for Embedded
Programming?
Portability : code can be retargeted to different processors Portability of a
language is mainly important when an application program must be
moved to other operating systems. Code developed in C is more portable
and user can compile it on other platforms with least modifications.
Readability : C code is efficient, easy to understand, maintain and debug.
A program can be written in one machine and can be run on other
machines.
Processor independent : C language is not specific to any
microprocessor/micro-controller or any system. It can work on various
hardware configuration. C doesn’t require same set of hardware to run a
program.
Data Types Sizes
3 methods to find out the exact sizes of the data
types:
1. Read the compiler manual.
2. Use pseudo function sizeof().
3. Use C99 data types.
ISO C99 integer data types and their
ranges
Data type Size Range Min Range Max
int8_t 1 byte -128 127
uint8_t 1 byte 0 to 255
int16_t 2 bytes -32,768 32,767
uint16_t 2 bytes 0 65,535
int32_t 4 bytes -2,147,483,648 2,147,483,647
uint32_t 4 bytes 0 4,294,967,295
int64_t 8 bytes -9,223,372,036,854,775,808 9,223,372,036,854,775,807
uint64_t 8 bytes 0 18,446,744,073,709,551,615
Data type examples
• Read bits from GPIOA (16 bits, non-numeric)
uint16_t n; n = GPIOA->IDR; //or: unsigned short n;
• Write TIM2 prescale value (16-bit unsigned)
uint16_t t; TIM2->PSC = t; //or: unsigned short t;
• Read 32-bit value from ADC (unsigned)
uint32_t a; a = ADC; //or: unsigned int a;
• System control value range [-1000…+1000]
int32_t ctrl; ctrl = (x + y)*z; //or: int ctrl;
• Loop counter for 100 program loops (unsigned)
uint8_t cnt; //or: unsigned char cnt;
for (cnt = 0; cnt < 20; cnt++) {
Bit-wise logical operators in C
C = A & B; A 0 1 1 0 0 1 1 0
(AND) B 1 0 1 1 0 0 1 1
C 0 0 1 0 0 0 1 0
C = A | B; A 0 1 1 0 0 1 0 0
(OR) B 0 0 0 1 0 0 0 0
C 0 1 1 1 0 1 0 0
C = A ^ B; A 0 1 1 0 0 1 0 0
(XOR) B 1 0 1 1 0 0 1 1
C 1 1 0 1 0 1 1 1
B = ~A; A 0 1 1 0 0 1 0 0
(COMPLEMENT) B 1 0 0 1 1 0 1 1
Setting ,Clearing (masking) and Testing
bits with bit-wise operators in C
Anything ORed with a “1“ results in a “1”; anything ORed with a “0” results
in no change.
Anything ANDed with a “1”results in no change; anything ANDed with a “0”
results in a zero.
Anything EX-ORed with a “1” results in the complement; anything EX-ORed
with a “0” results in no change.
When it is necessary to test a given bit to see if it is high or low, the unused
bits are masked and then the remaining data is tested.
Bit set/reset/complement/test
• Use a “mask” to select bit(s) to be altered
C = A & 0xFE; A a b c d e f g h
0xFE 1 1 1 1 1 1 1 0 Clear selected bit of A
C a b c d e f g 0
C = A & 0x01; A a b c d e f g h
0x01 0 0 0 0 0 0 0 1 Clear all but the selected bit of A
C 0 0 0 0 0 0 0 h
C = A | 0x01; A a b c d e f g h
0x01 0 0 0 0 0 0 0 1 Set selected bit of A
C a b c d e f g 1
C = A ^ 0x01; A a b c d e f g h
0x01 0 0 0 0 0 0 0 1 Complement selected bit of A
C a b c d e f g h’
Bit-wise shift operation in C
Operation Symbol Format of Shift Operation
Shift Right >> data >> number of bit-positions to be shifted right
Shift Left << data << number of bit-positions to be shifted left
Shift operators:
x >> y (right shift operand x by y bit positions)
x << y (left shift operand x by y bit positions)
Vacated bits are filled with 0’s.
B = A << 3; A 1 0 1 0 1 1 0 1
(Left shift 3 bits) B 0 1 1 0 1 0 0 0` filled with 0
B = A >> 2; A 1 0 1 1 0 1 0 1
(Right shift 2 bits) B 0 0 1 0 1 1 0 1
filled with 0
Compound Operators
One way to ease the generation of the mask is to use the shift left operator.
▪ To generate a mask with bit n set to 1, use the expression: 1 << n
If more bits are to be set in the mask, they can be “or” together.
▪ To generate a mask with bit n and bit m set to 1, use the expression:
(1 << n) | (1 << m)
▪ Example:
register |= (1 << 6) | (1 << 1);
Setting the value in a multi-bit field
Example:
register |= 1 << 30;
register &= ~(1 << 29);
register |= 1 << 28;
/* Function definitions*/
int function1(char x) { //parameter x passed to the function, function returns an integer value
int i,j; //local (automatic) variables – allocated to stack or registers
-- instructions to implement the function
}
/* Main program */
void main(void) {
unsigned char sw1; //local (automatic) variable (stack or registers)
int k; //local (automatic) variable (stack or registers)
Declare local variables
/* Initialization section */
-- instructions to initialize variables, I/O ports, devices, function registers Initialize variables/devices
/* Endless loop */
while (1) { //Can also use: for(;;) {
-- instructions to be repeated Body of the program
} /* repeat forever */
}
Memory Map of STMF446RE
Microcontroller “header file”
if (work == 1)
{
statement s1;
statement s2;
}
IF-THEN-ELSE structure
if (condition) statement1;
else statement2 ;
▪If the condition is met (true), the statement1 is executed,
otherwise (false), statement2 is executed
if (a == 0)
{
statement s1;
statement s2; Yes No
S1; a == 0 S3;
} S2; ? S4;
else
{
statement s3;
statement s4:
}
Multiple ELSE-IF structure
• Multi-way decision, with expressions
evaluated in a specified order
if (n == 1)
statement1; //do if n == 1
else if (n == 2)
statement2; //do if n == 2
else if (n == 3)
statement3; //do if n == 3
else
statement4; //do if any other value of n (none of the above)
while (a < b)
{ S1;
statement s1; a<b S2;
? Yes …
statement s2;
No “loop” through these
…. statements while a < b
}
Read
PORTB
do “loop” through
S1;
{ S2; these statements
statement s1; … until a < b
statement s2;
….
Yes
} a<b
while (a < b); ?
No
FOR loop structure
}
FOR loop structure
Configure PA5 as
output
Turn on LED
Switching ON LED
START
Configure PA5 as
output
Turn on LED
Delay 0.5s
User LED connection for STM32F446RE Nucleo board
Delay 0.5s
TOOGLE OR BLINKING LED
Reading a Switch Condition
Eg. Write a program in C language that will turn on Green LED (LD2) on the
STM32F446RE board when the switch B1 is pressed (pull up register).
PA5
Reading a Switch Condition
START
Turn on LED
Configure PC13 as
input
Reading a Switch Condition