Programming PIC16F877A Using CCS C Compiler and US-Burn

45
Programming PIC16F877A Using CCS C Compiler and US- Burn

Transcript of Programming PIC16F877A Using CCS C Compiler and US-Burn

Page 1: Programming PIC16F877A Using CCS C Compiler and US-Burn

Programming PIC16F877A

Using CCS C Compiler and US-

Burn

Page 2: Programming PIC16F877A Using CCS C Compiler and US-Burn

Why Use C for Embedded Programming?

1. Portable – code can be retargeted to different processors

2. Clarity – C is easier to understand than assembly

3. Compilers produce code that is close to manually-tweaked assembly language in both code size and performance

Page 3: Programming PIC16F877A Using CCS C Compiler and US-Burn

From .c to .hex

Page 4: Programming PIC16F877A Using CCS C Compiler and US-Burn

C Compilers for Embedded Systems

� CCS C Compiler

� HI TECH Compiler

Page 5: Programming PIC16F877A Using CCS C Compiler and US-Burn

CCS vs. HI TECH

� The CCS Compiler has an extensive range of inbuilt functions

→ Save time and speed up the learning process

� The HI TECH Compiler is based towards those who are following a company policy specifying the ANSI standard with hardware functions sometimes resembling Assembler more than C

Page 6: Programming PIC16F877A Using CCS C Compiler and US-Burn

Installing CCS C Compiler IDE

� Copy CCS C directory to your hard drive

� Run pcwhupd.exe (in this directory)

� Copy the following files to installed directory

� : PCM.CRG for 16-bit PIC

� , PCH.CRG,

� PCB.CRG

� Run Patch.exe file

� Run Pcw.exe file

Page 7: Programming PIC16F877A Using CCS C Compiler and US-Burn

Installing US-Burn

1. Copy USB burn driver directory to your harddrive

2. Plug USB cable (connected to the USB port of SaigonTech PIC Board) to computer USB port

3. Turn on the power switch on the PIC board (see the next slides)

Page 8: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 9: Programming PIC16F877A Using CCS C Compiler and US-Burn

…\USB burn driver

�Click the Browse button to select the driver folder

Page 10: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 11: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 12: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 13: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 14: Programming PIC16F877A Using CCS C Compiler and US-Burn

Succefull installation

announce

�Do the steps 2 and 3 for other USB ports.

Page 15: Programming PIC16F877A Using CCS C Compiler and US-Burn

A Sample C Program:

Blinking 8 LEDs

Page 16: Programming PIC16F877A Using CCS C Compiler and US-Burn

Program Description

� Make LED’s on PORTB blink every second

Page 17: Programming PIC16F877A Using CCS C Compiler and US-Burn

Cable Wiring

� Use a 8 pin ribbon cable to connect PIC’s

jumpers B0 – B7 to LEDs’ jumpers L0 – L7

Page 18: Programming PIC16F877A Using CCS C Compiler and US-Burn

Electronic Circuit

Page 19: Programming PIC16F877A Using CCS C Compiler and US-Burn

Writing Program in PCW C

Compiler IDE

Page 20: Programming PIC16F877A Using CCS C Compiler and US-Burn

Creating a .c File

Page 21: Programming PIC16F877A Using CCS C Compiler and US-Burn

Creating a .c File

� File name: Blink8LEDs

� Choose the path

� Click Save

Page 22: Programming PIC16F877A Using CCS C Compiler and US-Burn

Editing Blink8LEDs.c

Page 23: Programming PIC16F877A Using CCS C Compiler and US-Burn

Compiling a C Program

Page 24: Programming PIC16F877A Using CCS C Compiler and US-Burn

Using US-Burn to Load a .hex

File to the PIC

Page 25: Programming PIC16F877A Using CCS C Compiler and US-Burn

� Put 16F877A in the USB Programmer socket (pay attention to the PIC direction on the socket, otherwise you will damage the PIC)

� Plug the USB cable to the PC and to the PIC board

� Turn on the power switch of PIC Board

� Run Usburn14.exe

