Hi!
Well, I started a new thread, as not to confuse it with another previously posted thread concerning a PIC12F1822 and CSM application.
I have an application that I want to use 3 capacitive touch buttons for brightness up, brightness down, and a firmware 1/0 "on/off" button for full on and full off.
I have attached my code thus far. It works fairly well, although I would like the on off function to be latching, where the up/down can be held to step up and down in each direction. Currently the on/off function toggles at the same rate I am repeating the up/down function.Code:'****************************************************************'* Notes : Copyright (c) 2016 * '* : Editor MicroCode Studio Plus PBP 3.0.6.0 * '* : MUST BUILD PROJECT WITH MPASM ASSEMBLER * '* : Device = 16LF722A-I/SO * '* Date : 1/18/2016 * '* Version : 1z * '* Name : LS1236_1Z.BAS * '* Author : R. WRIGHT * '* : * '**************************************************************** 'SET CONFIGURATION FOR INTERNAL OSCILLATOR WITH PLL MULTIPLIER ON #CONFIG __config _CONFIG1, _INTRC_OSC_NOCLKOUT & _WDTE_OFF & _BOR_OFF & _PLLEN_ON #ENDCONFIG define OSC 4 INCLUDE "DT_INTS-14.bas" ' PBP interrupt routines (MUST BE IN SAME FOLDER) INCLUDE "ReEnterPBP.bas" ' Included for use of CPS module interrupt ' Courtesy of Derrel Taylor, PBP Wizard! Include "modedefs.bas" OSCCON = %00010000 'SETS INT OSC TO 4Mhz WPUB = %00000000 'DISABLES WEAK PULL-UP BITS ANSELB = %00111111 'SETS INPUTS B0-B5 AS ANALOG TRISB = %00111111 'SETS INPUTS B0-B5 AS INPUTS OPTION_REG = %10000111 '1 = All weak pull-ups are disabled (except MCLR, if it is enabled) ' 0 = Interrupt on falling edge of RB0/INT pin ' 0 = Timer0 clock is internal cycle clock (Fosc/4) ' 0 = Increment on low-to-high transition on RA4/T0CKI pin ' 0 = Prescaler is assigned to the Timer0 module ' 111 = 1:256 Timer0 prescaler rate T1CON = %11000001 'enable timer 1 (bit 0) & source the clock from the CPS module (bit 6 & 7 =11) 'set the CPS module highest frequency availabe (for vcc mode) '+ timer0 clock sourced from CPS module. (BIT 1) CPSCON0 = %10001100 '1 = CPS module is enabled ' 000 = unused ' 11 = Oscillator is in High Range. Charge/Discharge Current is nominally 18 µA ' 0 = Capacitive Sensing Oscillator Status bit (only readable) ' 0 = Timer0 clock source is controlled by the core/Timer0 module and is FOSC/4 CPSCON1 = %00000001 '0001 = channel 1, (CPS1) ' Software variables CPSX_PRESENT var WORD 'VARIABLE FOR BUTTON CURRENT VALUE CPSX_THRESHOLD var WORD 'VARIABLE FOR BUTTON THRESHOLD VALUE CPSX_LAST var WORD 'VARIABLE FOR PREVIOUS BUTTON VALUE Duty VAR WORD 'VARIABLE FOR DUTY CYCLE stepp var byte 'VARIABLE FOR BRIGHTNESS LEVEL incnt var byte 'BUTTON PUSH INCRIMENT ' variables used by interrupt handler, comment out as per assembler wsave VAR BYTE $20 SYSTEM ' location for W if in bank0 'wsave VAR BYTE $70 SYSTEM ' alternate save location for W ' if using $70, comment wsave1-3 ' --- IF any of these three lines cause an error ?? ------------------------ ' Comment them out to fix the problem ---- ' -- Which variables are needed, depends on the Chip you are using -- wsave1 VAR BYTE $A0 SYSTEM ' location for W if in bank1 'wsave2 VAR BYTE $120 SYSTEM ' location for W if in bank2 'wsave3 VAR BYTE $1A0 SYSTEM ' location for W if in bank3 ' SET DEVICE I/O PINS LED1 VAR PORTA.1 ' SET LED LEVEL INDICATORS LED2 VAR PORTA.2 LED3 VAR PORTA.3 LED4 VAR PORTA.4 LED5 VAR PORTA.5 ' Software/Hardware initialization PAUSE 50 ' OSC settle delay Duty = 255 ' SET INITIAL BRIGHTNESS TO FULL ON stepp = 5 ' SET INITIAL LOOKUP TABLE TO 5 incnt = 1 ' set input counter to input 1 hpwm 1,DUTY,1000 ' INITIALIZE HARDWARE PWM TO SELECTED LEVELS pause 1000 ' LET EVERYTHING SETTLE HIGH LED5 ' TURN ON OUTPUT FOR LED 5 ASM INT_LIST macro ; IntSource, Label, Type, ResetFlag? INT_Handler TMR0_INT, _Timer0_Int, pbp, yes endm INT_CREATE ; Creates the interrupt processor ENDASM TMR0 = 0 'clear TIMER0 @ INT_ENABLE TMR0_INT ;enable timer0 interrupts PAUSE 100 '****************************************************************************** '------------------------------< Main program >-------------------------------- 'SCAN THE THREE INPUTS UNTIL FINGER DEPRESSION IS DETECTED VIA TIMER0 INTERRUPT Main: incnt = 2 cpscon1 = %00000001 ' 0001 = channel 1, (CPS1), DOWN BUTTON ANSELB = %00000010 ' SETS INPUT B1 AS ANALOG TRISB = %00000010 ' SETS INPUTS B1 AS INPUTS pause 5 INcnt = 3 cpscon1 = %00000010 ' 0010 = channel 2, (CPS2), UP BUTTON ANSELB = %00000100 ' SETS INPUT B2 AS ANALOG TRISB = %00000100 ' SETS INPUT B2 AS INPUT PAuse 5 incnt = 1 cpscon1 = %00000000 ' 0000 = channel 0, (CPS0), 1/0 BUTTON ANSELB = %00000001 ' SETS INPUT B0 AS ANALOG TRISB = %00000001 ' SETS INPUT B0 AS INPUT Pause 5 ' KEEP CYCLING UNTIL INTERRUPT OCCURS Goto Main '****************************************************************************** '****************************************************************************** '--------------------------<INTERRRUPT ROUTINE>-------------------------------- Timer0_Int: @ INT_DISABLE TMR0_INT ; stop timer0 interrupts when TMR0 overflow occurs pause 100 ; PAUSE TO GET TIMER 1 COUNT ' store away the present timer1 count for the next time we come into the interrupt routine CPSX_PRESENT = TMR1 ' get Timer1's present count. CPSX_THRESHOLD = CPSX_PRESENT - ((CPSX_LAST/10)*6) ' this sets the 'trigger' up for a 60% diversion (finger press) CPSX_LAST = CPSX_PRESENT 'DETERMINES WHICH BUTTON HAS BEEN PRESSED IF CPSX_PRESENT < CPSX_THRESHOLD THEN if (incnt = 3) AND (STEPP > 1) then ' down button pressed so decriment stepp stepp = stepp - 1 GOTO SKIP1 ENDIF If (incnt = 2) AND (STEPP < 5) then ' up button pressed so incriment stepp stepp = stepp + 1 GOTO SKIP1 ENDIF IF (INCNT = 1) AND (stepp > 0) THEN ' 1/0 button pressed for off STEPP = 0 GOTO SKIP1 ENDIF IF (INCNT = 1) AND (stepp = 0) THEN ' 1/0 button pressed for on STEPP = 5 ENDIF ' lights appropriate level indicator leds according to the current lookup table value SKIP1: IF STEPP = 5 THEN HIGH LED5 ENDIF IF STEPP = 4 THEN HIGH LED4 ENDIF IF STEPP = 3 THEN HIGH LED3 ENDIF IF STEPP = 2 THEN HIGH LED2 ENDIF IF STEPP = 1 THEN HIGH LED1 ENDIF if stepp = 0 THEN LOW LED1 endif ' SETS HARDWARE PWM OUTPUT 1 TO DESIRED LEVEL at a frequency of 1Khz lookup stepp, [0,16,32,64,128,255],duty ' choose level, 5 levels plus OFF hpwm 1,duty,1000 ' set new level, frequency IF INCNT = 1 THEN ' extra delay for press of PAUSE 200 ' I/O button ENDIF ENDIF pause 10 ' settle time TMR0 = 0 ' clear TIMER0 TMR1 = 0 ' clear TIMER1 @ INT_ENABLE TMR0_INT ; re-enable timer0 interrupt @ INT_RETURN '***************************************************************************** END
Bookmarks