Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5430
Number of posts: 16951

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Need Help With Code Posted by fsoender on 22 Jan 2013 at 8:31 AM
I need some help with my project. Im reading a accelerometer with ADC on port PA0 on a Atmega16. Im reading only X-axes. I want to add Y and Z also, but I dont know how.
Im reading the input with TeraTerm, line by line downwards. I want the y and Z to be added on the side of the X value.
IM using AVR studio 4.

Can anyone help me with thsis?

Code:

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


#define LONGOUT 7 
char buffer[LONGOUT];

void adc_init(void); 
unsigned int adc_read(void); 
void adc_conversion(uint16_t); 




// ADC configuration 
void ADC_init(void) { 
  ADMUX=(1<<REFS0); 
  ADCSRA=(1<<ADEN)|(7<<ADPS0); 
} 

uint16_t ADC_get_reading(void) { 
  ADCSRA |= (1<<ADSC); 
  while(ADCSRA & (1<<ADSC)); 
  return ADC; 
} 

uint16_t ADC_read(void) { 
  uint8_t i; 
  uint16_t retval = 0; 
  ADC_get_reading(); // dummy read - just discarded 
  for (i=0; i<8; i++) { 
    retval += ADC_get_reading(); 
  } 
  return retval / 8; 
} 


void init_UART(void)
{
	UCSRB = ((1 << RXCIE) | (1 << RXEN) | (1 << TXEN)); 
    UCSRC = ((1 << UCSZ1) | (1 << UCSZ0)); 
    
    //Baud rate till 19200.  
    UBRRL = 25; 
    UBRRH = 0; 
}


void uart_sendchar(char c) { 
   while(!(UCSRA & (1<<UDRE))); 
   UDR = c; 
} 

void uart_printstring(char * str) { 
  while (*str) { 
   uart_sendchar(*str++); 
  } 
} 


void main(void) {
	
 DDRC = 0x00;	
 init_UART(); 
 ADC_init();
 
 
  while(1)
  {
	 if (PINC & 0x01) //If PortC pin 0 is True, program stops.
	{ 
 ADC_read(); 
           
 char buffer[8]; 
 itoa(ADC, buffer, 10); 
 uart_printstring(buffer);
 uart_printstring("\r\n"); 
 _delay_ms(1500); 
 
    } 
  }
}




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.