CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

pic 16F1937 capacitive pad

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
enyman



Joined: 09 Feb 2012
Posts: 8

View user's profile Send private message Yahoo Messenger

pic 16F1937 capacitive pad
PostPosted: Fri Mar 09, 2012 6:52 pm     Reply with quote

hi,
I'm using pic 16f1937 with a touchpad and I'm trying to make a LED (vorbiti) to light while I'm with the finger on the button and it's not working.

Code:

#include <16f1937.h>
#use delay(clock=500KHz)
                                 
#use touchpad (RANGE=L,THRESHOLD=6,SCANTIME=32,PIN_A5 ='1',pin_a4='2',pin_d0='3',pin_b0='4')

#define vorbiti pin_c4     
#define acces pin_c2           
#define mute pin_a3                                     
#define vol_H pin_a0                                       
#define vol_M pin_b7
#define vol_L pin_b6

void main()
{                               
TOUCHPAD_STATE(1);   
enable_interrupts(GLOBAL);
                                     
char c;           
int volum;         
volum=3;

while (TRUE)           
         {
 c='X';           
                                                         
           if (touchpad_hit())
           c=TOUCHPAD_GETC();           
                           
              switch(c) {
                case '1':  switch(volum)
                                   {                           
                                    case 3: output_low(vol_H);volum=2;break;   
                                    case 2: output_low(vol_M);volum=1;break;   
                                    case 1: output_low(vol_L);output_high(mute);volum=0;break;
                                    }   break;
               
                case '2':  switch (volum) 
                                    {               
                                     case 0: output_low(mute);output_high(vol_L);volum=1;break;           
                                     case 1: output_high(vol_M);volum=2;break;                   
                                     case 2: output_high(vol_H);volum=3;break;   
                                     }  break;               
                case '3':  access();break;
                case '4':                                                                             
                           while(c=='4')
                          {
                          output_high(vorbiti);
                          delay_ms(100);
                          c='Y';
                          if(touchpad_hit())
                          c=TOUCHPAD_GETC();
                          }output_low(vorbiti);
         }
}
}


With pushbutton it's simple :
Code:

while(input(PIN_X))
{
output_high(PIN_Y);
}

But with touchpad it's not working.
I am using PCWHD 4.126.
Please help.
Thank you.
enyman



Joined: 09 Feb 2012
Posts: 8

View user's profile Send private message Yahoo Messenger

PostPosted: Wed Mar 14, 2012 7:58 am     Reply with quote

can someone help me ? please ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 14, 2012 1:19 pm     Reply with quote

1. Post your #fuses line. You need one.

2. Your code doesn't compile. It gives an Undefined identifier error for:
Code:

access()


3. You are declaring variables in the middle of code:
Quote:

void main()
{
TOUCHPAD_STATE(1);
enable_interrupts(GLOBAL);

char c;
int volum;

volum=3;

This is not supported by CCS. It can produce strange bugs if you do it.
Variables must be declared at the start of a function. This includes main().


4. Pins B6 and B7 are used by the ICD programmer/debugger to talk to
the PIC. Choose some other pins for your project.
Quote:
#define vorbiti pin_c4
#define acces pin_c2
#define mute pin_a3
#define vol_H pin_a0
#define vol_M pin_b7
#define vol_L pin_b6
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Mar 14, 2012 2:23 pm     Reply with quote

A few more remarks:
5)
Code:
#use delay(clock=500KHz)
Is your CPU really running a 500kHz clock speed? It is very slow and not a standard crystal frequency.

6) The C programming language gives you a lot of freedom when it comes to the layout of your program, but freedom comes with responsibilities.
A) Your code has very wild indentation, sometimes lines are starting at the first column and then without reason jumping to the middle of the page. Your code will be easier to read when you only add an extra level of indentation after every '{' character.

B) The original C language is case sensitive, meaning that a command or variable is only understood when spelled exactly the same in the whole program. The CCS compiler is easier on this rule, unless you specify the #case directive. For easier reading of your program I recommend you to be consequent and stick to the original C rule. Write the function names in lower capitals as specified in the CCS manual (touchpad_getc, touchpad_hit), write constants and defines in full capitals (PIN_A4, PIN_D0, VOL_H, VOL_M, VORBITI, MUTE, etc.).

