Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

input and save input

Status
Not open for further replies.
yes I think I do
the 1024 is the 10 bit number the ADC reads.
the 500 is the 5 volts integer value. the + 1 ??
Nope!!
1024 is the span of the digital map, 500 is the span of the analogue map and the + 1 is because you never get to 5.00v..
 
the digital map, isn't that a fancy way of describing the 10 bit number?
the analogue map is the same fancy way of describing the 5 volts?
just my take on the subject
 
If you supply PIC with 5 V and your reference voltage is Vsupply then in case of 10 bit ADC you have 2^10 -1 = 1024 - 1 = 1023 steps for 5 V .
5 V / 1023 = 4.88 mV -> each step of ADC represents 4.88 mV . The resolution of ADC is 4.88 mV .
Therefore x steps of ADC represent x*4.88 mV .
 
If you supply PIC with 5 V and your reference voltage is Vsupply then in case of 10 bit ADC you have 2^10 -1 = 1024 - 1 = 1023 steps for 5 V .
5 V / 1023 = 4.88 mV -> each step of ADC represents 4.88 mV . The resolution of ADC is 4.88 mV .
Therefore x steps of ADC represent x*4.88 mV .
Correct... You do know the maths..

However!!! Your application doesn't care for 5v... To read 10 different voltages, you need the largest value available to you which is 1023... that's 100 ( ish) between values.. All the others are saying is 1024 is twice as big as 500..

I couldn't give a monkey's how you work out your values... All I'm trying to do is help you understand them...

1) MAXADC / 10 = 100....
2) (MAXADC * 500/1024 ) / 10 = 50 ...

I think you will get it right eventually, I'm trying to stop the others keeling over an dying...
 
Thanks
After It was suggested to drop the math, I have dropped the math off the FUNCTION.
 
Is this going to be a 20 page or 40 page train wreck? A month or year long wreck?

Mike.
 
