Dependencies:   mbed

Committer:
martwerl
Date:
Thu Nov 15 18:10:36 2018 +0000
Revision:
0:3b4026dea19d
StopwatchLCD

Who changed what in which revision?

UserRevisionLine numberNew contents of line
martwerl 0:3b4026dea19d 1 // Stopuhr: min:sec:hsec am LCD
martwerl 0:3b4026dea19d 2 // Start: Joystick Up
martwerl 0:3b4026dea19d 3 // Stopp: Joystick Down
martwerl 0:3b4026dea19d 4 // Reset: Joystick Center (sets value to 0 and retarts ..)
martwerl 0:3b4026dea19d 5
martwerl 0:3b4026dea19d 6 #include "mbed.h"
martwerl 0:3b4026dea19d 7 #include "C12832_lcd.h"
martwerl 0:3b4026dea19d 8
martwerl 0:3b4026dea19d 9 InterruptIn iiUp(p15);
martwerl 0:3b4026dea19d 10 DigitalIn diUp(p15);
martwerl 0:3b4026dea19d 11 InterruptIn iiDown(p12);
martwerl 0:3b4026dea19d 12 DigitalIn diDown(p12);
martwerl 0:3b4026dea19d 13 InterruptIn iiCenter(p14);
martwerl 0:3b4026dea19d 14 DigitalIn diCenter(p14);
martwerl 0:3b4026dea19d 15 Ticker myTimer;
martwerl 0:3b4026dea19d 16 uint32_t hSec;
martwerl 0:3b4026dea19d 17 bool runFlag = false;
martwerl 0:3b4026dea19d 18 bool updateLcd = true;
martwerl 0:3b4026dea19d 19
martwerl 0:3b4026dea19d 20 // prototypes
martwerl 0:3b4026dea19d 21
martwerl 0:3b4026dea19d 22 // functions
martwerl 0:3b4026dea19d 23 // Diese Funktion ist zum Entprellen von Tastern geeignet:
martwerl 0:3b4026dea19d 24 // Durch den zusätzlichen Code kann eine Entprellzeit von durchschnittlich 1,5-4ms
martwerl 0:3b4026dea19d 25 // (mindestens 8*150us = 1,2ms) erreicht werden.
martwerl 0:3b4026dea19d 26 // Grundsätzlich prüft die Funktion den Pegel des Pins eine DigitalIn.
martwerl 0:3b4026dea19d 27 // Wenn der Pegel 8 Mal konstant war, wird die Schleife verlassen.
martwerl 0:3b4026dea19d 28 // Diese Funktion kann sehr gut eingesetzt werden, um in einer Endlosschleife Taster
martwerl 0:3b4026dea19d 29 // anzufragen, da sie, wie erwähnt, eine kurze Wartezeit hat.
martwerl 0:3b4026dea19d 30 uint8_t debounce(DigitalIn myIn)
martwerl 0:3b4026dea19d 31 {
martwerl 0:3b4026dea19d 32 #define LEVEL_CHECKS 8
martwerl 0:3b4026dea19d 33 #define MAX_LOOPS 30 // stoppt das Überprüfen des Prellen nach max. MAX_LOOPS Durchläufen
martwerl 0:3b4026dea19d 34 unsigned char port_buffer;
martwerl 0:3b4026dea19d 35 unsigned char debounceCounter = 0;
martwerl 0:3b4026dea19d 36 uint8_t loopCounter = 0;
martwerl 0:3b4026dea19d 37
martwerl 0:3b4026dea19d 38 do
martwerl 0:3b4026dea19d 39 {
martwerl 0:3b4026dea19d 40 port_buffer = myIn;
martwerl 0:3b4026dea19d 41 wait_us(100);
martwerl 0:3b4026dea19d 42 loopCounter++;
martwerl 0:3b4026dea19d 43 if(myIn == port_buffer)
martwerl 0:3b4026dea19d 44 debounceCounter++; // mindestens 'LEVEL_CHECKS' Abtastungen in Folge: gleicher Pegel
martwerl 0:3b4026dea19d 45 else
martwerl 0:3b4026dea19d 46 debounceCounter = 0;
martwerl 0:3b4026dea19d 47 }
martwerl 0:3b4026dea19d 48 while ((debounceCounter <= LEVEL_CHECKS) && (loopCounter <= MAX_LOOPS));
martwerl 0:3b4026dea19d 49 return loopCounter;
martwerl 0:3b4026dea19d 50 }
martwerl 0:3b4026dea19d 51
martwerl 0:3b4026dea19d 52 // ISR
martwerl 0:3b4026dea19d 53 void cntUp() { // Start stop watch
martwerl 0:3b4026dea19d 54 debounce(diUp);
martwerl 0:3b4026dea19d 55 runFlag = true;
martwerl 0:3b4026dea19d 56 }
martwerl 0:3b4026dea19d 57
martwerl 0:3b4026dea19d 58 void cntDown() { // Stopp stop watch
martwerl 0:3b4026dea19d 59 debounce(diDown);
martwerl 0:3b4026dea19d 60 runFlag = false;
martwerl 0:3b4026dea19d 61 updateLcd = true;
martwerl 0:3b4026dea19d 62 }
martwerl 0:3b4026dea19d 63
martwerl 0:3b4026dea19d 64 void cntReset() { // reset stop watch
martwerl 0:3b4026dea19d 65 debounce(diCenter);
martwerl 0:3b4026dea19d 66 hSec = 0;
martwerl 0:3b4026dea19d 67 updateLcd = true;
martwerl 0:3b4026dea19d 68 }
martwerl 0:3b4026dea19d 69
martwerl 0:3b4026dea19d 70 void count10msec(void) {
martwerl 0:3b4026dea19d 71 if (runFlag) {
martwerl 0:3b4026dea19d 72 hSec++;
martwerl 0:3b4026dea19d 73 if (hSec%10 == 0) // update every 100 msec
martwerl 0:3b4026dea19d 74 updateLcd = true;
martwerl 0:3b4026dea19d 75 }
martwerl 0:3b4026dea19d 76 }
martwerl 0:3b4026dea19d 77
martwerl 0:3b4026dea19d 78 // main program
martwerl 0:3b4026dea19d 79 int main() {
martwerl 0:3b4026dea19d 80 uint32_t tempHsec=0;
martwerl 0:3b4026dea19d 81 uint8_t sec=0;
martwerl 0:3b4026dea19d 82 uint16_t min=0;
martwerl 0:3b4026dea19d 83 C12832_LCD lcd;
martwerl 0:3b4026dea19d 84 lcd.cls();
martwerl 0:3b4026dea19d 85 lcd.locate(0,0);
martwerl 0:3b4026dea19d 86 lcd.printf("BULME 5ABELI Stopuhr");
martwerl 0:3b4026dea19d 87 iiUp.rise(&cntUp);
martwerl 0:3b4026dea19d 88 iiDown.rise(&cntDown);
martwerl 0:3b4026dea19d 89 iiCenter.rise(&cntReset);
martwerl 0:3b4026dea19d 90 myTimer.attach(&count10msec, 0.01); // Hunderstel-Sec
martwerl 0:3b4026dea19d 91
martwerl 0:3b4026dea19d 92 while(1) {
martwerl 0:3b4026dea19d 93 if (updateLcd) {
martwerl 0:3b4026dea19d 94 updateLcd = false;
martwerl 0:3b4026dea19d 95 lcd.locate(0,10);
martwerl 0:3b4026dea19d 96 tempHsec = hSec;
martwerl 0:3b4026dea19d 97 min = tempHsec/(100*60);
martwerl 0:3b4026dea19d 98 sec = tempHsec/100 - min*60;
martwerl 0:3b4026dea19d 99 lcd.printf("%02u:%02u:%02u", min, sec, tempHsec%100);
martwerl 0:3b4026dea19d 100 }
martwerl 0:3b4026dea19d 101 }
martwerl 0:3b4026dea19d 102 }
martwerl 0:3b4026dea19d 103
martwerl 0:3b4026dea19d 104