Skip to content

Commit

Permalink
Fixed code to enable/disable digital buffers (they were inverted!). F…
Browse files Browse the repository at this point in the history
…ixed ADC enable/disable: for a type, the code worked on the wrong register
  • Loading branch information
leomil72 committed Apr 3, 2018
1 parent 17a730a commit 25fea87
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 57 deletions.
6 changes: 4 additions & 2 deletions README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

analogComp

This little library can be used to set and manage the analog
This little library can be used to set and manage the analog
comparator that is integrated in a wide variety of
Atmel microcontrollers

Expand All @@ -12,6 +12,7 @@ Written by Leonardo Miliani <www DOT leonardomiliani DOT com>
***********************
Version history

v. 1.2.4: fixed code to enable/disable digital buffers (they were inverted!) - thanks to ami2go. Fixed ADC enable/disable: for a type, the code worked on the wrong register
v. 1.2.3: added support for ATmega32U4 (old Leonardo boards) - thanks to stefandz
v. 1.2.2: added compatibility with Arduino IDE >= 1.6.7
v. 1.2.1: fixed a bug that let the ADC off after a comparison
Expand Down Expand Up @@ -139,6 +140,7 @@ them inside the library, please edit the analogComp.h file and change the value
6 to 8 of the following compiler's directive:
#define ATMEGAx8
#define NUM_ANALOG_INPUTS 6

***********************
Licence

Expand All @@ -155,4 +157,4 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
***********************
Document revision

