Introduction to Embedded C
Programming with PIC18
Chapter 1
Objectives
• Upon completion of this chapter, you will
be able to:
– Examine C data types for the PIC18
– Program C code for time delay
– Program C code for I/O operations
– Program C code for logic and arithmetic
operations
Why program the PIC18 in C?
• It is easier and less time consuming
• C is easier to modify and update
• Easily import code available in function
libraries (i.e. delays, uart, adc, etc.)
• C code is portable to other microcontroller
with little or no modification (ANSI C
format)
• We will be using Microchip C18 compiler
C-Compiler for PIC18
A Simple C Program
Variable and Data Types
Data Types
double ent373
Data Type Qualifiers
*PIC18 has a limited number of registers and data RAM locations
Literal Constant
Example 1
Write a C program to send values 00 – FF to Port B
Time Delay
• Three ways to create time delay in PIC18:
– Using simple loop
– Using delay function library
– Using the PIC18 timers
Example 2
Write a C program to toggle all the bits of PORTB (simple loop)
Example 3
itime = 0 to 65535
delays.h Function Library
Function Example Note
Delay1TCY Delay1TCY(); Inserts a single NOP instruction into the
program
Delay10TCYx Delay10TCYx(10); Inserts 100 instruction cycles (number must
be between 0 and 255) (0 causes a delay of
causes 2560)
Delay100TCYx Delay100TCYx(10); Inserts 1000 instruction cycles (number must
be between 0 and 255) (0 causes a delay of
25,600)
Delay1KTCYx Delay1KTCYx(3); Inserts 3000 instruction cycles (number must
be between 0 and 255) (0 causes a delay of
256,000)
Delay10KTCYx Delay10KTCYx(20); Inserts 200,000 instruction cycles (number
must be between 0 and 255) (0 causes a
delay of 2,560,000)
*All the related libraries can be found in the MPLAB C18 Libraries
*x = multiplication
Example 4
Write a C program to toggle all the bits of PORTB
(delay function library)
Time Delays using the delays.h
• To determine the time generated by the delays.h
functions use the following equation.
4
= instruction time, TCY
Clock Frequency
• If the clock frequency is 4 MHz then the
instruction time is 1.0 μs so if a Delay10TCYx(8)
is used in a program it causes an 80 μs time
delay.
I/O Programming in C (Exercise 1)
Write a C program to get a byte of data from PORTB, wait 0.5
second, and then send it to PORTC.
1) Assume XTAL = 4MHz
2) Assume XTAL = 10MHz
3) Assume XTAL = 20MHz
Exercise 1 Solution
Write a C program to get a byte of data from PORTB, wait 0.5
second, and then send it to PORTC. Assume XTAL = 4MHz.
I/O Programming in C (Exercise 2)
Write a C program to get a byte from PORTC. If it is less than 100,
send it to PORTB; otherwise, send it to PORTD
Exercise 2 Solution
Write a C program to get a byte from PORTC. If it is less than 100,
send it to PORTB; otherwise, send it to PORTD
Bit-addressable I/O Programming
Write a C program to monitor bit RB4. If it is HIGH, send 55H to
PORTC; otherwise, send AAH to PORTD
Logic Operations in C
• AND & (Ex: 0x35 & 0x0F = 0x05)
• OR | (Ex: 0x04 | 0x68 = 0x6C)
• XOR ^ (Ex: 0x54 ^ 0x78 = 0x2C)
• Invert ~ (Ex: ~0x55 = 0xAA)
• Shift right 3 times >>3 (Ex: 0x9A>>3 = 0x13)
• Shift left 4 times <<4 (Ex: 0x06 <<4 = 0x60)
Can you demonstrate using logic gates?
Example 5
Example 6
Data Conversion programs in C
• ASCII Numbers
– Used in data transmission (i.e. serial port)
• Packed BCD to ASCII conversion
• ASCII to packed BCD conversion
– Used in Real-Time Clock (RTC) module
ASCII Table
Example 6
Example 7
PIC18F4580 Pin Diagram
RE3/
/AN9
/AN8
/AN10
RA7/
*Additional functions in PIC18F4580 as compared to PIC18F458
PIC18 Configuration Registers
Important CONFIG:
•config OSC (oscillator)
•config BOR (brown-out reset)
•config BORV (brown-out reset voltage)
•config PWRT (power-up timer)
•config WDT (watchdog timer)
•config DEBUG (in-circuit debugger)
•config LVP (low voltage programming)
•config STVR (stack overflow)
*these config is also known as FUSES
CONFIG Settings
*We will be using this config settings throughout this course unless stated or otherwise
Simple LED Connection (Pictorial)
• If RD5 = ‘Logic 1’ = 5V
– LED ON
• If RD5 = ‘Logic 0’ = 0V
– LED OFF
33
Simple LED Connection (Schematic)
• If RD5 = ‘Logic 1’ = 5V
– LED ON
• If RD5 = ‘Logic 0’ = 0V
– LED OFF
34
How to Turn “ON” and “OFF” LED?
LED 1 LED 2 RB7 RB6
OFF OFF
OFF ON
ON OFF
ON ON
35
Review - 1
OFF ON
36
Simple Switch Connection
• If switch not pressed
– RD5 = 5V = ‘Logic 1’
• If switch pressed
– RD5 = 0V = ‘Logic 0’
37
Review - 2
• Decimal = 12
• Binary = 0b00001100
• Hexadecimal = 0x0C
or 0Ch
38
Multiple LED
Connections
• What is the LED condition
when:
a. PORTB = 0b01010101
b. PORTB = 201
c. PORTB = 0x76
d. PORTB = Ah
e. PORTB = 257 39
I/O Ports on PIC18F4580
• Consist of:
– PORTA (RAx)
– PORTB (RBx)
– PORTC (RCx)
– PORTD (RDx)
– PORTE (REx)
• Total 36 I/O pins!!!
40
PIC18F4580 I/O PORTS REGISTERS
41
Review 3 – Problem
• Discuss traffic light with pedestrian friendly
function.
42
I/O - Revisited
• What is the LED condition
when:
a. PORTB = 0b10011001
b. PORTB = 301
c. PORTB = 0xC6
d. PORTB = A0h
e. PORTB = 101 43
Active-High
Logic
1 – LED ON
0 – LED OFF
Active-Low
Logic??
SW ON – 1
We will use this
SW OFF – 0 convention throughout
this course
44
Programming 101
• Sequential Flow: Program starts from ‘top’
and progress ‘line by line’.
Start
Sequential
Instruction 1
Loop Instruction 2
Instruction …n
End
• Looping: An instruction causes execution to
branch to an earlier point (repeat again).
45
1st : Pseudo-Code
• Descriptive statements to describe what the
program will do.
• Question 1: Program 8-LED on/off (blinking):
1. Start
2. Configure I/O
3. Turn on LED
4. Wait for ½ second
5. Turn off LED
6. Wait for ½ second
7. Repeat: Go back to No. 3 46
2nd : Flowchart
Start
Configure
I/O
LED On
Wait 0.5
Seconds
LED Off
Wait 0.5
Seconds
47
3rd : Convert to Code ?
Start
Configure
I/O
LED On
Wait 0.5
Seconds
LED Off
Wait 0.5
Seconds
48
3rd : Convert to Code ?
Start
// repeat forever
Configure TRISB = 0x00; // Set all PORTB I/O as output
I/O PORTB = 0x00; // Reset PORTB
LED On PORTB = 0xFF; // PORTB I/O output high ‘1’
Wait 0.5
delay_500ms(); // Wait 500 miliseconds
Seconds
while(1)
LED Off PORTB = 0x00; // PORTB I/O output low ‘0’
Wait 0.5
delay_500ms(); // Wait 500 miliseconds
Seconds
49
Program Solution Question 1
50
Question 2
• Write a loop function that performs LED
‘running light’ on PORTB.
Pseudo-code
Flowchart
Source code
Schematic
51
Program Solution Question 2
52
Question 3
• Write a program to blink 8-LED on PORTB
when a push-button is pressed.
Pseudo-code
Flowchart
Source code
Schematic
53
Program Solution Question 3
When push-button is
pressed on RC0, RC0 equal
to ‘1’
54
I/O Ports
Example TRISA & TRISB Setting
PROTOM Research Group, UTM 56
Logic Operators
Question 4
Perform the following task by using a microcontroller system. Write your c program
and simulate using Proteus
60
End of Chapter 1