embedded systems 4&5
DESCRIPTION
TRANSCRIPT
![Page 1: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/1.jpg)
Introduction to Embedded C
![Page 2: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/2.jpg)
OverviewOverview
C for microcontrollersC for microcontrollers Review of C basicsReview of C basics Compilation flow for SiLabs IDECompilation flow for SiLabs IDE C extensionsC extensions In-line assemblyIn-line assembly Interfacing with CInterfacing with C
ExamplesExamples Arrays and PointersArrays and Pointers I/O CircuitryI/O Circuitry Functions and Header FilesFunctions and Header Files Multitasking and multithreadingMultitasking and multithreading
![Page 3: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/3.jpg)
C for MicrocontrollersC for Microcontrollers
Of higher level languages, C is the Of higher level languages, C is the closest to assembly languagesclosest to assembly languages bit manipulation instructionsbit manipulation instructions pointers (indirect addressing)pointers (indirect addressing)
Most microcontrollers have available C Most microcontrollers have available C compilers compilers
Writing in C simplifies code Writing in C simplifies code development for large projects.development for large projects.
![Page 4: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/4.jpg)
Available C CompilersAvailable C Compilers
Keil C – integrated with the IDE we have Keil C – integrated with the IDE we have been using for labs.been using for labs.
Reads51 – available on web site (Reads51 – available on web site (http://www.rigelcorp.com/reads51.htmhttp://www.rigelcorp.com/reads51.htm))
Freeware: SDCC - Small Device C Freeware: SDCC - Small Device C Compiler (Compiler (http://sdcc.sourceforge.net/http://sdcc.sourceforge.net/))
Other freeware versions …Other freeware versions …
![Page 5: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/5.jpg)
Compilation Process (Keil)Compilation Process (Keil)
program.c
program.OBJ
program.M51
compile
program.LST
build/make
no SRC option
![Page 6: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/6.jpg)
Modular ProgrammingModular Programming
Like most high level languages, C is a Like most high level languages, C is a modular programming language (but modular programming language (but NOT an object oriented language)NOT an object oriented language)
Each task can be encapsulated as a Each task can be encapsulated as a function.function.
Entire program is encapsulated in Entire program is encapsulated in “main” function.“main” function.
![Page 7: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/7.jpg)
Basic C Program StructureBasic C Program Structure
1.1. Compiler directives and include filesCompiler directives and include files
2.2. Declarations of global variables and constantsDeclarations of global variables and constants
3.3. Declaration of functionsDeclaration of functions
4.4. Main functionMain function
5.5. Sub-functionsSub-functions
6.6. Interrupt service routinesInterrupt service routines
![Page 8: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/8.jpg)
Back to C BasicsBack to C Basics All C programs consists of:All C programs consists of:
VariablesVariables Functions (one must be “main”)Functions (one must be “main”)
StatementsStatements
To define the SFRs as To define the SFRs as variablesvariables::#include <reg51.h>#include <reg51.h>
![Page 9: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/9.jpg)
VariablesVariables All variables must be declared at top of All variables must be declared at top of
program, before the first statement.program, before the first statement. Declaration includes Declaration includes type type and list of variables.and list of variables.
Example: void main (void) {Example: void main (void) { int var, tmp;int var, tmp;
Types:Types: intint (16-bits in our compiler) (16-bits in our compiler) charchar (8-bits) (8-bits) shortshort (16-bits) (16-bits) longlong (32-bits) (32-bits) sbitsbit (1-bit) (1-bit) others that we will discuss laterothers that we will discuss later
not standard C – an 8051 extension
must go HERE!
![Page 10: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/10.jpg)
VariablesVariables The following variable types can be The following variable types can be
signed or unsigned:signed or unsigned:signed char (8 bits) –128 to +127signed short (16 bits) –32768 to +32767signed int (16 bits) –32768 to +32767signed long (32 bits) –2147483648 to +2147483648
unsigned char (8 bits) 0 to + 255unsigned short (16 bits) 0 to + 65535unsigned int (16 bits) 0 to + 65535unsigned long (32 bits) 0 to + 4294967295
NOTE: Default is signed – it is best to specify.
![Page 11: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/11.jpg)
StatementsStatements Assignment statement:Assignment statement:
variable = variable = constantconstant or or expressionexpression or or variablevariable
examples: examples: upper = 60upper = 60;;
I = I + 5;I = I + 5;
J = I;J = I;
![Page 12: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/12.jpg)
OperatorsOperators
Arithmetic: +, -, *, /Arithmetic: +, -, *, / Relational comparisons: >, >=, <, <=Relational comparisons: >, >=, <, <= Equality comparisons: ==, !=Equality comparisons: ==, != Logical operators: && (and), || (or)Logical operators: && (and), || (or) Increment and decrement: ++, --Increment and decrement: ++, -- Example:Example:
if (x != y) && (c == b)if (x != y) && (c == b){ {
a=c + d*b;a=c + d*b;a++;a++;
}}
![Page 13: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/13.jpg)
Example – Adder program Example – Adder program (add 2 16-bit numbers)(add 2 16-bit numbers)
$INCLUDE (C8051F020.inc) $INCLUDE (C8051F020.inc) XL equ 0x78 XL equ 0x78 XH equ 0x79 XH equ 0x79 YL equ 0x7A YL equ 0x7A YH equ 0x7BYH equ 0x7B
cseg at 0 cseg at 0 ljmp Main ljmp Main
cseg at 100h cseg at 100h ; Disable watchdog timer; Disable watchdog timerMain: Main: mov 0xFF, #0DEhmov 0xFF, #0DEh mov 0xFF, #0ADhmov 0xFF, #0ADh
mov a, XLmov a, XLadd a, YLadd a, YLmov XL, amov XL, a mov mov
a, XHa, XHaddc a, YHaddc a, YHmov XH, amov XH, anopnop
endend
#include <reg51.h> #include <reg51.h>
void main (void)void main (void)
{{
int x, y, z; //16-bit variablesint x, y, z; //16-bit variables
// disable watchdog timer// disable watchdog timer
WDTCN = 0xde;WDTCN = 0xde;
WDTCN = 0xad;WDTCN = 0xad;
z = x + y;z = x + y;
}}
The C version
The assembly version
![Page 14: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/14.jpg)
Compilation Process (Keil)Compilation Process (Keil)
adder.c
adder.OBJ
adder.M51
compile
adder.SRC
build/make
Use the #pragma CODE compiler directive to get assembly code generated in SRC file.
Map file shows where variables are stored. One map file is generated per project.
Symbol Table in M51 file:------ DO D:0008H SYMBOL x D:000AH SYMBOL y D:000CH SYMBOL z ------- ENDDO
look here in RAMwhen debugging
assemble
![Page 15: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/15.jpg)
adder.SRCadder.SRC x?040: DS 2 y?041: DS 2 z?042: DS 2main:
; SOURCE LINE # 12; int x, y, z;; WDTCN = 0xde; // disable watchdog timer
; SOURCE LINE # 14MOV WDTCN,#0DEH
; WDTCN = 0xad;; SOURCE LINE # 15
MOV WDTCN,#0ADH ; z = x + y;
; SOURCE LINE # 17MOV A,x?040+01HADD A,y?041+01HMOV z?042+01H,AMOV A,x?040ADDC A,y?041MOV z?042,A
; } ; SOURCE LINE # 18RET
; END OF mainEND
![Page 16: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/16.jpg)
Bitwise Logic InstructionsBitwise Logic Instructions
ANDAND OROR XORXOR left shiftleft shift right shiftright shift 1’s 1’s
complementcomplement
&|^
<<>>~
n = n & 0xF0;
n = n & (0xFF << 4)
n = n & ~(0xFF >> 4)
Examples:
![Page 17: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/17.jpg)
Example – Logic in Assembly Example – Logic in Assembly and Cand C
Main: Main:
mov WDTCN, #0DEh mov WDTCN, #0DEh
mov WDTCN, #0ADh mov WDTCN, #0ADh
xrl a, #0xF0 ; invert bits 7-4 xrl a, #0xF0 ; invert bits 7-4
orl a, #0x0C ; set bits 3-2 orl a, #0x0C ; set bits 3-2
anl a, #0xFC ; reset bits 1-0anl a, #0xFC ; reset bits 1-0
mov P0, amov P0, a ; send to port0 ; send to port0
void main (void) {void main (void) {
char x;char x;
WDTCN = 0xDE;WDTCN = 0xDE;
WDTCN = 0xAD;WDTCN = 0xAD;
x = x ^ 0xF0;x = x ^ 0xF0;
x = x | 0x0C;x = x | 0x0C;
x = x & 0xFC;x = x & 0xFC;
P0 = x;P0 = x;
}}
![Page 18: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/18.jpg)
Loop Statements - WhileLoop Statements - While
While loop:While loop:
while (condition) { statements }while (condition) { statements }
while condition is true, execute statementswhile condition is true, execute statements
if there is only one statement, we can lose the if there is only one statement, we can lose the {}{}
Example: Example: while (1) ;while (1) ; // loop forever// loop forever
![Page 19: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/19.jpg)
Loop Statements - ForLoop Statements - For For statement:For statement:
for (initialization; condition; increment) {statements}for (initialization; condition; increment) {statements}
initializationinitialization done before statement is executed done before statement is executed
conditioncondition is tested, if true, execute statements is tested, if true, execute statementsdo do incrementincrement step and go back and test condition again step and go back and test condition again
repeat last two steps until condition is not truerepeat last two steps until condition is not true
![Page 20: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/20.jpg)
Example: for loopExample: for loop
for (n = 0; n<1000; n++)for (n = 0; n<1000; n++)
n++ means n = n + 1n++ means n = n + 1
Be careful with signed integers!Be careful with signed integers!
for (i=0; i < 33000; i++) LED = ~LED;for (i=0; i < 33000; i++) LED = ~LED;
Why is this an infinite loop?Why is this an infinite loop?
![Page 21: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/21.jpg)
Loops: do - whileLoops: do - while
dodo
statementsstatements
while (expression);while (expression);
Test made at the bottom of the loopTest made at the bottom of the loop
![Page 22: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/22.jpg)
Decision – if statementDecision – if statement
if (condition1)if (condition1)
{statements1}{statements1}
else if (condition2)else if (condition2)
{statements2}{statements2}
……
elseelse
{statementsn}{statementsn}
![Page 23: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/23.jpg)
Decision – switch statementDecision – switch statement
switch (expression) {switch (expression) {
case const-expr: statementscase const-expr: statements
case const-expr: statementscase const-expr: statements
default: statementsdefault: statements
}}
![Page 24: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/24.jpg)
Example: switch Example: switch
switch (unibble) {switch (unibble) {
case 0x00 : return (0xC0); case 0x00 : return (0xC0);
case 0x01 : return (0xF9); case 0x01 : return (0xF9);
case 0x02 : return (0xA4); case 0x02 : return (0xA4);
case 0x03 : return (0xC0); case 0x03 : return (0xC0);
default : return (0xFF);default : return (0xFF);
}}
Need a statement like “return” or “break” or execution falls through to the next case (unlike VHDL)
![Page 25: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/25.jpg)
C Extensions: Additional C Extensions: Additional KeywordsKeywords
Specify where variables goin memory
For accessing SFRs
![Page 26: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/26.jpg)
Accessing Specific MemoryAccessing Specific Memory
![Page 27: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/27.jpg)
C Access to 8051 MemoryC Access to 8051 Memorycode: program memory accessed by movc @a + dptr data
idata
bdata
xdata
![Page 28: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/28.jpg)
C Extensions for 8051 C Extensions for 8051 (Cygnal)(Cygnal)
New data types:New data types:
Example:Example:
bitbit bit new_flag; //stored in 20-2Fbit new_flag; //stored in 20-2F
sbitsbit sbit LED = P1^6;sbit LED = P1^6;
sfrsfr sfr SP = 0x81;sfr SP = 0x81; //stack pointer//stack pointer
sfr16 sfr16 sfr16 DP = 0x82; // data pointersfr16 DP = 0x82; // data pointer
$INCLUDE ($INCLUDE (c8051F020.hc8051F020.h) )
![Page 29: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/29.jpg)
C Data Types With C Data Types With ExtensionsExtensions
![Page 30: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/30.jpg)
Declaring Variables in Declaring Variables in MemoryMemory
char data temp;char idata varx;int xdata array[100];char code text[] = “Enter data”;
![Page 31: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/31.jpg)
Example: Accessing External Example: Accessing External MemoryMemory
Program defines two 256 element Program defines two 256 element arrays in external memoryarrays in external memory
First array is filled with values that First array is filled with values that increase by 2 each location.increase by 2 each location.
First array is copied to second array.First array is copied to second array. Similar to block move exercise done Similar to block move exercise done
in assembly.in assembly.
![Page 32: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/32.jpg)
Interrupts – Original 8051Interrupts – Original 8051
void timer0 (void) interrupt 1 {
if (++interruptcnt == 4000) /* count to 4000 */{
second++; /* second counter */interruptcnt = 0; /* clear int counter */}
}
Specify register bank 2
![Page 33: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/33.jpg)
In-line AssemblyIn-line Assembly
When it is more efficient, or easier, When it is more efficient, or easier, can insert assembly code in C can insert assembly code in C programs.programs.
#pragma asm#pragma asm
put your assembly code hereput your assembly code here
#pragma endasm#pragma endasm
![Page 34: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/34.jpg)
Compilation Process (Keil)Compilation Process (Keil)program.c
program.OBJ
program.M51
compile
program.LST
build/make
program.SRC
.OBJ or .SRC canbe generated, not both
program.OBJ
rename file
program.asm
assemblebuild/make
no SRC option
with SRC option
Must use this path for C programs with in-line assemblyIt is also necessary to add #pragma SRC to code
![Page 35: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/35.jpg)
Case study :: Temperature Case study :: Temperature DisplayDisplay
Temperature Sensor programTemperature Sensor program Configures the external oscillatorConfigures the external oscillator Configures the ADC0 for temp. sensorConfigures the ADC0 for temp. sensor Configures Port1 so LED can be usedConfigures Port1 so LED can be used Configures Timer3 to synch the ADC0Configures Timer3 to synch the ADC0 Uses ADC0 ISR to take temperature samples Uses ADC0 ISR to take temperature samples
and averages 256 of them and posts average and averages 256 of them and posts average to global variableto global variable
Main program compares average temp. to Main program compares average temp. to room temp. and lights LED if temp is warmer.room temp. and lights LED if temp is warmer.
![Page 36: EMBEDDED SYSTEMS 4&5](https://reader034.vdocuments.net/reader034/viewer/2022052522/548ed1c4b479592f108b4704/html5/thumbnails/36.jpg)
THANK YOUTHANK YOU