KL25Z Comparator demo

Dependencies:   ComparatorIn mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "ComparatorIn.h"
00002 #include "mbed.h"
00003 
00004 DigitalOut blinker (LED_BLUE);
00005 DigitalOut cmpled (LED_GREEN);
00006 DigitalOut cmp_en (PTD5);
00007 AnalogIn cmp_lvl (PTB0);
00008 ComparatorIn compi(PTC8, PTE30); // in+ = PTC8, in- = 12-bit DAC 
00009 
00010 // Comparator callback functions
00011 void cmp_rise_ISR(void)
00012 {
00013     cmpled = 0;
00014 }
00015 
00016 void cmp_fall_ISR(void)
00017 {
00018     cmpled = 1;
00019 }
00020 
00021 
00022 
00023 int main()
00024 {
00025     cmp_en = 1;
00026     cmpled = 1;
00027 
00028     compi.rising(&cmp_rise_ISR);                // Set pointer to rising interrupt function
00029     compi.falling(&cmp_fall_ISR);               // Set pointer to falling interrupt function
00030     compi.treshold(0.3);                        // Set comparator threshold to 1V = 0.3 * 3.3V
00031 
00032     while(1)
00033     {
00034         printf("Light sensor : %7.5f Volt\n",cmp_lvl*3.3);
00035         blinker = 1;
00036         wait(1);
00037         blinker = 0;
00038         wait(0.2);
00039         if (compi.status() == 0x01)
00040         {
00041             printf("*** Treshold reached : %7.5f\n",cmp_lvl*3.3);
00042         }
00043     }
00044 }
00045 
00046 
00047