Saturday, December 10, 2011

keil C Programming Tutorial: Functions

►Functions in Keil C

Keil C compiler provides number of extensions for standarad C function declerations. These extensions allows you to:
  • Specify a function as an interrupt procedure
  • Choose the register bank used
  • Select memory model


►Function Declaration:

[Return_type] Fucntion_name ( [Arguments] ) [Memory_model] [reentrant] [interrupt n] [using n]

Return_type: The type of value returned from the function. If return type of a function is not specified, int is assumed by default.

Function_name: Name of function.

Arguments: Arguments passed to function.


Options:
These are options that you can specify along with function declaration.
Memory_model: explicit memory model (Large, Compact, Small) for the function. Example:
CODE:
int add_number (int a, int b) Large
 


reentrant: To indicate if the function is reentrant or recursive. This option is explained later in the tutorial.

interrupt: Indicates that function is an interrupt service routine. This option is explained later in the tutorial.

using: Specify register bank to be used during function execution. We have three register banks in 8051 architecture. These register banks are specified using number 0 for Bank 0 to 3 for Bank 3 as shown in example
CODE:
void function_name () using 2{ //function uses Bank 2
        //function code
}
 



►Interrupt Service Routines:
A function can be specified as an interrupt service routine using the keyword interrupt and interrupt number. The interrupt number indicates the interrupt for which the function is declared as service routine.

Following table describes the default interrupts:

8051 Interrupt vector
As 8051 vendors create new parts, more interrupts are added. Keil C51 compiler supports interrupt functions for 32 interrupts (0-31). Use the interrupt vector address in the following table to determine the interrupt number.

Interrupt vector
The interrupt function can be declared as follows:
CODE:
void isr_name (void) interrupt 2 {
// Interrupt routine code

}

 


Please make sure that interrupt service routines should not have any arguments or return type except void.


►Reentrant Functions:
In ANSI C we have recursive function, to meet the same requirement in embedded C, we have reentrant function. These functions can be called recursively and can be called simultaneously by two or more processes.

Now you might be thinking, why special definition for recursive functions?
Well you must know how these functions work when they are called recursively. when a function is running there is some runtime data associated with it, like local variables associated with it etc. when the same function called recursively or two process calls same function, CPU has to maintain the state of function along with its local variables.

Reentrant functions can be defined as follows:
CODE:
void function_name (int argument) reentrant {
        //function code
}
 


Each reentrant function has reentrant stack associated with it, which is defined by startup.A51 file. Reentrant stack area is simulated internal or external memory depending upon the memory model used:
  • Small model reentrant functions simulate reentrant stack in idata memory.
  • Compant model reentrant functions simulate reentrant stack in pdata memory.
  • Large model reentrant functions simulate reentrant stack in xdata memory.



►Real-time Function Tasks:

Keil or C51 provides support for real-time operating system (RTOS) RTX51 Full and RTX51 Tiny. Real-time function task are declared using _task_ and _priority_ keywords. The _task_ defines a function as real-time task. The _priority_ keyword specify the priority of task.

Fucntions are declared as follows:
CODE:
void func (void) _task_ Number _priority_ Priority {
        //code
}
 

where:

Number: is task ID from 0 to 255 for RTX51 Full and 0 to 15 for RTX51 Tiny.

Priority: is priority of task.

Real-time task functions must be declared with void return type and void argument list (say no arguments passed to task function).

No comments:

Post a Comment