Relaxation oscillators (for capacititive touch sensing)


Closed Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    A total count from Timer1 is a start!
    Relate it to a period of time, and your good, beit Timer0 or Timer2.

    <object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/yCir5Wwpvos&hl=en_US&fs=1&"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/yCir5Wwpvos&hl=en_US&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
    DT

  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Well, tonight I managed to nail the third & final stage - sampling the timer1 count (measuring frequency from the relaxation oscillator) every time TMR0 reaches 255. If the sample has drifted significantly down from the norm, then a finger has pressed the psuedo button. (which happens to be a small capacitor in my present circuit). The quiescent Timer1 count for my circuit seems to be about 600, if I touch the cap, it almost instantly drops to 450 & countinues way down if I hold it - upon removing my finger it's right back at 600 instantly.

    It works very well...my test LED toggles on/off every time I touch that small capacitor (that I have in lieu of a capacititve sensor pad - which I've not bothered to make yet).

    here's a short youtube clip...



    Now I'm no programmer, & I make lots of embarassing simple code/syntax errors, but in the spirit of openess, I lay myself bare & post up my simple 16F690 code/register settings ...possibly to be laughed/winced at, just in the hope I can save others a lot of time, who are interested in getting their 16F690 to work with capacitive touch sensors.

    I also realise that the final timer associated stage would be a lot slicker using interupts (vs my lazy/kludgey way of triggering via timer0 hitting a 255 count), but that's for others to integrate interupts into the code! (I'm just getting to grips with them!)

    Code:
    'Pins used...
    '2    Input - T1CKI from C2OUT
    '6    Output C2Out to T1CKI (RC4 not an analogue)
    '16   Input C2IN+ From VCC/4 divider network (ie VCC/4) (RC0 AN4)
    '18   Input C12IN0- (RA1 AN1) 
    
    @MyConfig = _INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_ON  
    @MyConfig = MyConfig & _MCLRE_OFF & _BOR_OFF 
    @ __config  MyConfig
    
    
    DEFINE OSC 4
    DEFINE DEBUG_REG PORTA
    DEFINE DEBUG_BIT 0      ' RA0 = TX out to PICKit2 programmer USART tool
    DEFINE DEBUG_BAUD 9600
    DEFINE DEBUG_MODE 0     ' 1 = inverted, 0 = true
    DEFINE	ADC_BITS 10     ' Set number of bits in result
    DEFINE	ADC_CLOCK 1     ' Set clock source Fosc/8 "2uS"
    DEFINE	ADC_SAMPLEUS 50 ' Set sampling time in uS
    
    CM1CON0= %10010100    'from the Microchip AN1011a application note.
    CM2CON0= %10100000    'from the Microchip AN1011a application note.
    CM2CON1= %00110010    'from the Microchip AN1011a application note.
    SRCON  = %11110000    'from the Microchip AN1011a application note.
    VRCON  = %10000111    'from the Microchip AN1011a application note.
    ANSEL  = %00001010    'AN1 & AN4 analogue, the rest Digital.
    TRISA  = %11111011    'RA1 (C12IN0-) as input, RA2 (C1Out ) output RA5 Input (T1CKI)  
    TRISB  = %00000000    'all output
    TRISC  = %00000011    'RC0 (VCC/4) as input, RC1 Input (not sure if it's in scope either), RC4 output (C2OUT)
    LOW PORTB.7            'test LED attached to this PIN (pin 10)
    
    '----- Timer Setup
    
    T1CON = %10000111 'Timer clock source=CAPOSC, prescale=1:1, dedicated OSC disabled, no external clock synchronize, timer on
    'PIR1.7 = 0   'Clear Gate Interrupt Flag
    'PIR1.1 = 0   'clear the TMR2 interupt flag
    
    OPTION_REG = %10000000
    
    
    '-----Allocate Variables
    timer1count   var    word  ' raw count from TMR1
    
    '-----Clear Timers
     
    TMR0 = 0      ' clear Timer 0
    TMR1L = 0     ' clear Timer1 Low
    TMR1H = 0     'Clear Timer1 High
    
    '----Main loop-----------------------------------------------------------
    Main:
    
    if TMR0 = 255 then     'when Timer0 reads 255 clock pulses, then lets 'sample' Timer1's value  to see if the 'norm' value has dropped  (finger press)
    timer1count = TMR1L + TMR1H << 8
    if timer1count < 500 then '600+ is quiescent state reading on my circuit (ie no finger press)
    toggle PORTB.7    'if button press deteced  then toggle the LED
    DEBUG "Switch press detected - TMR0 = ", DEC TMR0," timer1Count = ", DEC timer1count, 13, 10
    pause 200               ' debounce pause value
    endif
    TMR0 = 0                'clear the counters
    TMR1L = 0               'clear the counters
    TMR1H = 0               'clear the counters
    endif
    GOTO Main
    end
    ....it might be cack handed, it might read awful...but hey, it works, it's mine & I'm chuffed to bits

    Note: My simple code above will need some form of averaging algorith added in to take account of changes in the relaxation osc frequency due to enviromentals (that simple code above has conditional values hard coded - not good if the frequency shifts a lot due to say humidity, temperature etc)...in other words, you're gonna need bells & whistles added in - my simple code is just to get you started!

    (note if you're using the Pickit2 as I am, your Pic's Pin18 which goes back to the Pickit2 pin 5 must be removed after programming the PIC, else the relaxation oscillator will not work)
    Last edited by HankMcSpank; - 24th November 2009 at 01:21.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Not a drill in sight ...


  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default

    Hey, that look great! Congrats!

    Ioannis

  5. #5
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    Were you able to get it to work with more than 1 button on the 16F690?

  6. #6
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rmteo View Post
    Were you able to get it to work with more than 1 button on the 16F690?

    Not yet...that was meant to be my little project while here in Brazil (visiting relatives here for a month - he has broadband :-) ), but I'm a really bad packer & forgot to bring the extra resistors etc needed to experiment with that aspect.

    Hey ho...I'll just have to drink Caiparinha now!

Similar Threads

  1. Need a cheap touch sensor idea.. here it is
    By mister_e in forum Code Examples
    Replies: 20
    Last Post: - 16th April 2016, 22:42

Members who have read this thread : 0

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

Posting Permissions

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