Udo Juerss / Mbed 2 deprecated Nucleo_Photo_LED

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Module..: Photo_LED
00002 // Version.: 1.0
00003 // Chip....: STM32F411RE
00004 // Date....: November 2014
00005 // Author..: UJ
00006 //-----------------------------------------------------------------------------
00007 
00008 // This program is a simple demonstration how a normal LED can be used as a
00009 // photo diode. The hardware platform I have used is the NUCLEO F411RE, but
00010 // the principle will run on every modern controller with high-impedance
00011 // Schmitt-trigger inputs.
00012 //
00013 // Harware sketch:
00014 //      _____________________
00015 //     |                     |
00016 //     |                   __|__
00017 //     |                   \   / LED (20mA, green or red)
00018 //     |                    \ /
00019 //     |                  -------
00020 //     |                     |
00021 //     |                    _|_
00022 //     |                   |   |
00023 //     |                   |100| Ohm
00024 //     |                   |_ _|
00025 //     |                     |
00026 //     |                     |
00027 //     |                     |
00028 //     Anode Pin (PC10)      Cathode Pin (PC11)
00029 //
00030 // How it works:
00031 // In the first phase the LED is turned on in it´s normal mode for 50ms.
00032 // In the second phase the LED is driven with a reversed voltage for 5ms
00033 // (anode negative, cathode positive). This will charge the internal capacity
00034 // with reverse potential. Then the anode pin is configured as a high-impedance
00035 // input pin. It takes some time until the voltage at the anode pin will raise
00036 // above the Schmitt-trigger level. This time is depending on the over all
00037 // circuit resistance and the ambient light.
00038 //
00039 // Hint:
00040 // The capacity of the LED is very low (5..35pF). Do not place the board
00041 // to a conductive ground. Even humidity will change the blink frequency.
00042 // Do not touch the components.
00043 //-----------------------------------------------------------------------------
00044 
00045 #include "mbed.h"
00046 //-----------------------------------------------------------------------------
00047 
00048 #define IWDG_RESET() (IWDG->KR = 0xAAAA)
00049 //-----------------------------------------------------------------------------
00050 
00051 Serial terminal(USBTX, USBRX);
00052 
00053 DigitalInOut anode_pin(PC_10);
00054 DigitalOut cathode_pin(PC_11);
00055 //-----------------------------------------------------------------------------
00056 
00057 int main(void)
00058 {
00059   unsigned char cnt;
00060 
00061   terminal.baud(115200);
00062   terminal.printf("Photo LED\r\n");
00063 
00064   while (1)
00065   {
00066     IWDG_RESET();
00067 
00068     // Set normal LED operation, turn on LED for 50ms
00069     // Setup PC10 as output
00070     anode_pin.output();
00071 
00072     // Turn on LED for 50ms
00073     // Set anode (PC10) high and cathode (PC11) low
00074     anode_pin = 1;
00075     cathode_pin = 0;
00076 
00077     for (cnt = 0; cnt < 10; cnt++)
00078     {
00079       wait(0.005f);
00080       IWDG_RESET();
00081     }
00082 
00083     // Apply reverse voltage to the LED for reverse charging the LED´s capacity
00084     // Set anode (PC10) low and cathode (PC11) high
00085     anode_pin = 0;
00086     cathode_pin = 1;
00087     wait(0.005f);
00088 
00089     // Setup anode (PC10) as input
00090     anode_pin.input();
00091 
00092     // Wait until the input voltage raises above the Schmitt trigger level
00093     // This time is strongly ambient light depending
00094     while (anode_pin.read() == 0)
00095     {
00096       IWDG_RESET();
00097       wait(0.005f);
00098     }
00099   }
00100 }