Skip to content

Commit

Permalink
Create mcu_analogInput
Browse files Browse the repository at this point in the history
  • Loading branch information
lizxue committed Jan 21, 2018
1 parent 2ead849 commit 7621ce1
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions MCU_GPIO/mcu_gpio/mcu_analogInput
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

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;
}

if (bit_is_clear(ADCSRA, ADSC)) // the ADC clears the bit when done
{
int value = ADC; // read result
working = false;
Serial.println (value* (5.0 / 1023.0)); //Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V)
delay (500);
}
} // end of loop

0 comments on commit 7621ce1

Please sign in to comment.