Académique Documents
Professionnel Documents
Culture Documents
Advantages
It is a mid-level, with high-level features (such as support for functions and modules), and low-level features (such as good access to hardware via pointers)
C is the most common Embedded language 85%, of embedded applications are coded in C.
C , when used correctly is as safe and robust as any other high level language. It directly manipulates the hardware and memory addresses. It is very efficient, It is popular and well understood Good, well proven compilers are available for every embedded processor(8-bit to 32-bit or more)
Data Types
Data types Bit Signed char Unsigned char enum Signed short Unsigned short Signed int Unsigned int Signed long Unsigned long Float sbit sfr Bits 1 8 8 8\16 16 16 16 16 32 32 32 1 8 Bytes 1 1 1\2 2 2 2 2 4 4 4 1 Value range 0 to 1 -128 to +127 0 to 255 -128 to +127 or -32768 to +32767 -32768 to +32767 0 to 65535 -32768 to +32767 0 to 65535 -2147483648 to 2147483647 0 to 4294967295 1.175494E-38 to 3.402823E+38 0 to 1 0 to 255
Memory models
The memory model determines which default memory type to use for automatic variables, and declarations with no explicit memory type specifier.
If the memory type specifier is omitted in a variable declaration, the default or implicit memory type is automatically selected. Automatic variables which cannot be located in registers are also stored in the default memory area. The default memory type is determined by the SMALL, COMPACT and LARGE compiler control directives.
- It is the same as if they were declared explicitly using the data memory type specifier.
- Variable access is very efficient. Stack size is critical because the real stack size depends upon the nesting depth of the various functions. - Using this memory model, the number of global variables must be kept to a minimum to allow the linker's OVERLAY function to work to best effect. However the amount of space required for the stack must be kept in mind. - This approach is generally best for large, time-critical applications, as the SMALL global model guarantees that local variables and function parameters will have the fastest access, while large arrays can be located off-chip.
- It is as if they were explicitly declared using the pdata memory type specifier.
- This memory model can accommodate a maximum of 256 bytes of variables. The limitation is due to the addressing scheme used, which is indirect through registers R0 and R1 (@R0, @R1. - The compact model is rarely used for an entire program, but more usual in combination with the SMALL switch reserved for interrupt routines. - COMPACT is especially useful for programs with a large number of medium speed 8 bit variables. - It can be useful in applications where stack usage is very high, meaning that data needs to be off-chip.
Function Declaration
Cx51 provides you with a number of extensions for standard C function declarations. These extensions allow you to: - Specify a function as an interrupt procedure - Choose the register bank used - Select the memory model return_type funcname (args) [{small | compact | large}] [interrupt n] [using n] where: return_type is the type of the value returned from the function. If no type is specified, int is assumed.
funcname is the name of the function. args is the argument list for the function. small, compact, or large is the explicit memory model for the function. interrupt indicates that the function is an interrupt function. using specifies which register bank the function uses.
Interrupt Example
static int counter; void Ex0Isr(void) interrupt 0 using 1 { counter++; } void main(void) { EX0= 1; //enable external interrupt 0 EA= 1; //enable global interrupts while(1){} }