int led = 13; // name the LED pin
int inCompNeg = 12; // neg comparator input
int inCompPos = 11; // pos comparator input
bool debugMode = false;
void setup() {
pinMode(led, OUTPUT); // name LED output
pinMode(inCompNeg, INPUT); // name comparator neg in
pinMode(inCompPos, INPUT);
Serial.begin(9600);
delay(7000); // wait for serial monitor to open
// setup comparator CMP0 on pins 11 and 12 (CMP0 output is CMP0_OUT) PAGE 633 onward in ref manual
// PTC6 and PTC7 default as Comp 0 inputs
// setup for no filter, clock or hyst
if (debugMode == true)
{
CMP0_CR0 = B00000000; //cmp control reg equals 0 at start, leave
CMP0_CR1 = B00000011; //also 0 reset, set to CMP output pin and comp module enable
CMP0_SCR = B00010000; // read LSB for analog comparator output state
CMP0_MUXCR = B00000001; // Input pins select = IN0 = plus and IN1 = neg
// now select DAC set at Vdd/2 as input to CMP- input
CMP0_DACCR = B10011111; // enable DAC, use ref as Vin1in and set output voltage to Vin/2
}
Serial.println("Comparator Configured");
}
void loop()
{
// now trigger comparator and print trigger
digitalWrite(led, HIGH); // turn the LED off by making the voltage LOW
if (debugMode == true)
{
if (CMP0_SCR > 16) // if pos trig
{
Serial.println("Comp postive trigger");
delay(500);
}
else
{
// not trig
Serial.println("Comp not trigger");
}
}
else Serial.println("debugMode = false; to ...");
delay(1000);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(500);
}