Hot!Using CPS with Noise Detection on a PIC16F1825

Author
kart
Starting Member
  • Total Posts : 33
  • Reward points : 0
  • Joined: 2016/12/01 07:55:28
  • Location: 0
  • Status: offline
2017/10/06 09:17:49 (permalink)
0

Using CPS with Noise Detection on a PIC16F1825

Dear all,
 
I just try to implement some form of touch detection via our 8 BIT pic (this is the production Hardware now). We want to attach the metal parts of a LED lamp to the PIC and detect touching for switching the lamp on and off.
 
We are using RA4/CPS3 as Input and Timer0 shall be driven by CPS.
 
The opiton "Noise detection" sounds promising, nevertheless reading the short description gives me the Impression that the whole technical implementation will Change by using this Option ... but I do get no Information how to work with this Option.
 
Can you please Review the very short Setup Code below? Basically whenever I read a Overflow Interrupt from Timer0 I compare with a running Counter to find out if the freuquency has changed.
 
Any Feedback is highly appreciated
 
thanks
Michael
// PIC16F1825 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON        // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
 
*/

#include <xc.h>
#include <stdio.h>
void main(void) {
   
    int counter = 0;
    int reference = 100;
   
    TRISAbits.TRISA4 =1; //set RA4 to input
    ANSELAbits.ANSA4 =1; // set RA4 to analog
   
    CPSCON1bits.CPSCH = 0b0011;  // select CPS3
   
 
    CPSCON0bits.CPSRNG =0b00; //     Noise Detection
    CPSCON0bits.CPSRM = 1;  // variable voltage reference
    CPSCON0bits.T0XCS = 1; // Timer0 is used
    CPSCON0bits.CPSON =1; //enable       
   
    OPTION_REGbits.TMR0CS =0;
    
   
    
    
    while (1) {    
       
        counter++;
       
        if (INTCONbits.TMR0IF == 1) // Overflow Timer 0
            {      
            // Compare counter with reference
            counter = 0; // reset Counter after comparrision between Counter and reference
            INTCONbits.TMR0IF = 0;  // Reset timer 0
        }
      
    }    
#1

4 Replies Related Threads

    DarioG
    leaving this planet
    • Total Posts : 53071
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: porcodioland
    • Status: offline
    Re: Using CPS with Noise Detection on a PIC16F1825 2017/10/07 07:48:37 (permalink)

    Dario Greggio
    --
    how does it feel, never having been loved? as life fades away, day after day, on this planet made of s h i t...

    #2
    kart
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2016/12/01 07:55:28
    • Location: 0
    • Status: offline
    Re: Using CPS with Noise Detection on a PIC16F1825 2017/10/07 10:39:31 (permalink)
    +1 (1)
    Dear Dario (or others),
     
    Reading the AN1101 the concept of capcity sensing seems quite understandable, but I have a problem with the data sheet for my PIC16F1825 as different sections seem to missmatch:
     
    CPSCON0 Register
    T0XCS: Timer0 External Clock Source Select bit
    If TMR0CS = 1:
    The T0XCS bit controls which clock external to the core/Timer0 module supplies Timer0:
    1 = Timer0 clock source is the capacitive sensing oscillator
    0 = Timer0 clock source is the T0CKI pin
    If TMR0CS = 0:
    Timer0 clock source is controlled by the core/Timer0 module and is FOSC/4
     
    But I would prefere using Timer1 as suggested in AN1101.  But one page before the section above I read
     
    27.6.2 TIMER1
    To select Timer1 as the timer resource for the CPS
    module, set the TMR1CS<1:0> of the T1CON register
    to ‘11’. When Timer1 is chosen as the timer resource,
    the capacitive sensing oscillator will be the clock
    source for Timer1. Because the Timer1 module has a
    gate control, developing a time base for the frequency
    measurement can be simplified by using the Timer0
    overflow flag
     
    How do this two sections fit togehter? What value do I have to enter for TMROCS is I am using TMR1 ... this option is not listed!
     
    thanks
    Michael
     
    #3
    kart
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2016/12/01 07:55:28
    • Location: 0
    • Status: offline
    Re: Using CPS with Noise Detection on a PIC16F1825 2017/10/07 11:21:26 (permalink)
    0
     
    I have now addaptey my Code with respect to what I read in AN1101 and in the up datasheet. I want to use TMR0 as my refrerence Counter running on internal clock and TMR1 as timer driven by my CPS module.
     
    The big question is, what do I have to set for T0XCS if I am using TMR1 and not TMR0?!
     
    Can you please give me a hint for the Setting and if my Setup for TMR0 and TMR1 is complete?
     
    thanks
    Michael

    #include <xc.h>
    #include <stdio.h>
    void main(void) {
       
        unsigned int average = 500;
        unsigned int raw = 0;
        unsigned int trip = 100;
        
       
        TRISAbits.TRISA4 =1; //set RA4 to input
        ANSELAbits.ANSA4 =0; // set RA4 to analog
       
        CPSCON1bits.CPSCH = 0b0011;  // select CPS3
       
     
        CPSCON0bits.CPSRNG =0b11; //  Oscillatior in high range
        CPSCON0bits.CPSRM = 0;  // fixed voltage reference
        CPSCON0bits.CPSOUT = 0; //current is flowing into pin
        CPSCON0bits.T0XCS = 0; // ????????????
        CPSCON0bits.CPSON =1;  //enable CPS    
       
        OPTION_REGbits.TMR0CS =0;  // FOSC/4
       
        T1CONbits.TMR1CS = 0b11; //Activate timer 1 for CPM
        
        while (1) {     
                
            if (INTCONbits.TMR0IF == 1) // Overflow Timer 0
                {      
                // Compare counter with reference
                raw=TMR1;
               
                if (raw < (average - trip)) {} //button pressed
                else {}; //not pressed
               
                TMR0=0;
                TMR1=0;
                INTCONbits.TMR0IF = 0;  // Reset timer 0
            }
          
        }       
               
       
    }
    post edited by kart - 2017/10/07 11:24:27
    #4
    kart
    Starting Member
    • Total Posts : 33
    • Reward points : 0
    • Joined: 2016/12/01 07:55:28
    • Location: 0
    • Status: offline
    Re: Using CPS with Noise Detection on a PIC16F1825 2017/10/09 05:34:07 (permalink)
    0
    Dear all,
     
    Actually I am encountering the reverse result than expected! When Debugging the solution I get the result that TMR1 Counts HIGHTER?! when I touch the pin instead of lower (Looks like the CP chooses TMR0 instead of TMR1). Can you please let me know what I have to do so that TMR1 is used for CP.
     
     TRISAbits.TRISA4 =1; //set RA4 to input
        ANSELAbits.ANSA4 =0; // set RA4 to analog
        //TRISCbits.TRISC3 = 0;   // enable output driver for RC3 for PWM
       
        CPSCON1bits.CPSCH = 0b0011;  // select CPS3
       
     
        CPSCON0bits.CPSRNG =0b11; //  Oscillatior in high range
        CPSCON0bits.CPSRM = 0;  // fixed voltage reference
        CPSCON0bits.CPSOUT = 0; //current is flowing into pin
        CPSCON0bits.T0XCS = 1; // ????????????
        CPSCON0bits.CPSON =1;  //enable CPS    
       
        OPTION_REGbits.TMR0CS =0;  // FOSC/4
       
        T1CONbits.TMR1CS = 0b11; //Activate timer 1 for CPM
        T1CONbits.TMR1ON = 1; //timer on
     
    thanks
    Michael
    #5
    Jump to:
    © 2017 APG vNext Commercial Version 4.5