From f82f299d531585fdf3111f367c1d787370c06a43 Mon Sep 17 00:00:00 2001 From: askiiart Date: Sat, 22 Oct 2022 18:29:06 -0500 Subject: [PATCH] Add arduino code --- Arduino/rx_speed/rx_speed.ino | 49 +++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 Arduino/rx_speed/rx_speed.ino diff --git a/Arduino/rx_speed/rx_speed.ino b/Arduino/rx_speed/rx_speed.ino new file mode 100644 index 0000000..88d1f03 --- /dev/null +++ b/Arduino/rx_speed/rx_speed.ino @@ -0,0 +1,49 @@ +#include "SerialTransfer.h" + +SerialTransfer transfer; + +const byte OC1A_PIN = 9; +const byte OC1B_PIN = 10; + +const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency (Frequency in HZ!) (Set currently to 25kHZ) +const word TCNT1_TOP = 16000000/(2*PWM_FREQ_HZ); + +void setup() { + // Setup Serial transfer with PC + Serial.begin(115200); + transfer.begin(Serial); + + pinMode(OC1A_PIN, OUTPUT); + + // Clear Timer1 control and count registers + TCCR1A = 0; + TCCR1B = 0; + TCNT1 = 0; + + // Set Timer1 configuration + // COM1A(1:0) = 0b10 (Output A clear rising/set falling) + // COM1B(1:0) = 0b00 (Output B normal operation) + // WGM(13:10) = 0b1010 (Phase correct PWM) + // ICNC1 = 0b0 (Input capture noise canceler disabled) + // ICES1 = 0b0 (Input capture edge select disabled) + // CS(12:10) = 0b001 (Input clock select = clock/1) + + TCCR1A |= (1 << COM1A1) | (1 << WGM11); + TCCR1B |= (1 << WGM13) | (1 << CS10); + ICR1 = TCNT1_TOP; +} + +void loop() { + if(myTransfer.available()) + { + // send all received data back to Python + for(uint16_t i=0; i < myTransfer.bytesRead; i++) + myTransfer.packet.txBuff[i] = myTransfer.packet.rxBuff[i]; + + myTransfer.sendData(myTransfer.bytesRead); + } +} + +void setPwmDuty(byte duty) { + OCR1A = (word) (duty*TCNT1_TOP)/100; +}