Hi all -
A simple question I think.
Trying to get values for an analog sensor.
Using a Sharp IR analogue range sensor with an Atmega8.
At the moment I am only checking the ADCH value. But there are 2 bits returned ADCL and ADCH.
The question is how do I combine these into an integer?
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
int set_PORTD_bit(int position, int value);
int main (void)
{
DDRD = 0x03; // Set LED1 as output
//ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz
ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
ADMUX |= (1 << ADLAR); // Left adjust ADC result to allow easy 8 bit reading
// No MUX values needed to be changed to use ADC0
ADCSRA |= (1 << ADFR); // Set ADC to Free-Running Mode
ADCSRA |= (1 << ADEN); // Enable ADC
ADCSRA |= (1 << ADSC); // Start A2D Conversions
for(;;) // Loop Forever
{
// bits are ADCL and ADCH
if(ADCH < 140)
{
set_PORTD_bit(0,0);
set_PORTD_bit(1,0);
}
else
{
set_PORTD_bit(0,1);
_delay_ms(50);
set_PORTD_bit(0,0);
_delay_ms(50);
/// LED 2
set_PORTD_bit(1,1);
_delay_ms(50);
set_PORTD_bit(1,0);
_delay_ms(50);
}
}
}
int set_PORTD_bit(int position, int value)
{
// Sets or clears the bit in position 'position'
// either high or low (1 or 0) to match 'value'.
// Leaves all other bits in PORTB unchanged.
if (value == 0)
{
PORTD &= ~(1 << position); // Set bit position low
}
else
{
PORTD |= (1 << position); // Set high, leave others alone
}
return 1;
}
Thanks