Professional Documents
Culture Documents
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
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
ELE 574
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
13
ELE 574
14
Program Blank
// Specify PIC MCU // Include library routines // Start main block // Declare global variables // Start control loop // Program statements // End main block
ELE 574
15
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
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;
ELE 574
18
ELE 574
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
20
21
ELE 574
22
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
23
24
ELE 574
26