k80-linux-cooling/Arduino/rx_speed.ino

62 lines
1.5 KiB
Arduino
Raw Normal View History

2022-10-22 18:29:06 -05:00
#include "SerialTransfer.h"
2022-10-24 22:21:16 -05:00
SerialTransfer myTransfer;
2022-10-22 18:29:06 -05:00
const byte OC1A_PIN = 9;
const byte OC1B_PIN = 10;
2022-10-24 22:21:16 -05:00
const word PWM_FREQ_HZ = 25000; //Adjust this value to adjust the frequency (Frequency in HZ!) (Set currently to 25kHZ)
2022-10-22 18:29:06 -05:00
const word TCNT1_TOP = 16000000/(2*PWM_FREQ_HZ);
2022-10-24 22:21:16 -05:00
struct STRUCT {
char val;
} myStruct;
2022-10-22 18:29:06 -05:00
void setup() {
// Setup Serial transfer with PC
Serial.begin(115200);
2022-10-24 22:21:16 -05:00
myTransfer.begin(Serial);
2022-10-22 18:29:06 -05:00
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)
2022-10-24 22:21:16 -05:00
2022-10-22 18:29:06 -05:00
TCCR1A |= (1 << COM1A1) | (1 << WGM11);
TCCR1B |= (1 << WGM13) | (1 << CS10);
ICR1 = TCNT1_TOP;
}
void loop() {
2022-10-24 22:21:16 -05:00
2022-10-22 18:29:06 -05:00
if(myTransfer.available())
{
2022-10-24 22:21:16 -05:00
2022-10-22 18:29:06 -05:00
// send all received data back to Python
2022-10-24 22:21:16 -05:00
for(uint16_t i=0; i < myTransfer.bytesRead; i++){
2022-10-22 18:29:06 -05:00
myTransfer.packet.txBuff[i] = myTransfer.packet.rxBuff[i];
2022-10-24 22:21:16 -05:00
}
2022-10-22 18:29:06 -05:00
myTransfer.sendData(myTransfer.bytesRead);
2022-10-24 22:21:16 -05:00
uint16_t recSize = 0;
recSize = myTransfer.rxObj(myStruct, recSize);
2022-10-25 22:28:49 -05:00
int x = myStruct.val;
2022-10-24 22:21:16 -05:00
setPwmDuty(x);
2022-10-22 18:29:06 -05:00
}
}
void setPwmDuty(byte duty) {
OCR1A = (word) (duty*TCNT1_TOP)/100;
}