Skip to content

Instantly share code, notes, and snippets.

@azechi
Last active February 18, 2022 01:24
Show Gist options
  • Save azechi/3ecae7fa515ac8157370aa2a8be6d317 to your computer and use it in GitHub Desktop.
Save azechi/3ecae7fa515ac8157370aa2a8be6d317 to your computer and use it in GitHub Desktop.
Infared remote control for AEHA format with Arduino Uno.
#include <util/atomic.h>
int T = 470;
void setup() {
pinMode(8, OUTPUT);
digitalWrite(8, LOW);
pinMode(9, OUTPUT);
TCCR1A = 0;
TCCR1B = 0;
// Compare Output Mode = "inverting mode. Set bit on compare match, clear bit at BOTTOM."
TCCR1A |= _BV(COM1A1) | _BV(COM1A0);
// Waveform Generation Mode = "14 Fast PWM TOP=ICR1"
TCCR1A |= _BV(WGM11);
TCCR1B |= _BV(WGM13) | _BV(WGM12);
// write 16bit register
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
// TOP
ICR1 = 421U;
// Compare
OCR1A = 140U;
}
// Clock Select = "no prescaling"
TCCR1B |= _BV(CS10);
Serial.begin(9600);
Serial.setTimeout(15000UL);
}
void command(byte* cmd, size_t len_cmd) {
// leader
digitalWrite(8, HIGH);
delayMicroseconds(T * 8);
digitalWrite(8, LOW);
delayMicroseconds(T * 4);
// data
for(int i = 0; i < len_cmd; i++) {
byte b = cmd[i];
for(int j = 7; j >= 0; j--){
digitalWrite(8, HIGH);
delayMicroseconds(T * 1);
digitalWrite(8, LOW);
delayMicroseconds(T * (bitRead(b, j)? 3: 1));
}
}
// trailer
digitalWrite(8, HIGH);
delayMicroseconds(T * 1);
digitalWrite(8, LOW);
delay(1);
}
void loop() {
if(Serial.available()){
String s = Serial.readStringUntil('\n');
byte cmd[6];
int len_cmd = 0;
char buf[3];
buf[2] = '\0';
int len = s.length() - 1;
for(int i = 0; i < len; i++) {
buf[0] = s[i++];
buf[1] = s[i];
byte b = strtol(buf, NULL, 16);
Serial.print(b, HEX);
Serial.print(" ");
cmd[len_cmd++] = b;
}
command(cmd, len_cmd);
Serial.println("");
}
}
$ python3 -m serial.tools.miniterm -e /dev/ttyAMA0 9600
#-----
344A90BC2C
#-----
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment