Skip to content

Add all analog references supported by the ATtinyX5 series #5300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 8, 2016
Merged

Add all analog references supported by the ATtinyX5 series #5300

merged 2 commits into from
Sep 8, 2016

Conversation

mischnic
Copy link
Contributor

Now it should possible to use the 2.56V voltage reference on the ATtinyX5's.

Should resolve #1739 , where the formula is derived from.

@sandeepmistry
Copy link
Contributor

@mischnic thanks for submitting. The changes look good to me testing with the following sketch (based on the AnalogInput example):

int sensorPin = A0;    // select the input pin for the potentiometer
int ledPin = 13;      // select the pin for the LED
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() {
  // declare the ledPin as an OUTPUT:
  pinMode(ledPin, OUTPUT);

  analogReference(EXTERNAL); // added
}

void loop() {
  // read the value from the sensor:
  sensorValue = analogRead(sensorPin);
  // turn the ledPin on
  digitalWrite(ledPin, HIGH);
  // stop the program for <sensorValue> milliseconds:
  delay(sensorValue);
  // turn the ledPin off:
  digitalWrite(ledPin, LOW);
  // stop the program for for <sensorValue> milliseconds:
  delay(sensorValue);
}

Here are the sketch size differences:

Board Before After Difference
Uno 1,042 bytes 1,054 bytes 12 bytes
ATtiny25 748 bytes 762 bytes 14 bytes
ATtiny45 752 bytes 766 bytes 14 bytes
ATtiny85 752 bytes 766 bytes 14 bytes

Without the additional call to analogReference(EXTERNAL); inside setup() there was no change in code size.

@cmaglie @facchinm @matthijskooijman any opinions on the extra code size? We could wrap the change in wiring_analog.c in an #ifdef to avoid the additional code size on non-ATtinyX5 boards like the Uno.

@facchinm
Copy link
Member

facchinm commented Sep 7, 2016

I'd personally avoid to change the ADMUX line for all targets, since the same can be accomplished with saner INTERNAL2V56 and INTERNAL2V56_EXTCAP defines and an #ifdef guard.

2016-09-07-092843_1366x768_scrot

Something like

 +#elif defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
 +  #define DEFAULT 0
 +  #define EXTERNAL 4
 +  #define INTERNAL1V1 8
 +  #define INTERNAL INTERNAL1V1
 +  #define INTERNAL2V56 9
 +  #define INTERNAL2V56_EXTCAP 13

and

#if defined(ADMUX)
if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = (analog_reference << 4) | (pin & 0x07);
#else
ADMUX = (analog_reference << 6) | (pin & 0x07);
#endif

@sandeepmistry
Copy link
Contributor

@facchinm that's a great suggestion!

@mischnic what do you think? Do you have time to update your pull request with the suggestion?

@mischnic
Copy link
Contributor Author

mischnic commented Sep 7, 2016

@sandeepmistry Ok

sandeepmistry added a commit to sandeepmistry/Arduino that referenced this pull request Sep 8, 2016
Add all analog references supported by the ATtinyX5 series
@sandeepmistry sandeepmistry merged commit a2a17a0 into arduino:master Sep 8, 2016
@sandeepmistry
Copy link
Contributor

@mischnic thank you!

@sandeepmistry sandeepmistry added this to the Release 1.6.12 milestone Sep 8, 2016
@sandeepmistry sandeepmistry added Component: Core Related to the code for the standard Arduino API Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) labels Sep 8, 2016
@mischnic mischnic deleted the attiny_anlReference branch September 8, 2016 20:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) Component: Core Related to the code for the standard Arduino API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

wiring_analog.c doesn't properly set REFS2 bit on ATTiny platform(s).
3 participants