From 32ded446fd128cdc8b742e99b21c6582b9f9ade2 Mon Sep 17 00:00:00 2001 From: Manfred Steiner Date: Thu, 25 Jul 2024 18:48:17 +0200 Subject: [PATCH] Testprogram: Poti+Encoder --- software/test_2024-07-23/Makefile | 7 +- software/test_2024-07-23/src/main.cpp | 47 ++++++++--- .../test_2024-07-23/src/units/encoder.cpp | 84 +++++++++++++++++++ .../test_2024-07-23/src/units/encoder.hpp | 21 +++++ software/test_2024-07-23/src/units/poti.cpp | 41 +++++++++ software/test_2024-07-23/src/units/poti.hpp | 15 ++++ software/test_2024-07-23/src/units/seg7.cpp | 2 + 7 files changed, 202 insertions(+), 15 deletions(-) create mode 100644 software/test_2024-07-23/src/units/encoder.cpp create mode 100644 software/test_2024-07-23/src/units/encoder.hpp create mode 100644 software/test_2024-07-23/src/units/poti.cpp create mode 100644 software/test_2024-07-23/src/units/poti.hpp diff --git a/software/test_2024-07-23/Makefile b/software/test_2024-07-23/Makefile index 2749913..8f5c7f2 100644 --- a/software/test_2024-07-23/Makefile +++ b/software/test_2024-07-23/Makefile @@ -57,6 +57,9 @@ simuc: sim/$(NAME).elf gdb: sim/$(NAME).elf avr-gdb $< +isp: + avrdude -c usbasp -p m644p + isp-flash: dist/$(NAME).elf all avrdude -c usbasp -p m644p -e -U flash:w:$< @@ -68,8 +71,8 @@ picocom: # picocom sends CR for ENTER -> convert cr (\r) to lf (\n) picocom -b 115200 --omap crlf /dev/ttyUSB0 -fuse: - avrdude -c usbasp -p m644p -U lfuse:w:0xEE:m -U hfuse:w:0xD9:m -U efuse:w:0xFF:m +isp-fuse: + avrdude -c usbasp -p m644p -U lfuse:w:0xFF:m -U hfuse:w:0xD9:m -U efuse:w:0xFF:m -U lock:w:0xFF:m clean: @rm -r dist diff --git a/software/test_2024-07-23/src/main.cpp b/software/test_2024-07-23/src/main.cpp index 68bc39f..1f2a0d4 100644 --- a/software/test_2024-07-23/src/main.cpp +++ b/software/test_2024-07-23/src/main.cpp @@ -9,6 +9,8 @@ #include "units/switch.hpp" #include "units/rgb.hpp" #include "units/seg7.hpp" +#include "units/poti.hpp" +#include "units/encoder.hpp" extern "C" { void __cxa_pure_virtual () { @@ -33,6 +35,14 @@ extern "C" { static volatile uint32_t timer1ms = 0; static volatile int keyUart0 = EOF; + + Led led; + Switch sw; + Rgb rgb; + Seg7 seg7; + Poti poti; + Encoder encoder; + } void setTimer (uint32_t ms) { @@ -53,7 +63,7 @@ int wait (uint32_t ms) { int main () { - + // Nano-644 LEDs (Green, Orange, Red) DDRC |= (1 << DDC4) | (1 << DDC3) | (1 << DDC2); PORTC &= ~((1 << PORT4) | (1 << PORT3) | (1 << PORT2)); @@ -69,9 +79,10 @@ int main () { UBRR0H = 0; UBRR0L = F_CPU / 8 / 115200 - 1; - TCCR1B = (1 << WGM12) | (1 << CS11); // CTC, F_CPU/8=1.5MHz - OCR1A = 1500; // 1500/1.5E6 = 1ms - TIMSK1 = (1 << OCIE1A); + TCCR2A = (1 << WGM21); + TCCR2B = (1 << CS21); // CTC, F_CPU/8=1.5MHz + OCR2A = 150; // 150/1.5E6 = 0.1ms + TIMSK2 = (1 << OCIE2A); stdout = &mystdout; stderr = &mystderr; @@ -80,13 +91,9 @@ int main () { printf("\n\nTEST Nano-X-Base\n"); printf("======================================"); - - Led led; - Switch sw; - Rgb rgb; - Seg7 seg7; - TestUnit *unit[] = { &led, &sw, &rgb, &seg7 }; + + TestUnit *unit[] = { &led, &sw, &rgb, &seg7, &poti, &encoder }; for (uint8_t i = 0; i < sizeof(unit) / sizeof(unit[0]); i++) { TestUnit *pu = unit[i]; @@ -122,9 +129,23 @@ ISR (USART0_RX_vect) { keyUart0 = b; } -ISR (TIMER1_COMPA_vect) { - if (timer1ms > 0) { - timer1ms--; +ISR (TIMER2_COMPA_vect) { // every 100us + static uint16_t timer500ms = 0; + static uint8_t timer100us = 0; + + timer100us++; + encoder.tick100us(); + if (timer100us >= 10) { + timer100us = 0; + if (timer1ms > 0) { + timer1ms--; + } + } + + timer500ms++; + if (timer500ms >= 5000) { + PORTC ^= (1 << PORTC3); // orange LED blinking + timer500ms = 0; } } diff --git a/software/test_2024-07-23/src/units/encoder.cpp b/software/test_2024-07-23/src/units/encoder.cpp new file mode 100644 index 0000000..b42647b --- /dev/null +++ b/software/test_2024-07-23/src/units/encoder.cpp @@ -0,0 +1,84 @@ +#include +#include + +#include "encoder.hpp" +#include "../main.hpp" + +// PB0/T0 ... Encoder A +// PB1/T1 ... Encoder B +// PB2/INT2 ... push switch of encoder (pushed = 0) + +// Encoder signals on rotation clockwise 1 step: +// A -----____------ one char app. 1ms..2ms (rotation speed) +// B -------___----- +// one step when: A = 0, B= 1->0 + +// Encoder signals on rotation counterclockwise 1 step: +// A -----____------ one char app. 1ms..2ms (rotation speed) +// B --______----- +// one step when: A = 0, B= 0->1 + + +void Encoder::cleanup () { + DDRB &= ~((1 << PB2) | (1 << PB1) | (1 << PB0)); + PORTB &= ~((1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0)); +} + +int8_t Encoder::run (uint8_t subtest) { + switch (subtest) { + case 0: { + printf("init"); + DDRB &= ~((1 << PB2) | (1 << PB1) | (1 << PB0)); + PORTB |= (1 << PORTB2) | (1 << PORTB1) | (1 << PORTB0); // enable pullup + return 0; + } + + case 1: { + while (wait(10) == EOF) { + printf("\r => Encoder (push to clear): "); + printf("%5d (0x%02x) ", count, (uint8_t)count); + if ((PINB & (1 << PINB2)) == 0) { + reset(); + } + } + return 0; + } + + case 2: { + printf("\r => Encoder end%30s", " "); + break; + } + } + + return -1; +} + +struct EncoderState { + uint8_t a:1; // signal A + uint8_t b:1; // signal B +}; + +void Encoder::tick100us () { + static EncoderState lastState = { 1, 1 };; + static EncoderState lastStableState = { 1, 1 }; + + if ((DDRB & 0x03) || (PORTB & 0x07) != 0x07) { + count = 0; + return; // Enocder pins not configured + } + EncoderState nextState = { (PINB & 0x01) == 0x01, (PINB & 0x02) == 0x02 }; + if (nextState.a == lastState.a && nextState.b == lastState.b) { + if (lastStableState.a == 0 && nextState.b != lastStableState.b) { + if (nextState.b == 0) { + count = count < 127 ? count + 1 : 127; + } else { + count = count > -128 ? count - 1 : -128; + } + } + lastStableState.a = nextState.a; + lastStableState.b = nextState.b; + } + lastState.a = nextState.a; + lastState.b = nextState.b; +} + diff --git a/software/test_2024-07-23/src/units/encoder.hpp b/software/test_2024-07-23/src/units/encoder.hpp new file mode 100644 index 0000000..99aaed0 --- /dev/null +++ b/software/test_2024-07-23/src/units/encoder.hpp @@ -0,0 +1,21 @@ +#ifndef ENCODER_HPP +#define ENCODER_PP + +#include +#include "../main.hpp" + + +class Encoder : public TestUnit { + public: + int8_t count; + + public: + Encoder () { reset(); }; + virtual void cleanup (); + virtual int8_t run (uint8_t subtest); + virtual const char *getName () { return "Encoder"; } + void reset () { count = 0; } + void tick100us (); +}; + +#endif \ No newline at end of file diff --git a/software/test_2024-07-23/src/units/poti.cpp b/software/test_2024-07-23/src/units/poti.cpp new file mode 100644 index 0000000..5f8fcdb --- /dev/null +++ b/software/test_2024-07-23/src/units/poti.cpp @@ -0,0 +1,41 @@ +#include +#include + +#include "poti.hpp" +#include "../main.hpp" + +void Poti::cleanup () { + ADMUX = 0; + ADCSRA = 0; +} + +int8_t Poti::run (uint8_t subtest) { + switch (subtest) { + case 0: { + printf("init"); + ADMUX = (1 << REFS0); // ADC0, VREF=AVCC=3.3V + ADCSRA = (1 << ADEN) | 7; // ADC Enable, Prescaler 128 + return 0; + } + + case 1: { + printf("\n"); + while (wait(10) == EOF) { + printf("\r => Measure ADC0: "); + ADCSRA |= (1 << ADSC); // start ADC + while (ADCSRA & (1 << ADSC)) {} // wait for result + printf("%4d (0x%03x)", ADC, ADC); + } + return 0; + } + + case 2: { + printf("\r => ADC end%20s", " "); + break; + } + } + + return -1; +} + + diff --git a/software/test_2024-07-23/src/units/poti.hpp b/software/test_2024-07-23/src/units/poti.hpp new file mode 100644 index 0000000..df368e8 --- /dev/null +++ b/software/test_2024-07-23/src/units/poti.hpp @@ -0,0 +1,15 @@ +#ifndef POTI_HPP +#define POTI_PP + +#include +#include "../main.hpp" + +class Poti : public TestUnit { + public: + Poti () {}; + virtual void cleanup (); + virtual int8_t run (uint8_t subtest); + virtual const char *getName () { return "Poti"; } +}; + +#endif \ No newline at end of file diff --git a/software/test_2024-07-23/src/units/seg7.cpp b/software/test_2024-07-23/src/units/seg7.cpp index 49030c3..d2fdbb9 100644 --- a/software/test_2024-07-23/src/units/seg7.cpp +++ b/software/test_2024-07-23/src/units/seg7.cpp @@ -85,6 +85,8 @@ int8_t Seg7::run (uint8_t subtest) { wait(400); return 0; + } else { + printf("end"); } return -1; -- 2.39.5