I am trying to code the Arduino Uno to run an analog to digital conversion. I have a pot connected to the A0 pin and am simply trying to input the analog value it reads from A0 and the digital output into the serial monitor. I have troubleshooted this quite a bit and am not getting anything on the serial monitor. Sorry I have to do this but here is my code, if you see my problems can you please help. Thanks
Rich (BB code):
/* ADC Test
For an Atmega 48 88 or 168
Sends ADC results to the serial port
Set your terminal to 2400 N 8 1
Atmega168 DIP TX PD1 (pin3)
Atmega168 DIP RX PD0 (pin2)
Atmega168 DIP ADC2 PC2 (PIN25) */
#define F_CPU 1000000UL
#define UBRR_1200 51
#define UBRR_2400 25 // for 1Mhz
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
int inputValue = 0; // value output to the PWM (analog out)
int adc_temp = 0; // value read from the pot
// #define UBRR_2400 207 // for 8Mhz with .2% error
// #define UBRR_9600 51 // for 8Mhz with .2% error
// #define UBRR_19200 25 // for 8Mhz with .2% error
#include <avr/io.h>
#include <util/delay.h>
#include <stdio.h>
void read_adc(void); // Function Declarations
void adc_init(void);
void setup() { // initialize serial communications at 9600 bps:
Serial.begin(9600);
adc_temp = analogRead(analogInPin);
inputValue = map(adc_temp, 1 , 1023, 1, 4);
Serial.print("ADC = " );
Serial.print(inputValue);
}
int main(void)
{
adc_init(); // Initialize the ADC
_delay_ms(125); // wait a bit
while(1)
{read_adc();
_delay_ms(125); // wait a bit
}
}
/* INIT ADC */
void adc_init(void)
{
/** Setup and enable ADC **/
ADMUX = (0<<REFS1)| // Reference Selection Bits
(1<<REFS0)| // AVcc - external cap at AREF
(0<<ADLAR)| // ADC Left Adjust Result
(0<<MUX2)| // Analog Channel Selection Bits
(1<<MUX1)| // ADC2 (PC2 PIN25)
(0<<MUX0);
ADCSRA = (1<<ADEN)| // ADC ENable
(0<<ADSC)| // ADC Start Conversion
(0<<ADATE)| // ADC Auto Trigger Enable
(0<<ADIF)| // ADC Interrupt Flag
(0<<ADIE)| // ADC Interrupt Enable
(1<<ADPS2)| // ADC Prescaler Select Bits
(0<<ADPS1)|
(1<<ADPS0);
// Timer/Counter1 Interrupt Mask Register
TIMSK1 |= (1<<TOIE1); // enable overflow interrupt
TCCR1B |= (1<<CS11)|
(1<<CS10); // native clock
}
/* READ ADC PINS */
void read_adc(void)
{
unsigned char i = 4;
adc_temp = 0;
while (i--)
{
ADCSRA |= (1<<ADSC);
while(ADCSRA & (1<<ADSC));
adc_temp+= ADC;
_delay_ms(50);
}
adc_temp = adc_temp / 4; // Average a few samples
Serial.print("conversion = " );
Serial.print(adc_temp);
delay(10);
}
Last edited by a moderator: