Uno R4: Generating Interrupts Using Built-In Comparator

I am attempting to utilize the built in comparator to compare an input voltage to an internal reference voltage generated by the DAC8, which if greater should generate an interrupt which will change the value of a global variable. I have followed the steps outlined on page 1179 of the Rensas RA4M1 Group Users Manual to setup the comparator, including linking the ICU Event Link Setting Register (IELSR0 b7-b0, pg 233) to the ACMPLP event source (using the event number). However I do not know how to proceed from here to access the interrupt in the code.

I think I should be using the attatchInterrupt function with the first parameter being the corresponding interrupt vector to the IELSR0 register, however the interrupt vector table on page 237 only lists the vector offset which I have tried using and returns an error.

I'm not sure if I am on the right lines or not so any help would be greatly appreciated. Here is the code I have so far:

#define COMPMDR   *((volatile uint8_t*) 0x40085E00) // ACMPLP mode setting register
#define COMPSEL1  *((volatile uint8_t*) 0x40085E05) // Comparator reference voltage select register
#define COMPFIR   *((volatile uint8_t*) 0x40085E01) // ACMPLP filter control register
#define IELSR0    *((volatile uint32_t*)0x40006300) // ICU event link setting register 0
#define DACS0     *((volatile uint8_t*) 0x4009E000) // D/A conversion value setting register
#define DAM       *((volatile uint8_t*) 0x4009E003) // D/A converter mode register

int i = 0x040;

bool test = false;

void check(){
  test = true;
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  
  // Setting registers for DAC8
  DACS0 |= (1<<0); // Setting output value (0.6V)
  DACS0 |= (1<<1);
  DACS0 |= (1<<2);
  DACS0 |= (1<<3);
  DACS0 |= (1<<4);
  DAM   |= (1<<4); // Enabling D/A conversion for channel 0
  
  // Setting registers for comparator operation
  MSTPCRD   &= ~(1<<29);  // Low-Power Analog Comparator Module Stop
  PmnPFS    |=  (1<<15);  // ASEL (analog input enable)
  COMPOCR   |=  (1<<7);   // SPDMD (ACMPLP speed selection)
  COMPSEL1  &= ~(1<<0);   // CRVS[2:0] ACMPLP0 reference voltage selection (must be set to 000b prior to changing)
  COMPSEL1  |=  (1<<1);   // CRVS[2:0] ACMPLP0 reference voltage selection (010b = DAC8 channel 0 output)
  COMPMDR   |=  (1<<0);   // C0ENB (ACMPLP0 operation enable)
  delay(1);
  COMPFIR   |= (1<<1);    // C0FCK[1:0] (ACMPLP0 filter select, 10 = sampling at PCLKB/8)
  
  IELSR0 |= (1<<0);       // IELS[7:0] (ICU event link select)
  IELSR0 |= (1<<1);
  IELSR0 |= (1<<2);
  IELSR0 |= (1<<3);
  IELSR0 |= (1<<5);

  attachInterrupt(i,check);
}

void loop() {
  if (test == true){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(10000);   
    digitalWrite(LED_BUILTIN, LOW);
    test = false;        
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.