I have a circuit that charges a capacitor and discharges it. The length of the discharge pulse is supposed to be determined by the ADC value from a POT. The pot is wired as a voltage divider and I have tested that the voltage to the pic pin varies nicely from 0V to 5V,
But it seems to only sort of work Huh
I'm using a PIC12F675 which has a 10-bit ADC. I'm just using the ADRESH byte so that it works pretty much like an 8-bit ADC.
As I turn up the voltage, initially the pulse time is still at minimum. When almost at the middle, the time jumps suddenly to a higher value, then again later until maximum at 5V.
So it is as if there's only about 4 possible input values rather than 256. It must be something in my code, either the reading of the ADC, or using the ADC result for making the delay. Please take a look below and tell me if there's anything obvious there..
BCF STATUS,RP0 ; BANK 0 BCF ADCON0, ADFM ; Left justified (clear and use just ADRESH for an 8bit result) BCF ADCON0, VCFG ; Vdd reference BCF ADCON0, CHS1 ; Set AN0 as analogue input BCF ADCON0, CHS0 ; Set AN0 as analogue input BSF STATUS,RP0 ; BANK 1 MOVLW b'00010001' ; Fosc/8 (2us), AN0 is selected for reading MOVWF ANSEL
When the cap is charged, a digital input triggers this routine...
DISCHARGE BSF DISCHARG ; DISCHARGE CALL READPOT CALL WAITPOT CALL READPOT CALL WAITPOT BCF DISCHARG ; END DISCHARGE RETURN
The ADC routine...
READPOT BSF ADCON0, ADON ; Enable AD BSF ADCON0, GO ; Start AD conversion AD BTFSC ADCON0, GO ; TEST if conversion is finished GOTO AD ; Loop until conversion is done MOVF ADRESH, W ; High side byte from AD (8-bit) MOVWF POT ; Stores ns delay value in POT BCF ADCON0, ADON ; Disable AD RETURN
The delay routine...
WAITPOT INCF POT ; make sure not zero WAITPOTL DECFSZ POT,F ; Decrement POT GOTO WAITPOTL ; Loop until 0 RETURN