benryves wrote:
If using C, it's just a simple

Code:
uint16_t value = ADC;

Otherwise, you have to bear in mind the following:
Quote:
When ADCL is read, the ADC Data Register is not updated until ADCH is read. Consequently, if the result is left adjusted and no more than 8-bit precision is required, it is sufficient to read ADCH. Otherwise, ADCL must be read first, then ADCH.
Thanks, Ben. Smile I really need to learn more about the AVRs and coding for them in C and ASM; at this point I just kinda splash around in my ugly hybrid of Arduino and C code.
i connected the joystick as you told me to but will 128 to 775 arduino be same for AVR atmega8 ? or will it vary . Because i wrote the code for the same and I am not getting the output at all .
saransh123007 wrote:
i connected the joystick as you told me to but will 128 to 775 arduino be same for AVR atmega8 ? or will it vary . Because i wrote the code for the same and I am not getting the output at all .
Well, what kind of values are you getting, then? Do you have a way of checking what the actual values are?
well i implemented it using LEDs as output for joystick input but did not get anything . How do i check the joystick output using a multimeter ie should i check the resistance if yes, then what range
saransh123007 wrote:
well i implemented it using LEDs as output for joystick input but did not get anything . How do i check the joystick output using a multimeter ie should i check the resistance if yes, then what range
No, connect the +5V and GND connections to the joystick, then measure the voltage between GND and the X axis contact or the Y axis contact. It should vary between 0v and 5v.
okay will do that and check whether my connections right or not thank you
saransh123007 wrote:
okay will do that and check whether my connections right or not thank you
Sure thing. And based on the voltage levels that you can see, we can verify what kind of thresholds you'll need for your ADC code.
okay that will be great will do it as soon as possible
saransh123007 wrote:
okay that will be great will do it as soon as possible
Excellent, keep me posted. Smile
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 ?
saransh123007 wrote:
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 ?
Well, you can firstly recognize that the joystick is indeed working properly. Secondly, have you read stuff like this guide on using the AtMega8's ADC?

http://avrbeginners.net/architecture/adc/m8_adc_example.html

Particularly: "The recommended ADC clock range is 50 kHz to 200 kHz. If a faster ADC clock is used, the resolution will go down. The ADC clock is prescaled from the main clock by a seperate ADC prescaler. The division factor is selected with the ADPS2..0 bits in ADCSR (see pages 203/204 of the datasheet). At 4 MHz, appropriate values are 32 (resulting ADC clock is 125 kHz) and 64 (resulting ADC clock is 62.5 kHz). We'll use 32."

The ADC has a resolution of 10 bits, which means that the valid values range from 0 (0v) to 1024 (5v). How about if you set a threshold on one of the axes to 512 or so, and just see if you can make an LED light when you push the joystick one way but not the other way?
yes i read the atmega8 datasheet regarding adc thanks alot for the info i figured out how to get the input in decimal and then converted the same to hex . ADC= Vin*1024/ Vref
where Vin is the analog data input and Vref is the internal or external Voltage we use for ADC i am using internal ie 2.56V
and got the dat thank u
That's great! So does that mean that everything is working now?
no i guess my code ain't right then its a pain now . Working past 2 days I hope it works before my final display
saransh123007 wrote:
no i guess my code ain't right then its a pain now . Working past 2 days I hope it works before my final display
You're welcome to post some of your code here if you'd like us to help you proofread it. Smile

Code:

#include<avr/io.h>
#include<util/delay.h>
#include <transmit.h>
//#include<uart.h>
#include<avr/interrupt.h>
#define set(x,y) x|=(1<<y)
#define clear(x,y) x&=(~(1<<y))
#define toggle(x,y) x^=(1<<y)
# define check(x,y) (x&(1<<y))
void forward(void)
   {
     set(PORTD,2);
     clear(PORTD,3);
     set(PORTD,5);
     clear(PORTD,4);
   }
   void stop(void)
   {
      clear(PORTD,3);
     clear(PORTD,2);
     clear(PORTD,4);
     clear(PORTD,5);
    }
   void right(void)
   {
     set(PORTD,2);
     clear(PORTD,3);
     clear(PORTD,5);
     set(PORTD,4);
   }
   void left(void)
   {
     clear(PORTD,2);
     set(PORTD,3);
     set(PORTD,5);
     clear(PORTD,4);
   }
   void reverse(void)
   {
   clear(PORTD,2);
     set(PORTD,3);
     clear(PORTD,5);
     set(PORTD,4);
   }
   void read_x(void)
   {
   ADMUX=0xE1;
      }
   void read_y(void)
   {
   ADMUX=0xE2;
   }
   int adc_init(void)
   {
   ADMUX=0xE1;
   ADCSRA=0xAF;
   return 0;
   }
   int adc_start(void)
   {
   set(ADCSRA,ADSC);
   return 0;
   }
   int main (void)
   {
//   unsigned int i;
   adc_init();
   uart_init();
   DDRC=0x00;
   DDRD=0xFF;
   PORTC=0xFF;
   PORTD=0xFF;
   //while(1)
   return 0;
   }
   ISR(ADC_vect)
   {
   int i;
   read_x();
   adc_start();
   if(ADCH<0xDC)
   {
   i=1;
   }
   else if(ADCH>0x58)
   {i=2;}
   read_y();
   adc_start();
    if(ADCH<0xDC)
   {i=3;}
   else if(ADCH>0x5E)
   {i=4;}
   while (i==1)
   {left();}
   while (i==2)
   {right();}
   while (i==3)
   {forward();}
   while (i==4)
   {reverse();}
   }
   
Well, you're just reading ADCH, not the ADCH/ADCL pair. That's a problem. Smile
If you only want an eight-bit result, set ADLAR in ADMUX (ADMUX |= _BV(ADLAR)) and read ADCH only. This "left adjusts" the result (ADCH contains bits ADC9..ADC2), so ADCH contains ADC9..ADC2 as opposed to six empty bits and ADC9..ADC8.

Edit: Though, of course, your code does set ADLAR (I'd recommend using symbols rather than magic numbers). Wink I'm not sure why your code is not working as you'd expect, though it's tricky to say without having the circuit in front of me (I'm not too good at running code in my head).
yes i guess thats what i realized today that i am using only ADCH and not ADCL il write a new code using ADCL and run it again. And i hav done a few mistakes too i have used free running mode and ADSC which does not go hand in hand a realized that a little late . I am reviving the whole code all over again
If you're only interested in an 8-bit value (from 0..255) then set ADLAR (as you currently do) and use ADCH (as you also currently do). If you want the full 10-bit range (0..1023) then reset ADLAR and read ADC.
  
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 2 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