Sign In

Trouble In Reading Accelereometer Values

RAPOLU GOPIKRISHNA gopikrishnarapolu759@gmail.com India

Hardware: Atmega 16 Running At 1mhz Software: C Using Atmel Studio 4

I am getting accelerometer values in digital ,but both x and y axis are producing same outputs after adc conversion and digital values of x and y axis are in the range of 200-400 but getting through 0-1023.
please help me out.

Images

Program

#include<avr/io.h>
#include<util/delay.h>

#define	F_CPU	1000000UL
#define		LCD_DATA_PORT		PORTB
 

#define 	LCD_CONT_PORT		PORTD


#define 	LCD_RS 		PD0


#define 	LCD_RW 		PD1


#define 	LCD_EN 		PD2


 #define X_MIN	310


#define X_MAX	390


#define Y_MIN	310


#define Y_MAX	390

void ADC_init(void);
unsigned int ADC_read(unsigned char ch);
int read_adxl335_x_value(unsigned char ch);




int read_adxl335_y_value(unsigned char ch);


void lcd_data_write(char data);
void lcd_command_write( char command);
void lcd_init();
void lcd_string_write( char *string);
void lcd_number_write(int number,unsigned char radix);


 
// ------------------------------------------------
  
int main(void)
s {
	DDRB=0xff;
	

	DDRD=0x07;
	
        
	int x_axis,y_axis;
	
 
	ADC_init();

	lcd_init();
	
 	
	lcd_string_write("major project");
	

_delay_ms(500);
	lcd_command_write(0xc0);
	

	lcd_string_write("robot2.0");
	
		

	_delay_ms(500);
	_delay_ms(500);
	_delay_ms(500);
	_delay_ms(500);
	
	


	while(1)
	{
        x_axis=read_adxl335_x_value(0);
         
        
		y_axis=read_adxl335_y_value(1);
       
	
	lcd_init();
	

        lcd_command_write(0x01);
       
        lcd_string_write("Sensor Output:-");
        
        lcd_command_write(0xc0);
       

        lcd_string_write("X:");
        

        lcd_number_write(x_axis,10);
        
        
        lcd_command_write(0xc5);
  
        
		lcd_string_write("Y:");
        

        lcd_number_write(y_axis,10);
   
        
    
        _delay_ms(500);
		_delay_ms(500);
        
	}                                                                                                                                                                                                                     
} 


//------------------------------------------------
 
void ADC_init(void) 
{
ADMUX=(1<<REFS0); // AVcc with external capacitor at AREF
ADCSRA=(1<<ADEN)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0);
SFIOR=0x00; 

}
 
unsigned int ADC_read(unsigned char ch)
{
ch= ch & 0b00000111;
ADMUX |= ch; 
 
ADCSRA|=(1<<ADSC); 
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF); 
 
return (ADC);
}

int read_adxl335_x_value(unsigned char ch)
{
int x_value;
x_value=ADC_read(ch);
return x_value;
}

int read_adxl335_y_value(unsigned char ch)
{
int y_value;
y_value=ADC_read(ch);
return y_value;
}

void lcd_data_write(char data)
{
LCD_CONT_PORT=_BV(LCD_EN)|_BV(LCD_RS);
LCD_DATA_PORT=data;
_delay_ms(1);
LCD_CONT_PORT=_BV(LCD_RS);
_delay_ms(1);
}


void lcd_command_write(char command)
{
LCD_CONT_PORT=_BV(LCD_EN);
LCD_DATA_PORT=command;
_delay_ms(1);
LCD_CONT_PORT=0x00;
_delay_ms(1);
}

void lcd_init()
{
	lcd_command_write(0x38);
	lcd_command_write(0x01);
	lcd_command_write(0x06);
	lcd_command_write(0x0e);	
}

void lcd_string_write(char *string)
{
while (*string)
lcd_data_write(*string++);
}

void lcd_number_write(int number,unsigned char radix)
{
char *number_string="00000";
itoa(number,number_string,radix);
lcd_string_write(number_string);
}   

Replies

R Avinash Gupta
2017-05-05 04:42:39
This happens when you copy/paste code without basic understanding.
Change the implementation of this function as shown :-
unsigned int ADC_read(unsigned char ch)
{
  ch= ch & 0b00000111;
  ADMUX&=0b11100000;
  ADMUX |= ch; 
 
  ADCSRA|=(1<<ADSC); 
  while(!(ADCSRA & (1<<ADIF)));
  ADCSRA|=(1<<ADIF); 
 
  return (ADC);
}

Post a reply to RAPOLU GOPIKRISHNA

Think you can help RAPOLU GOPIKRISHNA ? Then post your thoughts that might help RAPOLU GOPIKRISHNA. You will earn a lot of reputation in the technical community.

If your program is in multiple files, then ZIP the entire folder and attach below

Images and Screenshots

These helps other better understand your suggestion.