Sunday, July 6, 2014

Up and running with a PIC16

Up and running with a PIC16
This tutorial covers basics on how to hook up your PIC16F877A, write a small program and upload a program to your chip.
Section A : Wiring up the chip
Stuff you will need: A PIC16F877A, A breadboard, PICKIT 3, A 10k resistor, a 20Mhz xtal and an LED.
           

Crystal

Wiring this chip is very simple. You just need to follow the following instructions.
1.       Place your chip on a breadboard. Remember how to read the pins(small notch indicates pin 1)
2.       5V to Pin 1
3.       5v  to Pin 11
4.       Ground to Pin 12
5.       Crystal pins in Pins 13 and 14(Polarity does not matter)
6.       5v to Pin 32
7.       Ground to Pin 31
After successfully following these steps, your breadboard should look like this.


On breadboard

Section 2: Writing a small program
In this section, we take a look at how to write a led blinking program.
The program is first written in MikroBasic©, in BASIC LANGUAGE.
To proceed, follow these instructions:
1.       Get a free demo copy of Mikrobasic(http://www.mikroe.com/mikrobasic/pic/)
2.       Open Mikrobasic and click on new project.


Screen shot 1

1.       Click next on the new project wizard window pop up.
2.       Select PIC16F877A and Set clock to 20Mhz.


Screen shot 2

1.       Click on Next and Finish
2.       We will now write the program after taking a look at the fundamentals of BASIC Programming.
Format:
Program YourProgramName
Main:
     Code
End.

The following program will flash a led on port B.
program cassosl_1
main:
 TRISB = 0                            >Set Port B as output
 while true                          >Start an infinite while loop
  PORTB = %11111111      >Set all outputs of PortB to 1(HIGH)
  delay_ms(1000)               >Set a delay of 1000ms
  PORTB = 000000      >Set all outputs of PortB to 1(HIGH)
  delay_ms(1000)               >Set a delay of 1000ms
 wend                                   >End loop
 end.                                     >End Program

3.       Click Build. Many files will be generated in your save folder. You should look for an HEX file, in particular. Copy and paste it on your desktop.

Uploading your program:

In this step, we take a look at how to upload your program to your chip. In this step, you will need a programmer. The programmer is an interface between the chip and your computer. It usually has 6 output pins.
I use a PICKIT 3.

The PICKIT 3


Pin outs

You will connect lines from these output pins to your chip as follows:
PIN 1(ARROW) – Pin 1
PIN2 – Power
PIN3 – Ground
PIN4- Pin 4O
PIN5-Pin 39
PIN6-No connection
You should be having something like this.




Final layout

Next,  you should get MPLAB IDE and load it.
Click on Programmer?Pickit3.
You will get an error message. Click Programmer, settings, power. Set to 5V.
Next, click file, import and select the HEX file. Once loaded, click program.



Snap shot 3

Finally, connect a led to any pin out of port B and it should flash.







No comments:

Post a Comment