22
Introduction to Embedded C Programming with PIC18 Chapter 1

Chapter1 - Introduction to Embedded C Programming With PIC18

Embed Size (px)

Citation preview

Page 1: Chapter1 - Introduction to Embedded C Programming With PIC18

Introduction to Embedded C Programming with PIC18

Chapter 1

Page 2: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 3: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 4: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 5: Chapter1 - Introduction to Embedded C Programming With PIC18

Example 1Write a C program to send values 00 – FF to Port B

Page 6: Chapter1 - Introduction to Embedded C Programming With PIC18

Time Delay

• Three ways to create time delay in PIC18:– Using simple loop– Using delay function library– Using the PIC18 timers

Page 7: Chapter1 - Introduction to Embedded C Programming With PIC18

Example 2Write a C program to toggle all the bits of PORTB (simple loop)

Page 8: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 9: Chapter1 - Introduction to Embedded C Programming With PIC18

Example 3Write a C program to toggle all the bits of PORTB (delay function library)

Page 10: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 11: Chapter1 - Introduction to Embedded C Programming With PIC18

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.

Page 12: Chapter1 - Introduction to Embedded C Programming With PIC18

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.

Page 13: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 14: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 15: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 16: Chapter1 - Introduction to Embedded C Programming With PIC18

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?

Page 17: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 18: Chapter1 - Introduction to Embedded C Programming With PIC18

Checksum byte in ROM

• Please refer Example 7-26 in the textbook

Page 19: Chapter1 - Introduction to Embedded C Programming With PIC18

PIC18F4580 Pin Diagram

/AN10/AN8

/AN9

RE3/

RA7/

*Additional functions in PIC18F4580 as compared to PIC18F458

Page 20: Chapter1 - Introduction to Embedded C Programming With PIC18

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

Page 21: Chapter1 - Introduction to Embedded C Programming With PIC18

CONFIG Settings

*We will be using this config settings throughout this course unless stated or otherwise

Page 22: Chapter1 - Introduction to Embedded C Programming With PIC18

End of Chapter 1