so, this is a continuation of the thread started by ero, named 'Capacitive Touch Button by using ADC channel (the CVD system)' that I sort of hijacked. HankMcSpank helped me out a ton. Many thanks to him. He left enough clues in his code snippits to point me in the right direction, but if you blindly copy and paste his code, you'll be using timer0 for both cap sense counting, and timing, which is not going to work. Once I figured that out, that was my 'Ah-Ha' moment!
I've been avoiding interrupts like the plague, so this will, I'm sure, help me (and maybe somebody else)learn something about them. I'll be thinking out loud some of my questions, like, how often is this interrupt firing, and how fast is the cap sensor oscillating? but for now, I'm going to bed! (with a big smile on my face, cuz this thing is working now!)
I get about 19000 with nothing near the sensor, and about 7500 with my fingers covering as much of the sensor as possible, and about 6000 if I actually touch the copper tape.
I couldn't program the chip with my PICKIT2, I needed the PICKIT3. So I program with the PICKIT3 and then connect the PICKIT2 to use the UART tool. Doesn't the PICKIT3 have a uart monitor?
I'm guessing I don't have to clear the timer0 registers at the end of the interrupt routine...
here is the code
Code:
'MPLAB 8.70, PICBASIC PRO Compiler 2.60C, Darrel Taylor Instant Interrupts-14_V110 (http://darreltaylor.com/DT_INTS-14/intro.html)
'PIC16F1823 14 PIN dip package
@ __CONFIG _CONFIG1, _FCMEN_OFF & _FOSC_INTOSC & _WDTE_SWDTEN & _MCLRE_OFF & _CP_OFF & _IESO_OFF & _BOREN_OFF & _PWRTE_OFF
@ __CONFIG _CONFIG2, _LVP_OFF
DEFINE OSC 8 'tell picbasic what speed the oscillator is
DEFINE DEBUG_REG PORTA ' asyncronous bit-banged RS-232 output on PORTA...
DEFINE DEBUG_BIT 0 ' bit 0, which can be seen with the PICKIT2 UART tool, but you can't program this chip with the PICKIT2?!?!
DEFINE DEBUG_BAUD 9600 ' 9600 Baud,
DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true,like for using max232, or pickit2
DEFINE DEBUG_PACING 1000 ' 1mS delay
INCLUDE "DT_INTS-14.bas" '
INCLUDE "ReEnterPBP.bas" ' Include if using PBP interrupts
OSCCON = %01110010
'0 = 4x PLL is disabled
'1110 = 8 MHz
'0 = unused
'1X = Internal oscillator
ANSELA = %00000000 'make port A pins digital
TRISA = %00000000 'make port A pins outputs
ANSELC = %00000010 'make pin 9 (RC1) (CPS5) analog
TRISC = %00000010 'make pin 9 (RC1) (CPS5) input
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
'(TMR0CS) 0 = Transition on RA4/T0CKI pin
'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
CPSCON0 = %10001100 'set the CPS module highest frequency availabe (for vcc mode) + timer0 clock sourced from CPS module. (BIT 1)
'1 = CPS module is enabled
'0 = CPS module is in the low range. Internal oscillator voltage references are used.
'00 = 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 = %00000101 '0101 = channel 5, (CPS5)
CM1CON0 = 0 ' COMPARATOR OFF
CM1CON1 = 0 ' COMPARATOR OFF
DEBUG "Starting Up",13,10
CPS5_PRESENTCOUNT var WORD
CPS5_THRESHOLD var WORD
CPS5_LASTCOUNT var WORD
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
main:
pause 100
@ INT_DISABLE TMR0_INT ' stop timer0 interrupts
DEBUG DEC CPS5_PRESENTCOUNT,13,10
@ INT_ENABLE TMR0_INT ' re-enable timer0 interrupt
goto main
'**************************************************************************************************
Timer0_Int:
@ INT_DISABLE TMR0_INT ' stop timer0 interrupts while we're in here
CPS5_PRESENTCOUNT = TMR1 ' take a snapshot of Timer1's present count.
'CPS5_THRESHOLD = CPS5_LASTCOUNT - ((CPS5_LASTCOUNT/100)*2) ' this sets the 'trigger' up for a 20% diversion (finger press)
''CPS5_LASTCOUNT = CPS5_PRESENTCOUNT ' store away the present timer0count for the next time we come into the interrupt routine
'if CPS5_PRESENTCOUNT < CPS5_THRESHOLD then 'if the present incoming timer0 count is 20% below the last count, then a finger has been placed on the sensor
TMR0 = 0 ' clear TIMER0
TMR1 = 0 ' clear TIMER1
@ INT_ENABLE TMR0_INT ' re-enable timer0 interrupt
@ INT_RETURN
end
Bookmarks