Lukas Adlboller
/
4CHEL_Labor_DAC
DIC DAC Beispiel
main.cpp@1:c8943c51b10d, 2020-01-13 (annotated)
- Committer:
- Lukas_Adlboller
- Date:
- Mon Jan 13 10:14:10 2020 +0000
- Revision:
- 1:c8943c51b10d
- Parent:
- 0:2aca3f53fe79
NEW VERSION!!
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Lukas_Adlboller | 0:2aca3f53fe79 | 1 | #include "mbed.h" |
Lukas_Adlboller | 0:2aca3f53fe79 | 2 | |
Lukas_Adlboller | 0:2aca3f53fe79 | 3 | #define PI 3.14159 |
Lukas_Adlboller | 0:2aca3f53fe79 | 4 | |
Lukas_Adlboller | 0:2aca3f53fe79 | 5 | AnalogOut dacOutput(p18); // Ausgangs Pin 18 |
Lukas_Adlboller | 0:2aca3f53fe79 | 6 | Ticker valueTicker; |
Lukas_Adlboller | 0:2aca3f53fe79 | 7 | float voltageValues[100]; // 100 Punkte |
Lukas_Adlboller | 0:2aca3f53fe79 | 8 | int actualPoint; // Aktueller Punkt |
Lukas_Adlboller | 1:c8943c51b10d | 9 | int outputMode = 4; // 1 = Sägezahn, 2 = Rechteck, 3 = Sinus, 4 = Dreieck |
Lukas_Adlboller | 0:2aca3f53fe79 | 10 | |
Lukas_Adlboller | 0:2aca3f53fe79 | 11 | void outputToDAC() |
Lukas_Adlboller | 0:2aca3f53fe79 | 12 | { |
Lukas_Adlboller | 0:2aca3f53fe79 | 13 | dacOutput.write(voltageValues[actualPoint]); |
Lukas_Adlboller | 0:2aca3f53fe79 | 14 | actualPoint++; |
Lukas_Adlboller | 0:2aca3f53fe79 | 15 | if(actualPoint >= 100) |
Lukas_Adlboller | 0:2aca3f53fe79 | 16 | { |
Lukas_Adlboller | 0:2aca3f53fe79 | 17 | actualPoint = 0; |
Lukas_Adlboller | 0:2aca3f53fe79 | 18 | } |
Lukas_Adlboller | 0:2aca3f53fe79 | 19 | } |
Lukas_Adlboller | 0:2aca3f53fe79 | 20 | |
Lukas_Adlboller | 0:2aca3f53fe79 | 21 | int main() |
Lukas_Adlboller | 0:2aca3f53fe79 | 22 | { |
Lukas_Adlboller | 0:2aca3f53fe79 | 23 | actualPoint = 0; |
Lukas_Adlboller | 0:2aca3f53fe79 | 24 | |
Lukas_Adlboller | 0:2aca3f53fe79 | 25 | for(int i = 0; i < 100; i++) |
Lukas_Adlboller | 0:2aca3f53fe79 | 26 | { |
Lukas_Adlboller | 0:2aca3f53fe79 | 27 | switch(outputMode) |
Lukas_Adlboller | 0:2aca3f53fe79 | 28 | { |
Lukas_Adlboller | 0:2aca3f53fe79 | 29 | case 1: |
Lukas_Adlboller | 1:c8943c51b10d | 30 | voltageValues[i] = (float)i / 100.f;; |
Lukas_Adlboller | 0:2aca3f53fe79 | 31 | break; |
Lukas_Adlboller | 0:2aca3f53fe79 | 32 | case 2: |
Lukas_Adlboller | 0:2aca3f53fe79 | 33 | voltageValues[i] = i <= 49 ? 1 : 0; |
Lukas_Adlboller | 0:2aca3f53fe79 | 34 | break; |
Lukas_Adlboller | 0:2aca3f53fe79 | 35 | case 3: |
Lukas_Adlboller | 0:2aca3f53fe79 | 36 | voltageValues[i] = 0.5f * sin(((float)i * PI) * 3.6f / 180) + 0.5f; // w = 6283 = 2 * PI * f |
Lukas_Adlboller | 0:2aca3f53fe79 | 37 | break; |
Lukas_Adlboller | 1:c8943c51b10d | 38 | case 4: |
Lukas_Adlboller | 1:c8943c51b10d | 39 | voltageValues[i] = (i > 50 ? i * 2 - 100.f : 100.f - i * 2 ) / 100.f; |
Lukas_Adlboller | 1:c8943c51b10d | 40 | break; |
Lukas_Adlboller | 0:2aca3f53fe79 | 41 | } |
Lukas_Adlboller | 0:2aca3f53fe79 | 42 | } |
Lukas_Adlboller | 0:2aca3f53fe79 | 43 | |
Lukas_Adlboller | 0:2aca3f53fe79 | 44 | valueTicker.attach_us(&outputToDAC, 10); |
Lukas_Adlboller | 0:2aca3f53fe79 | 45 | |
Lukas_Adlboller | 1:c8943c51b10d | 46 | while(1); |
Lukas_Adlboller | 0:2aca3f53fe79 | 47 | } |