Simple program to demonstrate the basic functions of the analog comparator of the FRDM-KL25Z board.

Dependencies:   ComparatorIn mbed

Fork of ComparatorIn_demo by Frank Vannieuwkerke

ComparatorIn demo

Simple program to demonstrate the basic functions of the analog comparator of the FRDM-KL25Z board.

The blue LED flashes after 2 sec delay. The green LED is driven by the interrupts of the Analog Comparator (LED ON at rising edge, LED OFF at falling edge).

The output of the Analog Comparatir is also tested by polling, the result is printed out at the standard output (UART0). The default speed and format (9600 bps, 8,N,1) are used.

Credits

This program is based on the ComparatorIn library and the ComparatorIn_demo program written by Frank Vannieuwkerke

Used library:

Hardware requirements:

  • FRDM-KL25 board
  • CdS photoresistor pulled up by 10 k

/media/uploads/icserny/05_comparator_demo.png

Revision:
2:bb19e60cb933
Parent:
1:fb35e688f9f4
Child:
4:07dd82e3f934
--- a/main.cpp	Wed Jun 05 18:51:15 2013 +0000
+++ b/main.cpp	Sun Jun 16 15:17:56 2013 +0000
@@ -1,51 +1,48 @@
-#include "ComparatorIn.h"
-#include "mbed.h"
-
-DigitalOut myled(LED1); // Blue
-DigitalOut cmpled(LED2); // Green
-DigitalOut cmp_en (PTD5);
-AnalogIn cmp_lvl (PTB0);
-ComparatorIn compi(PTC8, NC); // light sensor comparator input
-
-// Comparator callback function
-void cmp_ISR(unsigned int cmp_ret)
-{
-    printf("\n%02X - Light sensor : %7.5f Volt - ",cmp_ret,cmp_lvl*3.3);
-    if ((cmp_ret & CMP_SCR_CFR_MASK)==CMP_SCR_CFR_MASK)
-    {
-        printf("<INT=Rising edge on HSCMP0>\n");
-    }
-    if ((cmp_ret & CMP_SCR_CFF_MASK)==CMP_SCR_CFF_MASK)
-    {
-        printf("<INT=Falling edge on HSCMP0>\n ");
-    }
-}
-
-
-
-int main()
-{
-    cmp_en = 1;
-
-    compi._callbackISR(&cmp_ISR);               // Set comparator callback to cmp_ISR
-    compi.treshold(0x20);                       // Set comparator threshold to 1.65V = 32 * 3.3V / 64
-
-    while(1)
-    {
-//        printf("InP : %02X - InM : %02X - DACCR : %02X\n",(CMP0->MUXCR & 0x38)>>3,(CMP0->MUXCR & 0x07),(CMP0->DACCR & 0x3f));
-        printf("Light sensor : %7.5f Volt\n",cmp_lvl*3.3);
-        myled = 1;
-        wait(1);
-        myled = 0;
-        wait(0.2);
-        if (compi.status() == 0x01)
-        {
-            cmpled = 0;
-        }
-        else
-        {
-            cmpled = 1;
-        }
-    }
-}
-
+#include "ComparatorIn.h"
+#include "mbed.h"
+
+DigitalOut blinker (LED_BLUE);
+DigitalOut cmpled (LED_GREEN);
+DigitalOut cmp_en (PTD5);
+AnalogIn cmp_lvl (PTB0);
+ComparatorIn compi(PTC8, PTE30); // in+ = PTC8, in- = 12-bit DAC 
+
+// Comparator callback functions
+void cmp_rise_ISR(void)
+{
+    cmpled = 0;
+}
+
+void cmp_fall_ISR(void)
+{
+    cmpled = 1;
+}
+
+
+
+int main()
+{
+    cmp_en = 1;
+    cmpled = 1;
+
+    compi.rising(&cmp_rise_ISR);                // Set pointer to rising interrupt function
+    compi.falling(&cmp_fall_ISR);               // Set pointer to falling interrupt function
+    compi.treshold(0.3);                        // Set comparator threshold to 1V = 0.3 * 3.3V
+    compi.int_en(1);                            // Enable comparator interrupts
+
+    while(1)
+    {
+        printf("Light sensor : %7.5f Volt\n",cmp_lvl*3.3);
+        blinker = 1;
+        wait(1);
+        blinker = 0;
+        wait(0.2);
+        if (compi.status() == 0x01)
+        {
+            printf("*** Treshold reached : %7.5f\n",cmp_lvl*3.3);
+        }
+    }
+}
+
+
+