Requirement:
- I have input 4KHz pulse input
- Divide it's frequency in half
- Arbitrarily adjust phase
- Arbitrarily adjust duty
- Out put the modified pulse
My solution (CPU):
uint8_t cnt = 0;
uint16_t t1 = 235;
uint16_t t2 = 5;
String cmd;
void setup() {
pinMode(3, OUTPUT);
pinMode(2, INPUT);
Serial.begin(9600);
delay(500);
attachInterrupt(digitalPinToInterrupt(2), rising_edge, RISING);
Serial.println("PWM init");
}
void loop() {
if (Serial.available() > 0) {
cmd = Serial.readString();
if (cmd.substring(0,8) == "setdelay"){
t1 = cmd.substring(9).toInt();
Serial.print("setting delay to ");
Serial.print(t1);
Serial.println(" us");
}
else if (cmd.substring(0,7) == "sethigh"){
t2 = cmd.substring(8).toInt();
Serial.print("setting high time to ");
Serial.print(t2);
Serial.println(" us");
}
Serial.flush();
}
}
void rising_edge() {
cnt = cnt + 1;
if (cnt >= 2) { //divide by 2
delayMicroseconds(t1);
digitalWrite(3,HIGH);
delayMicroseconds(t2);
digitalWrite(3,LOW);
cnt = 0;
}
}
However this makes heavy use of CPU, and I need to use CPU for something else.Is it possible to implement all this with TC ?
Thanks in Advance !