From d73f11ca76e0100d1b451adc28ce3ea6960b32ae Mon Sep 17 00:00:00 2001 From: H3wastooshort Date: Fri, 30 Aug 2024 17:57:36 +0200 Subject: [PATCH] i did this ages ago sig gen now outputs correct hi-frq signal --- .../BikeUSBv2_test_generator.ino | 39 ++++++++++++++++--- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/firmware/BikeUSBv2_test_generator/BikeUSBv2_test_generator.ino b/firmware/BikeUSBv2_test_generator/BikeUSBv2_test_generator.ino index e14174b..0cca129 100644 --- a/firmware/BikeUSBv2_test_generator/BikeUSBv2_test_generator.ino +++ b/firmware/BikeUSBv2_test_generator/BikeUSBv2_test_generator.ino @@ -1,6 +1,37 @@ void setup() { Serial.begin(115200); pinMode(7, OUTPUT); + pinMode(6, OUTPUT); + + + noInterrupts(); + // Clear registers + TCCR2A = 0; + TCCR2B = 0; + TCNT2 = 0; + + // 10000 Hz (16000000/((24+1)*64)) + OCR2A = 24; + // CTC + TCCR2A |= (1 << WGM21); + // Prescaler 64 + TCCR2B |= (1 << CS22); + // Output Compare Match A Interrupt Enable + TIMSK2 |= (1 << OCIE2A); + interrupts(); +} + +uint16_t divider = 9999; +uint16_t counter = 0; +bool state = 0; +ISR(TIMER2_COMPA_vect) { + counter++; + if (counter > divider) { + digitalWrite(7, state); + digitalWrite(6, !state); + state ^= 1; + counter = 0; + } } union { @@ -8,19 +39,15 @@ union { uint8_t b[sizeof(i)]; } val; -uint16_t t = 100; - void loop() { val.i = analogRead(A0); Serial.write(val.b, sizeof(val.b)); - digitalWrite(7, (millis() % t) > (t / 2)); - if (Serial.available()) { switch (Serial.read()) { - case 't': + case 'F': delay(100); - t = Serial.parseInt(); + divider = Serial.parseInt(); break; case 'i': analogReference(INTERNAL); case 'd': analogReference(DEFAULT);