Embedded "C"? Basic Concepts

Embedded "C" Basic Concepts

Basic Data Types

A data type is a classification of data which tells the compiler or interpreter how the programmer intends to use the data. Most programming languages support various types of data, including integer, real, character or string, and Boolean.

No alt text provided for this image

Eclipse "SDK" Basic Data Types

xbasic_types.h

No alt text provided for this image

xil_types.h

No alt text provided for this image

Local vs Global Variables

Variables in C can be classified by their scope

No alt text provided for this image

Local Variables

  • The ‘static’ access modifier causes that the local variable to be permanently ?allocated storage in memory, like a global variable, so the value is preserved ?between function calls (but still is local)
  • Local variables only occupy RAM while the function to which they belong is ?running
  • Usually the stack pointer addressing mode is used (This addressing mode ?requires one extra byte and one extra cycle to access a variable compared to ?the same instruction in indexed addressing mode)
  • If the code requires several consecutive accesses to local variables, the compiler will usually ?transfer the stack pointer to the 16-bit index register and use indexed addressing instead

Global Variables

  • Global variables are allocated permanent storage in memory at an absolute
  • address determined when the code is linked
  • The memory occupied by a global variable cannot be reused by any other ?variable
  • Global variables are not protected in any way, so any part of the program?can?access a global variable at any time
  • This means that the variable data could be corrupted if part of the variable is derived from one value and the rest of the variable is derived from another value
  • The 'static' access modifier may also be used with global variables
  • This gives some degree of protection to the variable as it restricts access to the ?variable to those functions in the file in which the variable is declared
  • The compiler will generally use the extended addressing mode to access?global?variables or indexed addressing mode if they are accessed though a pointer

Other Application for the ‘static’ modifier

By default, all functions and variables declared in global space have external ?linkage and are visible to the entire program. Sometimes you require global ?variables or functions that have internal linkage: they should be visible ?within a single compilation unit, but not outside. Use the static keyword to ?restrict the scope of variables.

No alt text provided for this image

Volatile Variable

The value of volatile variables may change from outside the program.

For example, you may wish to read an A/D converter or a port whose value is ?changing.

Often your compiler may eliminate code to read the port as part of the compiler's ?code optimization process if it does not realize that some outside process is ?changing the port's value.

You can avoid this by declaring the variable volatile.

No alt text provided for this image
No alt text provided for this image



要查看或添加评论,请登录

Ali Shan??的更多文章

  • Difference Between C & Embedded C

    Difference Between C & Embedded C

    Embedded systems programming is different from developing applications on a desktop computers. Key characteristics of…

  • ZYNQ program is solidified to FLASH and SD card

    ZYNQ program is solidified to FLASH and SD card

    The SDK project directory is as follows after launch the Vivado project in SDK First step: Board Support Package…

社区洞察

其他会员也浏览了