Vous êtes sur la page 1sur 13

Embedded C

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)

Cx51 Cross compiler supports all of the ANSI Standard C directives.

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

8051 Memory Areas


The 8051 architecture supports a number of physically separate memory areas for program and data. Each memory area offers certain advantages and disadvantages. Program Memory Internal Data memory External Data memory code bdata, data, idata xdata, pdata

Explicitly declared Memory types


code: Program memory (64 Kbytes); accessed by opcode MOVC A, @A+DPTR. data: Directly addressable internal data memory; fastest access to full internal address space (256 bytes). idata: Indirectly addressable internal data memory; accessed across the internal address space (128 bytes). bdata: Bit-addressable internal data memory; allows mixed bit and byte access (16 bytes). xdata: External data memory (64 Kbytes); accessed by opcode MOVX @DPTR. pdata: Paged (256 bytes) external data memory; accessed by opcode MOVX @Rn.

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.

Small Model: Internal RAM 128 bytes


- All variables, by default, reside in the internal data memory of the 8051 system.

- 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.

Compact Model: RAM 256 bytes off-chip


- All variables, by default, reside in one page of external data memory.

- 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.

Large Model: Total RAM up to 64KB


- In this model, all variables, by default, reside in external data memory (up to 64 Kbytes). - It is the same as if they were explicitly declared using the xdata memory type specifier. - Memory access through this data pointer is inefficient, especially on variables with a length of two or more bytes. - This type of data access mechanism generates more code than the small or compact models. - Permits slow access to a very large memory space and is perhaps the easiest model to use. - Not often used for an entire program, but in combination with SMALL. Register variables are still used and so efficiency remains reasonable.

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){} }

Interrupt Service Routine


static int counter; void Ex0Isr(void) interrupt 0 using 1 { counter++; }
Declare counter global so both ISR and Main can see it. Interrupt 0 makes this Ex0s ISR Ex0 interrupts vector to location C:0003 using 1 causes code to use Register Bank 1 Context switches to bank 1

Vous aimerez peut-être aussi