• No results found

PROTOTYPING FUNCTIONS

In document c What Happens ebook (Page 155-160)

Using a function prototype allows a function to be defined before the actual code is used. If a prototype is used, the function is available for use with different arguments in other parts of the program as the need arises - ala built-in functions.

In a prototype, the variables to be passed are defined.

Cs24b

//"subroutine" receives values of datal // and data2

//declare answer variable in sub //multiplies datal times data2

//declare answer variable in main //declare datal variable

//declare data2 variable

//values of datal and data2 are passed // answer is returned

//display result via port B LEDs //circle, always

147

To crcate a prototype, put an exact duplicate of the function's first line somewhere before main( ) .

function prototype; //ahead of main

main() {

function name (argument list); //calls function }

function header " //same as prototype except no

{

function definition //function itself

}

When the function is called, the correct data types must appear in the proper positions in the function argument list, ie. proper order as defined in the prototype.

////returning value demo - prototype Cs24c

/include <16F818.h>

/fuses INTRCIO

sub(int datal, int data2);

main()

subAnswer = datal * data2;

return (subAnswer); w'ould not know wrhat the function is because it has not been prototyped.

Prototypes are not needed in small programs, but they are useful in large programs that include lots of linked libraries and include files. Typically, all the important user functions are proto­

//function prototype

//declare answer variable in main //declare datal variable

//declare data2 variable

//values of datal and data2 are passed // answer is returned

//display result via port B LEDs //circle, always

//function header - "subroutine" receives // values of datal and data2

//declare answer variable in sub //multiplies datal times data2

OPERATORS

An operator is a symbol that instructs the C compiler to perform a specific operation on one or more operands.-An operand is an expression that the operand acts on. All the operators are listed here as a reference.

Assignment operator equal sign

variable = expression;

Assign the value of expression to variable

= not used in math, only as assignment operator Relational operators

== Equal to

>

<

>=

<=

!= Not equal to

Comparisons using relational operators Logical operators

&& And I I O r

! Not

Increment and decrement ++ Increment

— Decrement

Used with counters

149

Mathematical operators

+ Addition

- Subtraction

* Multiplication

/ Division

% Modulus - gives division remainder

+ = Addition assignment

- = Subtraction assignment

* — Multiplication assignment

/ = Division assignment

«= Left shift assignment

»= Right shift assignment

& Bitwise AND

Bitwise exclusive OR Bitwise inclusive OR

&= Bitwise AND assignment

A= Bitwise exclusive OR assignment

|= Bitwise inclusive OR assignment Pointer operators

& Address-of operator Address unary

* Deferencing operator Indirection unary Structure operators

Dot operator or structure member operator -> Structure pointer

Operators that don’t fit in the categories ( ) Grouping

, Sequential evaluation sizeof Size in bytes unary

?: Conditional expression Not an operator

: Used in structure bit fields to indicate the number of bits allocated to a field

INTERRUPTS

When an event occurs which demands the microcontroller's attention, an interrupt may be gener­

ated which will cause the microcontroller to drop what it is doing, take care of the task that needs to be performed, and go back to what it was doing.

When an interrupt occurs, the instruction currently being executed is completed. Then the PIC 16F818 jumps to address 0x004 in program memory and executes the instruction stored there. This program is called an interrupt service routine. An interrupt service routine may cause (as required) the microcontroller to first take notes on the status of the program it was exe­

cuting when the interrupt occurred so that it can find its place when it comes back. Then the interrupt service routine will handle the interrupt by doing whatever needs to be done. On com­

pletion, the routine will review its notes, set everything back to the way it was and take up the main program where it left off. The code needed to both preserve and restore the status of the program that was being executed when the interrupt occurred is created automatically by the compiler. This is called context saving.

Interrupts are caused by events which must trigger a response from the microcontroller at the time they occur. For the PIC16F818, interrupts may come from one of several sources:

• External - outside the microcontroller via the RBO/INT pin.

• Timer 0 overflow from OxFF to 0x00.

• Port B logic level change on bits 7,6,5,4.

• A/D conversion complete.

• Data EEPROM write complete.

• And others.

An interrupt flag is a bit in one of the Special Function Registers which, when set, indicates that a specific interrupt has occurred. The flag will remain set until it is eleared by software which is usually done in the interrupt service routine related to that specific interrupt.

Interrupts may be enabled or disabled (masked) at two levels, global (all interrupts regardless of source) or specific (enable/disable specific interrupt sources).

151

Using external interrupts requires both hardware and software. Hardware must be provided to sense an event or condition which requires an interrupt to bring about some action. A signal must be generated and fed to the microcontroller INT pin.

RBO/INT is a general puipose interrupt pin. The INT input is connected directly (internally) to a Schmitt trigger whose output is directed to the external interrupt tlag, so the TRJS register is not involved.

The external interrupt is edge-triggered. The INTEDG bit in one of the control registers deter­

mines whether a rising or falling edge triggers an interrupt.

The E X T I N T E D G E Q built-in function is used to make this selection.

ext_int_edge(edge)

edge H_TO_L high to low transition at INT pin edge L_TO_H low to high transition at INT pin

An INT interrupt can be disabled using the INT enable (INTE) flag so that interrupts will be ignored until the INTE flag is set. INT interrupts can be serviced or ignored under software control.

If an INT interrupt occurs, the INTF flag is set. The INTF flag must be cleared via software as part of the interrupt service routine before reenabling the interrupt or continuous interrupts will occur. The compiler generates code to take care of this automatically.

• Disable further interrupts (clear INTE flag).

• Service the interrupt.

• Clear INTF interrupt flag.

• Enable interrupts (set INTE flag).

INTERNAL INTERRUPT SOURCES

In document c What Happens ebook (Page 155-160)