so can i read ADC directly because my range is higher that 255 using uinit_16 ? but i did not get this part correctly
The ADC returns a 10-bit value in two eight-bit registers, ADCH (the most-significant part) and ADCL (the least-significant part). If you left-align the result (setting ADLAR) then ADH contains the eight most significant bits (between 0..255 for the entire voltage range). If you right-align the result (clearing ADLAR) then both parts of the ADC register can be combined (by reading ADC) to read all ten bits (between 0..1023 for the entire voltage range).

Have you read the ATmega8 datasheet? It goes into some detail about the operation of the ADC.
yes, i have read the data sheet of atmega8 only the adc part . Thanks for this idea sir
In other words, if you do this 8-bit read then you just lose a factor of 4 of precision. Now 0=0v and 255=Vref, and you can easily figure out the intermediate scaling from there.
my decimal values with respect to atmega8 - 600 to 1500 (y- axis )
and 350 to 1500 (x-axis). so i am thinking to use the ADCL values only will that work fine ?
saransh123007 wrote:
my decimal values with respect to atmega8 - 600 to 1500 (y- axis )
and 350 to 1500 (x-axis). so i am thinking to use the ADCL values only will that work fine ?
No, that won't work fine, and those decimal values don't make sense. Even when you use all ten bits, you can only get between 0 and 1023. If you use the method that you were using in your code earlier in the thread, you use eight bits in ADCH (NOT ADCL), which means ADCH= 0 = 0v and ADCH = 255 = VRef.
yes then i think i should change the value to adc > 900 .
And to read these values i should use something like this right

Code:

if ((ADCL>n)&&(ADCH>m))
i=1 ;

Code:
if (ADC > 900) {
    i = 1;
}

should be sufficient.
You totally lost me. You said this earlier:

I am getting x-axis - 4.32 to 0.74 volts
and y-axis - 4.02 to 1.36 volt how do i interpret this data ?

With your current code in the topic, you should just read ADCH as eight bits, and scale 4.32 and 0.74 and 4.02 and 1.36 according to 0 to 255 and your VRef.
but if i Use the formula ADC= Vin * 1023/ Vref
where Vref= 2.56 and Vin is 4.32 to 0.74 volts and 4.32 to 0.74 volts
so i get the values beyond 1500 . How do i convert this to 0 to 255 ?
benryves wrote:

Code:
if (ADC > 900) {
    i = 1;
}

should be sufficient.



okay so i should one can use ADC directly i did not know that because i have used 89c51 also but not adc part (as it need an interfacer for it) in that we have two different registers like L and H and we could read it as L and H only not combined . So i thought it would be that same here i'll try using ADC too.
Quote:
The reference voltage for the ADC (VREF) indicates the conversion range for the ADC. Single ended channels that exceed VREF will result in codes close to 0x3FF. VREF can be selected as either AVCC, internal 2.56V reference, or external AREF pin.

In short, if you need to measure voltages above 2.56V, you cannot use the internal 2.56V reference - the ADC will only return values between 0..1023, and if the voltage exceeds the range it will be clipped. Use AVCC (which I presume is 5V) to measure your analogue input.

Edit:
saransh123007 wrote:
[...] we have two different registers like L and H and we could read it as L and H only not combined . So i thought it would be that same here i'll try using ADC too.

The C library allows you to access paired 8-bit registers as if they were a single 16-bit register. If you were using assembly you would have to access the two registers individually and in the correct order (you must read ADCL first, then ADCH).
saransh123007 wrote:
but if i Use the formula ADC= Vin * 1023/ Vref
where Vref= 2.56 and Vin is 4.32 to 0.74 volts and 4.32 to 0.74 volts
so i get the values beyond 1500 . How do i convert this to 0 to 255 ?
You needed to measure with respect to your Vref. If you convert those values, you'd get:

2.221 V through 0.3789 V = 221 through 38 in eight-bit values
2.058 V through 0.694V = 206 through 70 in eight-bit values

Edit: This assumes that you feed the joystick VRef instead of +5V. If you continue to use +5V to power the joystick, you need VRef to also be +5V, as Ben said.

Edit#2: Please do not double-post!
benryves wrote:
Quote:
The reference voltage for the ADC (VREF) indicates the conversion range for the ADC. Single ended channels that exceed VREF will result in codes close to 0x3FF. VREF can be selected as either AVCC, internal 2.56V reference, or external AREF pin.

In short, if you need to measure voltages above 2.56V, you cannot use the internal 2.56V reference - the ADC will only return values between 0..1023, and if the voltage exceeds the range it will be clipped. Use AVCC (which I presume is 5V) to measure your analogue input.

Edit:
saransh123007 wrote:
[...] we have two different registers like L and H and we could read it as L and H only not combined . So i thought it would be that same here i'll try using ADC too.

The C library allows you to access paired 8-bit registers as if they were a single 16-bit register. If you were using assembly you would have to access the two registers individually and in the correct order (you must read ADCL first, then ADCH).



okay so i can use something like this if ADC>0x3ff i wont need an external supply then this will also reduce the cost right ?
ADC can never go above 0x3FF (1023). If the input voltage is above the reference voltage the ADC will return 0x3FF (1023) or very close to it, but not above it.
sorry for the double post did not realize it .
So if one uses ADC>0x300 then i guess it would be fine ?
Sort of - you'd be better off using the same voltage for the ADC reference and the joystick power supply so that you get a nice linear relationship between the full range of the joystick position and the sampled ADC value. By using a ADC reference voltage that is roughly half of the joystick voltage you'll 0..1023 for one half of its possible range of positions, then 1023 for the entirety of the other half.
okay my AREF - AVCC pin is connected to 5 v so i can use it for ADC>1500 ; [/img]
Bottom line: Try to make the joystick and VRef both +5V, or both +2.56V. If they're both the same, then the full possible range of values will be in 0 to 1023. If you choose to drop the lowest 2 bits, it will of course then be in 0 to 255, but you don't have to do that.

Edit: To repeat several more times what Ben already repeated, the ADC will NEVER read above 1023. It is physically impossible.
ADC will still not rise above 1023, but this now means that an ADC reading of 0 corresponds roughly to an input voltage of 0V and an ADC reading of 1023 corresponds roughly to an input voltage of 5V.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 3 of 4
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement