Relaxation oscillators (for capacititive touch sensing)


Closed Thread
Results 1 to 25 of 25

Hybrid View

  1. #1
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    The significance is that you are trying to use features that your selected PIC does not have (TMR1GIF).

    So you will need to make more than syntax changes to get it to work.
    The overall method used will need to be different, and it's one that I have never investigated.
    <br>
    Ok, roger that...I'm in the Sierra Hotel Indigo Tango (hey ho, two nights apparently wasted). Thanks for the early interjection at this point though ...I'd have ended up looking like a 'ZZ Top Guitarist after a night on the razzle' trying to sort this one myself.

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


    Did you find this post helpful? Yes | No

    Default

    Don't give up ...

    I'm sure it can be done.
    But copy/paste from a 16F726 program will only confuse you.

    You've got an Oscillator running ...
    So if the C2OUT pin were connected to the T1CKI pin, then Timer1 could count the pulses.

    Make a "Time Base" with Timer2 and the CCP1 pin (a.k.a. HPWM at lowest freq) connected to the T1G pin.

    Then use TMR2_INT to read the Timer1 value, which should be directly proportional to the frequency. Not sure how good the resolution will be, but that's what experimentation is for.
    <br>
    DT

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Thanks for the moral support (& the idea, which sounds great!)

    Like I say, I'm out my depth & frankly I need to go & learn about timers now (never had to use 'em before)

    So my next 'challenge' is o find a way of ensuring that Timer1 is seeing (counting) pulses in from C2out at the T1CKI Pin (I've a sneaking suspiscion, that that's going to be like drawing back teeth for me). I'm pretty sure it's not counting pulses yet, I took a bit of a flyer & lifted the parts I thought were most appropriate rom byte_butchers code...

    Code:
     
    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)  
    TRISC  = %00000011            'RC0 (VCC/4) as input, RC1 Input (not sure if it's in scope either), RC4 output (C2OUT)
    
    ' the above seems to get me my oscillator output.
    
    
    
    'Timer Setup   (all byte_butcher's code & likely not going to work with the 16F690)
    T2CON = %01110110 'bit7=unimplemented, bit6-3=postscaler, bit2=TMRON, bit1-0=prescaler 
    PR2 = %11111111       'give PR2 a number for TMR2 to match
    PIR1.1 = 0      'Clear the TMR2 interupt flag
    PIE1.1 = 1      'Turn TMR2 interrupt enable on
    T1CON = %11000101 '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
    
    '-----Allocate Variables
    timercount   var    word  ' raw count from TMR1
    timercount = TMR1L + TMR1H << 8
    
    '----Main loop-----------------------------------------------------------
    Main:
        DEBUG " timerCount= ", DEC timercount, 13, 10
        pause 100  
    GOTO Main
    end
    (the 'timercount' onscreen DEBUG output remains steadfastly at zero.... I guess I'm gonna have to get deep down 'n dirty & try & figure out all those specific Timer register settings are for the 16F690!

    Once I'm sure my Timer1 is counting, then I guess I can enter the brave new world of getting creative with interupts!

    Thanks once again.

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


    Did you find this post helpful? Yes | No

    Default

    You're still copying from a 16F726 program ...
    Just forget about byte_butchers code. It doesn't apply, because there is no CSM in the 16F690!

    I'm purposely being vague here because you only spent 45 minutes on the last version. I know ... "pouring scorn" ...

    To select the T1CKI pin as input to Timer1, T1CON.1 (TMR1CS) should be set to 1.
    Other bits in T1CON might be different too.

    And when I hinted to HPWM, it should have been a big red flag.
    <br>
    _________________
    DT
    &nbsp; Fishing is cruel, why would anyone teach people to fish?

  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Well thanks for checking back...and holding back on the scorn!

    I've got Timer1 counting now, here's the magic combination of 1 & 0's (just in case any other 16f690'ers wanting to get their relaxation oscilator working into Timer1 & stumble upon this thread via Google)...

    T1CON = %10000111

    Also....

    Code:
    '-----Allocate Variables
    timercount   var    word  ' raw count from TMR1
    
    '----Main loop-----------------------------------------------------------
    Main:
    timercount = TMR1L + TMR1H << 8
        DEBUG " timerCount= ", DEC timercount, 13, 10
        pause 50  
    GOTO Main
    end
    .....of course the bit in bold needed to be in my loop....else I'd always have the same timer1 value showing up onscreen.

    Anyway, not a totally unsuccessful night...got my oscillator oscillating and my timer1 incrementing - gotta pull all this together now with something like you've suggested Darrel. (I can't help but base my code on others ....even if it only applies loosely - I have to start with something & start stripping away the bits I don't need - if nothing else it shows me what registers are in play & forces me to try & understand them!)

    & so....to bed.

    Thanks to you all for bearing with me in my somewhat 'white noisy' thread.

    PS: Just so I've something to wake up to in the morning, what's wrong with this high level concept for measuring the incoming frequency at Timer1 input....

    Clear Timer0 (have it counting the internal clock)
    When Timer0 overflows (which should always be the same time period?), use the Timer0 'overflow interupt' to then trigger a bit of code to read in the contents of timer1
    if the contents of timer1 is more or less the same as the previous sample content, then no finger has gone near the sensor, if the value is significantly lower, then essentially a pseudo-button has been pressed
    Clear timer 1
    Clear timer 0
    rinse, repeat?


    [dives for cover waiting to be shot down in flames]
    Last edited by HankMcSpank; - 23rd November 2009 at 01:07.

  6. #6
    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

  7. #7
    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.

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