Capacitive sensing with the CSM module, 16F1823 & learning about interrupts


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2008
    Location
    Milwaukee, WI
    Posts
    37

    Default Capacitive sensing with the CSM module, 16F1823 & learning about interrupts

    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


  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Capacitive sensing with the CSM module, 16F1823 & learning about interrupts

    Firstly big oops on my part... no, you can't use timer0 for both the interrupt AND the CPS oscillator output counting, (like I say, my snippets were from a humungous program that I hacked very quickly & tried to condense as much as possible - got carried away clearly) but hey you got there!!

    Any way as you've done, it's best to use timer1 for counting the CPS pulses, and timer0 for the timebase.

    You can program the 16f1823 with a Pickit2 (http://www.microchip.com/stellent/id...cName=en027813) ....just make sure you've the latest PIckit2 device file

    Yes, it's best to clear timer0 at the end of the timer0 interupt (though this'll be the point we're the old hands chime in "no need, it gets cleared for you", etc)

    The thing about CPS that doesn't get mentioned, is using DT's interrupts coupled with the high interrupt rate normally needed for a decent sensor feel/response, means your little pic is working quite hard just for cap touch alone ...therefore, you want to interrupt as little as you can get away with but still have a decent sensor response feel. For example, if you only interrupt every half second, it might feel sluggish (also the longer you make the period between interrupts, the more you timer1 is gonna count oscillator pulses - watch the timer1 oscillator count doesn't get near 65536 and overflow, if it's getting towards that, take the CPS oscillator frequency down with the CPSCON register setting (bits 4-2)
    Last edited by HankMcSpank; - 10th September 2011 at 10:09.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Capacitive sensing with the CSM module, 16F1823 & learning about interrupts

    I'm wondering if we can tempt one of the experienced assembly guys here, to leverage on the "Automatic Context saving" interrupt capability aspect of these new line of PICs, which would place much less burden on the PIC when cap touch is used for switching.

    All we need the associated assembly interrupt to do is this...

    1. store the present timer1 count
    2. setup a running count 'threshold' (by deducting x% from the last timer1 count)
    3. compare present timer1 count with the 'threshold' .....if it is below, then a sensor has been touched, set a 'sensor touched' type flag, if the present timer1 count is not below the threshold, then store 'present timer1 count' away as 'last timer1 count' (for the next interrupt)

    job done!

    Else, if you have a lot of time sensitive stuff going on in your main loop, I fear you almost need to dedicate one pic (eg a 12f1822) solely to cap touch switching duties, due to the high frequency of interrupts needed & the chunky overhead they presently incur.
    Last edited by HankMcSpank; - 10th September 2011 at 14:46.

  4. #4
    Join Date
    Jun 2008
    Location
    Milwaukee, WI
    Posts
    37


    Did you find this post helpful? Yes | No

    Default Re: Capacitive sensing with the CSM module, 16F1823 & learning about interrupts

    OK. I'm missing something here. How do I make sure I have the latest PICKIT2 device file? will I be able to program the 16F1823 in MPLab, or just the stand alone program?

    on Microchip's PICkit 2 Development Programmer/Debugger page, I see the stand alone program v2.61, then the device file, which gives me the PK2DeviceFile-1.62.09.dat file. I deleted the -1.62.09 from the name and replaced the old file with the new one in the program files\Microchip\PICkit 2v2 directory.

    Then when I run the stand alone PICkit2 v2.61 program, I still don't see the 16F1823 in the drop down menu. and MPLAB still has the PICKIT2 greyed out when I load my workspace.

    THen, back to the Microchip PICkit 2 Development Programmer/Debugger page I see the PK2CMDv1.20 - do I need to put that somewhere?

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Capacitive sensing with the CSM module, 16F1823 & learning about interrupts

    It sounds like you've done everything right, but the only thing I do different is not try & select it in the Pickit standalone app device drop down list (cos it's not listed there!). When you connect your pickit2 to your PIC, then start the pickit2 standalone application, it should detect it.

    If not, try....

    In the pickit2 app, select 'device family' - 'midrange' - '1.8V min

    then 'tools' - 'check communication ...and it should then find it (ie you should see your PIC listed next to the device field)

    re MPLab....still finding my feet with that interface, I don't go there much for now, but here the pickit2 is greyed out too....go figure, the pickit2 standalone app works, but microchips IDE doesn't!

    re the Pickit3...in my opinion it blows (for a start, there's no UART tool)
    Last edited by HankMcSpank; - 10th September 2011 at 22:15.

  6. #6
    Join Date
    Jun 2011
    Posts
    25


    Did you find this post helpful? Yes | No

    Default Re: Capacitive sensing with the CSM module, 16F1823 & learning about interrupts

    I try to use the CVD method on a pic18f but i did not have any luck
    The 18f does not have CSM , they have only mtouch
    The issue with mtouch is that you must touch the sensor to get a response, and if you put a piece of plastic 2-3 mm no way to get it working
    Does anybody has a pieces of working code for pic18f so i can experiment wit capacitive voltage divider method ?
    Thank you

Members who have read this thread : 2

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts