Blink led with delay control using timers

In: Project Code: ES000005
Advertisements
Share

In this project I will create a specific delay using timers on a PIC microcontroller and use the delay between led on and off. If you are new to PIC microcontroller programming we suggest you to execute the basic led blinking project before trying this project. If you don't have access to your own hardware for this project, you can and execute it remotely.

course thumb

Project Content


Required hardware and software


The required hardware and software for this project is as below:

  • A breadboard
  • 1 x 4mmLED
  • 1 x resistor between 220 Ohm to 1 kOhm
  • Hookup cables
  • Development board hosting PIC16F877A 8-bit microcontroller
  • MPLAB IDE(v5.05) from Microchip
  • PICKIT3 Programmer, connecting cable from PICKIT3 to your computer.
  • Power supply for the development board

  • Schematic diagram


    The schematic diagram for this project is as shown below:

    Required Components

    PIC16f877A Timer description


    Timer hardware is a crucial component of most embedded systems. In some cases, a timer measures elapsed time (counting processor clock ticks). In others, we want to count or time external events. An 8-bit timer can only count up to 256 (8-bit timer) where as an 16-bit counter can count up to 65535 (16-bit timer).

    In this project we will use timer0 to create a delay and use the delay to control the led blinking frequency.

    Prepare source code, building program source code, creating binary


    Prepare source code
    
    #include "xc.h"
    #define _XTAL_FREQ 40000000
    #pragma config FOSC = HS        
    #pragma config WDTE = OFF       
    #pragma config PWRTE = ON      
    #pragma config BOREN = OFF     
    #pragma config LVP = OFF       
    #pragma config CPD = ON         
    #pragma config WRT = OFF        
    #pragma config CP = OFF         
    void timer0_delay();
    void main()
    {
      TRISAbits.TRISA0 = 0;
      while(1)
      {
          PORTAbits.RA0 = 1;
          timer0_delay();
          PORTAbits.RA0 = 0;
          timer0_delay();
      }
    } 
    void timer0_delay()
    {
           OPTION_REGbits.PSA = 0; //Means Prescaler assigned to Timer 0 
           OPTION_REGbits.PS = 0b1000;  //Set the prescaler to 1:256
           OPTION_REGbits.T0CS = 0;    //Use the instruction clock (Fcy/4) as the timer clock. 
           INTCONbits.T0IF = 0; // Clear the interrupt timer 0 flag
           TMR0 = 0; //Load a value of 0 in timer 0
           while(INTCONbits.T0IF == 0) //Wait for the interrupt to occur. This
           {                           //happens when the TMR0 register rolls over.
               NOP();
           }
           INTCONbits.T0IF = 1; // Enable the interrupt timer 0 flag
    }
    

    Let me explain about the above source code developed for this project. The oscillator is used as a timing source to count the number of clock cycles. We are using OPTION_REG register from PIC16F877A microcontroller which contains various control bits to configure the TMR0. The various control bits of OPTION_REG register is shown below.

    Required Components

    Follow the procedure as followed in to load the .hex file in the microcontroller


    Looking for a new project? Write and upload your project information .
    Share

    Articles


    C Programming

    More Articles Categories

    Technical articles

    Prepare for software jobs


    Test your skills