Upload
mysteriousmiracle
View
596
Download
4
Embed Size (px)
Citation preview
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
-32768 to +3276716-bitint
0 to 6553516-bitunsigned int
-128 to +1278-bitchar
0 to 2558-bitunsigned char
Data RangeBit SizeData Type
*PIC18 has a limited number of registers and data RAM locations
Example 1Write 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 2Write a C program to toggle all the bits of PORTB (simple loop)
delays.h Function Library
Inserts 20,000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 2,560,000)
Delay10KTCYx(20);Delay10KTCYx
Inserts 3000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 256,000)
Delay1KTCYx(3);Delay1KTCYx
Inserts 1000 instruction cycles (number must be between 0 and 255) (0 causes a delay of 25,600)
Delay100TCYx(10);Delay100TCYx
Inserts 100 instruction cycles (number must be between 0 and 255) (0 causes a delay of causes 2560)
Delay10TCYx(10);Delay10TCYx
Inserts a single NOP instruction into the program
Delay1TCY();Delay1TCY
NoteExampleFunction
*All the related libraries can be found in the MPLAB C18 Libraries
Example 3Write 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.
4Clock 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.
= instruction time, TCY
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 SolutionWrite 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 toPORTC; 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
/AN10/AN8
/AN9
RE3/
RA7/
*Additional functions in PIC18F4580 as compared to PIC18F458
PIC18 Configuration RegistersImportant 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