...Am wondering if the code I am using is at fault?? won't be the first time.
Trust me. You don't have to wonder about that. It goes without saying.
I must say, the Swordfish documentation and example files are the worst. They don't explain why they are doing what they do and they are not complete.
I suggest you read and understand the following. Barring that, the ONLY things you need in your code as far as this switch ladder is concerned is in blue below. Copy and paste NOTHING ELSE.
First,
INCLUDE ADC.BAS. This is the module that handles the details for you.
Next, forget about the examples you have read. You do not want to convert the readings to volts. This is needless math and it's just wrong because you're operating from 3-something volts, so using Vdd as a reference where you are assuming it's 5 volts makes the numbers wrong anyway. You want the
raw ADC output, which is a 10 bit number meaning it can range between 0 - 1023. A reading of 1023 means the level you are measuring is the same as Vdd, a reading of 512 means the level is 50% of Vdd, etc. We don't know what Vdd from your battery is and
we don't care what the actual voltage is. The only thing we care about is the ratio of the measured level compared to full scale.
From the Swordfish help file, next we need:
// initialise <-------- this goes at the start of the program code, after subroutines
TRISA.0 = 1 // configure AN0 as an input
ADCON1.7 = 1 // set analogue input on PORTA.0
delayms (500)
This function is in the ADC module, and is what we will use to read the ADC.
function Read(pChannel
as byte)
as word <---------- This is in the ADC module. Don't put this in your code any place. You need to look at it to understand what you are doing.
ADResult as word <---------- This is in the ADC module. Don't put this in your code any place. You need to look at it to understand what you are doing.
After making a call to Read, ADResult will hold the last ADC sample value.
So your brain freezes up at pChannel. It's just the ADC channel to read.
When you want to read the ADC value, this is what you use:
Read(AN0) <------------- this reads the ADC
Now to do something with it. The value you read is in ADResult.
Select ADResult
Case <93
(this has all been explained in other posts. Figure it out from here)