Part 2 C Programming Essentials
Part 2 C Programming Essentials
8-bit PIC
Microcontrollers
in C
Martin Bates
Elsevier 2008
This presentation contains illustrations from the book
‘Programming 8-bit PIC Microcontrollers in C’ by Martin Bates
C
PROGRAMMING
ESSENTIALS
Listing 2.1 A program to output a binary code
f Figure 2.1 MPLAB IDE Screenshot
Figure 2.2 ISIS dialogue to attach program
Figure 2.3 OUTBYTE.DSN test circuit with output LEDs
Listing 2.2 Variables
Compiler Directives
#include source files Include another source code or header file
#use functions(parameters) Include library functions
C Blocks
main(condition) {statements } Main program block
while(condition) {statements } Conditional loop
if(condition) {statements } Conditional sequence
for(condition) {statements } Preset loop
C Functions
delay_ms(nnn) Delay in milliseconds
delay_us(nnn) Delay in microseconds
output_x(n) Output 8-bit code at Port X
output_high(PIN_nn) Set output bit high
output_low(PIN_nn) Set output bit low
input(PIN_nn) Get input
Table 2.1 Integer Variables
int1 1 bit 0 1
Conditio
n True? Statement
Block
Statement
Block Conditio
n True?
label
Statement
Block
Continue
Goto
Break
Listing 2.10 Continue, Break & Goto
// CONTINUE.C
// Continue, break and goto jumps
#include "16F877A.H"
#use delay(clock=4000000)
main()
{
int outbyte;
again: outbyte=0; // Goto destination
while(1)
{
output_C(outbyte); // Loop operation
delay_ms(10);
outbyte++;
YES NO
Condition Conditio
True? n
NO True?
YES
If If Else
block block block
Figure 2.10 Switch..case branching structure
Test Variable
NO
YES Procedure 2
Value = 2?
NO
YES Procedure 3
Value = 3?
NO
YES Procedure n
Value = n?
NO
Default
Procedure
Listing 2.11 Comparison of Switch and If..Else control
// SWITCH.C
// Switch and if..else sequence control
// Same result from both sequences
#include "16F877A.h"
void main()
{
int8 inbits;
while(1)
{
inbits = input_D(); // Read input byte
// Switch..case option................................................
void fun1()
{
statements
...
...
Main() }
{
statements
fun1()
statements
statements
....
.... void fun2(arg) void fun3
.... { {
.... statements statements
statements ... ...
fun2(arg) fun3 ...
statements ... }
} return(val)
}
Listing 2.12 Basic function call
// FUNC1.C
// Function call structure
#include "16F877A.H"
int8 outbyte=1;
int16 n;
void out() // Start of function block
{
while (outbyte!=0) // Start loop, quit when output =0
{
output_C(outbyte); // Output code 1 – 0xFF
outbyte++; // Increment output
for(n=1;n<500;n++); // Delay so output is visible
}
}
main()
{
out(); // Function call
while(1); // Wait until reset
}
Listing 2.13 Passing a parameter to the function
// FUNC2.C
#include "16F877A.H"
int8 outbyte=1; // Declare global variables
int16 n,count;
void out() // Function block
{
while (outbyte!=0)
{ output_C(outbyte);
outbyte++;
for(n=1;n<count;n++);
}
}
main()
{
count=2000;
out(); // Call function
while(1);
}
Listing 2.14 Local variables
// FUNC3.C
// Use of local variables
#include "16F877A.H"
int8 outbyte=1; // Declare global variables
int16 count;
int out(int16 t) // Declare argument types
{
int16 n; // Declare local variable
while (input(PIN_D0)) // Run output at speed t
{ outbyte++;
for(n=1;n<t;n++);
}
return outbyte; // Return output when loop stops
}
main()
{
count=50000;
out(count); // Pass count value to function
output_C(outbyte); // Display returned value
while(1);
}