I developed a quick custom PCB using the ATSAM3X8EA-AU as I wanted to use Arduino to program it as an Arduino DUE, I kept most of the schematic the same, however I used pin A14 pin 52 as an analog input as I was going by the Arduino Schematic and MCU ATSAM3X8EA-AU datasheet, stupidly without checking the product page or Arduino code first. Therefore I have PCBs on the way and no way to use A14 natively.
With the help of AI I have developed working code that allows me to use A14, it works as expected and doesn't seem to break anything. I'd like to get peoples opinions on it, is it likely to break anything if I use other functions in the future or try to use it in other code?
I will likely pull it out into a separate library.
//#define A14_PIN 52 // No longer used
#define ADC14_CHANNEL ADC_CHANNEL_14 // <-- FIXED
void setup() {
Serial.begin(9600);
//pinMode(A14_PIN, INPUT);
//digitalWrite(A14_PIN, LOW); // Disable pull-up - This was not actually needed
analogReadResolution(12); // For consistency with rest of ADC usage
// Enable ADC peripheral - Not sure is this is needed as it works without, however AI tells me its an important failsafe.
pmc_enable_periph_clk(ID_ADC);
adc_init(ADC, SystemCoreClock, ADC_FREQ_MAX, ADC_STARTUP_FAST);
adc_configure_timing(ADC, 0, ADC_SETTLING_TIME_3, 1);
adc_configure_trigger(ADC, ADC_TRIG_SW, 0);
adc_disable_interrupt(ADC, 0xFFFFFFFF);
adc_disable_all_channel(ADC);
// Configure PB21 (pin 52) for ADC
PIOB->PIO_PDR = PIO_PB21;
PIOB->PIO_ABSR |= PIO_PB21;
// Disable ADC write protection
ADC->ADC_WPMR = (0x414443u << 8); // "ADC"
}
uint16_t readA14() {
// Enable only ADC14
adc_disable_all_channel(ADC);
adc_enable_channel(ADC, ADC14_CHANNEL);
// Start conversion
ADC->ADC_CR = ADC_CR_START;
// Wait for conversion to complete
while ((ADC->ADC_ISR & ADC_ISR_DRDY) != ADC_ISR_DRDY);
// Read result
uint16_t result = ADC->ADC_LCDR;
// Disable ADC14 again to avoid conflicts
adc_disable_channel(ADC, ADC14_CHANNEL);
// Read and return result
return result;
}
void loop() {
uint16_t valA14 = readA14();
uint16_t valA0 = analogRead(A0);
uint16_t valA1 = analogRead(A1);
uint16_t valA11 = analogRead(A11);
Serial.print("A14 (PB21): ");
Serial.print(valA14);
Serial.print(" | A0: ");
Serial.print(valA0);
Serial.print(" | A1: ");
Serial.print(valA1);
Serial.print(" | A11: ");
Serial.println(valA11);
delay(500);
}