Keil C51 Cross Compiler: Embedded Systems 2 2 - 1 C Programming
Keil C51 Cross Compiler: Embedded Systems 2 2 - 1 C Programming
Keil C51 Cross Compiler: Embedded Systems 2 2 - 1 C Programming
ANSI C Compiler Generates fast compact code for the 8051 and its derivatives Advantages of C over Assembler Do not need to know the microcontroller instruction set Register allocation and addressing modes are handled by the compiler Programming time is reduced Code may be ported easily to other microcontrollers (not 100% portable) C51 supports a number of C language extensions that have been added to support the 8051 microcontroller architecture e.g. Data types Memory types Pointers Interrupts
2-1 C Programming
Embedded Systems 2
C vs Assembly Language
Code efficiency can be defined by 3 factors: 1. How long does it take to write the code 2. What size is the code? (how many Bytes of code memory required?) 3. How fast does the code run? C is much quicker to write and easier to understand and debug than assembler. Assembler will normally produce faster and more compact code than C Dependant on C compiler A good knowledge of the micro architecture and addressing modes will allow a programmer to produce very efficient C code
Embedded Systems 2
2-2
C Programming
C51 Keywords
Embedded Systems 2
2-3
C Programming
= extension to ANSI C
Embedded Systems 2 2-4 C Programming
Embedded Systems 2
2-5
C Programming
Select Model
Embedded Systems 2
2-6
C Programming
Embedded Systems 2
2-7
C Programming
Embedded Systems 2
2-8
C Programming
Embedded Systems 2
2-9
C Programming
Embedded Systems 2
2 - 10
C Programming
Embedded Systems 2
2 - 11
C Programming
Program Branching
Most embedded programs need to make some decisions e.g. perform a certain task only if a switch input is high An if-else statement is the most common method of coding a decision box of a flowchart. if (P1^0 == 1) { //task A } else { //task B } Note that the brackets are only required if the if or else statement contains more then 1 line of code
Embedded Systems 2 2 - 12
P1.0 = 1?
Task A
Task B
C Programming
Wait Loops
In some applications the program must wait until a certain condition is true before progressing to the next program task (wait loop) e.g. in a washing machine wait for the water to heat before starting the wash cycle A while statement is often used here //task A while(P1^0 == 0); //will stay here while P1.0 is low //can also write as while(P1^0 == 0) {} //task B
Task A
P1.0 = 1? Y Task B
Embedded Systems 2
2 - 13
C Programming
Continuous Loops
An embedded program never ends It must contain a main loop than loops continuously e.g. a washing machine program continually tests the switch inputs A continuous loop is programmed using a loop with no condition while(1){ } while(1); for(;;){ } for(;;);
Embedded Systems 2
2 - 14
C Programming
C Bitwise Operators
NOT ~ char data x = 0x05; x = ~x; //x=0xFA AND & char data x = 0x45; x &= 0x0F; //x = 0x05 Useful for clearing specific bits within a variable (masking) OR | char data x = 0x40; x |= 0x01; //x = 0x41 Useful for setting individual bits within a variable EXCLUSIVE OR ^ char data x = 0x45; x ^= 0x0F; //x = 0x4A Useful for inverting individual bits within a variable
2 - 15 C Programming
Embedded Systems 2
Question
What is the value of variable x after the following code executes? unsigned char data x; x = 21; x &= 0xF0; x |= 0x03; Write C code to set the upper 4 bits of a char to 1100.
Embedded Systems 2
2 - 16
C Programming
C Logical Operators
Bitwise operators will change certain bits within a variable Logical operators produce a true or false answer They are used for looping and branching conditions C Logical Operators AND OR Equals Not Equal Less Less or Equal Greater Greater or Equal
Embedded Systems 2
Reading from a port unsigned char data status; status = P1; //read from Port 1 Before reading from a port pin, always write a 1 to the pin first.
Embedded Systems 2
2 - 19
C Programming
Embedded Systems 2
2 - 20
C Programming
Embedded Systems 2
2 - 21
C Programming
Embedded Systems 2
Generating Delays
Software Delays Uses looping mechanisms in C or assembly Does not use any microcontroller hardware resources Ties up the CPU while the delay is running Delay produced depends on the compiler Hardware Delays Uses a microcontroller timer Uses very little CPU resources (runs in background)
Embedded Systems 2
2 - 24
C Programming
Software Delay
Use a for loop Does not use any timer resources Uses CPU resources while running i.e. no other task can be performed during delay loop Can result in large amounts of machine code being generated Results may be different for different compilers The following code results in a 204 usecond delay for the 8051 operating off the 12MHz oscillator For loops can be nested to produce longer delays
Software Delay
Using a decrementing for loop will generate a DJNZ loop. Loop execution time = 204 machine cycles [1 + 1 + (100 *2) + 2] = 204usec for a 12MHz crystal. Change starting value to 98 to get a 200usec delay
Embedded Systems 2
2 - 26
C Programming
Hardware Delay
Use timer 0 or timer 1 Very efficient mechanism of producing accurate delays Timer mode, control and counter registers must be configured Timer run bit is used to start/stop the timer The timer flag can be polled to determine when required time delay has elapsed Using timer in interrupt mode uses very little CPU resources See timer notes for more details
Embedded Systems 2
2 - 27
C Programming
C51 Functions
Can specify Register bank used Memory Model Function as an interrupt Re-entrancy [return_type] func_name([args]) [model] [re-entrant] [interrupt n] [using n] int square(int x) { return(x * x); }
Embedded Systems 2
2 - 28
C Programming
Re-entrant Functions
A re-entrant function is a function that can be called while it is still running. e.g. an interrupt occurs while the function is running and the service routine calls the same function. Keil compiler supports re-entrant functions. Beware of stack limitations
Embedded Systems 2
2 - 29
C Programming
Scope of Variables
A variable defined within a function will default to an automatic variable An automatic variable may be overlaid i.e. the linker may use the variables memory space for a variable in another function call. This will cause the variable to lose its value between function calls. If you want a variable to maintain its value between function calls, declare it as a static variable. static int x; Static variables cannot be overlaid by the linker Declare a variable as volatile if you want its value to be read each time it is used
Embedded Systems 2
2 - 30
C Programming
Embedded Systems 2
2 - 31
C Programming
Embedded Systems 2
2 - 32
C Programming
Interrupt Functions
Interrupt vector number is provided in the function declaration void timer0_int() interrupt 1 using 2 { . } Contents of A, B, DPTR and PSW are saved on stack when required All working registers used in the ISR are stored on the stack if the using attribute is not used to specify a register bank All registers are restored before exiting the ISR
Embedded Systems 2
2 - 33
C Programming
Embedded Systems 2
2 - 34
C Programming
C51 Pointers
Generic pointers Same as ANSI C pointers char *ptr; Stored using 3 bytes Can be used to access any memory space Code generated executes more slowly than memory specific pointers Memory Specific Pointers Specify the memory area pointed to char data *ptr1; //pointer to char in data memory Stored as 1 (data, idata) or 2 bytes (code or xdata) Can also specify where pointer is stored char data * xdata ptr2; //pointer to data stored in xdata
Embedded Systems 2
2 - 35
C Programming