Getting Started With PIC16

Description

PIC16 is a microcontroller series from Microchip. It is not difficult for a beginner to learn but powerful enough to be used in high level applications.

PIC16 microcontrollers have High-Performance RISC CPU with high operating frequencies. Some of it’s features are as follows:-

  • Timer0: 8-bit timer/counter with 8-bit prescaler
  • Timer1: 16-bit timer/counter with prescaler, can be incremented during Sleep via external crystal/clock
  • Timer2: 8-bit timer/counter with 8-bit period register, prescaler and postscaler
  • Two Capture, Compare, PWM modules
    – Capture is 16-bit, max. resolution is 12.5 ns
    – Compare is 16-bit, max. resolution is 200 ns
    – PWM max. resolution is 10-bit
  • Synchronous Serial Port (SSP) with SPI (Master mode) and I2C (Master/Slave)
  • Universal Synchronous Asynchronous Receiver  Transmitter (USART/SCI) with 9-bit address detection
  • 10-bit, up to 8-channel Analog-to-Digital Converter (A/D)
  • Data EEPROM Retention > 40 years
  • Power saving Sleep mode
  • In-Circuit Debug (ICD) via two pins

Let’s get started to our first program i.e. LED blinking using a PIC16f877A microcontroller.

Things Required

1.) First and of course the most important one is a development board. You can also use Proteus to learn but it is highly recommended that you buy a  development board. It helps in debugging and also the actual conditions are a lot different from virtual ones.

2.) You will also need a programmer like PICkit 3.

3.) A breadboard will be useful

4.) Few components like LEDs, 16×2 LCD, jumper wires etc…

Applications to download

1.) Microchip -X IDE Download http://www.microchip.com/mplab/mplab-x-ide

2.) MPLAB -XC8 compiler Download http://www.microchip.com/mplab/compilers

HOW To Set Up

—>Install the IDE and compiler.

—>Connect the PICKkit Programmer to the board and Open MPLAB IPE.

—>Connect your board to the power supply and click connect in the IPE. If you are doing it first time, It will download a new firmware and flash it. Once the device is found, you can close this.

—> Open the MPLAB -X IDE and close the start page or anything that is opened by default. Next click on New and select project.

—> Select Standalone Project and click Next

—> Select the PIC board you have (PIC16f877A is a mid range Mcu) and click next

—> Using PICKit 3 tool, If you don’t have one, select Simulator

—>  XC8 compiler and click Next

—> Setup the  the project name and location and click finish

—> Right click the Source File and select new C file

—> Give an appropriate name to this C file and click Finish

—> Now Before writing the program, we need to set Configuration bits. So go to production and click select Configuration bits

—> Change FOSC to HS ans setting to HS Oscillator. Click Generate Source code to output

—> Copy the code and paste it in the *.c file before the main function

A look at the basic Programming

PORTS and REGISTERS

PORTS represents the collection of actual pins which can be programmed as input or output. PIC16F877A has 5 PORTS (PORT A to PORT E). PORT A contains 6 pins (RA0 to RA5), PORT B , PORT C, PORT D each contains 8 pins (RX0 to RX7) and PORT E contains 3 Pins (RE0 to RE2).

TRIS Register is used at very basic level. It is also known as direction Register which is mainly used for setting the direction of  PIN/PORT input or output.

For eg- To set the direction of PIN 0 of PORT B as input, TRIS should be set as TRISB0=1, and for setting it as output, TRISB0=0. TRIS Register is basically like IODIR register used in ARM7 microcontrollers.

This is enough to blink the LED and read the button. We will cover more Registers while doing TIMERS, ADC, I2C, SPI etc..

Let’s see the code for the button 

......
TRISB0 = 1;  // set RB0 (button) as input
TRISD0 = 0;  // set RD0 (LED) as output

while (1)   // infinite loop
{
   if (RB0 != 0) // if pin is high
    {
      RD0 = 0; // LED OFF
    }

  else RD0=1; // else LED on
}

 

Result

Compile the code and flash it in the PIC Development Board

Or You can test in the Proteus by using the .HEX file located at /led/led.X/dist/default/production/led.X.production.hex

When button is pressed, LED turns ON

When button is Released, LED turns OFF

 

Check out the video below

DOWNLOAD

You can buy me a coffee sensor 🙂

download the CODE below

, , , ,

Leave a Reply

avatar
  Subscribe  
Notify of
Menu