0% found this document useful (0 votes)
89 views

Part 2 C Programming Essentials

This document provides a summary of chapters from the book "Programming 8-bit PIC Microcontrollers in C" by Martin Bates. It discusses programming 8-bit PIC microcontrollers using the C programming language. Part 1 describes the architecture of the PIC16F887A microcontroller. Part 2 covers basic C programming and interfacing with I/O devices using simple example programs. Part 3 provides examples of using timers, interrupts and other peripherals.

Uploaded by

Sashikanth Betha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

Part 2 C Programming Essentials

This document provides a summary of chapters from the book "Programming 8-bit PIC Microcontrollers in C" by Martin Bates. It discusses programming 8-bit PIC microcontrollers using the C programming language. Part 1 describes the architecture of the PIC16F887A microcontroller. Part 2 covers basic C programming and interfacing with I/O devices using simple example programs. Part 3 provides examples of using timers, interrupts and other peripherals.

Uploaded by

Sashikanth Betha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 33

Programming

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

Part 1 Microcontroller Systems


describes in detail the internal architecture and interfaces
available in the PIC 16F887A, a typical PIC chip, as well as
outlining the main features of the development system

Part 2 C Programming Essentials


provides simple example programs for the microcontroller
which show the basic principles of C programming,
and interfacing to basic I/O devices

Part 3 C Peripheral Interfaces


provides example programs for operating PIC chips with a
full range of peripherals, using timers and interrupts
Part 2

C
PROGRAMMING
ESSENTIALS
Listing 2.1 A program to output a binary code

/* Source code file: OUTNUM.C


Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs an 8-bit code
Simulation circuit: OUTBYTE.DSN
 *******************************************************/
 
#include "16F877A.h" // MCU select
 
void main() // Main block
{
output_D(255); // Switch on outputs
}
 

 
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

/* Source code file: VARI.C


Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs an 8-bit variable
Simulation circuit: OUTBYTE.DSN 
*******************************************************/
 
#include "16F877A.h"
 
void main()
{
int x; // Declare variable and type

x=99; // Assign variable value


output_D(x); // Display the value in binary
}
   
Listing 2.3 Endless loop

/* Source code file: ENDLESS.C


Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs variable count
Simulation circuit: OUTBYTE.DSN 
*******************************************************/
 
#include "16F877A.h"
 
void main()
{
int x; // Declare variable
 
while(1) // Loop endlessly
{ output_D(x); // Display value
x++; // Increment value
  }
}
Figure 2.4 INBIT.DSN test circuit with input switch
Listing 2.4 IF statement

/* Source code file: IFIN.C


Author, date, version: MPB 11-7-07 V1.0
Program function: Tests an input
Simulation circuit: INBIT.DSN
*******************************************************
/
 
#include "16F877A.h"
 
void main()
{
int x; // Declare test var.
output_D(0); // Clear all outputs
 
while(1) // Loop always
{
x = input(PIN_C0); // Get input
 
if(x==1)output_high(PIN_D0); // Change out
}
}
Listing 2.5 Conditional loop
/* Source code file: WHILOOP.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Input controls output loop
Simulation circuit: INBIT.DSN
*******************************************************/
 
#include "16F877A.h"
#use delay (clock=1000000) // MCU clock = 1MHz
 
void main()
{
while(1)
{
while(input(PIN_C0)); // Repeat while switch open
{ output_high(PIN_D0);
delay_ms(300); // Delay 0.3s
output_low(PIN_D0);
delay_ms(500); // Delay 0.5s
}
output_low(PIN_D0); // Switch off LED
}
 
}
 Listing 2.7 Siren Program
/* Source code file: SIREN.C
Author, date, version: MPB 11-7-07 V1.0
Program function: Outputs a siren sound
Simulation circuit: INBIT.DSN
 *******************************************************/
 
#include "16F877A.h"
#use delay (clock=1000000)
 
void main()
{
int step;
 
while(1)
{
while(!input(PIN_C0)) // loop while switch ON
{
for(step=0;step<255;step++) // Loop control
{
output_high(PIN_D0); // Sound sequence
delay_us(step);
output_low(PIN_D0);
delay_us(step);
}
}
}
  }
Listing 2.8 Program Blank

/* Source Code Filename:


Author/Date/Version:
Program Description:
Hardware/simulation:
***************************************************************/
 
#include "16F877A.h" // Specify PIC MCU
#use // Include library routines
 
 void main() // Start main block
{
int // Declare global variables
 
while(1) // Start control loop
{
// Program statements
}
} // End main block
 
Table 2.1 A basic set of CCS C components

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

Name Type Min Max

int1 1 bit 0 1

unsigned int8 8 bits 0 255

signed int8 8 bits -127 +127

unsigned int16 16 bits 0 65525

signed int16 16 bits -32767 +32767

unsigned int32 32 bits 0 4294967295

