Easy PIC Micro State Transition Interrupt Code

A PICBasic program which interrupts the PIC when a high/low transition occurs on pin PortB.0

The code is available for download as a .rar or just copy and paste it from here.

I got the starting code from the microengineering website:
http://www.melabs.com/resources/samples.htm

but I added the register bit definitions and changed a little to make it easier to use.

Step 1: The Code

‘ This program will turn on an LED on PortB.7
‘ when you change the state of the PortB.0 pin, the LED will
‘ turn off for 0.5 seconds, then turn back on
‘ you can change the ways in which this interrupt behaves by
‘ fiddling with the OPTION_REG and INTCON bits
‘ see the bottom of this code for bit definitions

led var PORTB.7

OPTION_REG = %01111111 ‘ Enable PORTB pullups
On Interrupt Goto myint ‘ Define interrupt handler
INTCON = %10010000 ‘ Enable INTE interrupt

loop: High led ‘ Turn LED on
Goto loop ‘ Do it forever

‘ Interrupt handler
Disable ‘ No interrupts past this point
myint:
Low led ‘ If we get here, turn LED off
Pause 500 ‘ Wait .5 seconds
INTCON.1 = 0 ‘ Clear interrupt flag
Resume ‘ Return to main program
Enable

‘################################################################
‘#####################–OPTION_REG Bit Definitions–#############
‘################################################################

‘OPTION_REG Register (Address 81h)

‘bit 7: *RBPU: PORTB Pull-up Enable bit
‘1 = PORTB pull-ups are disabled
‘0 = PORTB pull-ups are enabled (by individual port latch values)

‘bit 6: INTEDG: Interrupt Edge Select bit
‘1 = Interrupt on rising edge of PB0/INT pin
‘0 = Interrupt on falling edge of PB0/INT pin

‘bit 5: T0CS: TMR0 Clock Source Select bit
‘1 = Transition on PA4/T0CKI pin
‘0 = Internal instruction cycle clock (CLKOUT)

‘bit 4: TOSE: TMR0 Source Edge Select bit
‘1 = Increment on high-to-low transition on PA4/T0CKI pin
‘0 = Increment on low-to-high transition on PA4/T0CKI pin

‘bit 3 PSA: Prescaler Assignment bit
‘1 = Prescaler assigned to the Watchdog Timer
‘0 = Prescaler assigned to TMR0

‘bit 2-0: PS2:PS0: Prescaler Rate Select bits
‘Please refer to the PIC16F84’s data sheet.

‘###############################################################
‘#################–INTCON Register Bits DEFINITONS–###########
‘###############################################################

‘The INTCON register is located at 0BH and it is defined as follows:

‘bit 7: GIE: Global Interrupt Enable bit
‘1 = Enables all un-masked interrupts
‘0 = Disables all interrupts

‘bit 6: EEIE: EE Write Complete Interrupt Enable bit
‘1 = Enables the EE write complete interrupt
‘0 = Disables the EE write interrupt

‘bit 5: T0IE: TMR0 Overflow Interrupt Enable bit
‘1 = Enables the TMR0 interrupts
‘0 = Disables the TMR0 interrupts

‘bit 4: INTE: PB0/INT Interrupt Enable bit
‘1 = Enables the PB0/INT interrupt
‘0 = Disables the PB0/INT interrupt

‘bit 3: RBIE: PB Port Change Interrupt Enable bit
‘1 = Enables the PB port change interrupt
‘0 = Disables the PB port change interrupt

‘bit 2: T0IF: TMR0 Overflow Interrupt Flag bit
‘1 = TMR0 has overflowed (must be cleared by software)
‘0 = TMR0 did not overflow

‘bit 1: INTF: PB0/INT Interrupt Flag bit
‘1 = The PB0/INT interrupt occurred
‘0 = The PB0/INT interrupt did not occur

‘bit 0: RBIF: PB Port Change Interrupt Flag bit
‘1 = When at least one of the PB4-PB7 pins change state (must be cleared in software)
‘0 = None of the PB4-PB7 pins have changed state

Attachments

Source: Easy PIC Micro State Transition Interrupt Code

About The Author

Muhammad Bilal

I am a highly skilled and motivated individual with a Master's degree in Computer Science. I have extensive experience in technical writing and a deep understanding of SEO practices.