• PicŪ Basic

  • Simple Single button ON & off using interrupt

    Hello all,
    The program below is how a single push button can turn off an entre circuit by using a DC/DC regulator either step-down or boost that have a shut down pin that has to be maintain high to operate. This little marvels have a shutdown current as low as 5-10 uA. On the diagram created for the simulation I use a relay instead of a regulator because I could not find a regulator with shutdown capability on the Proteus components, but the principle is the same.


    '************************************************* ***************
    '* Name : ON-off_routine with interrupt.BAS *
    '* Author : [Yves Mazzon] *
    '* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
    '* : All Rights Reserved *
    '* Date : 24/11/2013 *
    '* Version : 1.0 *
    '* Notes : *
    '* : *
    '************************************************* ***************
    ' This program is to control the shutdown of many DC/DC power regulators that have s shutdown pin
    ' When the button is pressed the PIC is powered a high pulse to the shutdown pin
    ' Then the regulator power the PIC and the PIC turns the PORTC.2 or
    ' "on_off_hold" on, that keeps the regulator runing. To turn off the PORTB.0 "on_off_sense"
    ' is set as an Input and the interrupt on rising edge. When the button is pressed again it triggers
    ' a interrupt routine that turns off the "on_off_hold" pin and the PIC commits "suicide"
    ' by bringing the "on_off_hold" down hence power down

    'Device = 18F25K20
    Include "Amicus18.inc" ' Configure the compiler to use the Amicus18 hardware (18F25K20, 64MHz)
    Dim LED1 As PORTC.0
    Dim I As Word
    Dim on_off_sense As PORTB.0
    Dim on_off_hold As PORTC.2
    Output on_off_hold 'make PORTC.2 as an output
    HRSOut Cls
    DelayMS 500
    On_Hardware_Interrupt GoTo Turn_off

    ' Simulation of the ON-OFF button not yet pressed
    HRSOut Cls
    While on_off_sense = 0 ' waiting for the on and off button to be pressed
    HRSOut At 2,1,"Press button", At 3,1, " to start "
    DelayMS 20

    'Turns on the on_off_hold
    INTCONbits_INT0IE = 0 ' Disable the INT0 external interrupt
    High on_off_hold 'Now the device is switched on by holding the shunt down step-down regulator high
    DelayMS 500
    GoTo Main 'Jump over interrupt handler

    '----------------------interrupt handler----------------------------------
    Turn_off: ' Interrupt service handler

    Context Save 'No need of this in this case
    If INTCONbits_INT0IF = 1 Then ' Was it an INT0 that triggered the interrupt?

    Low on_off_hold 'Pic commit suicide by turning off its own power
    HRSOut Cls,At 2,2,"power off "
    'Input PORTB.0 ' Except the INT0 pin, which must remain an input
    INTCONbits_INT0IF = 0 ' Clear the INT0 flag


    High_Int_Sub_End ' No need in in this case the pic is off
    Context Restore ' Restore any variables or SFRs and exit the interrupt


    HRSOut Cls
    '------------Set up the PORT.B4 as interrupt on change---------------------------------
    ' Setup for an interrupt on INT0 (PORTB.0)
    DelayMS 10 ' Allow time to fully initialise
    Input PORTB.0 ' Make the INT0 pin an input (PORTB.0)
    INTCON2bits_INTEDG0 = 0 ' Interrupt on falling edge of INT0
    INTCONbits_INT0IF = 0 ' Clear the INT0 flag
    INTCONbits_INT0IE = 1 ' Enable the INT0 external interrupt
    INTCONbits_PEIE = 1 ' Enable peripheral interrupts
    INTCONbits_GIE = 1 ' Enable global interrupts
    Input on_off_sense ' The INT0 pin, PORTB.0
    '------This is where the program really is------------------------------------------
    While 1 = 1 ' Create an infinite loop waiting for an interrupt to occur

    Inc I
    HRSOut Cls,At 2,2," Now On ",At 3,1," ", Dec I," "
    DelayMS 100 ' Delay for 100ms
    Low LED1
    DelayMS 100
    High LED1

    Wend ' Do it forever