A/D on 16F767 for light tracking.
Hey folks, I have some code here that doesn't seem to want to work. I think i got some of it from some example somewhere, which i've looked for again but can't seem to find, so if you wrote it, thank you and i hope you don't mind if i borrow it! anyway, Im using this for a light following robot and this is just part of the light tracking routine. the code is large so i won't post everything here, just the part that doesn't seem to run. I've isolated it so that just this bit of code and the motion control routine, which i know works. I'm using TAOS TSL14S Light to Voltage converters for my light sensors, these work pretty much like a LDR in a divider network without the extra room needed for resistors: i.e. just a VDD, VSS and output voltage. they do work according to my scope. I've also taken them out of circuit and pulled one A/D port up and the other down with resistors to know that i should have enough unbalance to get a result if everything is working as it should..
ALSO, i dont know if i like this code, it is either full left, or full right, I'd like a little center margin, like fuzzy logic, so if there is a small difference between the two sensors it'll still track straight, and only when it goes over a threshold, one way or the other will it go in that direction. but I'm having trouble thinking that through.. i've got a pretty busy life and can't spend as much time thinking about PBP code as i'd like!
so here is what I have, there might be some settings that are duplicated, or contradict each other, so feel free to point them out or point and laugh!
@ DEVICE PIC16F767, WDT_ON, INTRC_OSC_NOCLKOUT, MCLR_ON, PROTECT_OFF, PWRT_ON
OSCCON.4 = 1 ' SET INT OSC TO 8 MHZ
OSCCON.5 = 1 ' }
OSCCON.6 = 1 ' SET INT OSC TO 8 MHZ
DEFINE OSC 8 ' DEFINE 0SC AT 8 MHZ FOR PBP
CMCON = 7
' -------------------------------------------------------------
' A/D CONVERSION SETUP
ADCON1= %01001100
DEFINE ADC_BITS 10 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 5 ' ADC clock source (Fosc/16)
DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)
TRISA.0 = 1 ' SET TO INPUT FOR LEFT TLS SENSOR
TRISA.1 = 1 ' SET TO INPUT FOR RIGHT TLS SENSOR
' -------------------------------------------------------------
LIGHTSEEK:
ADCON0 = %01000000 ' SET FOSC/16, SET ADC CHANNEL 0, ENABLE ADC MODULE
PAUSEUS ' 50uS PAUSE TO ALLOW SAMPLING CAPACITOR TO CHARGE
ADCON0.2 = 1 ' START CONVERSION
WHILE ADCON0.1 = 1:WEND ' WAIT FOR CONVERSION TO COMPLETE
LFT_TLS.HIGHBYTE = ADRESH ' WRITE TO HIGHBYTE OF LFT_TLS
LFT_TLS.LOWBYTE = ADRESL ' WRITE TO LOWBYTE OF LFT_TLS
PAUSEUS 100 ' PAUSE 100uS BETWEEN CONVERSIONS
ADCON0 = %01001000 ' SET FOSC/16, SET ADC CHANNEL 0, ENABLE ADC MODULE
PAUSEUS 50 ' 50uS PAUSE TO ALLOW SAMPLING CAPACITOR TO CHARGE
ADCON0.2 = 1 ' START CONVERSION
WHILE ADCON0.1 = 1:WEND ' WAIT FOR CONVERSION TO COMPLETE
RHT_TLS.HIGHBYTE = ADRESH ' WRITE TO HIGHBYTE OF RHT_TLS
RHT_TLS.LOWBYTE = ADRESL ' WRITE TO LOWBYTE OF RHT_TLS
PAUSEUS 50
IF LFT_TLS > RHT_TLS THEN FWDLFT_TRN: ' IF MORE LIGHT TO LEFT, TURN LEFT
IF RHT_TLS > LFT_TLS THEN FWDRHT_TRN: ' IF MORE LIGHT TO RIGHT, TURN RIGHT
GOSUB FORWARD: ' GOTO FORWARD SUBROUTINE
RETURN