ero, Martin, or anybody
I've been trying to do this with a PIC16F819, with only mediocre results. My 819 has a 10 bit A2D, while I see that you are trying it with a 8 bit A2D, so theoretically, your results should be even worse than mine, if that's possible. so, I'm interested in what your typical RAW value is, what it drops to with a finger press, and if it stays relatively constant while your finger is near, because mine does not.
I started out not tying any of the A2D inputs directly to VCC, and my program began with the A2D pins TRIS being outputs. It worked, sort of, but the A2D value was not constant while my finger was near the pad. Then, I read Martin's suggestion, tied my 'dummy' A2D channel to VCC, modified the code, and ... same thing.
I've got my PIC16F819 on a olimex 18 pin dev board. I have two copper tape pads, about 1 inch square, covered with electrical tape, on pins 2 and 18.
my debug statement spits out usually 30 for my untouched S2_AVE (which is not really an average) and when i put my finger on the sensor, it jumps (rings) between 1 and 37. :-(
I tried putting 10uS delays all over my sense subroutine, tried a longer or no pause before running the A2D, and I tried messing with the A2D clock source, which now doesn't match the define statement.
oh yeah, I tried to manually control the A2D, but that didn't work. I think I'm pointing to the wrong bits
If anybody is still with me, here is my down-n-dirty, non-averaging, non-optimized, soon to be pitched, code.
I'd love to hear any suggestions.
Code:' PIC BASIC PRO 2.6 & MPLAB v8.40 PIC16F819 ' ================ @ __CONFIG _HS_OSC & _MCLR_OFF & _LVP_OFF & _WDT_OFF & _PWRTE_OFF & _BODEN_OFF DEFINE DEBUG_REG PORTB ' asyncronous RS-232 output on PORTB... DEFINE DEBUG_BIT 7 ' bit 7 DEFINE DEBUG_BAUD 19200 ' 19200 Baud, DEFINE DEBUG_MODE 0 ' 1 = inverted, 0 = true,like for using max232, or pickit2 DEFINE DEBUG_PACING 1000 ' 1mS delay DEFINE ADC_BITS 10 ' Set number of bits in result DEFINE ADC_CLOCK 3 ' Set clock source (3=rc) DEFINE ADC_SAMPLEUS 50 ' Set sampling time in uS DEFINE OSC 20 PORTA = 0 PORTB = 0 TRISA = %00000100 ' dummy channel = PortA.2 = tied to VCC TRISB = %00000100 ' pushbutton input PUSHB var PORTB.2 ' Pushbutton (OLIMEX 18 pin PIC dev board - active low) REDLED var PORTB.3 ' Indicator LED (OLIMEX 18 pin PIC dev board) YELLOWLED var PORTB.5 GREENLED var PORTB.6 S1_AVE var word S2_AVE var word S1value var word S2value var word counter var word ADGO VAR ADCON0.0 ADDONE VAR ADCON0.2 DEBUG REP $00\8,13,10,"Starting Up" ADCON0 = %10010000 '10 = A2D freq FOSC/64 '010 = point to the dummy channel, AN2 '0 = status bit '0 = unused '0 = "ADON", 1 = on ADCON1 = %11000000 '1 = right justified '1 = A/D clock divide by 2 is used '00 = unused '0000 = all analog inputs PAUSE 1000 counter = 0 GOSUB SENSE1 GOSUB SENSE1 GOSUB SENSE1 S1_AVE = S1value GOSUB SENSE2 GOSUB SENSE2 GOSUB SENSE2 S2_AVE = S2value mainloop: GOSUB SENSE1 IF S1value < (S1_AVE-3) THEN GREENLED =1 ELSE GREENLED =0 ENDIF GOSUB SENSE2 IF S2value < (S2_AVE-3) THEN YELLOWLED =1 ELSE YELLOWLED =0 ENDIF IF PUSHB = 0 THEN DEBUG DEC S1_AVE, ", ",DEC S1value,", ", DEC S2_AVE, ", ",DEC S2value,13,10 ENDIF counter = counter +1 IF counter > 3000 THEN TOGGLE REDLED counter = 0 ENDIF GOTO mainloop ' * Microchip AN1298: CVD Touch Sensing using two ADC channels ' * ' * 1. Set 2nd channel to output, set high (VDD) ' * 2. Select 2nd channel on ADC (charges CHOLD to VDD). ' * 3. Set sensor line to output, set low (GND) ' * 4. Select sensor line as input (TRISx = 1) ' * 5. Select sensor channel on ADC (V-div sensor:CHOLD) ' * 6. Begin ADC conversion ' * 7. Reading is in ADRESH:ADRESL SENSE1: 'setup and read the first sensor (PORTA1, AN1, pin 18) 'TRISA.2 = 0 'Set Dummy channel to digital output 'PORTA.2 = 1 ' Dummy channel goes high ADCON0 = %10010000 ' set A/D to dummy channel PAUSEUS 10 ' let it charge PORTA.1 = 0 'Sensor line gets set low TRISA.1 = 0 'Sensor line gets set to digital output TRISA.1 = 1 'Sensor line gets set to digital/analog input ADCON0 = %10001000 'Point A/D to sensor line PAUSEUS 10 'wait for voltage to stabilize ADCIN 1, S1value ' Read channel 1 to variable 'ADGO = 1 'turn on A/D 'WHILE ADDONE = 1 'WEND 'S1value = ADRESL TRISA.1 = 0 'return sense line to be digital output RETURN SENSE2: 'setup and read the 2nd sensor (PORTA3, AN3, pin 2) 'TRISA.2 = 0 'Set Dummy channel to digital output 'PORTA.2 = 1 ' Dummy channel goes high ADCON0 = %10010000 ' set A/D to dummy channel PAUSEUS 10 ' let it charge PORTA.3 = 0 'Sensor line gets set low TRISA.3 = 0 'Sensor line gets set to digital output TRISA.3 = 1 'Sensor line gets set to digital/analog input ADCON0 = %10011000 'Point A/D to sensor line PAUSEUS 10 'wait for voltage to stabilize ADCIN 3, S2value ' Read channel 3 to variable 'ADGO = 1 'turn on A/D 'WHILE ADDONE = 1 'WEND 'S2value = ADRESL TRISA.3 = 0 'return sense line to be digital output RETURN END




Bookmarks