Your SlideShare is downloading. ×
Embedded c programming22 for fdp
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Embedded c programming22 for fdp

2,859
views

Published on

Published in: Education

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,859
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
112
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

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