Einführung HAPO WS2020 TH Bingen, Prof. Jens Altenburg, LV2

Dependencies:   mbed

Committer:
prof_al
Date:
Wed Nov 18 14:32:21 2020 +0000
Revision:
0:d56a66901ae9
Version 1.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
prof_al 0:d56a66901ae9 1 /***************************************************************************
prof_al 0:d56a66901ae9 2 * Titel : main.c
prof_al 0:d56a66901ae9 3 *
prof_al 0:d56a66901ae9 4 * Description : Labormodul "DIBI"
prof_al 0:d56a66901ae9 5 *
prof_al 0:d56a66901ae9 6 * Revision List:
prof_al 0:d56a66901ae9 7 --------------------------------------------------------------------------
prof_al 0:d56a66901ae9 8 Date | Author | Change
prof_al 0:d56a66901ae9 9 --------------------------------------------------------------------------
prof_al 0:d56a66901ae9 10 16.11.15 | J. Altenburg | Ersterstellung
prof_al 0:d56a66901ae9 11 --------------------------------------------------------------------------
prof_al 0:d56a66901ae9 12 | |
prof_al 0:d56a66901ae9 13 ----------------------------------------------------------------------------
prof_al 0:d56a66901ae9 14 ****************************************************************************/
prof_al 0:d56a66901ae9 15 #include "mbed.h"
prof_al 0:d56a66901ae9 16 #include "cdef.h"
prof_al 0:d56a66901ae9 17
prof_al 0:d56a66901ae9 18 /* Vereinbarungen zu den Tasteneingängen */
prof_al 0:d56a66901ae9 19 DigitalIn pinS1(A1);
prof_al 0:d56a66901ae9 20 DigitalIn pinS2(A2);
prof_al 0:d56a66901ae9 21 DigitalIn pinS3(A3);
prof_al 0:d56a66901ae9 22
prof_al 0:d56a66901ae9 23 //BusOut bLedShield(D13, D12, D11, D10);
prof_al 0:d56a66901ae9 24 BusOut bLedShield(D10, D11, D12, D13);
prof_al 0:d56a66901ae9 25
prof_al 0:d56a66901ae9 26 /* Anschlüsse der Schieberegister */
prof_al 0:d56a66901ae9 27 DigitalOut pinSdo(PA_9);
prof_al 0:d56a66901ae9 28 DigitalOut pinSck(PA_8);
prof_al 0:d56a66901ae9 29 DigitalOut pinLoad(PB_5);
prof_al 0:d56a66901ae9 30
prof_al 0:d56a66901ae9 31 /* ISR */
prof_al 0:d56a66901ae9 32 Ticker stTimer;
prof_al 0:d56a66901ae9 33
prof_al 0:d56a66901ae9 34 /***************************************************************************
prof_al 0:d56a66901ae9 35 * Defines und Makros *
prof_al 0:d56a66901ae9 36 ***************************************************************************/
prof_al 0:d56a66901ae9 37 //#define nTEMP /* Compilerschalter */
prof_al 0:d56a66901ae9 38
prof_al 0:d56a66901ae9 39
prof_al 0:d56a66901ae9 40 #define nTastShort 4 /* Taste 50 Millisekunden gedrückt */
prof_al 0:d56a66901ae9 41 #define nTastLong 150 /* Taste 1.5 Sekunden gedrückt */
prof_al 0:d56a66901ae9 42
prof_al 0:d56a66901ae9 43 #define n300ms 3
prof_al 0:d56a66901ae9 44
prof_al 0:d56a66901ae9 45 #define nDsp100ms 1
prof_al 0:d56a66901ae9 46 #define nDsp500ms 5
prof_al 0:d56a66901ae9 47 #define nDsp1Sek 10
prof_al 0:d56a66901ae9 48 #define nDsp2Sek 20
prof_al 0:d56a66901ae9 49
prof_al 0:d56a66901ae9 50 #define nOn 0
prof_al 0:d56a66901ae9 51 #define nOff 1
prof_al 0:d56a66901ae9 52
prof_al 0:d56a66901ae9 53 #define nSegA 0x01
prof_al 0:d56a66901ae9 54 #define nSegB 0x02
prof_al 0:d56a66901ae9 55 #define nSegC 0x04
prof_al 0:d56a66901ae9 56 #define nSegD 0x08
prof_al 0:d56a66901ae9 57 #define nSegE 0x10
prof_al 0:d56a66901ae9 58 #define nSegF 0x20
prof_al 0:d56a66901ae9 59 #define nSegG 0x40
prof_al 0:d56a66901ae9 60 #define nSegH 0x80
prof_al 0:d56a66901ae9 61
prof_al 0:d56a66901ae9 62
prof_al 0:d56a66901ae9 63 #define nZiffer0 ~(nSegA | nSegB | nSegC | nSegD | nSegE | nSegF)
prof_al 0:d56a66901ae9 64 #define nZiffer1
prof_al 0:d56a66901ae9 65 #define nZiffer2
prof_al 0:d56a66901ae9 66 #define nZiffer3
prof_al 0:d56a66901ae9 67 #define nZiffer4
prof_al 0:d56a66901ae9 68 #define nZiffer5
prof_al 0:d56a66901ae9 69 #define nZiffer6
prof_al 0:d56a66901ae9 70 #define nZiffer7
prof_al 0:d56a66901ae9 71 #define nZiffer8
prof_al 0:d56a66901ae9 72 #define nZiffer9
prof_al 0:d56a66901ae9 73 #define nZifferA
prof_al 0:d56a66901ae9 74 #define nZifferB
prof_al 0:d56a66901ae9 75 #define nZifferC
prof_al 0:d56a66901ae9 76 #define nZifferD
prof_al 0:d56a66901ae9 77 #define nZifferE
prof_al 0:d56a66901ae9 78 #define nZifferF
prof_al 0:d56a66901ae9 79 #define nZifferOff
prof_al 0:d56a66901ae9 80
prof_al 0:d56a66901ae9 81 enum{
prof_al 0:d56a66901ae9 82 enDoNothing = 0
prof_al 0:d56a66901ae9 83 ,enDisplay
prof_al 0:d56a66901ae9 84 };
prof_al 0:d56a66901ae9 85
prof_al 0:d56a66901ae9 86
prof_al 0:d56a66901ae9 87
prof_al 0:d56a66901ae9 88 byte bIdx; /*
prof_al 0:d56a66901ae9 89 * Description : Index auf Ausgabe
prof_al 0:d56a66901ae9 90 */
prof_al 0:d56a66901ae9 91
prof_al 0:d56a66901ae9 92 int8 iDir; /*
prof_al 0:d56a66901ae9 93 * Description : Zählrichtung
prof_al 0:d56a66901ae9 94 */
prof_al 0:d56a66901ae9 95
prof_al 0:d56a66901ae9 96 byte bBlinkTime; /*
prof_al 0:d56a66901ae9 97 * Description :
prof_al 0:d56a66901ae9 98 */
prof_al 0:d56a66901ae9 99
prof_al 0:d56a66901ae9 100 byte bS1Timer; /*
prof_al 0:d56a66901ae9 101 * Description : Timer für Taste S1
prof_al 0:d56a66901ae9 102 */
prof_al 0:d56a66901ae9 103
prof_al 0:d56a66901ae9 104 byte bS2Timer; /*
prof_al 0:d56a66901ae9 105 * Description : Timer für Taste S2
prof_al 0:d56a66901ae9 106 */
prof_al 0:d56a66901ae9 107
prof_al 0:d56a66901ae9 108 byte bS3Timer; /*
prof_al 0:d56a66901ae9 109 * Description : Timer für Taste S3
prof_al 0:d56a66901ae9 110 */
prof_al 0:d56a66901ae9 111
prof_al 0:d56a66901ae9 112 byte bDisplayState; /*
prof_al 0:d56a66901ae9 113 * Description : Zustandsautomat Blinker
prof_al 0:d56a66901ae9 114 */
prof_al 0:d56a66901ae9 115
prof_al 0:d56a66901ae9 116 word wSerial; /*
prof_al 0:d56a66901ae9 117 * Description : serielles Ausgaberegister
prof_al 0:d56a66901ae9 118 */
prof_al 0:d56a66901ae9 119
prof_al 0:d56a66901ae9 120 byte bDspTime; /*
prof_al 0:d56a66901ae9 121 * Description : Displaytimer
prof_al 0:d56a66901ae9 122 */
prof_al 0:d56a66901ae9 123
prof_al 0:d56a66901ae9 124 byte bDspTimeValue; /*
prof_al 0:d56a66901ae9 125 * Description : Zählzeitvorgabe
prof_al 0:d56a66901ae9 126 */
prof_al 0:d56a66901ae9 127
prof_al 0:d56a66901ae9 128 byte bBargraph; /*
prof_al 0:d56a66901ae9 129 * Description : Bargraph anzeigen
prof_al 0:d56a66901ae9 130 */
prof_al 0:d56a66901ae9 131
prof_al 0:d56a66901ae9 132 byte bBarBlink; /*
prof_al 0:d56a66901ae9 133 * Description : Bargraphblinker
prof_al 0:d56a66901ae9 134 */
prof_al 0:d56a66901ae9 135
prof_al 0:d56a66901ae9 136 /* Bitmuster */
prof_al 0:d56a66901ae9 137 const byte abBargraph[] = {~Bit0, ~(Bit0|Bit1), ~(Bit0|Bit1|Bit2), ~(Bit0|Bit1|Bit2|Bit3), 0x0f};
prof_al 0:d56a66901ae9 138 const byte abZiffer[] = {nZiffer0, nZiffer1, nZiffer2, nZiffer3, nZiffer4, nZiffer5, nZiffer6, nZiffer7, nZiffer8, nZiffer9, nZifferA, nZifferB, nZifferC, nZifferD, nZifferE, nZifferF, nZifferOff};
prof_al 0:d56a66901ae9 139 const byte abDspTime[] = {nDsp100ms, nDsp500ms, nDsp1Sek, nDsp2Sek};
prof_al 0:d56a66901ae9 140
prof_al 0:d56a66901ae9 141
prof_al 0:d56a66901ae9 142 /* Tastenabfrage */
prof_al 0:d56a66901ae9 143 void vTasteS1( void ){
prof_al 0:d56a66901ae9 144 if(pinS1 == 0){ /* User-Taste gedrückt */
prof_al 0:d56a66901ae9 145 if(bS1Timer < nTastLong+1){ /* Abfrage max. bis Taste lange gedrückt */
prof_al 0:d56a66901ae9 146 if(bS1Timer == nTastShort){ /* Taste "kurz gedrückt" erkannt */
prof_al 0:d56a66901ae9 147 #if 1
prof_al 0:d56a66901ae9 148 //...
prof_al 0:d56a66901ae9 149 #endif
prof_al 0:d56a66901ae9 150 }
prof_al 0:d56a66901ae9 151 if(bS1Timer == nTastLong){ /* Taste "lange gedrückt" erkannt */
prof_al 0:d56a66901ae9 152 //bBlinkState = enBlinkLong;
prof_al 0:d56a66901ae9 153 }
prof_al 0:d56a66901ae9 154 bS1Timer++;
prof_al 0:d56a66901ae9 155 }
prof_al 0:d56a66901ae9 156 }
prof_al 0:d56a66901ae9 157 else{ /* Taste nicht gedrückt */
prof_al 0:d56a66901ae9 158 if(bS1Timer >= nTastShort){ /* Taste wurde bereits erkannt */
prof_al 0:d56a66901ae9 159 #if 0
prof_al 0:d56a66901ae9 160
prof_al 0:d56a66901ae9 161 #endif
prof_al 0:d56a66901ae9 162 }
prof_al 0:d56a66901ae9 163 bS1Timer = 0;
prof_al 0:d56a66901ae9 164 }
prof_al 0:d56a66901ae9 165 }
prof_al 0:d56a66901ae9 166
prof_al 0:d56a66901ae9 167 void vTasteS2( void ){
prof_al 0:d56a66901ae9 168 if(pinS2 == 0){ /* User-Taste gedrückt */
prof_al 0:d56a66901ae9 169 if(bS2Timer < nTastLong+1){ /* Abfrage max. bis Taste lange gedrückt */
prof_al 0:d56a66901ae9 170 if(bS2Timer == nTastShort){ /* Taste "kurz gedrückt" erkannt */
prof_al 0:d56a66901ae9 171 #if 1
prof_al 0:d56a66901ae9 172 //...
prof_al 0:d56a66901ae9 173 #endif
prof_al 0:d56a66901ae9 174 }
prof_al 0:d56a66901ae9 175 if(bS2Timer == nTastLong){ /* Taste "lange gedrückt" erkannt */
prof_al 0:d56a66901ae9 176 }
prof_al 0:d56a66901ae9 177 bS2Timer++;
prof_al 0:d56a66901ae9 178 }
prof_al 0:d56a66901ae9 179 }
prof_al 0:d56a66901ae9 180 else{ /* Taste nicht gedrückt */
prof_al 0:d56a66901ae9 181 if(bS2Timer >= nTastShort){ /* Taste wurde bereits erkannt */
prof_al 0:d56a66901ae9 182 }
prof_al 0:d56a66901ae9 183 bS2Timer = 0;
prof_al 0:d56a66901ae9 184 }
prof_al 0:d56a66901ae9 185 }
prof_al 0:d56a66901ae9 186
prof_al 0:d56a66901ae9 187 void vTasteS3( void ){
prof_al 0:d56a66901ae9 188 if(pinS3 == 0){ /* User-Taste gedrückt */
prof_al 0:d56a66901ae9 189 if(bS3Timer < nTastLong+1){ /* Abfrage max. bis Taste lange gedrückt */
prof_al 0:d56a66901ae9 190 if(bS3Timer == nTastShort){ /* Taste "kurz gedrückt" erkannt */
prof_al 0:d56a66901ae9 191 #if 1
prof_al 0:d56a66901ae9 192 //...
prof_al 0:d56a66901ae9 193 #endif
prof_al 0:d56a66901ae9 194 }
prof_al 0:d56a66901ae9 195 if(bS3Timer == nTastLong){ /* Taste "lange gedrückt" erkannt */
prof_al 0:d56a66901ae9 196 }
prof_al 0:d56a66901ae9 197 bS3Timer++;
prof_al 0:d56a66901ae9 198 }
prof_al 0:d56a66901ae9 199 }
prof_al 0:d56a66901ae9 200 else{ /* Taste nicht gedrückt */
prof_al 0:d56a66901ae9 201 if(bS3Timer >= nTastShort){ /* Taste wurde bereits erkannt */
prof_al 0:d56a66901ae9 202 }
prof_al 0:d56a66901ae9 203 bS3Timer = 0;
prof_al 0:d56a66901ae9 204 }
prof_al 0:d56a66901ae9 205 }
prof_al 0:d56a66901ae9 206
prof_al 0:d56a66901ae9 207
prof_al 0:d56a66901ae9 208
prof_al 0:d56a66901ae9 209 void vCheckTasten( void ){
prof_al 0:d56a66901ae9 210 vTasteS1();
prof_al 0:d56a66901ae9 211 vTasteS2();
prof_al 0:d56a66901ae9 212 vTasteS3();
prof_al 0:d56a66901ae9 213 }
prof_al 0:d56a66901ae9 214
prof_al 0:d56a66901ae9 215 void vDisplayLed( void ){
prof_al 0:d56a66901ae9 216 switch(bDisplayState){
prof_al 0:d56a66901ae9 217 case enDoNothing: /* Bargraph blinken lassen */
prof_al 0:d56a66901ae9 218 bBarBlink++;
prof_al 0:d56a66901ae9 219 //...
prof_al 0:d56a66901ae9 220 break;
prof_al 0:d56a66901ae9 221 case enDisplay: /* Zählwerte für Anzeige ermitteln */
prof_al 0:d56a66901ae9 222 //...
prof_al 0:d56a66901ae9 223 break;
prof_al 0:d56a66901ae9 224 }
prof_al 0:d56a66901ae9 225 wSerial = abZiffer[bIdx&0x0f];
prof_al 0:d56a66901ae9 226 }
prof_al 0:d56a66901ae9 227
prof_al 0:d56a66901ae9 228 /* Ausschieben der Bits für die Anzeige */
prof_al 0:d56a66901ae9 229 void vShiftLoad(word wLoad){
prof_al 0:d56a66901ae9 230 word wMask = Bit15;
prof_al 0:d56a66901ae9 231 pinLoad = 0;
prof_al 0:d56a66901ae9 232 while(wMask){
prof_al 0:d56a66901ae9 233 if(wMask & wLoad) pinSdo = 1;
prof_al 0:d56a66901ae9 234 else pinSdo = 0;
prof_al 0:d56a66901ae9 235 pinSck = 1;
prof_al 0:d56a66901ae9 236 wMask >>= 1;
prof_al 0:d56a66901ae9 237 pinSck = 0;
prof_al 0:d56a66901ae9 238 }
prof_al 0:d56a66901ae9 239 pinLoad = 1;
prof_al 0:d56a66901ae9 240 }
prof_al 0:d56a66901ae9 241
prof_al 0:d56a66901ae9 242
prof_al 0:d56a66901ae9 243 /* Ausgabe der Daten an das 7-Segment-Display im Abstand von 2.5 ms */
prof_al 0:d56a66901ae9 244 void vSegmentDisplay( void ){
prof_al 0:d56a66901ae9 245 vShiftLoad(((wSerial << 8) & 0xfff0)| (1 << 3));
prof_al 0:d56a66901ae9 246 }
prof_al 0:d56a66901ae9 247
prof_al 0:d56a66901ae9 248 /***************************************************************************
prof_al 0:d56a66901ae9 249 * main()
prof_al 0:d56a66901ae9 250 ***************************************************************************/
prof_al 0:d56a66901ae9 251 int main() {
prof_al 0:d56a66901ae9 252 /* Variablen initialisieren */
prof_al 0:d56a66901ae9 253 byte i = 0;
prof_al 0:d56a66901ae9 254 bIdx = 0;
prof_al 0:d56a66901ae9 255 bDspTime = 0;
prof_al 0:d56a66901ae9 256 bDisplayState = enDoNothing;
prof_al 0:d56a66901ae9 257 stTimer.attach_us(&vSegmentDisplay, 2500);
prof_al 0:d56a66901ae9 258 bBargraph = 0;
prof_al 0:d56a66901ae9 259 bBarBlink = 0;
prof_al 0:d56a66901ae9 260 bDspTimeValue = nDsp100ms;
prof_al 0:d56a66901ae9 261 iDir = 1;
prof_al 0:d56a66901ae9 262 /* Lampentest */
prof_al 0:d56a66901ae9 263 bLedShield = 0x00;
prof_al 0:d56a66901ae9 264 wait(1);
prof_al 0:d56a66901ae9 265 bLedShield = 0x0f;
prof_al 0:d56a66901ae9 266 /* Endless loop */
prof_al 0:d56a66901ae9 267 while(1) {
prof_al 0:d56a66901ae9 268 vCheckTasten();
prof_al 0:d56a66901ae9 269 i++;
prof_al 0:d56a66901ae9 270 if(i == 10){
prof_al 0:d56a66901ae9 271 vDisplayLed();
prof_al 0:d56a66901ae9 272 i = 0;
prof_al 0:d56a66901ae9 273 }
prof_al 0:d56a66901ae9 274 wait_us(10000);
prof_al 0:d56a66901ae9 275 }
prof_al 0:d56a66901ae9 276 }