|
|
View previous topic :: View next topic |
Author |
Message |
gmtandi
Joined: 22 Apr 2013 Posts: 10
|
CVD touch - ANx Read, 16F1527 |
Posted: Thu May 02, 2013 10:47 am |
|
|
Hello,
I'm trying to build a capacitive touch switch, but found a few issues when reading ADC...
When i set AN0 as sensor and AN1 as reference, it works like a charm, but when i set AN1 as sensor and AN0 as reference, the values get wrong...
AN0 = PIN_A0
AN1 = PIN_A1
My compiler version: 4.130
Clock: internal, 16mhz
Datasheet: http://ww1.microchip.com/downloads/en/DeviceDoc/41458C.pdf
Code: | #include <16F1527.h>
#device adc=16
#FUSES NOWDT //No Watch Dog Timer
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES WDT_SW //No Watch Dog Timer, enabled in Software
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#use delay(clock=16000000)
#define LED1 PIN_E1
#define LED2 PIN_E0
#define LED3 PIN_G0
#define TOUCH_CHANNELS 5
#define TOUCH_TIMEOUT 25
int1 status[TOUCH_CHANNELS];
int8 timeout[TOUCH_CHANNELS];
int16 counter, value;
#INT_TIMER2
void int_timer2_isr(void)
{
for (int i = 0; i < TOUCH_CHANNELS; i++)
{
if (status[i] == 1)
{
timeout[i]++;
if (timeout[i] >= TOUCH_TIMEOUT)
{
status[i] = 0;
}
}
}
}
void main(void)
{
setup_timer_2(T2_DIV_BY_64,511,4);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER2);
counter = 0;
while(true)
{
counter++;
//delay_ms(1); // when printf();
output_high(PIN_A0);
set_adc_channel(sAN0); // (missing delay)
output_low(PIN_A1);
output_float(PIN_A1);
set_adc_channel(sAN1); // (missing delay)
value = read_adc();
if (value> 256)
{
status[1] = 1;
timeout[1] = 0;
}
if (status[0] == 1) { output_high(LED1); } else { output_low(LED1); }
if (status[1] == 1) { output_high(LED2); } else { output_low(LED2); }
if (status[2] == 1) { output_high(LED3); } else { output_low(LED3); }
if (counter == 200) {
//printf("%lu\n", leitura); //
counter = 0;
}
} |
the "value" when A0 is the sensor is almost 0 when no touch, and when A1 is the sensor, it's about 65472
The datasheet doesn't says nothing special for AN0 that is different than AN1, i think that my pads are fine, with the mTouch Demo it works fine, it must to be something with my code on CCS
Thanks, |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu May 02, 2013 11:46 am |
|
|
Your comments say "missing delay". The 16F1527 data sheet says
there should be a 5us delay after changing the A/D channel, before
you read the ADC. Why did you leave off the delay ? |
|
|
gmtandi
Joined: 22 Apr 2013 Posts: 10
|
|
Posted: Thu May 02, 2013 1:08 pm |
|
|
cause the delay was doing nothing when i was using AN0 as sensor and AN1 as reference, its working only with AN0 sensor, any other ADC is geting wrong results, i had already added a few delays and no different result got :\
that's why i added that comment, i had already tried with delays, i had changed the order a few events occurs and also didn't worked..
thanks |
|
|
|
|
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
|