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:
7:a4f884972a0e
Parent:
4:07dd82e3f934
Child:
8:a5b18dfe684f
--- a/main.cpp	Sun Aug 25 18:01:58 2013 +0000
+++ b/main.cpp	Wed Nov 11 08:12:13 2015 +0000
@@ -1,47 +1,64 @@
-#include "ComparatorIn.h"
-#include "mbed.h"
+/** comparatorin_demo
+ *  Simple program to demonstrate the basic functions
+ *  of the analog comparator of the FRDM-KL25Z board.
+ *
+ *  Used library:
+ *   - ComparatorIn //https://developer.mbed.org/users/frankvnk/code/ComparatorIn/
+ *     written by Frank Vannieuwkerke
+ *
+ *  Hardware requirements:
+ *   - FRDM-KL25 board
+ *   - CdS photoresistor pulled up by 10 k
+ *   - Connecect the midpoint of the divider to PTE29
+ *
+ *            10k          CdS photoresistor
+ *   +3.3V --/\/\/\---+---/\/\/\---- GND
+ *                    |
+ *                  PTE29
+ *
+ */
 
-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
+#include "mbed.h"
+#include "ComparatorIn.h"
+ 
+DigitalOut blinker (LED_BLUE);      // blinking LED
+DigitalOut cmpled (LED_GREEN);      // signing comparator status
+AnalogIn cmp_lvl (PTE29);           // ADC input to check comparator input
+ComparatorIn compi(PTE29, PTE30);   // in+ = PTE29, in- = 12-bit DAC 
+ 
+// Comparator interrupt callback functions
 void cmp_rise_ISR(void)
 {
-    cmpled = 0;
+    cmpled = 0;                     // LED ON at rising edge
 }
-
+ 
 void cmp_fall_ISR(void)
 {
-    cmpled = 1;
+    cmpled = 1;                     // LED OFF at falling edge
 }
-
-
-
+ 
+ 
+ 
 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
-
+    cmpled = 1;                     // LED OFF at the beginning
+ 
+    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
+ 
     while(1)
     {
         printf("Light sensor : %7.5f Volt\n",cmp_lvl*3.3);
-        blinker = 1;
-        wait(1);
-        blinker = 0;
+        blinker = 1;                // binking LED OFF
+        wait(2);                    // wait for 2 sec
+        blinker = 0;                // blinking LED ON
         wait(0.2);
-        if (compi.status() == 0x01)
+        if (compi.status() == 0x01) // poll comparator status
         {
             printf("*** Treshold reached : %7.5f\n",cmp_lvl*3.3);
         }
     }
 }
-
-
-
+ 
+