diff options
| author | manuel <manuel@mausz.at> | 2020-12-21 15:34:36 +0100 |
|---|---|---|
| committer | manuel <manuel@mausz.at> | 2020-12-21 15:34:36 +0100 |
| commit | 0947951e15a9ad81a7dea0205141cb73905287fa (patch) | |
| tree | f5d18f3b99eac9cd4737b173dcad048e29cd97fc | |
| parent | 0b8366c59fa0b34a70b6b453bfb0f8eaf34a2f16 (diff) | |
| download | arduino-0947951e15a9ad81a7dea0205141cb73905287fa.tar.gz arduino-0947951e15a9ad81a7dea0205141cb73905287fa.tar.bz2 arduino-0947951e15a9ad81a7dea0205141cb73905287fa.zip | |
Add initial glass lathe test code
| -rw-r--r-- | linz/glasslathe/.gitignore | 5 | ||||
| -rw-r--r-- | linz/glasslathe/platformio.ini | 15 | ||||
| -rw-r--r-- | linz/glasslathe/src/main.cpp | 104 |
3 files changed, 124 insertions, 0 deletions
diff --git a/linz/glasslathe/.gitignore b/linz/glasslathe/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/linz/glasslathe/.gitignore | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | .pio | ||
| 2 | .vscode/.browse.c_cpp.db* | ||
| 3 | .vscode/c_cpp_properties.json | ||
| 4 | .vscode/launch.json | ||
| 5 | .vscode/ipch | ||
diff --git a/linz/glasslathe/platformio.ini b/linz/glasslathe/platformio.ini new file mode 100644 index 0000000..f2cb76e --- /dev/null +++ b/linz/glasslathe/platformio.ini | |||
| @@ -0,0 +1,15 @@ | |||
| 1 | ; PlatformIO Project Configuration File | ||
| 2 | ; | ||
| 3 | ; Build options: build flags, source filter | ||
| 4 | ; Upload options: custom upload port, speed and extra flags | ||
| 5 | ; Library options: dependencies, extra library storages | ||
| 6 | ; Advanced options: extra scripting | ||
| 7 | ; | ||
| 8 | ; Please visit documentation for the other options and examples | ||
| 9 | ; https://docs.platformio.org/page/projectconf.html | ||
| 10 | |||
| 11 | [env:nanoatmega328new] | ||
| 12 | platform = atmelavr | ||
| 13 | board = nanoatmega328new | ||
| 14 | framework = arduino | ||
| 15 | lib_deps = paulstoffregen/TimerOne @ ^1.1 \ No newline at end of file | ||
diff --git a/linz/glasslathe/src/main.cpp b/linz/glasslathe/src/main.cpp new file mode 100644 index 0000000..85b49dd --- /dev/null +++ b/linz/glasslathe/src/main.cpp | |||
| @@ -0,0 +1,104 @@ | |||
| 1 | #include <Arduino.h> | ||
| 2 | #include <TimerOne.h> | ||
| 3 | |||
| 4 | void analogWriteSAH_Init(void) | ||
| 5 | { | ||
| 6 | // Stop the timer while we muck with it | ||
| 7 | TCCR1B = (0 << ICNC1) | (0 << ICES1) | (0 << WGM13) | (0 << WGM12) | (0 << CS12) | (0 << CS11) | (0 << CS10); | ||
| 8 | |||
| 9 | // Set the timer to mode 14... | ||
| 10 | // | ||
| 11 | // Mode WGM13 WGM12 WGM11 WGM10 Timer/Counter Mode of Operation TOP Update of OCR1x at TOV1 Flag Set on | ||
| 12 | // CTC1 PWM11 PWM10 | ||
| 13 | // ---- ----- ----- ----- ----- ------------------------------- ---- ----------------------- ----------- | ||
| 14 | // 14 1 1 1 0 Fast PWM ICR1 BOTTOM TOP | ||
| 15 | |||
| 16 | // Set output on Channel A to... | ||
| 17 | // | ||
| 18 | // COM1A1 COM1A0 Description | ||
| 19 | // ------ ------ ----------------------------------------------------------- | ||
| 20 | // 1 0 Clear OC1A/OC1B on Compare Match (Set output to low level). | ||
| 21 | TCCR1A = | ||
| 22 | (1 << COM1A1) | (0 << COM1A0) | // COM1A1, COM1A0 = 1, 0 | ||
| 23 | (0 << COM1B1) | (0 << COM1B0) | | ||
| 24 | (1 << WGM11) | (0 << WGM10); // WGM11, WGM10 = 1, 0 | ||
| 25 | |||
| 26 | // Set TOP to... | ||
| 27 | // | ||
| 28 | // fclk_I/O = 16000000 | ||
| 29 | // N = 1 | ||
| 30 | // TOP = 799 | ||
| 31 | // | ||
| 32 | // fOCnxPWM = fclk_I/O / (N * (1 + TOP)) | ||
| 33 | // fOCnxPWM = 16000000 / (1 * (1 + 799)) | ||
| 34 | // fOCnxPWM = 16000000 / 800 | ||
| 35 | // fOCnxPWM = 20000 | ||
| 36 | ICR1 = 799; | ||
| 37 | |||
| 38 | // Ensure the first slope is complete | ||
| 39 | TCNT1 = 0; | ||
| 40 | |||
| 41 | // Ensure Channel B is irrelevant | ||
| 42 | OCR1B = 0; | ||
| 43 | |||
| 44 | // Ensure Channel A starts at zero / off | ||
| 45 | OCR1A = 0; | ||
| 46 | |||
| 47 | // We don't need no stinkin interrupts | ||
| 48 | TIMSK1 = (0 << ICIE1) | (0 << OCIE1B) | (0 << OCIE1A) | (0 << TOIE1); | ||
| 49 | |||
| 50 | // Ensure the Channel A pin is configured for output | ||
| 51 | DDRB |= (1 << DDB1); | ||
| 52 | |||
| 53 | // Start the timer... | ||
| 54 | // | ||
| 55 | // CS12 CS11 CS10 Description | ||
| 56 | // ---- ---- ---- ------------------------ | ||
| 57 | // 0 0 1 clkI/O/1 (No prescaling) | ||
| 58 | TCCR1B = | ||
| 59 | (0 << ICNC1) | (0 << ICES1) | | ||
| 60 | (1 << WGM13) | (1 << WGM12) | // WGM13, WGM12 = 1, 1 | ||
| 61 | (0 << CS12) | (0 << CS11) | (1 << CS10); | ||
| 62 | } | ||
| 63 | |||
| 64 | void analogWriteD9(uint16_t value) | ||
| 65 | { | ||
| 66 | if ((value >= 0) && (value < 800)) | ||
| 67 | { | ||
| 68 | OCR1A = value; | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | void setup() | ||
| 73 | { | ||
| 74 | //analogWriteSAH_Init(); | ||
| 75 | Timer1.initialize(50); | ||
| 76 | Timer1.pwm(9, 1024/2); | ||
| 77 | Serial.begin(9600); | ||
| 78 | } | ||
| 79 | |||
| 80 | void loop() | ||
| 81 | { | ||
| 82 | static int old_poti = 0; | ||
| 83 | int poti = analogRead(A7) / 10; | ||
| 84 | if (poti != old_poti) { | ||
| 85 | // period_us = 1 / khz * 1000 | ||
| 86 | int period_us = (poti == 0) ? 0 : map(poti, 1, 102, 600, 50); | ||
| 87 | Timer1.initialize(period_us); | ||
| 88 | Timer1.pwm(9, 1024/2); | ||
| 89 | Serial.println(poti); | ||
| 90 | Serial.println(period_us); | ||
| 91 | old_poti = poti; | ||
| 92 | } | ||
| 93 | //Timer1.setPeriod(); | ||
| 94 | //Timer1.setPeriod(50); | ||
| 95 | //Timer1.restart(); | ||
| 96 | |||
| 97 | #if 0 | ||
| 98 | for (float frequency = 500; frequency < 3000; frequency = frequency + 1) { | ||
| 99 | float T = 1 / frequency * 1000000; | ||
| 100 | //Timer1.setPeriod(T); | ||
| 101 | delay(5); | ||
| 102 | } | ||
| 103 | #endif | ||
| 104 | } \ No newline at end of file | ||
