Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Mittelwert.cpp@0:648b08cf7fe8, 2018-11-15 (annotated)
- Committer:
- Wizo
- Date:
- Thu Nov 15 18:02:56 2018 +0000
- Revision:
- 0:648b08cf7fe8
Mittelwert
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Wizo | 0:648b08cf7fe8 | 1 | // Beispiel Mittelwerte |
| Wizo | 0:648b08cf7fe8 | 2 | // Peter Beck - 16.04.2018 |
| Wizo | 0:648b08cf7fe8 | 3 | |
| Wizo | 0:648b08cf7fe8 | 4 | #include "mbed.h" |
| Wizo | 0:648b08cf7fe8 | 5 | #include "C12832.h" |
| Wizo | 0:648b08cf7fe8 | 6 | |
| Wizo | 0:648b08cf7fe8 | 7 | C12832 lcd(p5, p7, p6, p8, p11); |
| Wizo | 0:648b08cf7fe8 | 8 | AnalogIn aiPoti1(p19); |
| Wizo | 0:648b08cf7fe8 | 9 | |
| Wizo | 0:648b08cf7fe8 | 10 | DigitalIn diDown(p12); |
| Wizo | 0:648b08cf7fe8 | 11 | DigitalIn diUp(p15); |
| Wizo | 0:648b08cf7fe8 | 12 | InterruptIn iiDown(p12); |
| Wizo | 0:648b08cf7fe8 | 13 | InterruptIn iiUp(p15); |
| Wizo | 0:648b08cf7fe8 | 14 | |
| Wizo | 0:648b08cf7fe8 | 15 | //AnalogIn aiPoti2(p20); |
| Wizo | 0:648b08cf7fe8 | 16 | Timeout BlinkTimeoutTmr; |
| Wizo | 0:648b08cf7fe8 | 17 | |
| Wizo | 0:648b08cf7fe8 | 18 | Ticker toreadPoti1; |
| Wizo | 0:648b08cf7fe8 | 19 | |
| Wizo | 0:648b08cf7fe8 | 20 | float poti1Wert; |
| Wizo | 0:648b08cf7fe8 | 21 | float poti1Durchschnitt; |
| Wizo | 0:648b08cf7fe8 | 22 | float poti1Array[10]; |
| Wizo | 0:648b08cf7fe8 | 23 | |
| Wizo | 0:648b08cf7fe8 | 24 | int slot = 0; |
| Wizo | 0:648b08cf7fe8 | 25 | int mode = 0; //Messmodus |
| Wizo | 0:648b08cf7fe8 | 26 | BusOut doLeds(LED1,LED2,LED3,LED4); |
| Wizo | 0:648b08cf7fe8 | 27 | |
| Wizo | 0:648b08cf7fe8 | 28 | |
| Wizo | 0:648b08cf7fe8 | 29 | // functions |
| Wizo | 0:648b08cf7fe8 | 30 | |
| Wizo | 0:648b08cf7fe8 | 31 | void BlinkTimeout() |
| Wizo | 0:648b08cf7fe8 | 32 | { |
| Wizo | 0:648b08cf7fe8 | 33 | doLeds = 0; |
| Wizo | 0:648b08cf7fe8 | 34 | } |
| Wizo | 0:648b08cf7fe8 | 35 | |
| Wizo | 0:648b08cf7fe8 | 36 | void readPoti1() |
| Wizo | 0:648b08cf7fe8 | 37 | { |
| Wizo | 0:648b08cf7fe8 | 38 | // Poti-Wert auslesen |
| Wizo | 0:648b08cf7fe8 | 39 | poti1Wert = 3.3 * aiPoti1.read(); |
| Wizo | 0:648b08cf7fe8 | 40 | |
| Wizo | 0:648b08cf7fe8 | 41 | poti1Array[slot] = poti1Wert; |
| Wizo | 0:648b08cf7fe8 | 42 | |
| Wizo | 0:648b08cf7fe8 | 43 | slot++; |
| Wizo | 0:648b08cf7fe8 | 44 | if (slot>9) slot = 0; |
| Wizo | 0:648b08cf7fe8 | 45 | |
| Wizo | 0:648b08cf7fe8 | 46 | // Ausgabe: |
| Wizo | 0:648b08cf7fe8 | 47 | |
| Wizo | 0:648b08cf7fe8 | 48 | |
| Wizo | 0:648b08cf7fe8 | 49 | poti1Durchschnitt = 0; |
| Wizo | 0:648b08cf7fe8 | 50 | for (int i=0; i<10; i++) { |
| Wizo | 0:648b08cf7fe8 | 51 | poti1Durchschnitt += poti1Array[slot]; |
| Wizo | 0:648b08cf7fe8 | 52 | } |
| Wizo | 0:648b08cf7fe8 | 53 | poti1Durchschnitt /= 10; |
| Wizo | 0:648b08cf7fe8 | 54 | |
| Wizo | 0:648b08cf7fe8 | 55 | lcd.locate(0,0); |
| Wizo | 0:648b08cf7fe8 | 56 | lcd.printf("Spannung aktuell = %5.3f V", poti1Wert); // Schreibe.. |
| Wizo | 0:648b08cf7fe8 | 57 | lcd.locate(0,10); // Setze Cursor auf X=0, Y=10 |
| Wizo | 0:648b08cf7fe8 | 58 | |
| Wizo | 0:648b08cf7fe8 | 59 | lcd.printf("Spannung gerundet = %.1f V", poti1Durchschnitt); // Schreibe.. |
| Wizo | 0:648b08cf7fe8 | 60 | lcd.fillrect(0, 20, (int)(poti1Wert*128/3.3), 24, 1); // Schreibe Block mit X=0,Y=20, X2=(errechnet), Y2=24 in der Farbe 1 |
| Wizo | 0:648b08cf7fe8 | 61 | lcd.fillrect(0, 25, (int)(poti1Durchschnitt*128/3.3), 28, 1); // Schreibe Block mit X=0,Y=25, X2=(errechnet), Y2=28 in der Farbe 1 |
| Wizo | 0:648b08cf7fe8 | 62 | |
| Wizo | 0:648b08cf7fe8 | 63 | lcd.fillrect((int)(poti1Durchschnitt*128/3.3), 20, 128, 28, 0); // Schreibe eine weissen Block auf der restlichen Fläche in der Farbe 1 |
| Wizo | 0:648b08cf7fe8 | 64 | lcd.copy_to_lcd(); |
| Wizo | 0:648b08cf7fe8 | 65 | |
| Wizo | 0:648b08cf7fe8 | 66 | } |
| Wizo | 0:648b08cf7fe8 | 67 | |
| Wizo | 0:648b08cf7fe8 | 68 | |
| Wizo | 0:648b08cf7fe8 | 69 | |
| Wizo | 0:648b08cf7fe8 | 70 | int main() |
| Wizo | 0:648b08cf7fe8 | 71 | { |
| Wizo | 0:648b08cf7fe8 | 72 | |
| Wizo | 0:648b08cf7fe8 | 73 | lcd.cls(); // löscht lcd (clear screen) |
| Wizo | 0:648b08cf7fe8 | 74 | lcd.locate(0,10); // Setze Cursor auf X=0, Y=10 |
| Wizo | 0:648b08cf7fe8 | 75 | lcd.printf("Willkommen!"); |
| Wizo | 0:648b08cf7fe8 | 76 | lcd.copy_to_lcd(); |
| Wizo | 0:648b08cf7fe8 | 77 | wait_ms(300); |
| Wizo | 0:648b08cf7fe8 | 78 | |
| Wizo | 0:648b08cf7fe8 | 79 | // Erstellung von 2 Arrays für die gleitende Mittelwertbildung |
| Wizo | 0:648b08cf7fe8 | 80 | // 2 Hz mit Ticker realisieren |
| Wizo | 0:648b08cf7fe8 | 81 | lcd.cls(); // löscht lcd (clear screen) |
| Wizo | 0:648b08cf7fe8 | 82 | lcd.locate(0,0); // x-position, y-position (x: 0-128; y: 0-32) |
| Wizo | 0:648b08cf7fe8 | 83 | lcd.printf("Werte von Poti 1 und 2:"); |
| Wizo | 0:648b08cf7fe8 | 84 | |
| Wizo | 0:648b08cf7fe8 | 85 | toreadPoti1.attach(&readPoti1, 0.5); |
| Wizo | 0:648b08cf7fe8 | 86 | |
| Wizo | 0:648b08cf7fe8 | 87 | |
| Wizo | 0:648b08cf7fe8 | 88 | while(1) { |
| Wizo | 0:648b08cf7fe8 | 89 | |
| Wizo | 0:648b08cf7fe8 | 90 | } |
| Wizo | 0:648b08cf7fe8 | 91 | } |
| Wizo | 0:648b08cf7fe8 | 92 | |
| Wizo | 0:648b08cf7fe8 | 93 |