More PIC Applications:PIC C Programming

PIC C Programming

For those readers unfamiliar with C programming, a simple example is shown in Program 13.3. The program will give the same output as BIN1.ASM assembly language program. The program must be converted to PIC 16-bit machine code using the MPLAB C18 Compiler,

PIC Microcontrollers-1334

PIC Microcontrollers-1335

which is supplied as an add-on to the development system. This compiler recognizes ANSI (American National Standards Institute) C, the standard syntax for microcontrollers. The C compiler must be selected in the development mode dialogue when building the application.

The main elements of the program are as follows:

/* comment */

Comments in C source code are enclosed between /* and */ and can be run over several lines. A semicolon is used in assembler.

#include<p18f4580.h>

The ‘include’ is a compiler directive that calls up a header file named ‘p18f4580.h’, which has the same function as the include file in assembler, in that it contains predefined register labels for that particular processor and the corresponding addresses.

int counter

This assigns a label to a register and declares that it will store an integer, or whole number. A standard integer in C is stored as a 16-bit number, requiring two data random access memory (RAM) general purpose register (GPR) locations. EQU provides the equivalent operation in assembler.

void main(void)

This rather peculiar syntax simply indicates, as far as we are concerned here, the start of the main program sequence. The following brace (curly bracket) starts the main program block, with a corresponding brace at the end. These are lined up in the same column and the main program tabbed in between them, so that they can be matched up correctly.

counter ¼ 0;

A value of 0 is initially placed in the variable location (low byte). The equivalent in assembler is MOVLW, followed by MOVWF.

TRISB ¼ 0;

A value 0 is loaded into the data direction register of port B to initialize the port bits for output to the LEDs.

while(1)

This starts a loop, which will run endlessly. A condition is placed in the brackets that control the loop. For example, the statement could read ‘while(count<256)’, in which case the following group of statements within the curly brackets (braces) would execute 255 times, counting up to the maximum binary value and stopping. The value 1 means the condition is ‘always true’, so the loop is endless, until reset. This translates into GOTO in assembler, with DECFSZ providing the conditional test.

PORTB ¼ counter;

The value in counter is copied to port B data register for display on the LEDs (assembler equivalent: MOVxx).

counterþþ;

The variable value is incremented each time the loop is executed. This causes the output to be incremented the next time (assembler equivalent: INCF).

Delay10KTCY(100);

This calls a predefined block of code, which provides a delay, so that the LED output changes are visible. At a maximum clock rate, the processor instruction cycle time is 0.1 ms, so the delay works out to 0.1 s (10 000 x 100 cycles). The overall count cycle will then take 25.6 s. The delay function is an example of a function call, corresponding to a subroutine in assembler. We know that this is implemented in assembler as a software delay loop or hardware timer operation.

The layout of the program, with tabs, is important for understanding the program and checking the syntax if there are logical errors. However, the layout does not affect the program function,

only the sequence of characters. Nevertheless, the statements must be all on one line; line returns are not allowed within a statement.

Each complete statement is terminated with a semicolon. Note that some are not complete in themselves, and do not have a semicolon. For example, ‘while(1)’ is not complete without the loop statements, or at least the pair of braces. The close brace terminates the ‘while’

statement. The whole of the main loop, and any functional subblock, must be enclosed between braces.

Leave a comment

Your email address will not be published. Required fields are marked *