You are on page 1of 25

ELE 574

Dr. Shadi Alboon

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 writing computer operating systems Starting from an existing language called BCPL they developed C C was used to write the next version of UNIX system software UNIX eventually became the world's first portable operating system
ELE 574 Dr. Shadi Alboon 3

Why Program PICs in C?


C is a portable language, requiring minimal modification when transferring programs from one processor to another Programming in a high-level language rather than assembler allows programs to be developed much more rapidly Typically a program which takes a few weeks in assembler can 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 the present generation of PICs
ELE 574 Dr. Shadi Alboon 4

CCS PIC Compiler


A compiler converts a high-level language program to machine 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 Most embedded systems are now programmed using the C/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 Alboon 5

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 the ICD2 debugger Code is executed on the PIC system and can be debugged (break points inspect variables single points, variables, step etc.) using PC
ELE 574 Dr. Shadi Alboon

CCS PIC Compiler


The CCS compiler comes with an integral syntax-aware editor CCS C is standard C plus limited support for reference parameters in functions PIC-specific pre-processor directives are provided in addition to the standard directives (#include, #define etc):
#inline #priority output_low() delay_us() Implement the following function inline set priority of interrupts set an I/O port bit low delay by a specified number of s

Additional functions supporting PIC hardware are provided:

ELE 574

Dr. Shadi Alboon

CCS PIC Compiler Data Types


PICs are optimized for processing single bits or 8-bit words, and this is reflected the CCS compiler word sizes:

Contrary to the C standard, CCS C integers are by default unsigned


ELE 574 Dr. Shadi Alboon 8

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 make use of the definitions (already in the device definition files): #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 variables
ELE 574 Dr. Shadi Alboon 9

Device Definition File


A CCS C program pre-processor will start with a number of directives similar to:
#include <18F452.H> #fuses HS,NOWDT,NOBROWNOUT,NOPROTECT,PUT #use delay(clock=20000000) #include "lcd.c

The first directive instructs the compiler to include the system header file 18F452.H This is a device-specific file that contains information about the location of SFRs and the values to be written to them
ELE 574 Dr. Shadi Alboon 10

Fuses
CCS C provides a fuse directive: #fuses HS,NOWDT,NOBROWNOUT,NOPROTECT,PUT which specifies the states of the configuration fuses that should be programmed onto the PIC In this example: HS NOWDT NOBROWNOUT NOPROTECT PUT
ELE 574

Clock is a high-speed crystal or resonator Watchdog timer is disabled Brown-out detector is disabled Code protect off Power-on timer is enabled
Dr. Shadi Alboon 11

Delays
CCS C provides functions for generating delays: delay_us() delay_ms() These delay functions actually delay by a number of machine cycles The compiler needs to know the clock frequency in order to 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 Alboon 12

Multiple Source Code Files


CCS C does not allow separate compilation and linking of source code files It is convenient (and good programming practice) to put commonly-used library functions in separate files #include "lcd.c" This directive instructs the compiler to include the user library 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 a few seconds
ELE 574 Dr. Shadi Alboon

13

CCS PIC Programming Examples

ELE 574

Dr. Shadi Alboon

14

Program Blank

#include "16F877A.h" #use void main() { int while(1) { } }

// Specify PIC MCU // Include library routines // Start main block // Declare global variables // Start control loop // Program statements // End main block

ELE 574

Dr. Shadi Alboon

15

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 } while(condition) {statements } if(condition) {statements } for(condition) {statements } C Functions delay_ms(nnn) delay_us(nnn) output_x(n) output_high(PIN_nn) output_low(PIN_nn) input(PIN_nn)
ELE 574

Main program block Conditional loop Conditional sequence Preset loop

Delay in milliseconds Delay in microseconds Output 8-bit code at Port X Set output bit high Set output bit low Get input
Dr. Shadi Alboon 16

Integer Variables
Name int1 unsigned int8 signed int8 unsigned int16 signed int16 unsigned int32 signed int32 Type 1 bit 8 bits 8 bits 16 bits 16 bits 32 bits 32 bits Min 0 0 -127 0 -32767 0 2147483647 Max 1 255 +127 65525 +32767 4294967295 +2147483647

ELE 574

Dr. Shadi Alboon

17

Conditional Operators

Operation Equal to Not equal to Greater than Less than Greater than or equal to Less than or equal to

Symbol

EXAMPLE if(a == 0) b=b+5; if(a != 1) b=b+4; if(a > 2) if(a < 3) b=b+3; b=b+2;

== != > < >= <=

if(a >= 4) b=b+1; if(a <= 5) b=b+0;

ELE 574

Dr. Shadi Alboon

18

A program to output a binary code

#include "16F877A.h" void main() { output_D(255); }

// MCU select // Main block // Switch on outputs

ELE 574

Dr. Shadi Alboon

19

U1
13 14 1 2 3 4 5 6 7 8 9 10 OSC1/CLKIN OSC2/CLKOUT MCLR/Vpp/THV RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD 33 34 35 36 37 38 39 40 15 16 17 18 23 24 25 26 19 20 21 22 27 28 29 30

RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RE0/AN5/RD RC2/CCP1 RE1/AN6/WR RC3/SCK/SCL RE2/AN7/CS RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 PIC16F877

LEDBARGRAPHRED

1 2 3 4 5 6 7 8 9 10

20 19 18 17 16 15 14 13 12 11

9 8 7 6 5 4 3 2 1

ELE 574

Dr. Shadi Alboon

20

Variables : Outputs an 8-bit variable

#include "16F877A.h" void main() { int x; x=99; output_D(x); }

// Declare variable and type // Assign variable value

// Display the value in binary

The same circuit as before


ELE 574 Dr. Shadi Alboon

21

Endless loop : Outputs variable count

#include "16F877A.h" void main() { int x; while(1) { output_D(x); x++; } }

// Declare variable // Loop endlessly // Display value // Increment value

ELE 574

The same circuit as before

Dr. Shadi Alboon

22

test circuit with input switch


U1
13 14 1 2 3 4 5 6 7 8 9 10 OSC1/CLKIN OSC2/CLKOUT MCLR/Vpp/THV RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD 33 34 35 36 37 38 39 40 15 16 17 18 23 24 25 26 19 20 21 22 27 28 29 30

RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RE0/AN5/RD RC2/CCP1 RE1/AN6/WR RC3/SCK/SCL RE2/AN7/CS RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 PIC16F877

R1
10k

D1
LED-RED

ELE 574

Dr. Shadi Alboon

23

IF statement: Tests an input

#include "16F877A.h" void main() { int x; output_D(0);

// Declare test var. // Clear all outputs

while(1) // Loop always { x = input(PIN_C0); // Get input if(x==1)output_high(PIN_D0); // Change out } }


ELE 574 Dr. Shadi Alboon

24

Conditional loop: Input controls output


loop (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 0.3s delay_ms(300); output_low(PIN_D0); delay_ms(500); // Delay 0.5s } output_low(PIN_D0); // Switch off LED } }
ELE 574 Dr. Shadi Alboon 25

ELE 574

Dr. Shadi Alboon

26

You might also like