Share | Tweet |
---|
In this project we will control led delay using timer interrupt implementation with an interrupt service routine(ISR) with PIC16F877A 8-bit microcontroller.If you are new to PIC microcontroller programming we suggest you to execute the basic led blinking project here before trying this project. If you don't have access to your own hardware for this project, you can schedule the project on our company hardware and execute it remotely.
The required hardware and software for this project is as below:
The schematic diagram for this project is as shown below:
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.
/*
* File: switch.c
* IDE: MPLAB X 5.05
* Programmer: PICKIT3
* Compiler: XC8
*/
#include
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config CP = OFF // FLASH Program Memory Code Protection bits (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low Voltage In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EE Memory Code Protection (Code Protection off)
#pragma config WRT = OFF // FLASH Program Memory Write Enable (Unprotected program memory may not be written to by EECON control)
void msDelay(unsigned int );
#define bit PORTAbits.RA0
void main()
{
unsigned int check;
TRISB7 = 1; //RD0 as Input PIN
TRISA0 = 0; //RA0 as Output PIN
check = 100;
unsigned int var = 0;
bit = 1; //LED ON
while(check > 1)
{
if(RB7 == 0) //If Switch is NOT pressed
{
msDelay(100);
}
else
{
bit = 0;//LED OFF
var = 1;
break;
}
check--;
};
while(1);
}
void msDelay(unsigned int x)
{
unsigned int i;
unsigned char j;
for(i = 0;i < x;i++)
for(j = 0;j < 165;j++);
}
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.
Follow the procedure as followed in this project to load the .hex file in the microcontroller
Share | Tweet |
---|