Relaxation oscillators (for capacititive touch sensing)


Closed Thread
Results 1 to 25 of 25
  1. #1
    Join Date
    Mar 2009
    Posts
    653

    Default Relaxation oscillators (for capacititive touch sensing)

    I have a 16F690, that I'd like to use in a capacitive touch sensor setup.

    I should set out my stall, I'm still very new to PIC programming & the confusing array of registers to set!

    The first part of getting capacititive touch sensors to work with the PIC, is to setupthe PIC's comparators in a relaxation oscillator config.

    I can't get it to work! (so I've essentially failed at the first hurdle!)

    Here's their simple schematic here (source = http://ww1.microchip.com/downloads/e...tes/01171B.pdf) ...



    ...which I've mapped the same 16f690 pins 'in play' here...




    that said, I don't understand why there are two 16f690 pins with C12IN- on them pins 15 & 18 as seen on this datasheet pinout extract...



    So RC0, RC1 & RC4 are the only ones of importance to get the relaxation oscilator, erhm oscillating! (or maybe RA1 - depending on which C12IN- Pin I should be using! What's the difference between the two pins marked c12in-?)


    Microchip have released an application note which outlines the register settings needed to setup the relaxation oscillator on a 16F887 - http://ww1.microchip.com/downloads/e...tes/01101a.pdf Appendix A (but not a 16f690), so I needed to translate these register settings over to the 16F690, which as it turns out, I reckon are the same as the 16f887 anyway...
    Code:
    CM1CON0 = %10010100
    CM2CON0 = %10100000
    CM2CON1 = %00110010
    SRCON = %11110000
    VRCON = %10000111
    but I'm struggling with the ANSEL register. On the 16f690, it seems that there are two ANSELS in play (ANSEL & ANSELH), and if my understanding is correct, these registers setswhether the corresponding pins are analogue or digital. So for the relaction oscillator, they'd need to be analogue.

    If I'm reading the pic16f690 datasheet correctly ( http://ww1.microchip.com/downloads/e...Doc/41262C.pdf )

    it seems that the only analogue pins I'm using are

    RC0 (Pin 16 , AN4)
    RC1 (Pin 15, AN5)
    RC4 is not an analogue pin?
    RA5 (Pin 18, AN1, again not sure if it's this C12IN- or the other one on pin 15!)

    therefore should my ANSEL register look something like this...
    Code:
    ANSEL=  %00110001
    ANSELH = 0

    Any other input greatfully received!


    PS I've read the post by byte_butcher (http://www.picbasic.co.uk/forum/show...t=byte_butcher) , but he used a 16F726 which has an inbuilt capacitive sensing module - I'd rather stick with the 16f690 which is apparently capable of capacitive touch sensing too.
    Last edited by HankMcSpank; - 20th November 2009 at 00:48.

  2. #2
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    I realise, that this whole relaxation oscillator melarkey is a little niche, but I only seek a little hand holding on some of the more simpler(to you guys/gals at least) registers (that said, I'm surprised that there aren't many many more people chomping at the bit to get funky proximity touch sensor switches working on there PICs)


    It seems there's a whole heap of errors abounding wrt 16f690, relaxation oscillators, schematics, & comparator naming etc (Microchip have released a newer datasheet too). The best schematic I've found wrt setting up the 16F690 as a relaxation oscillator is this one (it seems right)...




    With that schematic above in focus , could someone at least confirm I'm on the right track with the following simpler registers at least (there's a whole heap more register I have to configure, buit it'd be nice to eliminate these from my enquiries)

    Code:
    TRISA  = %11111111            'All inputs ...RA1 (C12IN0-) input 
    TRISC  = %00000001            'RC0 (VCC/4) input RC4 output (C2OUT)
    
    ANSEL  = %11111111             'All Analogue ....becuase relaxation oscillator pins are analogue-ish  &not digital as far as I can see!
    ANSELH = %11111111            'All Analogue
    Last edited by HankMcSpank; - 21st November 2009 at 23:46.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Going back to your first post...

    There is a difference... pin 18 is C12IN0- and pin 15 is C12IN1-

    This is taken from my Datasheet DS41262E (yours might be an extinct version).

    You asked about RC4. Whether you consider a Comparator OUTPUT as analog I suppose is up to you. I consider Comparators INPUT as ANALOG but technically their OUTPUT is DIGITAL since it can only be a High or a Low. RC4 is the OUTPUT for Comparator 2. If you are using Comparator 2, you DON'T want to set that pin to ANALOG (because ANALOG refers really only to INPUTS), but you want DIGITAL OUTPUT.

    Look at figures 8.2 & 8.3 in the Comparator Section. They show you the Comparator INPUT arrangement. For your convenience CxVin- has four possible sources.

    The 16F690 does NOT have a CSM Module.

    This means that if you want to play with capacitive sensing you have to configure the Comparators as an Oscillator yourself, the frequency of Oscillation will change when you place your Dabs on the Sensing Terminal. It is up to you to detect the change in frequency. So really any PIC with Comparators can do that. It's nice that the 16F690 has a quad input to each Comparator, which means that each Comparator can potentially service up to four capacitive buttons (NOT like your schematic in the first post which cannot differentiate which Button is being touched). That's the easy part.

    The Hard part comes when you discover that the frequency will SIGNIFICANTLY DRIFT with ambient TEMPERATURE and HUMIDITY. This means you have to continually sample and reset your base reference. Then some snotty kid comes along and presses your button with damp hands 'cos he's just been picking his nose, and your base reference swings wildly into the unknown. Enjoy...

    (that said, I'm surprised that there aren't many many more people chomping at the bit to get funky proximity touch sensor switches working on there PICs)
    Some of us have a life... *smiles*... (quite apart from the fact that a tactile PCB switch costs about 5 cents and is reliable. Mount it behind a flexible overlay and all you need is a pull-up Resistor and ONE LINE of code to see if anybody's pressed it or not. It doesn't care if the overlay is dry or wet or somewhere in between, and it doesn't care about the flavour of Ben & Jerry's you want to slap on it!).

  4. #4
    Join Date
    May 2007
    Posts
    604


    Did you find this post helpful? Yes | No

    Default

    I have implemented capacitive touch sensing using a PIC in a handheld terminal project. I needed the ability to sense up to 32 touch pads (and control a 128x64 pixel graphic LCD) and went with a different approach that did not require a PIC with a CSM module or comparators. I used an external oscillator (a CMOS version of the 555 operating at 2MHz) - this approach allows it to be implemented with any PIC (or other processor such as Atmel, MSP, etc. - I used a 44-pin PIC18F44K20 which controls everything). Works real well. Front and rear pics of the prototype below.


  5. #5
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Hello Melanie,

    firstly, thank you as ever for the comprehensive reply (& avoiding an overpowering temptation to pour scorn!)

    Quote Originally Posted by Melanie View Post
    Going back to your first post...

    There is a difference... pin 18 is C12IN0- and pin 15 is C12IN1-

    This is taken from my Datasheet DS41262E (yours might be an extinct version).
    Yes, I had an old datasheet, hey ho!

    Quote Originally Posted by Melanie View Post
    Going back to your first post...

    You asked about RC4. Whether you consider a Comparator OUTPUT as analog I suppose is up to you. I consider Comparators INPUT as ANALOG but technically their OUTPUT is DIGITAL since it can only be a High or a Low. RC4 is the OUTPUT for Comparator 2. If you are using Comparator 2, you DON'T want to set that pin to ANALOG (because ANALOG refers really only to INPUTS), but you want DIGITAL OUTPUT.
    I have to say, this confuses me - as an old analoguey dinosaur (desperately trying to find his feet in the whacky world of digital), a comparator is analogue, but thinking about it more, an SR latch is digital. But that said, I've tried several permutations with digital and analogue pins! I'll revisit this in the light of your reply.

    Quote Originally Posted by Melanie View Post
    Look at figures 8.2 & 8.3 in the Comparator Section. They show you the Comparator INPUT arrangement. For your convenience CxVin- has four possible sources.

    The 16F690 does NOT have a CSM Module.
    Yes, this is one bit I understood...but nevertheless it does have a Comparator SR latch relaxation oscillator possibility ...which ought to work!

    Quote Originally Posted by Melanie View Post
    This means that if you want to play with capacitive sensing you have to configure the Comparators as an Oscillator yourself, the frequency of Oscillation will change when you place your Dabs on the Sensing Terminal. It is up to you to detect the change in frequency. So really any PIC with Comparators can do that. It's nice that the 16F690 has a quad input to each Comparator, which means that each Comparator can potentially service up to four capacitive buttons (NOT like your schematic in the first post which cannot differentiate which Button is being touched). That's the easy part.
    yes that part of the schematic was wrong (and was pretty much lifted from Microchip's application note .... http://ww1.microchip.com/downloads/e...tes/01101a.pdf Appendix B - it's wrong!)

    Quote Originally Posted by Melanie View Post
    The Hard part comes when you discover that the frequency will SIGNIFICANTLY DRIFT with ambient TEMPERATURE and HUMIDITY. This means you have to continually sample and reset your base reference. Then some snotty kid comes along and presses your button with damp hands 'cos he's just been picking his nose, and your base reference swings wildly into the unknown. Enjoy...
    yes I know about the drift....but if you believe the hype on the microchip site, it can be accounted for in code (I should have something ready for this aspect in a few years)

    Quote Originally Posted by Melanie View Post
    Some of us have a life... *smiles*... (quite apart from the fact that a tactile PCB switch costs about 5 cents and is reliable. Mount it behind a flexible overlay and all you need is a pull-up Resistor and ONE LINE of code to see if anybody's pressed it or not. It doesn't care if the overlay is dry or wet or somewhere in between, and it doesn't care about the flavour of Ben & Jerry's you want to slap on it!).
    ......and some of us have guitars, the real estate of which is more sacrosanct than the Turin shroud! :-) believe me, if I could justify a h/w switch I would just use that ...but taking a drill to my vintage guitar would be like violation. (far better to have a capacitive touch sensor located somewhere under the scratchplate)

    Thanks for your help though ...it really is appreciated . :-)
    Last edited by HankMcSpank; - 22nd November 2009 at 16:21.

  6. #6
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    If your Guitar is so sacrosanct, then why even consider sticking stuff to it? Leave it the way the manufacturer intended it to sound and why not simply build a bigger custom wah-wah pedal box with a heap more buttons and functions for controlling your effects?

  7. #7
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    Ah... I see what I wrote with regard to DIGITAL OUTPUT... what I meant was that the COMPARATOR gives you the Digital Output. You NEED to select C1OE and C1ON (C2OE & C2ON) bits to 1 so the Comparator gives you a physical Hardware Output (rather than just setting a bit in a Register internally within the PIC), and you set the TRIS for that pin to 0 (treat it as DIGITAL OUTPUT - which is what it is).

  8. #8
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    If your Guitar is so sacrosanct, then why even consider sticking stuff to it? Leave it the way the manufacturer intended it to sound and why not simply build a bigger custom wah-wah pedal box with a heap more buttons and functions for controlling your effects?
    Because the end goal here is, is a guitar sustainer, ie infinite sustain (the holy grail for most guitarists) ...there isn't an effect pedal in existence that yields an equal effect (yes, there are guitar sustain pedals, but they're nothing more than glorified compressors/limiters...as the guitar note dies, the compressor pedal merely cranks up the gain to try and counteract, leaving you with a woeful signal to noise ratio & a still dying guitar note)....a proper guitar sustainer, uses an electromagnetic driver to keep the string moving - and that needs to go physically go on the guitar (as it goes this driver will literally be stuck onto the guitar - to save the unpaletteable thought of having to drill the guitar to mount it)

    For those that hold an interest, I made a short video clip where you can hear the end result (it's at a very rough & ready stage here though!) - complete with a breadboarded circuit hosting a 16F690 to one side of the frame (which presently has 5p momentary switches too - those are the puppies I'd like to replace with capacitive sensing switches - I can then use the screws on the scratchplate as on/off switches!)...

    (btw, that ain't no vintage guitar, but my test guitar...no worries if it gets scratched while I mess about mounting/removing driver coils to establish the best one)


    Quote Originally Posted by Melanie View Post
    Ah... I see what I wrote with regard to DIGITAL OUTPUT... what I meant was that the COMPARATOR gives you the Digital Output. You NEED to select C1OE and C1ON (C2OE & C2ON) bits to 1 so the Comparator gives you a physical Hardware Output (rather than just setting a bit in a Register internally within the PIC), and you set the TRIS for that pin to 0 (treat it as DIGITAL OUTPUT - which is what it is).
    I need to sit down in a quiet darkened room & digest that paragraph....but I will!

    rmteo ...thank you for your input, your chosen path - whilst brilliant - is excessive for my meagre switching needs! (I only need three switches & have an unhealthy obsession with keeping the component count low!)
    Last edited by HankMcSpank; - 22nd November 2009 at 18:16.

  9. #9
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Melanie View Post
    Ah... I see what I wrote with regard to DIGITAL OUTPUT... what I meant was that the COMPARATOR gives you the Digital Output. You NEED to select C1OE and C1ON (C2OE & C2ON) bits to 1 so the Comparator gives you a physical Hardware Output (rather than just setting a bit in a Register internally within the PIC), and you set the TRIS for that pin to 0 (treat it as DIGITAL OUTPUT - which is what it is).
    Ok, I think I understand, that's pretty much what their Application Note said (albeit for the 16f877, but doing some x correlation, it seems that most register settings on the 16F690 are very similar)...


    Edit: Bingo! it works now!(phew)

    The problem?

    The darned Pickit2 programmer pin 2 (which goes to pin 18 - ICSPCLK -on the 16F690), was seemingly dragging the whole cct down and stopping it from oscillating - when I removed that leg from my Pickit2 & I get the following...



    (which alters in frequency nicely when I touch my fingers across the 250nf cap I have in place in lieu of the C3 sensor pad/cap in the schematic above)

    For the capactive touch record, here are my register settings...

    Code:
    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 - but not sure if it's in scope here!
    TRISC  = %00000011     'RC0 (VCC/4) as input, RC1 Input (not sure if it's in scope either), RC4 output (C2OUT)
    Thanks for chiming in, your input made me revisit this one last time (I was flagging!) ....that's the simple bit over with (which has taken me two nights!), now the hard stuff - getting this all to work!
    Last edited by HankMcSpank; - 22nd November 2009 at 20:26.

  10. #10
    Join Date
    Jan 2009
    Location
    Miami, Florida USA
    Posts
    644


    Did you find this post helpful? Yes | No

    Default

    Hi HankMcSpank,

    Here's their simple schematic here (source = http://ww1.microchip.com/downloads/e...tes/01171B.pdf) ...
    The application note that you refered to in your first post is about a built-in capacitive sensing module in the PIC16F72x series. You are using something else, the 16F690. The capacitive sensors can be done with the 16F690, but that is the hard way. With the PIC16F72x series it is much simpler. You don't need comparators or any external components. I have built some of these capacitive switches for some of my projects and they work like a charm.

    Check out in this forum some posts by Byte_Butcher and myself about this touchsensors. We have already struggled to make these chips work but finally we got it.

    Just as a final note, by using an oscilloscope to see the signal changes you are adding a lot of capacitance to the line and this is no good when detecting capacitance from your fingers.

    Robert

  11. #11
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by rsocor01 View Post
    Hi HankMcSpank,



    The application note that you refered to in your first post is about a built-in capacitive sensing module in the PIC16F72x series. You are using something else, the 16F690. The capacitive sensors can be done with the 16F690, but that is the hard way. With the PIC16F72x series it is much simpler. You don't need comparators or any external components. I have built some of these capacitive switches for some of my projects and they work like a charm.

    Check out in this forum some posts by Byte_Butcher and myself about this touchsensors. We have already struggled to make these chips work but finally we got it.

    Just as a final note, by using an oscilloscope to see the signal changes you are adding a lot of capacitance to the line and this is no good when detecting capacitance from your fingers.

    Robert
    All good points well made...I did get a bit carried away with my linkage above, but I'm basing most of this 16F690 stuff on http://ww1.microchip.com/downloads/e...tes/01101a.pdf ...which is using the same method I'm targetting here comparator+SR latch vs the capactive sense module). I do have a PIC16F726 winging it's way to me...but the extra piN count (ie bigger size), is not that welcome!

    Good point about the scope though...since we're talking picofarads, you're quite right...it's going to have a major impact on the results (but allow me to wallow for just a few minutes & enjoy those oscillations onscreen ...it's taken me two nights to see 'em!)

    Re byte_butcher's post - yep, it's a good 'un (tks Msr Butcher!), albeit he uses the CSM of the 16f72x series.

    I've actually already stumbled at the next hurdle of this embryonic second stage ...ie where I'm presently parachuting in bits of byte_butchers interupt code into mine. (plagiarism is alive and well)....

    I saw DT's reply on that thread ( http://www.picbasic.co.uk/forum/show...00&postcount=5) & have modified the DTS-INTS-14.Bas file in accordance with his instructions (& the other bits & bobs), but when I come to compile I get a whole heap of erroRs along the lines of TMR1GIF Symbol not previously defined in the ASM file....????

    I'm well out my depth here...so now off to get a scuba tank.
    Last edited by HankMcSpank; - 22nd November 2009 at 21:02.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by HankMcSpank View Post
    I saw DT's reply on that thread ( http://www.picbasic.co.uk/forum/show...00&postcount=5) & have modified the DTS-INTS-14.Bas file in accordance with his instructions (& the other bits & bobs), but when I come to compile I get a whole heap of erroRs along the lines of TMR1GIF Symbol not previously defined in the ASM file....????
    The 16F690 does have a Gate control on Timer1.
    But it doesn't have a TMR1GIF bit, and doesn't generate interrupts for the gate.
    <br>
    DT

  13. #13
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    The 16F690 does have a Gate control on Timer1.
    But it doesn't have a TMR1GIF bit, and doesn't generate interrupts for the gate.
    <br>
    Hmm (rubs chin, pretending to understand the significance of what you're saying - but nevertheless knowing full well it's *BAD*). As it goes, I was just about to say, I'm getting this when I compile...

    "Symbol not previously defined (TMR1GIF)" (which may/may not be related to what you're saying, l- modified your files as per your instructions to byte_butcher - I'm out my depth at the moment...but learning fast!)

    From the Microchip application note- http://ww1.microchip.com/downloads/e...tes/01101a.pdf (which embraces this method with the 16F690)...

    "Once the oscillator is constructed, its frequency must
    be monitored to detect the drop in frequency caused by
    a finger press. Figure 5 shows a more complete schematic
    where C2OUT not only drives the oscillator, but
    also is fed into the clock input of Timer1, T1CKI. Each
    time C2OUT changes from ‘0’ to ‘1’ Timer1 will increment.
    Unhindered, Timer1 will increment non-stop and
    eventually roll over, but this is not useful for capacitive
    sensing.
    To make it useful, a fixed time base is used to measure
    the frequency over a defined period. Timer0 provides
    this fixed period time base. At the start of a measurement,
    Timer0 is cleared, and it will count up to 255 and
    then it will overflow. On overflow, the Timer0 interrupt,
    T0IF, causes the program to vector to the Interrupt Service
    Routine.
    "



    As a beginner, could you tell me the significance of your news...

    1. I can't use your interupt routines?
    2. Their proposed method wont work? even though from the same datasheet, they say this ...

    "There are currently three families of PIC microcontrollers
    able to use the method as shown. These are the
    Microchip PIC16F616 family, PIC16F690 family and
    PIC16F887 family."


    Grateful for the clarification!
    Last edited by HankMcSpank; - 22nd November 2009 at 22:19.

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


    Did you find this post helpful? Yes | No

    Default

    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>
    DT

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

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

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

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

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

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

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

  22. #22
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default

    Not a drill in sight ...


  23. #23
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default

    Hey, that look great! Congrats!

    Ioannis

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

  25. #25
    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 : 3

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