SlideShare a Scribd company logo
Introduction to Embedded C
Overview C for microcontrollers Review of C basics Compilation flow for SiLabs IDE C extensions In-line assembly Interfacing with C Examples Arrays and Pointers I/O Circuitry Functions and Header Files Multitasking and multithreading
C for Microcontrollers Of higher level languages, C is the closest to assembly languages bit manipulation instructions pointers (indirect addressing) Most microcontrollers have available C compilers  Writing in C simplifies code development for large projects.
Available C Compilers Keil  C – integrated with the IDE we have been using for labs. Reads51 – available on web site ( http://www.rigelcorp.com/reads51.htm ) Freeware: SDCC - Small Device C Compiler ( http://sdcc.sourceforge.net/ ) Other freeware versions …
Compilation Process (Keil) program.c program.OBJ program.M51 compile program.LST build/make no SRC option
Modular Programming Like most high level languages, C is a modular programming language (but NOT an object oriented language) Each task can be encapsulated as a function. Entire program is encapsulated in “main” function.
Basic C Program Structure Compiler directives and include files Declarations of global variables and constants Declaration of functions Main function Sub-functions Interrupt service routines
Back to C Basics All C programs consists of: Variables Functions (one must be “main”) Statements To define the SFRs as  variables : #include <reg51.h>
Variables All variables must be declared at top of program, before the first statement. Declaration includes  type  and list of variables. Example:  void main (void) {   int var, tmp; Types: int  (16-bits in our compiler) char  (8-bits) short  (16-bits) long  (32-bits) sbit  (1-bit) others that we will discuss later not standard C – an 8051 extension must go HERE!
Variables The following variable types can be signed or unsigned: signed char  (8 bits) –128 to +127 signed short (16 bits) –32768 to +32767 signed int  (16 bits) –32768 to +32767 signed long  (32 bits) –2147483648 to +2147483648  unsigned char  (8 bits) 0 to + 255 unsigned short (16 bits) 0 to + 65535 unsigned int  (16 bits) 0 to + 65535 unsigned long  (32 bits) 0 to + 4294967295  NOTE:  Default is signed – it is best to specify .
Statements Assignment statement: variable =  constant  or  expression  or  variable examples:  upper = 60 ;   I = I + 5;   J = I;
Operators Arithmetic: +, -, *, / Relational comparisons: >, >=, <, <= Equality comparisons: ==, != Logical operators: && (and), || (or) Increment and decrement: ++, -- Example: if (x != y) && (c == b) {  a=c + d*b; a++; }
Example – Adder program  (add 2 16-bit numbers) $INCLUDE (C8051F020.inc)  XL equ 0x78  XH equ 0x79  YL equ 0x7A  YH equ 0x7B   cseg at 0  ljmp Main  cseg at 100h  ; Disable watchdog timer Main:  mov 0xFF, #0DEh mov 0xFF, #0ADh mov a, XL add a, YL mov XL, a mov a, XH addc a, YH mov XH, a nop end #include <reg51.h>  void main (void) { int x, y, z;  //16-bit variables // disable watchdog timer WDTCN = 0xde; WDTCN = 0xad; z = x + y; } The C version The assembly version
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 RAM when debugging assemble
adder.SRC   x?040:  DS  2 y?041:  DS  2 z?042:  DS  2 main: ; SOURCE LINE # 12 ; int x, y, z; ;  WDTCN = 0xde; // disable watchdog timer ; SOURCE LINE # 14 MOV  WDTCN,#0DEH ;  WDTCN = 0xad; ; SOURCE LINE # 15 MOV  WDTCN,#0ADH  ;  z = x + y; ; SOURCE LINE # 17 MOV  A,x?040+01H ADD  A,y?041+01H MOV  z?042+01H,A MOV  A,x?040 ADDC  A,y?041 MOV  z?042,A ;  } ; SOURCE LINE # 18 RET  ; END OF main END
Bitwise Logic Instructions AND OR XOR left shift right shift 1’s complement & | ^ << >> ~ n = n & 0xF0; n = n & (0xFF << 4) n = n & ~(0xFF >> 4) Examples:
Example – Logic in Assembly and C Main:  mov WDTCN, #0DEh  mov WDTCN, #0ADh  xrl a, #0xF0  ; invert bits 7-4  orl a, #0x0C  ; set bits 3-2  anl a, #0xFC  ; reset bits 1-0 mov P0, a ; send to port0  void main (void) { char x; WDTCN = 0xDE; WDTCN = 0xAD; x = x ^ 0xF0; x = x | 0x0C; x = x & 0xFC; P0 = x; }
Loop Statements - While While loop: while (condition) { statements } while condition is true, execute statements if there is only one statement, we can lose the {} Example:  while (1) ; // loop forever
Loop Statements - For For statement: for (initialization; condition; increment) {statements} initialization  done before statement is executed condition  is tested, if true, execute statements do  increment  step and go back and test condition again repeat last two steps until condition is not true
Example: for loop for (n = 0; n<1000; n++) n++ means  n = n + 1 Be careful with signed integers! for (i=0; i < 33000; i++) LED = ~LED; Why is this an infinite loop?
Loops: do - while do statements while (expression); Test made at the bottom of the loop
Decision – if statement if (condition1) {statements1} else if (condition2) {statements2} … else {statementsn}
Decision – switch statement switch (expression) {   case const-expr: statements   case const-expr: statements   default: statements }
Example: switch  switch (unibble) { case 0x00 : return (0xC0);  case 0x01 : return (0xF9);  case 0x02 : return (0xA4);  case 0x03 : return (0xC0);    default : return (0xFF); } Need a statement like “return” or “break” or execution falls through to the next case (unlike VHDL)
C Extensions: Additional Keywords Specify where variables go in memory For accessing SFRs
Accessing Specific Memory
C Access to 8051 Memory code:  program memory accessed by movc @a + dptr data idata bdata xdata
C Extensions for 8051 (Cygnal) New data types: Example: bit bit new_flag;  //stored in 20-2F sbit sbit  LED  = P1^6; sfr sfr SP  =  0x81; //stack pointer sfr16  sfr16 DP  = 0x82;  // data pointer $INCLUDE ( c8051F020.h )
C Data Types With Extensions
Declaring Variables in Memory char data temp; char idata varx; int xdata array[100]; char code text[] = “Enter data”;
Example: Accessing External Memory Program defines two 256 element arrays in external memory First array is filled with values that increase by 2 each location. First array is copied to second array. Similar to block move exercise done in assembly.
Interrupts – 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
In-line Assembly When it is more efficient, or easier, can insert assembly code in C programs. #pragma asm put your assembly code here #pragma endasm
Compilation Process (Keil) program.c program.OBJ program.M51 compile program.LST build/make program.SRC .OBJ or .SRC can be generated, not both program.OBJ rename file program.asm assemble build/make no SRC option with SRC option Must use this path for C programs with in-line assembly It is also necessary to add  #pragma SRC  to code
Case study :: Temperature Display Temperature Sensor program Configures the external oscillator Configures the ADC0 for temp. sensor Configures Port1 so LED can be used Configures Timer3 to synch the ADC0 Uses ADC0 ISR to take temperature samples and averages 256 of them and posts average to global variable Main program compares average temp. to room temp. and lights LED if temp is warmer.
THANK YOU

More Related Content

PPTX
[ASM]Lab4
PPT
Verilog Lecture2 thhts
PDF
Verilog lab manual (ECAD and VLSI Lab)
PPT
Verilog tutorial
PPT
Coding verilog
PPTX
[ASM]Lab5
PDF
N_Asm Assembly arithmetic instructions (sol)
PPT
Fpga 04-verilog-programming
[ASM]Lab4
Verilog Lecture2 thhts
Verilog lab manual (ECAD and VLSI Lab)
Verilog tutorial
Coding verilog
[ASM]Lab5
N_Asm Assembly arithmetic instructions (sol)
Fpga 04-verilog-programming

What's hot (20)

PDF
C programming session10
PPT
Chapt 06
PPTX
[ASM]Lab6
PPTX
[ASM]Lab7
PDF
04 sequentialbasics 1
PDF
C programming part2
PPTX
[ASM] Lab1
PPT
Assembly Language Lecture 5
PDF
PPT
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
PPT
1344 Alp Of 8086
PDF
Verilog tutorial
PPT
Verilog Lecture4 2014
PDF
Embedded c
PPTX
Multiplication & division instructions microprocessor 8086
PDF
assembly language programming and organization of IBM PC" by YTHA YU
PDF
Chapter 6 Flow control Instructions
PDF
Lecture5(1)
PDF
Delays in verilog
C programming session10
Chapt 06
[ASM]Lab6
[ASM]Lab7
04 sequentialbasics 1
C programming part2
[ASM] Lab1
Assembly Language Lecture 5
Lec9 Intro to Computer Engineering by Hsien-Hsin Sean Lee Georgia Tech -- Com...
1344 Alp Of 8086
Verilog tutorial
Verilog Lecture4 2014
Embedded c
Multiplication & division instructions microprocessor 8086
assembly language programming and organization of IBM PC" by YTHA YU
Chapter 6 Flow control Instructions
Lecture5(1)
Delays in verilog
Ad

Similar to EMBEDDED SYSTEMS 4&5 (20)

PPT
C language programming
PPT
C language programming
PPTX
Session1
PDF
Lenguaje de Programación en C Presentacion
PPTX
ISA.pptx
PPT
ERTS UNIT 3.ppt
PPT
12 mt06ped008
PPTX
Instruction Set Architecture
PPTX
LCD_Example.pptx
PDF
ESL Anyone?
PPTX
Embedded C programming session10
PPT
Picmico
PDF
Assembly language part I
PDF
Assembly language part I
PPTX
Verilogspk1
PDF
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
PPT
Chapter Eight(3)
PDF
Short Notes on Verilog and SystemVerilog
PPT
Embedded c programming22 for fdp
PPT
class04_x86assembly.ppt hy there u need be
C language programming
C language programming
Session1
Lenguaje de Programación en C Presentacion
ISA.pptx
ERTS UNIT 3.ppt
12 mt06ped008
Instruction Set Architecture
LCD_Example.pptx
ESL Anyone?
Embedded C programming session10
Picmico
Assembly language part I
Assembly language part I
Verilogspk1
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Chapter Eight(3)
Short Notes on Verilog and SystemVerilog
Embedded c programming22 for fdp
class04_x86assembly.ppt hy there u need be
Ad

More from PRADEEP (20)

PPT
Unit 3 tables and data structures
PPT
Unit 2 software partitioning
PPT
Unit 1 introduction
PPT
Unit 5 multi-board system
PDF
16f877
PDF
Mp lab
PDF
19199406 embedded-c-tutorial-8051
PDF
13986149 c-pgming-for-embedded-systems
PDF
22323006 embedded-c-tutorial-8051
PDF
14157565 embedded-programming
PPT
Rtos 3 & 4
DOCX
Introduction to pic
PPT
Interrupts
PPTX
Chapter 3
PPT
Leadership lessons-from-obama-
PPT
Programming timers
PPT
Interfacing stepper motor
PPT
Interfacing rs232
PPT
Interfacing keypad
PPT
Interfacing adc
Unit 3 tables and data structures
Unit 2 software partitioning
Unit 1 introduction
Unit 5 multi-board system
16f877
Mp lab
19199406 embedded-c-tutorial-8051
13986149 c-pgming-for-embedded-systems
22323006 embedded-c-tutorial-8051
14157565 embedded-programming
Rtos 3 & 4
Introduction to pic
Interrupts
Chapter 3
Leadership lessons-from-obama-
Programming timers
Interfacing stepper motor
Interfacing rs232
Interfacing keypad
Interfacing adc

EMBEDDED SYSTEMS 4&5

  • 2. Overview C for microcontrollers Review of C basics Compilation flow for SiLabs IDE C extensions In-line assembly Interfacing with C Examples Arrays and Pointers I/O Circuitry Functions and Header Files Multitasking and multithreading
  • 3. C for Microcontrollers Of higher level languages, C is the closest to assembly languages bit manipulation instructions pointers (indirect addressing) Most microcontrollers have available C compilers Writing in C simplifies code development for large projects.
  • 4. Available C Compilers Keil C – integrated with the IDE we have been using for labs. Reads51 – available on web site ( http://www.rigelcorp.com/reads51.htm ) Freeware: SDCC - Small Device C Compiler ( http://sdcc.sourceforge.net/ ) Other freeware versions …
  • 5. Compilation Process (Keil) program.c program.OBJ program.M51 compile program.LST build/make no SRC option
  • 6. Modular Programming Like most high level languages, C is a modular programming language (but NOT an object oriented language) Each task can be encapsulated as a function. Entire program is encapsulated in “main” function.
  • 7. Basic C Program Structure Compiler directives and include files Declarations of global variables and constants Declaration of functions Main function Sub-functions Interrupt service routines
  • 8. Back to C Basics All C programs consists of: Variables Functions (one must be “main”) Statements To define the SFRs as variables : #include <reg51.h>
  • 9. Variables All variables must be declared at top of program, before the first statement. Declaration includes type and list of variables. Example: void main (void) { int var, tmp; Types: int (16-bits in our compiler) char (8-bits) short (16-bits) long (32-bits) sbit (1-bit) others that we will discuss later not standard C – an 8051 extension must go HERE!
  • 10. Variables The following variable types can be signed or unsigned: signed char (8 bits) –128 to +127 signed short (16 bits) –32768 to +32767 signed int (16 bits) –32768 to +32767 signed long (32 bits) –2147483648 to +2147483648 unsigned char (8 bits) 0 to + 255 unsigned short (16 bits) 0 to + 65535 unsigned int (16 bits) 0 to + 65535 unsigned long (32 bits) 0 to + 4294967295 NOTE: Default is signed – it is best to specify .
  • 11. Statements Assignment statement: variable = constant or expression or variable examples: upper = 60 ; I = I + 5; J = I;
  • 12. Operators Arithmetic: +, -, *, / Relational comparisons: >, >=, <, <= Equality comparisons: ==, != Logical operators: && (and), || (or) Increment and decrement: ++, -- Example: if (x != y) && (c == b) { a=c + d*b; a++; }
  • 13. Example – Adder program (add 2 16-bit numbers) $INCLUDE (C8051F020.inc) XL equ 0x78 XH equ 0x79 YL equ 0x7A YH equ 0x7B cseg at 0 ljmp Main cseg at 100h ; Disable watchdog timer Main: mov 0xFF, #0DEh mov 0xFF, #0ADh mov a, XL add a, YL mov XL, a mov a, XH addc a, YH mov XH, a nop end #include <reg51.h> void main (void) { int x, y, z; //16-bit variables // disable watchdog timer WDTCN = 0xde; WDTCN = 0xad; z = x + y; } The C version The assembly version
  • 14. 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 RAM when debugging assemble
  • 15. adder.SRC x?040: DS 2 y?041: DS 2 z?042: DS 2 main: ; SOURCE LINE # 12 ; int x, y, z; ; WDTCN = 0xde; // disable watchdog timer ; SOURCE LINE # 14 MOV WDTCN,#0DEH ; WDTCN = 0xad; ; SOURCE LINE # 15 MOV WDTCN,#0ADH ; z = x + y; ; SOURCE LINE # 17 MOV A,x?040+01H ADD A,y?041+01H MOV z?042+01H,A MOV A,x?040 ADDC A,y?041 MOV z?042,A ; } ; SOURCE LINE # 18 RET ; END OF main END
  • 16. Bitwise Logic Instructions AND OR XOR left shift right shift 1’s complement & | ^ << >> ~ n = n & 0xF0; n = n & (0xFF << 4) n = n & ~(0xFF >> 4) Examples:
  • 17. Example – Logic in Assembly and C Main: mov WDTCN, #0DEh mov WDTCN, #0ADh xrl a, #0xF0 ; invert bits 7-4 orl a, #0x0C ; set bits 3-2 anl a, #0xFC ; reset bits 1-0 mov P0, a ; send to port0 void main (void) { char x; WDTCN = 0xDE; WDTCN = 0xAD; x = x ^ 0xF0; x = x | 0x0C; x = x & 0xFC; P0 = x; }
  • 18. Loop Statements - While While loop: while (condition) { statements } while condition is true, execute statements if there is only one statement, we can lose the {} Example: while (1) ; // loop forever
  • 19. Loop Statements - For For statement: for (initialization; condition; increment) {statements} initialization done before statement is executed condition is tested, if true, execute statements do increment step and go back and test condition again repeat last two steps until condition is not true
  • 20. Example: for loop for (n = 0; n<1000; n++) n++ means n = n + 1 Be careful with signed integers! for (i=0; i < 33000; i++) LED = ~LED; Why is this an infinite loop?
  • 21. Loops: do - while do statements while (expression); Test made at the bottom of the loop
  • 22. Decision – if statement if (condition1) {statements1} else if (condition2) {statements2} … else {statementsn}
  • 23. Decision – switch statement switch (expression) { case const-expr: statements case const-expr: statements default: statements }
  • 24. Example: switch switch (unibble) { case 0x00 : return (0xC0); case 0x01 : return (0xF9); case 0x02 : return (0xA4); case 0x03 : return (0xC0); default : return (0xFF); } Need a statement like “return” or “break” or execution falls through to the next case (unlike VHDL)
  • 25. C Extensions: Additional Keywords Specify where variables go in memory For accessing SFRs
  • 27. C Access to 8051 Memory code: program memory accessed by movc @a + dptr data idata bdata xdata
  • 28. C Extensions for 8051 (Cygnal) New data types: Example: bit bit new_flag; //stored in 20-2F sbit sbit LED = P1^6; sfr sfr SP = 0x81; //stack pointer sfr16 sfr16 DP = 0x82; // data pointer $INCLUDE ( c8051F020.h )
  • 29. C Data Types With Extensions
  • 30. Declaring Variables in Memory char data temp; char idata varx; int xdata array[100]; char code text[] = “Enter data”;
  • 31. Example: Accessing External Memory Program defines two 256 element arrays in external memory First array is filled with values that increase by 2 each location. First array is copied to second array. Similar to block move exercise done in assembly.
  • 32. Interrupts – 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
  • 33. In-line Assembly When it is more efficient, or easier, can insert assembly code in C programs. #pragma asm put your assembly code here #pragma endasm
  • 34. Compilation Process (Keil) program.c program.OBJ program.M51 compile program.LST build/make program.SRC .OBJ or .SRC can be generated, not both program.OBJ rename file program.asm assemble build/make no SRC option with SRC option Must use this path for C programs with in-line assembly It is also necessary to add #pragma SRC to code
  • 35. Case study :: Temperature Display Temperature Sensor program Configures the external oscillator Configures the ADC0 for temp. sensor Configures Port1 so LED can be used Configures Timer3 to synch the ADC0 Uses ADC0 ISR to take temperature samples and averages 256 of them and posts average to global variable Main program compares average temp. to room temp. and lights LED if temp is warmer.