fsoender
Member level 2
- Joined
- Aug 7, 2012
- Messages
- 45
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- Activity points
- 1,553
I want to get a better result on my ADC port by reading it 8 times, and get an avarage meassurement, any help to get?
Code:
/* AccelerometerRead.c
* This program demonstrates the use of ADMUX to read multiple ADC inputs.
* Runs on a Atmega32, 8MHz, 19200 Baud.
* Compiled using AVR Studio 4.
* Jan 25 2013. FS.
*/
#include<avr/io.h>
#include<util/delay.h>
#include<avr/interrupt.h>
#include <stdio.h>
#define LONGOUT 15
char buffer[LONGOUT]="0000 0000 0000";
void adc_init(void);
unsigned int adc_read(void);
void adc_conversion(uint16_t);
unsigned char x;
unsigned char y;
unsigned char z;
// ADC configuration
void ADC_init(void)
{
ADMUX=(1<<REFS0);
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}
// Getting reading from ADC.
uint16_t ADC_get_reading(void)
{
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC));
return ADC;
}
unsigned int Read_ADC(unsigned char adc_input)
{
// clear the channel bits ane set the new channel
//ADMUX = (ADMUX & 0xF8) | adc_input
ADMUX = (ADMUX & 0xF8) | (adc_input & 0x07);
// Delay needed for the stabilization of the ADC input voltage
//_delay_us(10);
// Start the AD conversion
//ADCSRA |= 0x40;
ADCSRA |= (1 << ADSC);
// Wait for the AD conversion to complete
while (ADCSRA & (1 << ADSC))
// return result
return ADCW;
}
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)
{
init_UART();
ADC_init();
while(1)
{
// Reading x,y,z values
x=Read_ADC(0);
y=Read_ADC(1);
z=Read_ADC(2);
// Sending values to UART.
sprintf(buffer,"%.4u %.4u %.4u ",x,y,z);
uart_printstring(buffer);
uart_printstring("\r\n");
_delay_ms(5000);
}
}