• PicŪ Basic


  • Simple single button power on/off

    Here's a little test program that I used recently for a project of mine. I thought it may interest some users.

    It puts the microcontroller to sleep with a button press, then wakes it up with another press of the same button.

    Code:
    '
    ' Place the microcontroller in a sleep state with one press of a button
    ' And wake it up with another press of the same button.
    ' One end of the switch should be connected to ground, with a pull-up resistor connected to PORTB.0 (INT0)
    '
    ' Button bounce can be reduced with a capacitor across the switch.
    ' Or a more elaborate debounce mechanism can be implemented inside the interrupt handler, however, I'll leave that up to you.
    '
    ' Any external peripherals should be powered via a mosfet, so they can be switched off before the PICmicro goes to sleep
    ' Thus reducing power to a bare minimum
    '    
        Include "Amicus18.inc"                  ' Configure the compiler to use the Amicus18 board (18F25K20 at 64MHz)
        
        On_Hardware_Interrupt GoTo ISR_Handler
    '
    ' State saving variables
    '    
        Dim TRISA_StateSave As Byte
        Dim TRISB_StateSave As Byte
        Dim TRISC_StateSave As Byte
        
        Dim PORTA_StateSave As Byte
        Dim PORTB_StateSave As Byte
        Dim PORTC_StateSave As Byte
        
    '--------------------------------------------------------------------------------------------    
        GoTo Main                               ' Jump to the main routine
    '--------------------------------------------------------------------------------------------
    ' Interrupt service handler
    '
    ISR_Handler:
        Context Save
        If INTCONbits_INT0IF = 1 Then           ' Was it an INT0 that triggered the interrupt?               
            '
            ' Yes. So switch peripherals off here before going to sleep
            '
            TRISA_StateSave = TRISA             ' \
            TRISB_StateSave = TRISB             ' | Save the state of TRISA, TRISB and TRISC
            TRISC_StateSave = TRISC             ' /
            PORTA_StateSave = PORTA             ' \
            PORTB_StateSave = PORTB             ' | Save the state of PORTA, PORTB and PORTC
            PORTC_StateSave = PORTC             ' /
            Low PORTA                           ' \
            Low PORTB                           ' | Make the pins all low to save power
            Low PORTC                           ' /
            Input PORTB.0                       ' Except the INT0 pin, which must remain an input
            INTCONbits_INT0IF = 0               ' Clear the INT0 flag
            Sleep                               ' Go to sleep. Wake up with another INT0 trigger
            Nop                                 ' Required after Sleep
            PORTA = PORTA_StateSave             ' \
            PORTB = PORTB_StateSave             ' | Restore the state of PORTA, PORTB and PORTC
            PORTC = PORTC_StateSave             ' /
            TRISA = TRISA_StateSave             ' \
            TRISB = TRISB_StateSave             ' | Restore the state of TRISA, TRISB and TRISC
            TRISC = TRISC_StateSave             ' /
            '
            ' Switch peripherals back on here after waking up
            '
            INTCONbits_INT0IF = 0               ' Clear the INT0 flag so that it doesn't trigger another interrupt
        EndIf
        Context Restore
    '--------------------------------------------------------------------------------------------        
    Main:
        DelayMS 10                              ' Allow time to fully initialise
    '
    ' Setup for an interrupt on INT0 (PORTB.0)
    '
        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
    '
    ' Create a loop to flash an LED when the microcontroller is awake
    '    
        While 
            Low PORTA.0
            DelayMS 100 
            High PORTA.0
            DelayMS 100 
        Wend
    This article was originally published in forum thread: Simple single button power on/off started by Les View original post
  • Recent Activity

    joesaliba-353

    Hx711 calibration

    Thread Starter: amod

    Iam using load cell amplifier HX711 module.I have got raw values from module but iam not able to calibrate with load cell.I want make a universal...

    joesaliba Today, 15:48 Go to last post