8/13/2019 PIC_C introduction
1/25
ELE 574 Dr. Shadi Alboon2
8/13/2019 PIC_C introduction
2/25
What is C ?
In 1970 a team at Bell Labs led by Brian Kernighan were
developing the UNIX computer operating system
They required a high-level computer language for writingcomputer operating systems
Starting from an existing language called BCPL theydeveloped C
C was used to write the next version of UNIX system
software
UNIX eventually became the world's first portableoperating system
ELE 574 Dr. Shadi Alboon3
8/13/2019 PIC_C introduction
3/25
Why Program PICs in C?
C is a portable language, requiring minimal modificationwhen transferring programs from one processor to another
Programming in a high-level language rather thanassembler allows programs to be developed much morerapidly
Typically a program which takes a few weeks in assemblercan be written in C in a few days
Code efficiency of compiled C programs is typically 80% of
well-written assembler programs
The related language C++ is too complex for use with thepresent generation of PICs
ELE 574 Dr. Shadi Alboon4
8/13/2019 PIC_C introduction
4/25
CCS PIC Compiler
A compiler converts a high-level language program tomachine instructions for the target processor
A cross-compiler is a compiler that runs on a processor
(usually a PC) that is different from the target processor
os em e e sys ems are now programme us ng eC/C++ language
Several C compilers are available that target Microchip
PICs, for example HiTech, Microchip and CCS The PIC programming laboratory at Reading is equipped
with the CCS cross-compiler
ELE 574 Dr. Shadi Alboon5
8/13/2019 PIC_C introduction
5/25
CCS PIC Compiler
Programs are edited and compiled to PIC machine instructions on a PC
PIC machine instructions are uploaded from PC to PIC system via theICD2 debugger
Code is executed on the PIC system and can be debugged (breakpoints inspect variables single points, variables, step etc.) using PC
ELE 574 Dr. Shadi Alboon6
8/13/2019 PIC_C introduction
6/25
CCS PIC Compiler
The CCS compiler comes with an integral syntax-aware
editor
CCS C is standard C plus limited support for referenceparameters in functions
PIC-specific pre-processor directives are provided in addition
o e s an ar rec ves nc u e, e ne e c :#inline Implement the following function inline#priority set priority of interrupts
Additional functions supporting PIC hardware are provided:
output_low() set an I/O port bit low
delay_us() delay by a specified number of s
ELE 574 Dr. Shadi Alboon7
8/13/2019 PIC_C introduction
7/25
CCS PIC Compiler Data Types
PICs are optimized for processing single bitsor 8-bit words, and this is reflected the CCScompiler word sizes:
Contrary to the C standard, CCS C integersare by default unsigned
ELE 574 Dr. Shadi Alboon8
8/13/2019 PIC_C introduction
8/25
CCS PIC Compiler Data Types
In CCS C a short int is effectively a boolean variable
To make programs more readable it is a helpful to makeuse of the definitions (already in the device definitionfiles):
#define boolean short int
#define false 0
#define true 1
Now it is possible to declare boolean variables:
boolean finished = true;
The standard boolean operators (||, &&, ! etc) can be
used with these variablesELE 574 Dr. Shadi Alboon 9
8/13/2019 PIC_C introduction
9/25
Device Definition File
A CCS C program pre-processor will start with a numberof directives similar to:
#include #fuses HS,NOWDT,NOBROWNOUT,NOPROTECT,PUT
#use delay(clock=20000000)
" .
The first directive instructs the compiler to include thesystem header file 18F452.H
This is a device-specific file that contains informationabout the location of SFRs and the values to be writtento them
ELE 574 Dr. Shadi Alboon10
8/13/2019 PIC_C introduction
10/25
Fuses
CCS C provides a fuse directive:
#fuses HS,NOWDT,NOBROWNOUT,NOPROTECT,PUT
which specifies the states of the configuration fuses thatshould be programmed onto the PIC
n s examp e:
HS Clock is a high-speed crystal or resonator
NOWDT Watchdog timer is disabled
NOBROWNOUT Brown-out detector is disabled
NOPROTECT Code protect off
PUT Power-on timer is enabled
ELE 574 Dr. Shadi Alboon11
8/13/2019 PIC_C introduction
11/25
Delays
CCS C provides functions for generating delays:
delay_us()
delay_ms()
These delay functions actually delay by a number ofmachine c cles
The compiler needs to know the clock frequency in orderto calculate the required number of machine cycles
#use delay(clock=20000000)
This use-delay directive specifies that the clock
frequency of the PIC is 20 MHz
ELE 574 Dr. Shadi Alboon12
8/13/2019 PIC_C introduction
12/25
Multiple Source Code Files
CCS C does not allow separate compilation and linkingof source code files
It is convenient (and good programming practice) to put
commonly-used library functions in separate files
" ".
This directive instructs the compiler to include the userlibrary file lcd.c in the file currently being compiled
This is not particularly efficient (the library file is compiled
every time) - however typical PIC programs compile in afew seconds
ELE 574 Dr. Shadi Alboon13
8/13/2019 PIC_C introduction
13/25
CCS PICCCS PICProgramming ExamplesProgramming Examples
ELE 574 Dr. Shadi Alboon14
8/13/2019 PIC_C introduction
14/25
Program Blank
#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
ELE 57415
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
15/25
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 blockwhile(condition) {statements } Conditional loop
for(condition) {statements } Preset loop
C Functions
delay_ms(nnn) Delay in milliseconds
delay_us(nnn) Delay in microsecondsoutput_x(n) Output 8-bit code at Port Xoutput_high(PIN_nn) Set output bit highoutput_low(PIN_nn) Set output bit lowinput(PIN_nn) Get input
ELE 57416
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
16/25
Integer Variables
Name Type Min Max
int1 1 bit 0 1
unsigned int8 8 bits 0 255
-
unsigned int16 16 bits 0 65525
signed int16 16 bits -32767 +32767
unsigned int32 32 bits 0 4294967295
signed int32 32 bits -2147483647
+2147483647
ELE 57417
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
17/25
Conditional Operators
OperationSymbol
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
8/13/2019 PIC_C introduction
18/25
A program to output a binary code
#include "16F877A.h" // MCU select
void main() // Main block
{
output_D(255); // Switch on outputs}
ELE 57419
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
19/25
RA0/AN02RA1/AN1
3
RA2/AN2/VREF-4
RA4/T0CKI6
RA5/AN4/SS7
RE0/AN5/RD8
OSC1/CLKIN13
OSC2/CLKOUT14
RC1/T1OSI/CCP2 16
RC2/CCP1 17
RB7/PGD 40
RB6/PGC 39
RB5 38RB4 37
RB3/PGM 36
RB2 35
RB1 34
RB0/INT 33
RA3/AN3/VREF+5
RC0/T1OSO/T1CKI 15
MCLR/Vpp/THV1
U1
LED-BARGRAPH-
REDRE1/AN6/WR
RE2/AN7/CS
10 RC3/SCK/SCL
RD0/PSP0 19
RD1/PSP1 20
RD7/PSP7 30
RD6/PSP6 29
RD5/PSP5 28RD4/PSP4
27RD3/PSP3
22RD2/PSP2
21
RC7/RX/DT 26
RC6/TX/CK 25
RC5/SDO 24RC4/SDI/SDA
23
PIC16F877
1
23
45
67
8
20
1918
1716
1514
139 12
10 11 2
34
56
78
9
1
ELE 57420
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
20/25
Variables : Outputs an 8-bit variable
#include "16F877A.h"
void main(){
int x; // Declare variable and type
=
output_D(x); // Display the value in binary}
The same circuit as before
ELE 57421
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
21/25
Endless loop : Outputs variable count
#include "16F877A.h"
void main()
{
int x; // Declare variable
while(1) // Loop endlessly
{ output_D(x); // Display value
x++; // Increment value
}
}
The same circuit as beforeELE 574
22Dr. Shadi Alboon
8/13/2019 PIC_C introduction
22/25
RA0/AN02
RA1/AN13
RA2/AN2/VREF-4
RA4/T0CKI6
RA5/AN4/SS7
OSC1/CLKIN13
OSC2/CLKOUT14
16
RB7/PGD 40
RB6/PGC 39
RB5 38
RB4 37RB3/PGM 36
RB2 35
RB1 34
RB0/INT 33
RA3/AN3/VREF+5
RC0/T1OSO/T1CKI 15
MCLR/Vpp/THV1
U1
R110k
test circuit with input switch
RE0/AN5/RD8
RE1/AN6/WR9
RE2/AN7/CS10
RC2/CCP1 17
RC3/SCK/SCL 18
RD0/PSP0 19
RD1/PSP1 20
RD7/PSP7 30
RD6/PSP6 29
RD5/PSP5 28
RD4/PSP4 27RD3/PSP3 22
RD2/PSP2 21
RC7/RX/DT 26
RC6/TX/CK 25
RC5/SDO 24
RC4/SDI/SDA 23
PIC16F877
D1
LED-RED
ELE 57423
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
23/25
IF statement: Tests an input
#include "16F877A.h"
void main()
{
int x; // Declare test var.
ou pu _ ; ear a ou pu s
while(1) // Loop always
{
x = input(PIN_C0); // Get input
if(x==1)output_high(PIN_D0); // Change out}
}
ELE 57424
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
24/25
Conditional loop: Input controls outputloop
(While loop)
#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
}
}
ELE 57425
Dr. Shadi Alboon
8/13/2019 PIC_C introduction
25/25
ELE 574 Dr. Shadi Alboon26