I have connected a circuit to analog pin 0 in the Arduino.I have this code ,which I understand partially(online resources).The problem is that it prints nothing neither on Tera Term nor on Cool Term .Where am I mistaking ?(I am new to AVR programming).Here is the code:
#include<stdio.h>
void InitADC()
{
ADMUX=(1<<REFS0);// For Aref=AVcc;
ADCSRA=(1<<ADEN)|(7<<ADPS0);
}
uint16_t ReadADC(uint8_t ch)
{
//Select ADC Channel ch must be 0-7
ch=ch&0b00000111;
ADMUX|=ch;
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while(!(ADCSRA & (1<<ADIF)));
//Clear ADIF by writing one to it
ADCSRA|=(1<<ADIF);
return(ADC);
}
int main()
{
uint16_t adc_value;
InitADC();
while(1)
{
adc_value = ReadADC(0);
printf("%d",adc_value);
}
return 0;
}
-
That doesn't look like Arduino code. Are you using some other IDE instead of the Arduino IDE?Majenko– Majenko2015年09月12日 11:09:19 +00:00Commented Sep 12, 2015 at 11:09
-
I don't think printf will output to the serial port. At least not by default.Gerben– Gerben2015年09月12日 12:46:57 +00:00Commented Sep 12, 2015 at 12:46
1 Answer 1
Your code compiles, but as Gerben said, the printf won't do anything in its current state, as no output handlers are defined for it. Why go to all that trouble when you can do this?
void setup()
{
Serial.begin (115200);
}
void loop()
{
uint16_t adc_value;
adc_value = analogRead (0);
Serial.println (adc_value);
delay (100); // stop output spamming your monitor
}
You could conceivably make printf
work by following the tutorial here: http://playground.arduino.cc/Main/Printf
However I don't really see the point, when what I posted is much easier.
If performance is an issue, you might want to use the "advanced" code.
That "advanced" code above doesn't give you any more performance, because is still blocks. You can do a non-blocking version easily and readably enough:
const byte adcPin = 0; // A0
bool working;
void setup ()
{
Serial.begin (115200);
Serial.println ();
ADCSRA = bit (ADEN); // turn ADC on
ADCSRA |= bit (ADPS0) | bit (ADPS1) | bit (ADPS2); // Prescaler of 128
ADMUX = bit (REFS0) | (adcPin & 0x07); // AVcc
} // end of setup
void loop ()
{
if (!working)
{
bitSet (ADCSRA, ADSC); // start a conversion
working = true;
}
// the ADC clears the bit when done
if (bit_is_clear(ADCSRA, ADSC))
{
int value = ADC; // read result
working = false;
Serial.println (value);
delay (500);
}
// do other stuff here
} // end of loop
Now that code (which admittedly fiddles with the registers) doesn't block so you could be doing other things during the conversion. I don't see any reason you wouldn't use that on a commercial product.
See ADC conversion on the Arduino (analogRead) for more details.
-
Nice answer illustrating the idea/potential behind Arduino. The code given by the "Question Asker" would be the somewhat like the underlying code in the arduino analogRead() function. If performance is an issue, you might want to use the "advanced" code. As it separates the ADC init from the actual reading and is more "tweakable" than the arduino code. Though the arduino code is many times more readable. But on a commercial product it will be unlikely to use Arduino code.aaa– aaa2015年10月13日 14:12:12 +00:00Commented Oct 13, 2015 at 14:12
-
Thanks! You can quite easily write it without blocking, see amended answer.2015年10月13日 19:38:50 +00:00Commented Oct 13, 2015 at 19:38
-
I meant that using the Arduino library functions would be unlikely. But indeed you can use it. Much appreciated that you actually took the time to work out a non-blocking function. Though it does have a delay after printing the result? Is it for the ADC to get ready again or just so that it won't spam the serial output?aaa– aaa2015年10月14日 10:49:47 +00:00Commented Oct 14, 2015 at 10:49
-
For simple demos I usually put a delay in something that happens a lot to stop the serial output spamming, yes.2015年10月14日 19:51:25 +00:00Commented Oct 14, 2015 at 19:51
Explore related questions
See similar questions with these tags.