Thomas Lunzer / Mbed 2 deprecated revcounter

Dependencies:   TextLCD mbed SimpleIOMacros

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 Simple revolution counter, works stable for 1 - 50000 rpm's
00003 Uses 1 pulse/revolution at pin 10
00004 */
00005 #include "mbed.h"
00006 #include "TextLCD.h"
00007 #include "IOMacros.h"
00008 
00009 Ticker timer;
00010 Timer t;
00011 DigitalOut led(LED1);
00012 volatile float count,oldcount, revf;
00013 int rev;
00014 
00015 TextLCD lcd(p15, p16, p17, p18, p19, p20, TextLCD::LCD16x1);
00016 
00017 
00018 // Function prototype/forward declaration for ISR.
00019 void atint();
00020 
00021 /** EINT3_IRQHandler
00022  */
00023 extern "C" void EINT3_IRQHandler (void) {
00024 
00025     // The "event" is connected to pin p10 which is LPC1768 P0_1
00026     // so lets trap that and ignore all other GPIO interrupts.
00027     // Test for IRQ on Port0.
00028     if (LPC_GPIOINT->IntStatus & 0x1) {
00029         // If P0_1/p10 rises, call atint()
00030         if (LPC_GPIOINT->IO0IntStatR & (1 << 1))  atint();
00031     }
00032 
00033     // Clear this and all other possible GPIO generated interrupts as they don't concern us.
00034     LPC_GPIOINT->IO2IntClr = (LPC_GPIOINT->IO2IntStatR | LPC_GPIOINT->IO2IntStatF);
00035     LPC_GPIOINT->IO0IntClr = (LPC_GPIOINT->IO0IntStatR | LPC_GPIOINT->IO0IntStatF);
00036 }
00037 
00038 void event_irq_init(void) {
00039     // Use macro to set p10 as an input.
00040     p10_AS_INPUT;
00041     // Enable P0_1/p10 for rising edge interrupt generation.
00042     LPC_GPIOINT->IO0IntEnR |= (1UL << 1);
00043     // Enable the interrupt.
00044     NVIC_EnableIRQ(EINT3_IRQn);
00045 }
00046 
00047 void atint() {
00048     count = t.read();
00049     t.reset();
00050 }
00051 
00052 void attim() {
00053     LED1_TOGGLE; // Use macro instead of led =!led;
00054     if (count!=oldcount) {
00055         oldcount = count;
00056         rev= (60/count);
00057             if (rev >= 15000) // Reduce resolution to 100 rpm
00058                 rev = (rev/100)*100;
00059             else if (rev >= 1000) // Reduce resolution to 10 rpm
00060                 rev = (rev/10)*10;
00061         lcd.cls();
00062         lcd.printf("U/m %5d ",rev);
00063     }
00064 }
00065 
00066 int main() {
00067     count = 0;
00068     lcd.printf("Revolution counter",rev);
00069     wait(0.5);
00070     lcd.cls();
00071     timer.attach(&attim,1);
00072     event_irq_init();
00073     t.start();
00074     while (1) {
00075     }
00076 }