Page 26: Programming PIC16F877A Using CCS C Compiler and US-Burn

Microcontroller Setting

Page 27: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 28: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 29: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 30: Programming PIC16F877A Using CCS C Compiler and US-Burn
Page 31: Programming PIC16F877A Using CCS C Compiler and US-Burn

Run program on the board

Page 32: Programming PIC16F877A Using CCS C Compiler and US-Burn

� Turn off the power switch on the PIC board

� Put the programmed PIC in the Run socket on the board (pay attention to the PIC direction on the socket, otherwise you will damage the PIC)

� Turn on the power switch on the PIC board to see the LEDs blink.

Page 33: Programming PIC16F877A Using CCS C Compiler and US-Burn

Summary of the

Development

Process

PIC C

PIC C

PIC C

Page 34: Programming PIC16F877A Using CCS C Compiler and US-Burn

Discussion

Page 35: Programming PIC16F877A Using CCS C Compiler and US-Burn

Look at an Alternative of BlinkLEDs.c

� Is this listing better than the previous one? Why?

Page 36: Programming PIC16F877A Using CCS C Compiler and US-Burn

One of the Most Important Skills in

Programming

� Reuse

Page 37: Programming PIC16F877A Using CCS C Compiler and US-Burn

PIC16F877A

5/6 Programming pins

8 10-bit A/D channels

2 Oscillator Inputs

2 RS-232 inputs

33 I/O ports

2 PWM channels

Page 38: Programming PIC16F877A Using CCS C Compiler and US-Burn

The PIC16F877A

� PIC: Peripheral Interface Controller

Page 39: Programming PIC16F877A Using CCS C Compiler and US-Burn

Pin Functions

� MCLR – to reset the PIC

� VDD – power supply

� VSS – ground

� OSC1, OSC2 – connect to oscillator

� The pins having the notation R can all be used as digital inputs or outputs

� The pins that are labeled as AN can be used as analog inputs

� The pins CCP1 and CCP2 are used for PWM

Page 40: Programming PIC16F877A Using CCS C Compiler and US-Burn

Exercise

1. Delete NOLVP from #fuses and rerun the program. What is the effect of this action?

2. Instead of using the PORTB, use PORTC to rewrite the example

� Try to use NOLVP and without it

Page 41: Programming PIC16F877A Using CCS C Compiler and US-Burn

Why is NOLVP Needed?

� In Single Supply Programming or Low – Voltage Programming mode (using single voltage VDD 5V to programming):

� The LVP bit will be set

� Pin B3 will have PGM function → B3 is cleared

� To make RB3 become a normal I/O pin, LVP has to be disabled

� #fuses NOLVP

Page 42: Programming PIC16F877A Using CCS C Compiler and US-Burn

Exercise

� Instead of using the PORTB, use PORTA to rewrite the example

Page 43: Programming PIC16F877A Using CCS C Compiler and US-Burn

More Details on PORTA� In the PIC16 series Port A is the analog port

connected to the analog to digital converters

� By default this port is configured as analog

⇒ You will not be able to output or input data reliably on Port A until you configure it as a digital port

Pin RA4

PIC 16F877A

Can’t work

R

VDD

Page 44: Programming PIC16F877A Using CCS C Compiler and US-Burn

How to Make RA4 Work Properly

� RA4 is made to be an open-drain output

⇒ A pull-up resistor is needed to make it work properly

Pin RA4

PIC 16F877A

Can’t work R

VDD

Pin RA4

PIC 16F877A

Open-drain output

Pull-up Resistor

Page 45: Programming PIC16F877A Using CCS C Compiler and US-Burn

References

� Microcontroller. http://www.electronics.netmyne.com/microcontroller.html

� Programming PIC Microcontrollers in BASIC. mikroElektronika © 1998 – 2004. http://www.mikroelektronika.co.yu/english/product/books/picbasicbook/00.htm#preface

� http://www.picvietnam.com/forum//showthread.php?t=357

� PICC. An introduction to programming the Microchip PIC in C. Nigel Gardner. Bluebird Electronics 1998