Embedded c programming22 for fdp


Published on

Published in: Education
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Embedded c programming22 for fdp

  2. 2. OVERVIEW <ul><li>Embedded C Programming (introduction) </li></ul><ul><li>Preprocessor Directives </li></ul><ul><li>Scope of Variables and Parameter passing </li></ul><ul><li>Embedded C++ </li></ul><ul><li>Embedded Software Development </li></ul>
  3. 3. Embedded C <ul><li>Embedded C is a subset of the C programming language which is intended for embedded systems programming. </li></ul><ul><li>It excludes size and/or speed consuming C features that are not relevant for embedded systems. </li></ul>
  4. 4. Embedded C….. <ul><li>It is midlevel with high level features. </li></ul><ul><li>It is very efficient </li></ul><ul><li>Good, well proven compilers are available for every embedded processors.(8 bit to 32 bit or more) </li></ul>
  5. 5. Preprocessor Directives <ul><li>#define </li></ul><ul><li>#pragma </li></ul><ul><ul><li>To describe specific resources of your target hardware </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>#pragma portrw PORTA @ 0x0000 </li></ul></ul></ul><ul><ul><ul><li>#pragma portrw PORTB @ 0x0001; </li></ul></ul></ul><ul><ul><ul><li>#pragma portw DDRB @ 0x0005; </li></ul></ul></ul>
  6. 6. Preprocessor Directives <ul><li>#ifdef </li></ul><ul><ul><li>checks for the existence of macro definitions. </li></ul></ul><ul><ul><li>Example: </li></ul></ul><ul><ul><ul><li>#ifdef EXTENDED </li></ul></ul></ul><ul><ul><ul><li>#define MAX_LEN 74 </li></ul></ul></ul><ul><ul><ul><li>#else </li></ul></ul></ul><ul><ul><ul><li>#define MAX_LEN 50 </li></ul></ul></ul><ul><ul><ul><li>#endif </li></ul></ul></ul>
  7. 7. Scope of Variables <ul><li>Local or Auto </li></ul><ul><li>Global variables </li></ul><ul><li>Volatile variables </li></ul><ul><li>Register variables </li></ul><ul><li>Static variables </li></ul>
  8. 8. Local and Global <ul><li>Auto or local variables </li></ul><ul><ul><li>Variables declared inside the function </li></ul></ul><ul><ul><li>Stored in temporary memory location like Stack </li></ul></ul><ul><li>Extern variables </li></ul><ul><ul><li>The declaration is used to describe the variable that is externally defined. </li></ul></ul>
  9. 9. Local Variables <ul><li>A temporary information used by only one software module or function </li></ul><ul><li>Local variables usually stored in the Stack. </li></ul><ul><li>To access local variables, the compiler use the stack pointer addressing mode. </li></ul>
  10. 10. Local Variables <ul><li>int a=100; </li></ul><ul><li>void main() </li></ul><ul><li>{ </li></ul><ul><ul><li>int b=20; </li></ul></ul><ul><ul><li>Printf(“%d”,b); </li></ul></ul><ul><ul><li>} </li></ul></ul>
  11. 11. Volatile <ul><li>Volatile variables </li></ul><ul><ul><li>volatile keyword informs the compiler that it can not depend upon the value of a variable and should not perform any optimizations based on assigned values. </li></ul></ul>
  12. 12. Volatile Example <ul><ul><li>unsigned char data[100]; </li></ul></ul><ul><ul><li>#define PORTA *(unsigned char volatile *)(0x0000) </li></ul></ul><ul><ul><li>#define DDRA *(unsigned char volatile *)(0x0004) </li></ul></ul><ul><ul><li>void main(void) </li></ul></ul><ul><ul><li>{ </li></ul></ul><ul><ul><li>short i; </li></ul></ul><ul><ul><li>DDRA=0x00; /* make Port A an input */ </li></ul></ul><ul><ul><li>for(i=0;i<100;i++){ /* collect 100 measurements */ </li></ul></ul><ul><ul><li>data[i]=PORTA;  /* collect ith measurement */ </li></ul></ul><ul><ul><li>}} </li></ul></ul>
  13. 13. register <ul><li>Register variables </li></ul><ul><ul><li>To give a hint to the compiler that the variable should be allocated to a CPU register if possible. </li></ul></ul><ul><ul><li>Some compilers allow global register variable declaration is helpful in case when there are more number of interrupt handlers </li></ul></ul>
  14. 14. Static Variables <ul><li>Static variables </li></ul><ul><ul><li>Static local or static global, both are stored in the RAM </li></ul></ul><ul><ul><li>File static and function static varibles are treated as global variables by most of the compilers </li></ul></ul>
  15. 15. Parameter Passing <ul><li>Input parameter </li></ul><ul><ul><li>Data passed from the calling routine into the module during execution </li></ul></ul><ul><li>Output parameter </li></ul><ul><ul><li>Information returned from the module to the calling routine. </li></ul></ul>
  16. 16. Parameter passing…. <ul><li>Pass by value </li></ul><ul><ul><li>Only passes the copies of the objects. </li></ul></ul><ul><li>Pass by reference </li></ul><ul><ul><li>References to the actual arguments are passed instead of the copies of the actual arguments. </li></ul></ul>
  17. 17. Example <ul><li>Void main() </li></ul><ul><li>{ </li></ul><ul><li>Unsigned short angle=0; </li></ul><ul><li>Stepper_init(); </li></ul><ul><li>While(true){ </li></ul><ul><li>Stepper_step(); </li></ul><ul><li>Next(&angle); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>Void next (unsigned short *pt) </li></ul><ul><li>{ </li></ul><ul><li>(*pt)++; </li></ul><ul><li>If((*pt)==200) </li></ul><ul><li>(*pt)=0; </li></ul><ul><li>} </li></ul>
  18. 18. Embedded C++ <ul><li>It is a subset of full draft C++ standard </li></ul><ul><li>These are omitted in EC++ </li></ul><ul><ul><li>Multiple inheritance and virtual base classes </li></ul></ul><ul><ul><li>Runtime type identification (RTTI) </li></ul></ul><ul><ul><li>Exception handling, Templates, namespaces </li></ul></ul>
  19. 19. Optimizing C and C++ Code <ul><li>Minimize local variables </li></ul><ul><li>Declare local variables in the inner most scope </li></ul><ul><li>Reduce the number of parameters </li></ul><ul><li>Don't define a return value if not used </li></ul><ul><li>Prefer int over char. </li></ul><ul><li>Prefer initialization over assignment </li></ul><ul><li>In-line 1 to 3 line functions </li></ul>
  20. 20. Some C Optimisations Non Optimised code Optimised code Unsigned short int a; a /=8; Unsigned short int a; a>>=3; a*=2; a+=a; for(i=0; i<10; i++) { b = k * c; p[i] = b; } b = k * c; /* constant code moved outside the loop */ for(i=0; i<10; i++) { p[i] = b; }
  21. 21. Super Loop Architecture <ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>// Prepare run function X </li></ul><ul><li>X_Init(); </li></ul><ul><li>while(1) // ‘for ever’ (Super Loop) </li></ul><ul><li>{ </li></ul><ul><li>X(); // Run function X() </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  22. 22. ECU Task: control injection time (3 sub-tasks) T S P compute air flow compute injection time drive actuators air flow injection time air temperature engine temperature engine speed throttle position look-up table PWM signals air pressure
  23. 23. Control Injection Time <ul><li>void main(void) </li></ul><ul><li>{ </li></ul><ul><li>Control_ECU_Init(); </li></ul><ul><li>while(1) // 'for ever' (Super Loop) </li></ul><ul><li>{ </li></ul><ul><li>Control_ECU_Get_throttle_position(); </li></ul><ul><li>Control_ECU_Get_Actual_speed(); </li></ul><ul><li>Control_ECU_Get_Actual_temperature(); </li></ul><ul><li>Control_ECU_Get_Actual_pressure(); </li></ul><ul><li>Control_ECU_Control_airflow(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  24. 24. Embedded Software Development
  25. 25. Embedded C Guidelines <ul><li>Comments </li></ul><ul><ul><li>Overall purpose of the software </li></ul></ul><ul><ul><li>The names of the programmers </li></ul></ul><ul><ul><li>Update dates </li></ul></ul><ul><ul><li>Hardware/software configuration required to use the module </li></ul></ul><ul><ul><li>Copyright information (if available) </li></ul></ul>
  26. 26. Embedded C guidelines <ul><li>Included .h files </li></ul><ul><li>Extern variables </li></ul><ul><li>Preprocessor directives </li></ul><ul><li>Struct, union, enum statements </li></ul><ul><li>Global variables and constants </li></ul><ul><li>Function prototypes </li></ul><ul><li>Implementation of functions </li></ul>
  27. 27. Questions?