Blink LED with PIC16f microcontroller

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, 16x2 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

---> Select the PICKit 3 tool, If you don't have one, select Simulator

---> Select the 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 3 of PORT C as input, TRIS should be set as TRISC3=1, and for setting it as output, TRISC=0. TRIS Register is basically like IODIR register used in ARM7 microcontrollers.

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

Let's see the code for blinking the LED

TRISC0 = 0;  // set PORTC.0 as output 

while (1) // infinite loop 
 {
     RC0 = 1;  // set PORTC.0 as high    
      __delay_ms (1000);  // wait for 1 second
      RC0 = 1;  // set PORTC.0 as high
      __delay_ms (1000);  // wait for 1 second 
 }

CODE

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ  10000000


void main(void) {
    TRISC0 =0;
    
    while (1)
    {
        RC0 = 0;
        __delay_ms (300);
        RC0 = 1;
        __delay_ms (300);
    }
    return;
}


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



TO Download Full code, along with Proteus Simulation GO TO Controllerstech
Blink LED with PIC16f microcontroller Blink LED with PIC16f microcontroller Reviewed by Controllerstech on October 02, 2017 Rating: 5

No comments:

Powered by Blogger.