Tutorial 08_C Interrupts (LO3 & LO4)
Tutorial 08_C Interrupts (LO3 & LO4)
A microcontroller based system comes with a push button for mode selection. For all the odd
presses of the button, PORTC bit 0 to bit 3 should be logic HIGH. For all even presses, PORTC
bit 4 to bit 7 should be logic HIGH. Write a C code segment based on PIC16F microcontroller to
accommodate these requirements.
You may assume the following:
You are free to choose any clock frequency for the microcontroller.
Use interrupts to detect button presses.
All port pins may be used as general purpose digital I/O.
State any assumptions you make in your answer.
Solution
// PIC16F877A Configuration Bit Settings
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // 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)
#include <xc.h>
//#include <htc.h>
//#include <math.h>
// TRISC=0;
// PORTC=0;
while(!T0IF);
void main(void)
{
TRISB0 = 1; // To configure PORTB pin 2 as input
TRISC = 0x00;
INTF=0; // Reset the external interrupt flag
// INTE=1; // Enable external interrupts from RB0
// TRISB0 = 1; //DEfine the RB0 pin as input to use as interrupt pin
//INTEDG = 1; // Set Rising Edge Trigger for INT
// TRISB0 = 1; //DEfine the RB0 pin as input to use as interrupt pin
OPTION_REG = 0b00000000; // Enables PULL UPs
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
INTE = 1; //Enable RB0 as external Interrupt pin
while(1)
{
PORTC = 0x00; //Set some value at PortD
// __delay_ms(1000); // Half sec delay
// PORTC = 0xff; //Set some value at PortD
//
//INTF = 0;//; // Clear the interrupt 0 flag
}
return;
}