11th revision: 2016/11/08
Last revision: 2018/04/03
61 changes: 30 additions & 31 deletions analogComp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ uint8_t analogComp::setOn(uint8_t tempAIN0, uint8_t tempAIN1) {
ADMUX &= ~31; //reset the first 5 bits
#ifndef ATMEGAxU
ADMUX |= tempAIN1; //choose the ADC channel (0..NUM_ANALOG_INPUTS-1)
#else
#else
switch(tempAIN1){
// see p. 313 of Atmel-7766J-USB-ATmega16U4/32U4-Datasheet_04/2016
case 0:
Expand All @@ -111,58 +111,58 @@ uint8_t analogComp::setOn(uint8_t tempAIN0, uint8_t tempAIN1) {
break;
case 1:
ADMUX |= 0b00000001;
AC_REGISTER &= ~bit(MUX5);
AC_REGISTER &= ~bit(MUX5);
break;
case 2:
// not a valid choice - and not broken out onto Leonardo / Micro type boards
break;
case 3:
// not a valid choice - and not broken out onto Leonardo / Micro type boards
// not a valid choice - and not broken out onto Leonardo / Micro type boards
break;
case 4:
ADMUX |= 0b00000100;
AC_REGISTER &= ~bit(MUX5);
AC_REGISTER &= ~bit(MUX5);
break;
case 5:
ADMUX |= 0b00000101;
AC_REGISTER &= ~bit(MUX5);
AC_REGISTER &= ~bit(MUX5);
break;
case 6:
ADMUX |= 0b00000110;
AC_REGISTER &= ~bit(MUX5);
AC_REGISTER &= ~bit(MUX5);
break;
case 7:
ADMUX |= 0b00000111;
AC_REGISTER &= ~bit(MUX5);
AC_REGISTER &= ~bit(MUX5);
break;
case 8:
//ADMUX |= 0b00000000; // redundant
AC_REGISTER |= bit(MUX5);
AC_REGISTER |= bit(MUX5);
break;
case 9:
ADMUX |= 0b00000001;
AC_REGISTER |= bit(MUX5);
AC_REGISTER |= bit(MUX5);
break;
case 10:
ADMUX |= 0b00000010;
AC_REGISTER |= bit(MUX5);
ADMUX |= 0b00000010;
AC_REGISTER |= bit(MUX5);
break;
case 11:
ADMUX |= 0b00000011;
AC_REGISTER |= bit(MUX5);
ADMUX |= 0b00000011;
AC_REGISTER |= bit(MUX5);
break;
case 12:
ADMUX |= 0b00000100;
AC_REGISTER |= bit(MUX5);
ADMUX |= 0b00000100;
AC_REGISTER |= bit(MUX5);
break;
case 13:
ADMUX |= 0b00000101;
AC_REGISTER |= bit(MUX5);
ADMUX |= 0b00000101;
AC_REGISTER |= bit(MUX5);
break;
default:
break;
}
#endif
#endif
AC_REGISTER |= (1<<ACME);
} else {
AC_REGISTER &= ~(1<<ACME); //set pin AIN1
Expand All @@ -171,15 +171,15 @@ uint8_t analogComp::setOn(uint8_t tempAIN0, uint8_t tempAIN1) {

//disable digital buffer on pins AIN0 && AIN1 to reduce current consumption
#if defined(ATTINYx5)
DIDR0 &= ~((1<<AIN1D) | (1<<AIN0D));
DIDR0 |= ((1<<AIN1D) | (1<<AIN0D));
#elif defined(ATTINYx4)
DIDR0 &= ~((1<<ADC2D) | (1<<ADC1D));
DIDR0 |= ((1<<ADC2D) | (1<<ADC1D));
#elif defined (ATMEGAx4)
DIDR1 &= ~(1<<AIN0D);
DIDR1 |= (1<<AIN0D);
#elif defined (ATTINYx313)
DIDR &= ~((1<<AIN1D) | (1<<AIN0D));
DIDR |= ((1<<AIN1D) | (1<<AIN0D));
#elif defined (ATMEGAx8) || defined(ATMEGAx4) || defined(ATMEGAx0)
DIDR1 &= ~((1<<AIN1D) | (1<<AIN0D));
DIDR1 |= ((1<<AIN1D) | (1<<AIN0D));
#endif
_initialized = 1;
return 0; //OK
Expand Down Expand Up @@ -234,23 +234,22 @@ void analogComp::setOff() {
}
ACSR |= (1<<ACD); //switch off the AC

//reenable digital buffer on pins AIN0 && AIN1
//reenable digital buffer on pins AIN0 && AIN1
#if defined(ATTINYx5)
DIDR0 |= ((1<<AIN1D) | (1<<AIN0D));
DIDR0 &= ~((1<<AIN1D) | (1<<AIN0D));
#elif defined(ATTINYx4)
DIDR0 |= ((1<<ADC2D) | (1<<ADC1D));
DIDR0 &= ~((1<<ADC2D) | (1<<ADC1D));
#elif defined (ATMEGAx4)
DIDR1 |= (1<<AIN0D);
DIDR1 &= ~(1<<AIN0D);
#elif defined (ATTINYx313)
DIDR |= ((1<<AIN1D) | (1<<AIN0D));
DIDR &= ~((1<<AIN1D) | (1<<AIN0D));
#elif defined (ATMEGAx8) || defined(ATMEGAx4) || defined(ATMEGAx0)
DIDR1 |= ((1<<AIN1D) | (1<<AIN0D));
DIDR1 &= ~((1<<AIN1D) | (1<<AIN0D));
#endif

#ifndef ATTINYx313
//if ((AC_REGISTER & (1<<ACME)) == 1) { //we must reset the ADC
if (oldADCSRA & (1<<ADEN)) { //ADC has to be powered up
AC_REGISTER |= (1<<ADEN); //ACDSRA = oldADCSRA;
AC_REGISTER |= (1<<ADEN);
}
#endif
_initialized = 0;
Expand Down
42 changes: 21 additions & 21 deletions analogComp.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#define ANALOG_COMP_H

//library version
#define ANALOGCOMP_VERSION 123
#define ANALOGCOMP_VERSION 124

//Library is compatible both with Arduino <=0023 and Arduino >=100
#if defined(ARDUINO) && (ARDUINO >= 100)
Expand All @@ -41,37 +41,37 @@
#undef NUM_ANALOG_INPUTS
#endif

#define AC_REGISTER ADCSRB
#define AC_REGISTER ADCSRA
//check if the micro is supported
#if defined (__AVR_ATmega48__) || defined (__AVR_ATmega88__) || defined (__AVR_ATmega168__) || defined (__AVR_ATmega328__) || defined (__AVR_ATmega48P__) || defined (__AVR_ATmega88P__) || defined (__AVR_ATmega168P__) || defined (__AVR_ATmega328P__)
#define ATMEGAx8
#define NUM_ANALOG_INPUTS 6
#define ATMEGAx8
#define NUM_ANALOG_INPUTS 6
#elif defined (__AVR_ATtiny25__) || defined (__AVR_ATtiny45__) || defined (__AVR_ATtiny85__)
#define ATTINYx5
#define NUM_ANALOG_INPUTS 4
#define ATTINYx5
#define NUM_ANALOG_INPUTS 4
#elif defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__)
#define ATMEGA8
#undef AC_REGISTER
#define AC_REGISTER SFIOR
#define NUM_ANALOG_INPUTS 6
#define ATMEGA8
#undef AC_REGISTER
#define AC_REGISTER SFIOR
#define NUM_ANALOG_INPUTS 6
#elif defined (__AVR_ATtiny24__) || defined (__AVR_ATtiny44__) || defined (__AVR_ATtiny84__)
#define ATTINYx4
#define NUM_ANALOG_INPUTS 8
#define ATTINYx4
#define NUM_ANALOG_INPUTS 8
#elif defined (__AVR_ATmega640__) || defined (__AVR_ATmega1280__) || defined (__AVR_ATmega1281__) || defined (__AVR_ATmega2560__) || defined (__AVR_ATmega2561__)
#define ATMEGAx0
#define NUM_ANALOG_INPUTS 16
#define ATMEGAx0
#define NUM_ANALOG_INPUTS 16
#elif defined (__AVR_ATmega344__) || defined (__AVR_ATmega344P__) || defined (__AVR_ATmega644__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega644PA__) || defined (__AVR_ATmega1284P__)
#define ATMEGAx4
#define NUM_ANALOG_INPUTS 8
#define ATMEGAx4
#define NUM_ANALOG_INPUTS 8
#elif defined (__AVR_ATtiny2313__) || defined (__AVR_ATtiny4313__)
#define ATTINYx313
#define NUM_ANALOG_INPUTS 0
#define ATTINYx313
#define NUM_ANALOG_INPUTS 0
#elif defined (__AVR_ATmega32U4__)
#define ATMEGAxU
#define NUM_ANALOG_INPUTS 14 // there are more inputs on the 32u4 than are broken out to the board
#define ATMEGAxU
#define NUM_ANALOG_INPUTS 14 // there are more inputs on the 32u4 than are broken out to the board
// and they are not mapped sequentially
#else
#error Sorry, microcontroller not supported!
#error Sorry, microcontroller not supported!
#endif


Expand Down
3 changes: 1 addition & 2 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
name=analogComp
version=1.2.3
version=1.2.4
author=Leonardo Miliani
maintainer=Leonardo Miliani
sentence=Set and manage the integrated analog comparator
paragraph=
category=Signal Input/Output
url=http://www.leonardomiliani.com
architectures=avr

2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.3
1.2.4

0 comments on commit 25fea87

Please sign in to comment.