after I figure out why Burts code suggestion to measure voltages(post#249) I will be done with this portion of this project.
Contemplating adding a FUNCTION to burts code suggestion( post #249) seeing how it is measuring voltage but the first suggestion (post#246) works and it is measuring voltage as well. Same basic code. It might be something with the uart output ? Not sure but hacking at it when I get time, which is in short supply getting ready for a trip, trying to finish a kitchen cabinet and helping neighbor remodel his kitchen.
 
Look... the ADC.Read() function returns a number 0-1023. This is what the 10-bit ADC reads.

You can make life more difficult for yourself and add functions to multiply, divide, or transmorph that into any other number you want.

**OR**

You can directly use that 0-1023 number. Almost every example of code in this thread uses the direct ADC reading.
ONLY YOU keep insisting that you need to add another function into the mix.

Burt's code in post #246 and #249 use the direct ADC reading, NOT a converted voltage number.

Do you realize that if you add a function to change the ADC reading into a voltage then you have to recompute and change all of the SELECT-CASE statements as well?
 
I ran Burts suggested code (post#249) and I only get ? ? ? and depending on the input it has a small rectangle next to the question mark.
I understand why I do not want to insert a function. Something the code is not doing correctly to display the ADC voltage.
The post #246 works so studying what is going on and trying to find out why the voltage readings are not being displayed.
I could use post#246, adjust the SELECT CASE numbers but obtaining the voltages would help in selecting the correct CASE numbers.
I don't want to change Burts suggestion, just get it to display correctly.
 
19200 is what the serial is set too if it's not showing up it's because your to slow or to fast.
i got a bunch of ?????? at first it was the osc i changed to a crystal 20 mhz and that fixed that.
Code:
Clock = 20
and got rid of this
Code:
Include "SetDigitalIO.bas"
 
I've not used swordfish in years But this spits out adc really good.
You really need a function if you want the whole adc value or you'll end up with just a byte
There was some updates or maybe something missing
For me to post you working code I would need to no what chip your using or you post your code you want use with the chip listed.

This is what it takes for the 18f4520
Code:
Device = 18F4520
Clock = 20
Include "adc.bas"
Include "Utils.bas"
Include "usart.bas"
Include "convert.bas"
Dim value As Word
#option USART_BRGH = True
Function ADC_value() As Word
 
        result = ADC.Read(0)  
End Function
TRISA = %00000001
ADC.SetConfig($0E)      // set ADCON1 for AN0 analog, rest digital
SetBaudrate(br9600)
While true
    DelayMS(20)
     value = ADC_value
       USART.Write(DecToStr(value),13,10)
        DelayMS(200)
Wend
Would work with your chip if use a crystal 20 mhz
The case should work fine seeing it's not printing the adc value I worked fine for me.
 
Last edited:
We are only on page 14
I removed the setalldigital but never tried chaging the osc setting. Will try that
Thanks. In the actual prototype I am using the interinal osc as I want to keep parts count down.
Changing from internal osc shouldn't affect the ADCvoltages? I only need the voltages to get a ballpark figure for the 10 switch CASE SELECT data.
 
You really need a function if you want the whole adc value or you'll end up with just a byte
That's incorrect.

ADC.Read(0) will read AN0 and return a word. All you've done is wrapped the call with the overhead of another function.

For the code you have, the statement 'value = ADC.Read(0)' will work just fine.
 
We are only on page 14

Only on page 14....like that's reasonable for this simple project???? And ya ain't done yet!


In the actual prototype I am using the interinal osc as I want to keep parts count down.

I have a bias towards things that just work and some people will disagree with this, but I always use a crystal. It just works. Every single time. Using the internal oscillator can be problematic in Swordfish, so I avoidcit. If you were building a thousand of these things, keeping the BOM as small as possible makes sense. In this case, time and simplicity are worth the 14 cents a crystal and 2 caps add to the cost. I did use higher quality caps, so you could probably squeeze that down to 12 cents if the budget is tight.


Changing from internal osc shouldn't affect the ADCvoltages? I only need the voltages....

It would be so awesome that by page 14 of this....[interal agrument ensues over term to use - must be polite]....forum thread that you actually demonstrated that you learned something. You neither want nor need voltages from the ADC. And if you actually did want voltages, how would, how could using the internal oscillator or a crystal impact ADC readings???

Here's a final effort on ADC counts vs. voltage:

If you are trying to read the voltage a battery or from a power supply, you would want to end up with voltage. To read voltage, you need a stable reference. You don't have a stable reference, so what you keep trying to call volts is garbsge.

You are essentially trying to read a pot - a pot with fixed taps. You don't freaking care what the voltage is. You care only about the ratio between full scale and which tap position you have selected. Say you pick the middle position of the pot. The resistance from the wiper to either terminal is the same. If your supply voltsge is 5, the output voltage is half of that. If the supply voltage is 57, the output is still half of that. You don't care what the exact voltage is. Only the ratio between output and input...which is what the ADC values give you.

SmartSelectImage_2018-01-08-06-40-58.png
SmartSelectImage_2018-01-08-06-41-53.png
 
your right I only need the CASE SELECT data but reading the voltages gets me in the ballpark BUT using Burts code has invoked an additional issue, the voltages STOP at 511
The start at 103, 208, 304, 408, 506, 511
Going back to the code suggestion in post #246 and tweek the numbers.
While visiting California I will work on the snippets for the rest of the code. The player selection will be a private sub route so only need it once.
as for the crystal etc. I am considering it but first get a proto type working then maybe include a crystal. I just need ballpark figures, determine the high and low range of the CASE SELECT data then go half way in between. dead on accuracy is not really needed IMO.
If I was going to have several hundred units built then I would have boards assembled using a 44 pin pic instead of a 28 pin. Then the ADC would not be needed.
 
That's incorrect.

ADC.Read(0) will read AN0 and return a word. All you've done is wrapped the call with the overhead of another function.

For the code you have, the statement 'value = ADC.Read(0)' will work just fine.
Have you tried it without using a function I get a mismatch that's all I got say about that now swordfish is not like the best.
I had to edit the ADC bas because the chip I have only could use 5 adc pins it has 13 go figure
 
If I was going to have several hundred units built then I would have boards assembled using a 44 pin pic instead of a 28 pin. Then the ADC would not be needed.

Wow. You really don't understand how any of this works do you?

If you are building one, who cares if the micro costs a few bucks more? If you are building many then the costs matter; saving a few cents on each one built adds up to real money.

You just don't get it about "volts" do you? You are running this thing from batteries. You are converting to "volts" based on a reference of 5 volts but it's NEVER going to be 5 volts and won't be a stable number.

You are just dividing 1023 by what is a random number. It gives you less range to work with and it's not volts. It's not anything but crap. Get the whole concept of volts out of your head because what you are doing is garbage.

SmartSelectImage_2018-01-08-13-20-03.png
 
This is what I have had show up if I use adc.read and don't send the return there is no problem
But the code I posted to spit out the adc gives a mismatch if I use writebyte with adc.read() because of the ()
Maybe a way around that nothing wrong with how I did it mater of fact that's how most people do it with swordfish.
 
Status
Not open for further replies.

Latest threads

Back
Top