Chapter 5: Introduction to C Programming
Embedded Systems - Shape The World

Jonathan Valvano and Ramesh Yerraballi

As part of the edX online class, we made some interactive web pages to illustrate fundamental concepts


Interactive web pages: Table of Contents

2. Fundamental Concepts Number conversions
5. Introduction to C Flowcharts, C vs assembly
6. Microcontroller Ports Input/output, direction register
7. Design and Development Successive refinement, if-then, loops
10. Finite State Machines Vending machine and stepper motor
11. UART Serial Interface Blind, busy-wait, interrupt, serial port
12. Interrupts Mail box, context switch
13. DAC and Sound Sampling rate, precision, how a DAC works
14. ADC and Data Acquisition    How an ADC works, Nyquist Theorem

Interactive Tool 5.1

The following tool allows you to see the ordered sequence of steps (labeled A-H) executed as you interact with the Switch to give an input. When the code is executing in the Event handler subroutine you can click on the switch to turn it on and off and see the timeline (at the bottom) reflect this.


Main Event Event Return Output 0 to PortA1 Output 1 to PortA1 Input n from PA0 Input n from PA0 n n w w = 4 w = w-1 A B C D E F G H Cortex M4 Port A 6 PA0 PA1 3.3 V Microcontroller 0 >0 1 1 0 0 0 0 V 1600 Ω 10 kΩ Variables: w= n= No picture

 

Interactive Tool 5.2

The sum function (aka subroutine) in Program 5.7 has two 32-bit signed input parameters, and one 32-bit signed output parameter. The interesting part is that (in assembly) after the operations within the subroutine are performed, control returns to the place right after where the subroutine was called. It is the same in C. You will also see how the registers are manipulated as the code flows.

Assembly Code Registers
Address Machine Code Label Instruction Comment

0x00000660 EB010200 sum ADD R2,R1,R0 ;z=x+y
0x00000664 4610 MOV R0,R2 ;return value
0x00000666 4770 BX LR
0x00000668 F44F60FA main MOV R0,#2000 ;first parameter
0x0000066C F44F61FA MOV R1,#2000 ;second parameter
0x00000670 F7FFFFF6 BL sum ;call function
0x00000674 4603 MOV R3,R0 ;a=sum(2000,2000)
0x00000676 F04F0400 MOV R4,#0x00 ;b=0
0x0000067A 4620 loop MOV R0,R4 ;first parameter
0x0000067C F04F0101 MOV R1,#0x01 ;second parameter
0x00000680 F7FFFFEE BL sum ;call function
0x00000684 4604 MOV R4,R0 ;b=sum(b,1)
0x00000686 E7F8 B loop
Register Value
R0 0x00000000
R1 0x00000000
R2 0x00000000
R3 0x00000000
R4 0x00000000
R5 0x00000000
R6 0x00000000
R7 0x00000000
R8 0x00000000
R9 0x00000000
R10 0x00000000
R11 0x00000000
R12 0x00000000
R13(SP) 0x00000000
R14(LR) 0x00000000
R15(PC) 0x00000668
C Code
long sum(long x, long y){ long z;
z = x+y;
return(z);
} void main(void){ long a,b;
a = sum(2000,2000);
b = 0;
while(1){
b = sum(b,1);
}
}

Program 5.7. A function with two inputs and one output.


Reprinted with approval from Embedded Systems: Introduction to ARM Cortex-M Microcontrollers, 2016, ISBN: 978-1477508992, http://users.ece.utexas.edu/~valvano/arm/outline1.htm

 

Creative Commons License
Embedded Systems - Shape the World by Jonathan Valvano and Ramesh Yerraballi is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.
Based on a work at http://users.ece.utexas.edu/~valvano/arm/outline1.htm.