k80-linux-cooling/Demos/Arduino/display_rx.ino
2022-10-24 21:54:32 -05:00

62 lines
1.5 KiB
C++

#include "SerialTransfer.h"
SerialTransfer myTransfer;
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);
struct STRUCT {
char val;
} myStruct;
void setup() {
// Setup Serial transfer with PC
Serial.begin(115200);
myTransfer.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);
uint16_t recSize = 0;
recSize = myTransfer.rxObj(myStruct, recSize);
int x = myStruct.val * 2; // I have no idea why I have to multiply it by 2, but I do
setPwmDuty(x);
}
}
void setPwmDuty(byte duty) {
OCR1A = (word) (duty*TCNT1_TOP)/100;
}