0% found this document useful (0 votes)
12 views3 pages

Tutorial 08_C Interrupts (LO3 & LO4)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Tutorial 08_C Interrupts (LO3 & LO4)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Tutorial 07 – C Programming (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

// 'C' source line config statements

// 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)

// #pragma config statements should precede project file includes.


// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 20000000

#include <xc.h>
//#include <htc.h>
//#include <math.h>

//void __interrupt() isr(void); // ISR


int count=0;

void __interrupt() isr(void) // ISR


{
if (TMR0IF==1 && count==2)
{
__delay_ms(100); // Half sec delay
PORTC = 0x0f;

if (INTF==1 && count==0)


{ count++;//External Interrupt detected{
//
PORTC = 0xf0; // Invert (Toggle) the value at PortC
__delay_ms(100); // Half sec delay
INTF = 0;//; // Clear the interrupt 0 flag

if (INTF==1 && count==1)


{
count++;
TMR0=0; //TMR0 Initiation
T0CS=0; //Choosing to work with internal clk
T0SE=0; //Reacting on Low2High edge
PSA=0; //Choosing to work with a Prescaler
PS0=1;
PS1=1; //Prescaler value divides 256
PS2=1;
// TRISB=0;
// PORTB=0;

// TRISC=0;
// PORTC=0;

GIE=1; //Enable Global Interrupt


PEIE=1; //Enable the Peripheral Interrupt
TMR0IE=1;
TMR0IF=0;
T0IF=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;
}

You might also like