signed int32 32 bits -2147483647 +2147483647


Table 2.2 Microchip/CCS Floating Point Number Format

Exponent Sign Mantissa


xxxx xxxx x xxx xxxx xxxx xxxx xxxx xxxx
8 bits 1 23 bits

Table 2.4 Example of 32-bit floating point number conversion

  FP number: 1000 0011 1101 0010 0000 0000 0000 0000

Mantissa: 101 0010 0000 0000 0000 0000


 
   Exponent: 1000 0011
 
Sign: 1 = negative number
 
Figure 2.5 Variable Types
Table 2.5 ASCII Codes

Low High Bits


Bits 0010 0011 0100 0101 0110 0111
0000 Spac 0 @ P ` p
e
0001 ! 1 A Q a q
0010 " 2 B R b r
0011 # 3 C S c s
0100 $ 4 D T d t
0101 % 5 E U e u
0110 & 6 F V f v
0111 ' 7 G W g w
1000 ( 8 H X h x
1001 ) 9 I Y i y
1010 * : J Z j z
1011 + ; K [ k {
1100 , < L \ l |
1101 - = M ] m }
1110 . > N ^ n ~
1111 / ? O _ o Del
 
Table 2.6 Arithmetic and Logical Operations
OPERATION OPERATOR DESCRIPTION SOURCE CODE EXAMPLE RESULT
 
 
Single operand
Increment ++ Add one result = num1++; 0000 0000 0000
to integer   0001
Decrement -- Subtract one result = num1--; 1111 1111 1111
from integer 1110
Complement ~ Invert all bits result = ~num1; 0000 0000 1111
of integer 1111
 
Arithmetic Operation
Add + Integer or result = 0000 1010 0000
Float num1 + num2; + 0000 0011 1101

Subtract - Integer or result = 0000 1010 0000


Float num1 - num2; - 0000 0011 0111
Multiply * Integer or result = 0000 1010 0001
Float num1 * num2; * 0000 0011 1110
Divide / Integer or result = 0000 1100 0000
Float num1 / num2; / 0000 0011 0100
 
Logical Operation
Logical AND & Integer result = 1001 0011 0001
Bitwise num1 & num2; & 0111 0001 0001
Logical OR | Integer result = 1001 0011 1111
Bitwise num1 | num2; | 0111 0001 0011
Exclusive OR ^ Integer result = 1001 0011 1110
Bitwise num1 ^ num2; ^ 0111 0001 0010
Figure 2.6 Variable Operations
Table 2.7 Conditional Operators

Operation Symbol EXAMPLE

Equal to == if(a == 0) b=b+5;

Not equal to != if(a != 1) b=b+4;

Greater than > if(a > 2) b=b+3;

Less than < if(a < 3) b=b+2;

Greater than or equal to >= if(a >= 4) b=b+1;

Less than or equal to <= if(a <= 5) b=b+0;


Figure 2.3.1 Comparison of While and Do..While Loop

Conditio
n True? Statement
Block

Statement
Block Conditio
n True?

(a) While loop (b) Do..While loop


Listing 2.9 DOWHILE.C contains both types of ‘while’ loop
// DOWHILE.C
// Comparison of WHILE and DO WHILE loops
 
#include "16F877A.H"
 
main()
{
int outbyte1=0;
int outbyte2=0;
int count;
 
count=0; // This loop is not
while (count!=0) // executed
{ output_C(outbyte1);
outbyte1++;
count--;
}
 
count=0; // This loop is
do // executed
{ output_C(outbyte2);
outbyte2++;
  count--;
} while (count!=0);
 
while(1){};
}
Figure 2.8 Break, continue and goto

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++;

if (!input(PIN_D0)) continue; // Restart loop


if (!input(PIN_D1)) break; // Terminate loop
delay_ms(100);
if (outbyte==100) goto again; // Unconditional jump
}
}
Figure 2.9 Comparison of If and If..Else

YES NO
Condition Conditio
True? n
NO True?
YES

If If Else
block block block
Figure 2.10 Switch..case branching structure

Test Variable

Value = 1? YES Procedure 1

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................................................

switch(inbits) // Test input byte


{
case 1: output_C(1); // Input = 0x01, output = 0x01
break; // Quit block
case 2: output_C(3); // Input = 0x02, output = 0x03
break; // Quit block
case 3: output_C(7); // Input = 0x03, output = 0x07
break; // Quit block
default:output_C(0); // If none of these, output = 0x00
}
 
// If..else option....................................................
 
if (input(PIN_D0)) output_C(1); // Input RD0 high
if (input(PIN_D1)) output_C(2); // Input RD1 high
if (input(PIN_D0) && input(PIN_D1)) output_C(7); // Both high
else output_C(0); // If none of these, output = 0x00
 
}
}
Figure 2.11 Hierarchical C program structure

LEVEL 0 LEVEL 1 LEVEL 2

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);
}

You might also like