You are on page 1of 22

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 data types for the PIC18
Data Type Bit Size Data Range
unsigned char 8-bit 0 to 255

char 8-bit -128 to +127

unsigned int 16-bit 0 to 65535

int 16-bit -32768 to +32767

*PIC18 has a limited number of registers and data RAM locations


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)
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 20,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
Example 3
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. Assume XTAL = 4MHz.
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 5 times <<5 (Ex: 0x06 <<4 = 0x60)

Can you demonstrate using logic gates?


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
Checksum byte in ROM
• Please refer Example 7-26 in the textbook
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
End of Chapter 1

You might also like