Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Implementation of mTouch on PIC16F1939

Status
Not open for further replies.

pravin b

Member level 5
Joined
May 20, 2012
Messages
85
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
Mumbai, India
Activity points
2,083
Hello Friends,

I have successfully implemented a capacitive switch on PIC16F1939; however to extend it further, I need to determine the time for which the switch is touched/pressed. What logic should I use to determine that? I am using mTouch Framework v2.3.

Waiting for your inputs.

Thank YOu.
 

hi friends,

After using mTouch framework, I am trying my hand on dedicated CPS channels (CPS0-CPS16). I have written the code for same which looks running except some pitfalls as follows.

When I press input X (for example), LED for (X+1) glows. I see, everytime i call ISR kaflg[chindex] value is written at kflag[chindex+1]. please help me to debug this. What I am missing?
Code:
 if(t1count<=threshold)				//if frequncy less than thrshold iey is pressed else not
        {									//Threshold=(no_touch_count-touch_count)/2
            kflag[chindex]=1;				
        }
        else
            kflag[chindex]=0;

thank you.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* 
 * File:   main.c
 * Author: user
 *
 * Created on 11 April, 2016, 3:24 PM
 * Problem: status of kflag is wriiten on n+1 position.
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <htc.h>
#include "uart.h"
 
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & CPD_OFF & BOREN_ON & CLKOUTEN_ON & IESO_OFF & FCMEN_OFF);
__CONFIG(WRT_OFF & VCAPEN_OFF & PLLEN_ON & STVREN_OFF & BORV_HI & LVP_OFF);
 
/*
 * 
 */
#define _XTAL_FREQ 32000000 // <-- Oscillator frequency of your MCU in Hz
#define LED0 PORTCbits.RC0   //output LED
#define LED1 PORTCbits.RC1   //output LED
#define LED2 PORTCbits.RC2   //output LED
#define LED3 PORTCbits.RC3   //output LED
 
#define NO_OF_SENSORS 4
#define ON  1
#define OFF  0
 
void System_Init();
void Init_Timer0();
void Init_Timer1();
void delay_1ms(unsigned int num);          
void interrupt ISR(void);
 
unsigned int t1count,threshold=9,chindex=0;
unsigned char sbuff[20],dlflag,kflag[NO_OF_SENSORS];
 
 
void main()
{
    System_Init();
 
    while(1)
    {
        if(kflag[0]) {LED0=ON;}   else{LED0=OFF;}
        if(kflag[1]) {LED1=ON;}   else{LED1=OFF;}
        if(kflag[2]) {LED2=ON;}   else{LED2=OFF;}
        if(kflag[3]) {LED3=ON;}   else{LED3=OFF;}          
    }   
}
 
void System_Init()
{
    OSCCON  = 0b01110000;               // 32 MHz Fosc w/ PLLEN_ON (config bit)  
    TRISA   = 0b00110000;
    ANSELA  = 0b00110000;               // CPS7, CPS6 initialized as analog inputs
    TRISB   = 0b00111111;
    ANSELB  = 0b00111111;               // CPS0-CPS5 initialized as analog inputs
    TRISD   = 0b11111111;
    ANSELD  = 0b11111111;               // CPS8-CPS15 initialized as analog inputs
    TRISC   = 0b00000000;               //PORTC = OUTPUT
    CPSCON0 = 0b10001100;               // Cap sense on, high range oscillator,
    CPSCON1 = 0b00000000;               // Cap sense channel input 0 is selected
 
    init_UART();
    SendStringSerially("UART_OK ");     //power on msg to check working of UART, BAUDRATE: 57600
    Init_Timer0();
    Init_Timer1();
    GIE=1;
}
 
void Init_Timer0()
{
    OPTION_REG  = 0b11000111;      // fosc/4, hi-lo edge transition,101-1:64 prescaler; max TMR0 delay 2.05ms  
                                   //111-1:256 prescaler; max TMR0 delay 8.19ms
    TMR0=0;
    TMR0IF  = 0;                    // clear TMR0 interrupt flag
    TMR0IE  = 1;                    // enable TMR0 interrupt 
}
 
void Init_Timer1()
{
    T1CON   = 0b11000100;      // Timer1 initialization
    T1GCON  = 0b11100001;      // Timer1 gate init /Toggle Mode/TMR0 time base
    TMR1=0;              //clear timer 1
    TMR1GIF = 0;         // Clear Gate Interrupt Flag
    TMR1ON=1;
}
 
void interrupt ISR(void)
{
    if(TMR0IF)
    {     
       
        t1count=TMR1;           //gives frequency of CPS input
        sprintf(sbuff,"tmr s1= %d ",t1count);
        SendStringSerially(sbuff);          //print this frequency on UART
 
        if(t1count<=threshold)              //if frequncy less than thrshold iey is pressed else not
        {                                   //Threshold=(no_touch_count-touch_count)/2
            kflag[chindex]=1;               
        }
        else
            kflag[chindex]=0;
       
        TMR1=0;                                     
        TMR0=0xFF;                          //
        while(T1GVAL);                      //To make FF toggle input low       
        TMR0IF=0; 
        TMR0=0;
              
        chindex++;
        if(chindex>=NO_OF_SENSORS)
            chindex=0;
        chindex = chindex & 0xff;
        CPSCON1 = chindex;               
        
//        sprintf(sbuff,"(%d %d %d)",kflag[0],kflag[1],kflag[2]); // for debugging
//        SendStringSerially(sbuff);
    }
    
}
 
void delay_1ms(unsigned int num)        //1ms software delay routine          
{
    unsigned int i,j;
    for(i=1;i<=num;i++)
        for(j=0;j<=568;j++);
    dlflag=1;
 }



I am attaching source file "main.c" for your reference.
 
Last edited by a moderator:

Hello friends with my capacitive sensing application using PIC16F1939; Everything is working fine except; when I measure pulses with timer1 I get count near about "15" when electrode is not touched, where as near about "3" when electrode touched.
it is OK when I need to activate the output by touching the electrode directly, but my "TOUCH" is not detected when I "TOUCH" electrode with dielectric material (like acrylic, glass sheet; say of 5mm) between electrode & finger. What changes or modification or adoption shall I do to achieve this? Any further reading, suggestions are welcome.

PS: I have mounted circuit on breadboard with open ended single stranded wire as an electrode.

Thanks & Regards,
Pravin B
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top