Direct access MEGA 2560 ADC registers

I’m trying to play with different analog to digital settings on the MEGA 2560 but I am running into issues with some example code I found here:

Basically, it throws errors saying it doesn’t know what PCM or ADC are. like so:

error: ‘PMC’ was not declared in this scope

error: base operand of ‘->’ is not a pointer

Any help?

void setup() {
  PMC->PMC_PCER1 |= PMC_PCER1_PID37;                    // ADC power on
  ADC->ADC_CR = ADC_CR_SWRST;                           // Reset ADC
  ADC->ADC_MR |=  ADC_MR_TRGEN_EN                       // Hardware trigger select
              | ADC_MR_TRGSEL_ADC_TRIG3;             // Trigger by TIOA2

  ADC->ADC_CHER = ADC_CHER_CH6;                        // Enable ADC CH6 = A1

  // put your setup code here, to run once:
  pinMode(2, OUTPUT);
  //pinMode(A1, INPUT_PULLUP);

Your example says “due” which may explain why it doesn’t work on the mega.

As a quick test, set you board type to “due” and use the verify button… it’s OK you don’t actually have that board hooked up as you aren’t uploading the code or expectin it to run.

If it still barfs, you failed to get the entire “due” example copied into your sketch…

Look for an example meant for the board you have.


Looks like that program is very hardware specific to the Due - its trying to use an externally triggered ADC mode.

The Arduino Mega2560 is a completely different chip, with a completely different arrangement of registers associated with the ADC, and the ADC has different capabilities.

You will have to figure out what the Due code is trying to do, and then find out if the
ATmega style processors have an equivalent function, then program up specific to
the ATmega architecture.

If you intention is to learn about ATmega ADC registers, just dump this code and start
reading the datasheet and/or finding examples written for ATmega microcontrollers.

The ATmega chips are in general about 10 times simpler than the SAM3X used in the Due, you
probably won’t get anywhere fast if you try to understand both chips!