C) Only one command at a line! The compiler doesn't complain, but it makes reading a program so much more difficult.
Code:
Not recommended:
                                     case 0: output_low(mute);output_high(vol_L);volum=1;break;           
                                     case 1: output_high(vol_M);volum=2;break;
Suggestion:
                                     case 0:
                                         output_low(mute);
                                         output_high(vol_L);
                                         volum=1;
                                         break;           
                                     case 1:
                                         output_high(vol_M);
                                         volum=2;
                                         break;

D) When using an if-statement where you put the corresponding command at the next line, at least add an extra level of indentation to make clear the command belongs to the if-statement.
Code:
wrong:
                          if(touchpad_hit())
                          c=TOUCHPAD_GETC();
Good:
                          if(touchpad_hit())
                              c=TOUCHPAD_GETC();
Best:
                          if(touchpad_hit())
                          {
                              c=TOUCHPAD_GETC();
                          }


7)
Code:
 c='X';           
                                                         
           if (touchpad_hit())
           c=TOUCHPAD_GETC();

              switch(c) {
An unusual code construct. First I thought you made a mistake, but the construct is there in your code two times. As you are not doing anything with the value 'X' inside the switch statement this means you are wasting processing power on running the switch statement when you know beforehand that this is unnecessary. Suggestion: read up on the 'else' command in the if-statement.
enyman



Joined: 09 Feb 2012
Posts: 8

View user's profile Send private message Yahoo Messenger

PostPosted: Mon Mar 19, 2012 8:30 am     Reply with quote

Thank you very much for reply.
I corrected the mistakes listed by you.

The access() procedure i forgot to write in the code above but.
Code:

void access()
{
output_high(acces);
delay_ms(2000);
output_low(acces)
}

I have some fuses set in the .h file.

BUT I have the same problem, the led doesn't stay ON while I'm with the finger on the capacitive button! Why ? How can I do that ?

Thank you very much.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Mar 19, 2012 3:14 pm     Reply with quote

I tested the program shown below with vs. 4.126 and it does sort of work.
I don't have the best touchpad. I used this one from Twin Industries:
http://twinind.com/catalog_detail.php?id=236
I just used the touchpad board on the left side of the photo, and cable.
The pads are smaller than normal, and split into two pieces, which is
not desirable. The "common" side pad is not used, only the other half
of each pad is used. My PIC board is the PicDem2-Plus (non-Rohs vs.).

I couldn't find a cheap stand-alone touchpad board with full size pads
for development purposes. I was looking for something like the Sparkfun
keypad board, except without any IC's on it. Just a bare board with each
pad going to a connector pin. http://www.sparkfun.com/products/10250
But I couldn't find one, so I used the Twin Industries board.

Here are my connections:
Pad 1 --> pin D4
Pad 2 --> pin D5

That's all. It doesn't work if you connect the common pin on the touchpad
to ground on the PIC board. (That's expected. Microchip says only
the pads should go to the PIC pins. No other connections).

Anyway, the code below will turn on the LED if I tap Pad 1 quickly.
If I then tap Pad 2 quickly, it will usually turn off the LED. Sometimes
it takes two taps to turn it off. I put Scotch tape over the pads because
a Touchpad is supposed to be insulated. I also turned the board upside
down and touched the pad locations on the back side. It also worked
that way, which is expected. The board is 1/16 inch (1.575 mm) thick
of FR4 printed circuit fiberglass material.

It seems to work a little bit better with a scantime of 64 ms compared
to 32 ms. The threshold is very important. I could only make it work
with a threshold of 6 or 7. Using a threshold 5 or 10 didn't work at all.

One of the most important things to make it work was to make sure
that the PIC pins are only connected to the Touchpad board. I had to
study the schematic of my PicDem2-Plus (non-Rohs vs.) to find some
pins that didn't have external circuits on them, and that also were CPS
pins on the 16F1937. On my board, pins D4 and D5 were free.
I want to make this very clear: You can't have any pull-up resistors
or other components or IC's on the touchpad pins.

I also tested it with vs. 4.130 and got the same results.
Code:

#include <16F1937.h>
#fuses INTRC_IO,NOWDT,BROWNOUT,PUT
#use delay(clock=4M)
#use touchpad(scantime=64ms, threshold=7, Range=H, PIN_D4='1', PIN_D5='2')

#define LED_PIN  PIN_B0

//===========================
void main()
{
int8 c;

output_low(LED_PIN);   // LED Off

enable_interrupts(GLOBAL); // Required by #use touchpad() documenation
 
while(1)
  {
   c = touchpad_getc();   // Wait for pad 1 or 2 to be touched

   if(c == '1')
      output_high(LED_PIN);  // On

   if(c == '2')
      output_low(LED_PIN);   // Off


   delay_ms(10);
  }

}


The 10ms delay is in there just because I was playing around with
methods to make it detect the touches (finger taps on the pads) more
reliably. I think the delay helps a little.
octopuss83



Joined: 06 Nov 2011
Posts: 13

View user's profile Send private message

Same problem
PostPosted: Thu Mar 22, 2012 1:23 am     Reply with quote

I have the same problem as you, CCS routines are not adapted to the detection of the moment or the key is down. For me I opted for the rewrite of a routine in accordance with the application note AN1103 from Microchip.

You can find the code here:
http://ww1.microchip.com/downloads/en/AppNotes/AN1171_revB.zip

and the note here
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en534210

In my case I'm using the PIC16F1825. I had to modify the program especially at the records of micro registers:
Code:

#define L_Sound   PIN_A1
#define L_Light   PIN_A0
#define S_Touch   PIN_C1
#define L_Touch   PIN_C0

//    Declarations registres

#byte ANSELC = 0x18E   
#byte OPTION_REG = 0x95

#byte INTCON = 0x0B
#bit  TMR0IF  = INTCON.2
#bit  TMR0IE  = INTCON.5
#bit  PEIE    = INTCON.6
#bit  GIE     = INTCON.7

#byte T1CON   = 0x18
#bit  TMR1ON  = T1CON.0
#byte TMR1L   = 0x16
#byte TMR1H   = 0x17

#byte T1GCON  = 0x19
#byte CPSCON0 = 0x1E
#byte CPSCON1 = 0x1F

#bit  CPSOUT = CPSCON0.1

#byte PIR1    = 0x11
#bit  TMR1GIF = PIR1.7
#bit  TMR2IF  = PIR1.1

#byte PIE1    = 0x91
#bit  TMR1GIE = PIE1.7
#bit  TMR2IE  = PIE1.1

//     Definition des channel cap^sensor

#define CPS0   0
#define CPS1   1
#define CPS2   2
#define CPS3   3
#define CPS4   4
#define CPS5   5
#define CPS6   6
#define CPS7   7


To date the program was not working but I'm working on it from time to time.
_________________
______________________

-- Octopuss ---
octopuss83



Joined: 06 Nov 2011
Posts: 13

View user's profile Send private message

Design of cap sense
PostPosted: Thu Mar 22, 2012 1:37 am     Reply with quote

You can also see the AN1102 and AN1101 for layout of capsense

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en531112

http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=1824&appnote=en531109
_________________
______________________

-- Octopuss ---
octopuss83



Joined: 06 Nov 2011
Posts: 13

View user's profile Send private message

capacitive pad
PostPosted: Sat Mar 24, 2012 4:27 am     Reply with quote

Now it works, I've corrected option_reg error in an1171.
Contact me if you want source code.
_________________
______________________

-- Octopuss ---
JORATORCA



Joined: 30 May 2012
Posts: 1
Location: VALENCIA (SPAIN)

View user's profile Send private message

PostPosted: Wed May 30, 2012 3:10 pm     Reply with quote

Octopuss,
I have some time working on a code for a capacitive button with the 12F1822. The code was originally developed for the 16F727 and worked well. In trying to adapt the configuration to the 12F1822 can not get it to work. The PIC is of the same family as the 16F1825 that you have used. I think your code could guide me on what I'm doing wrong. Could you give me your code?.
octopuss83



Joined: 06 Nov 2011
Posts: 13

View user's profile Send private message

PostPosted: Thu May 31, 2012 11:14 am     Reply with quote

Ok, can I have your mail, I can send to you .C .h Files.
_________________
______________________

-- Octopuss ---
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group