Arduino AT1284p problem with emon Library

Hello,

i have a project to make an energy monitor with Atmel ATmega1284P DIP40. I have already setup the mighty 1284p library  and i am ready to program the ATmega1284P with arduino IDE. But this microcontroller do not supported from emon library, therefore i try to modify the library to work. The changes are shown below:

long EnergyMonitor::readVcc() {
  long result;
  
  //not used on emonTx V3 - as Vcc is always 3.3V - eliminates bandgap error and need for calibration http://harizanov.com/2013/09/thoughts-on-avr-adc-accuracy/

  #if defined(__AVR_ATmega168__) || defined(__AVR_ATmega328__) || defined (__AVR_ATmega328P__)
  ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);  
  #elif defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_AT90USB1286__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  ADCSRB &= ~_BV(MUX5);   // Without this the function always returns -1 on the ATmega2560 http://openenergymonitor.org/emon/node/2253#comment-11432
  
  //**********my modification*****************
  #elif defined(__AVR_ATmega1284P__)
  ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
  //**********end of my modification**********
  
  #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__)
  ADMUX = _BV(MUX5) | _BV(MUX0);
  #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
  ADMUX = _BV(MUX3) | _BV(MUX2);
    
  #endif

  #if defined(__AVR__) 
  delay(2);                                        // Wait for Vref to settle
  ADCSRA |= _BV(ADSC);                             // Convert
  while (bit_is_set(ADCSRA,ADSC));
  result = ADCL;
  result |= ADCH<<8;
  result = READVCC_CALIBRATION_CONST / result;  //1100mV*1024 ADC steps http://openenergymonitor.org/emon/node/1186
  return result;
 #elif defined(__arm__)
  return (3300);                                  //Arduino Due
 #else 
  return (3300);                                  //Guess that other un-supported architectures will be running a 3.3V!
 #endif
}

 

i have checked that the admux setup from ATmega1284P datasheet is right, but unfortunately don't gives me correct and constant values of current and voltage. I want to note that the hardware connections are correct and works properly with arduino UNO and Mega. Can anybody help me? Thank you in advance!

dBC's picture

Re: Arduino AT1284p problem with emon Library

What Vcc does your system use?  What value does readVcc() return?  Does it return the same value each time you call it?

alkotech's picture

Re: Arduino AT1284p problem with emon Library

Now it's OK, My fault was that i have two different GND not connected one with other. Thank you!

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.