I'm working on a project that will essentially become an oscilloscope in the long run. I am following this guide. My code is uploaded here for more reference.
In my .ino file I have called functions to set up register such that I have a timer ISR called at a frequency of 100 KHz, and an ADC clock rate of 2 MHz. The ADC is also set so that the result is left shifted so ADCH contains an 8-bit result. In a separate .h file I have an ADC ISR that looks like this:
ISR(ADC_vect)
{
// Read 8-bit conversion result from ADCH.
uint8_t byte0 = ADCL;
uint8_t byte1 = ADCH;
data_byte = byte1;
flag = true;
}
The loop() function is the following:
void loop()
{
if (flag == true)
{
//Serial.println("ISR HIT");
Serial.println(data_byte);
flag = false;
}
}
So, when the ISR is called the flag is set to true, and in the loop() function I am notified with an "ISR HIT" printed on the serial monitor, this operation is successful.
However, I also have a volatile global variable in my .ino function called data_byte (also declared as an extern in the .h file) that I set byte1 to. When byte1 is printed to the serial monitor the result is:
255
255
255
Any ideas on why the value of ADCH is stuck at 255? My hardware is simply a light sensor connected to pin A0, 3.3 V, and GND.
1 Answer 1
It would seem the problem lies here:
// Set Register to all zeros since Vref
// will be set to internal voltge and
// analog pin A0 is used.
ADMUX = 0x00;
According to the datasheet, the bits REFS[1:0]
of the ADMUX
register
select the voltage reference as:
- 00: AREF, Internal V ref turned off
- 01: AVCC with external capacitor at AREF pin
- 10: Reserved
- 11: Internal 1.1V Voltage Reference with external capacitor at AREF pin
Settings those bits to zero means that you are now supposed to provide your own reference through the AREF pin. If you want to use the 5 V supply as a reference, you should set them to 01. This is confirmed by the block schematic. Here is the part of that schematic relative to the reference voltage:
When REFS0
is zero, the highlighted transistor does not conduct, hence
the only way to provide a voltage reference is through the AREF pin.
-
1Answer accepted! Thank you again for such a through and well explained answer.CookieMonster317– CookieMonster3172018年10月06日 20:34:58 +00:00Commented Oct 6, 2018 at 20:34
Explore related questions
See similar questions with these tags.
data_byte
. Or maybe you do in a version of the program you didn't publish. If we cannot see the code, there is no point in trying to guess what could be wrong.