Simple program to demonstrate the basic functions of the analog comparator of the FRDM-KL25Z board.
Dependencies: ComparatorIn mbed
Fork of ComparatorIn_demo by
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:
- ComparatorIn written by Frank Vannieuwkerke
Hardware requirements:
- FRDM-KL25 board
- CdS photoresistor pulled up by 10 k
main.cpp@8:a5b18dfe684f, 2015-11-18 (annotated)
- Committer:
- icserny
- Date:
- Wed Nov 18 15:29:18 2015 +0000
- Revision:
- 8:a5b18dfe684f
- Parent:
- 7:a4f884972a0e
First version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
icserny | 8:a5b18dfe684f | 1 | /** 05_comparator_demo |
icserny | 7:a4f884972a0e | 2 | * Simple program to demonstrate the basic functions |
icserny | 7:a4f884972a0e | 3 | * of the analog comparator of the FRDM-KL25Z board. |
icserny | 7:a4f884972a0e | 4 | * |
icserny | 7:a4f884972a0e | 5 | * Used library: |
icserny | 7:a4f884972a0e | 6 | * - ComparatorIn //https://developer.mbed.org/users/frankvnk/code/ComparatorIn/ |
icserny | 7:a4f884972a0e | 7 | * written by Frank Vannieuwkerke |
icserny | 7:a4f884972a0e | 8 | * |
icserny | 7:a4f884972a0e | 9 | * Hardware requirements: |
icserny | 7:a4f884972a0e | 10 | * - FRDM-KL25 board |
icserny | 7:a4f884972a0e | 11 | * - CdS photoresistor pulled up by 10 k |
icserny | 7:a4f884972a0e | 12 | * - Connecect the midpoint of the divider to PTE29 |
icserny | 7:a4f884972a0e | 13 | * |
icserny | 7:a4f884972a0e | 14 | * 10k CdS photoresistor |
icserny | 7:a4f884972a0e | 15 | * +3.3V --/\/\/\---+---/\/\/\---- GND |
icserny | 7:a4f884972a0e | 16 | * | |
icserny | 7:a4f884972a0e | 17 | * PTE29 |
icserny | 7:a4f884972a0e | 18 | * |
icserny | 7:a4f884972a0e | 19 | */ |
frankvnk | 2:bb19e60cb933 | 20 | |
icserny | 7:a4f884972a0e | 21 | #include "mbed.h" |
icserny | 7:a4f884972a0e | 22 | #include "ComparatorIn.h" |
icserny | 7:a4f884972a0e | 23 | |
icserny | 7:a4f884972a0e | 24 | DigitalOut blinker (LED_BLUE); // blinking LED |
icserny | 7:a4f884972a0e | 25 | DigitalOut cmpled (LED_GREEN); // signing comparator status |
icserny | 7:a4f884972a0e | 26 | AnalogIn cmp_lvl (PTE29); // ADC input to check comparator input |
icserny | 7:a4f884972a0e | 27 | ComparatorIn compi(PTE29, PTE30); // in+ = PTE29, in- = 12-bit DAC |
icserny | 7:a4f884972a0e | 28 | |
icserny | 7:a4f884972a0e | 29 | // Comparator interrupt callback functions |
frankvnk | 2:bb19e60cb933 | 30 | void cmp_rise_ISR(void) |
frankvnk | 2:bb19e60cb933 | 31 | { |
icserny | 7:a4f884972a0e | 32 | cmpled = 0; // LED ON at rising edge |
frankvnk | 2:bb19e60cb933 | 33 | } |
icserny | 7:a4f884972a0e | 34 | |
frankvnk | 2:bb19e60cb933 | 35 | void cmp_fall_ISR(void) |
frankvnk | 2:bb19e60cb933 | 36 | { |
icserny | 7:a4f884972a0e | 37 | cmpled = 1; // LED OFF at falling edge |
frankvnk | 2:bb19e60cb933 | 38 | } |
icserny | 7:a4f884972a0e | 39 | |
icserny | 7:a4f884972a0e | 40 | |
icserny | 7:a4f884972a0e | 41 | |
frankvnk | 2:bb19e60cb933 | 42 | int main() |
frankvnk | 2:bb19e60cb933 | 43 | { |
icserny | 7:a4f884972a0e | 44 | cmpled = 1; // LED OFF at the beginning |
icserny | 7:a4f884972a0e | 45 | |
icserny | 7:a4f884972a0e | 46 | compi.rising(&cmp_rise_ISR); // Set pointer to rising interrupt function |
icserny | 7:a4f884972a0e | 47 | compi.falling(&cmp_fall_ISR); // Set pointer to falling interrupt function |
icserny | 7:a4f884972a0e | 48 | compi.treshold(0.3); // Set comparator threshold to 1V = 0.3 * 3.3V |
icserny | 7:a4f884972a0e | 49 | |
frankvnk | 2:bb19e60cb933 | 50 | while(1) |
frankvnk | 2:bb19e60cb933 | 51 | { |
frankvnk | 2:bb19e60cb933 | 52 | printf("Light sensor : %7.5f Volt\n",cmp_lvl*3.3); |
icserny | 7:a4f884972a0e | 53 | blinker = 1; // binking LED OFF |
icserny | 7:a4f884972a0e | 54 | wait(2); // wait for 2 sec |
icserny | 7:a4f884972a0e | 55 | blinker = 0; // blinking LED ON |
frankvnk | 2:bb19e60cb933 | 56 | wait(0.2); |
icserny | 7:a4f884972a0e | 57 | if (compi.status() == 0x01) // poll comparator status |
frankvnk | 2:bb19e60cb933 | 58 | { |
frankvnk | 2:bb19e60cb933 | 59 | printf("*** Treshold reached : %7.5f\n",cmp_lvl*3.3); |
frankvnk | 2:bb19e60cb933 | 60 | } |
frankvnk | 2:bb19e60cb933 | 61 | } |
frankvnk | 2:bb19e60cb933 | 62 | } |
icserny | 7:a4f884972a0e | 63 | |
icserny | 7:a4f884972a0e | 64 |