Hello,
I built two encoder methods for the x1527 encoder (common in garage openers and such) and the Holtek HT12E encoder (decoder code for the HT12 already exists on the arduino forum). All code has the addresses statically coded, but it should be very easy for someone to modify these or take them as arguments to the constructor.
The pulsetime might need to be modified to match the resistor/oscillator on the receiving side. The pulse time for the x1527 encoding is 32 oscilations. For HT12E the supplied code is set for a 3Khz oscillator.
hs1527.cpp:
/* EV1527, RT1527, FP1527 or HS1527 encoding */
#include "hs1527.h"
#define HS_1527_PULSETIME 474 // Time in microseconds for one pulse
HS1527::HS1527(int pin)
{
_pin = pin;
}
void HS1527::signal(boolean state) {
digitalWrite(_pin, state);
delayMicroseconds(HS_1527_PULSETIME);
}
void HS1527::signalBinary(boolean state) {
signal(HIGH);
signal(state);
signal(state);
signal(LOW);
}
/* Transmit a message */
void HS1527::sendMsg(boolean *bits) {
/* Sync bit */
signalBinary(LOW);
/* Pilot period */
for(int i = 0; i < 28; ++i) signal(LOW);
/* 17 Address bits */
signalBinary(LOW);
signalBinary(HIGH);
signalBinary(HIGH);
signalBinary(HIGH);
for(int i = 0; i < 6; ++i) signalBinary(LOW);
signalBinary(HIGH);
signalBinary(HIGH);
signalBinary(LOW);
signalBinary(HIGH);
signalBinary(LOW);
signalBinary(HIGH);
signalBinary(HIGH);
for(int i = 0; i < 3; ++i) signalBinary(LOW);
/* The 4 data bits */
for(int i = 0; i < 4; ++i) signalBinary(bits[i]);
for(int i = 0; i < 3; ++i) signalBinary(LOW);
}
hs1527.h:
/* EV1527, RT1527, FP1527 or HS1527 encoding */
#ifndef HS1527_h
#define HS1527_h
#include "Arduino.h"
class HS1527
{
public:
HS1527(int pin); // this is the constructor
void sendMsg(boolean *bits);
private:
int _pin; // this is Arduino input pin
void signalBinary(boolean state);
void signal(boolean state);
};
#endif
hs1527_sample Arduino code
#include "hs1527.h"
#define TXPIN 7 // Pin to transmit encoded data
HS1527 encoder(TXPIN);
void loop() {
boolean curBits[4];
for(int i = 0; i < 4; ++i) curBits[i] = LOW;
curBits[3] = HIGH;
encoder.sendMsg(curBits);
}
For the HT12E I never built a library, it is very simple though:
#define PULSETIME 290 // Time in microseconds for one pulse (This is set for 3Khz oscilator)
#define TXPIN 7 // Pin to transmit encoded data
void setup() {
pinMode(TXPIN, OUTPUT);
}
void signal(boolean state) {
digitalWrite(TXPIN, state);
delayMicroseconds(PULSETIME);
}
void signalBinary(boolean state) {
signal(LOW);
if(state == LOW) signal(HIGH);
else if(state == HIGH) signal(LOW);
signal(HIGH);
}
/* Transmit a message. Remember that HT12D requires the same data three times to accept it as a valid transmission */
void sendMsg(boolean *bits) {
/* Pilot period */
for(int i = 0; i < 33; ++i) signal(LOW); // 33 is pretty arbitary, should be 12 bits
/* Sync bit */
signalBinary(HIGH);
/* 8 Address bits */
for(int i = 0; i < 8; ++i) signalBinary(LOW);
/* The 4 data bits */
for(int i = 0; i < 4; ++i) signalBinary(bits[i]);
}
void loop() {
boolean bits[4] = {LOW, LOW, HIGH, LOW};
sendMsg(bits);
}