DECODING HT6P20 WITH attachinterrupt

////////////////////////////////// ENGLISH ////////////////////////////////////////////////////
/*
DECODER WITH ARDUINO HT6P20B 2M2 RESISTOR IN OSC.
USE THIS CODE NO TIMER, INTERRUPTION OR FOREIGN AND PIN SET INPUT.
IT MEASURES OF PILOT TIME PERIOD AND COMPARE IF YOU ARE INSIDE TRACK SET,
IF HE GOES TO CODE AND MAKING MEDIDÇÕES PERIOD OF TIME IN HIGH LEVEL
IF YOU HAVE INSIDE TRACK ACRECENTA BIT 1 0 OR VARIABLE IN NO EVENT _DATA ZERA VARIABLES AND RE-START
AFTER RECEIVING ALL IT TAKES THE BITS BITS 4 CODE OF ANTE AND TESTS TO KNOW IF THE CODE WAS RECEIVED
CORRECTLY, IF HE RECEIVED RIGHT PLACE IN A VARIABLE ANTCODE 1.

CREATED BY: JACQUES DANIEL MORESCO
DATE: 28/02/2012 USING ARDUINO 0022.
PHONES: 54 3324 2251 54 9206 7168
E-MAIL: ibisul@ibisul.com.br
Allowed public use, but should keep the author's name.

///////////////////////////////////////////////////////////////////////////////////////////////
CHANGES MADE TO WORK WITH STOP USING TIMER attachInterrupt LEAVING THE RELEASED FOR ARDUINO
OTHER TASKS
///////////////////////////////////////////////////////////////////////////////////////////////

ALTERED BY MARCELO LOPES SIQUEIRA
DATE 03/07/2013 USING ARDUINO 1.0.5
E-MAIL: bugalaws@hotmail.com
Allowed public use, but should keep the authorship.
*/
/////////////////////////////////////////////////////////////////////////////////////////

int x,startbit, startbit1,ctr,dataok,t1,larpulso,larpulso1,larpulso2,larpulso3,larpulso4,bt1,bt2,antcode=0;
unsigned long _data, _data2=0; // DATA é o Código recebido do HT6p20 todos os 28 BITS onde, 22 do coodigo+2 dos botões+4 do anti-codigo;
unsigned long _dur,_dur1; // Duração do Pulso
byte _pinIn = 2; // Pino que vai receber o sinal RF digital.

#define PPM_Pin 2 //this must be 2 or 3

void setup()
{
Serial.begin(9600);
Serial.println("ready");

pinMode(PPM_Pin, INPUT);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
attachInterrupt(PPM_Pin - 2, read_ppm, CHANGE);

TCCR1A = 0; //reset timer1
TCCR1B = 0;
TCCR1B |= (1 << CS11); //set timer1 to increment every 0,5 us

}

void loop()
{
Serial.println("Conteudo _data2 - " + String(_data2,BIN));
delay(500);
}

void read_ppm(){ //leave this alone
static unsigned long counter;

//1020 ciclos equivalem a um pulso 510us
//2000 1000us
//20000 10000us
// x 12000

counter = TCNT1; //Adicion a quantidade de ciclos
TCNT1 = 0;

_dur = counter;

// Testa o tempo piloto até o Bit de inicio;
if(_dur > 20000 && _dur < 24000 && startbit==0)
{
digitalWrite(13,!digitalRead(13));
larpulso=_dur/23;
larpulso1=larpulso-100;
larpulso2=larpulso+100;
larpulso3=larpulso+larpulso-50;
larpulso4=larpulso+larpulso+50;
startbit=1;
_dur=0;
_data=0;
dataok=0;
ctr=0;
}//Fim da condição do time pilot(inicio dos bites)

if(startbit == 1)
{
startbit++;
}else if (startbit==2 && dataok==0 && ctr < 28 && digitalRead(2) == HIGH) //Se possui um bit inicial válido
{
++ctr;
_dur1 = counter;

if(_dur1 > larpulso1 && _dur1 < larpulso2) // Se a largura de pulso é entre 1/4000 e 1/3000 segundos
{
//startbit = 1;
_data = (_data << 1) ; // anexar um * 1 * para a extremidade mais à direita do buffer
}
else if(_dur1 > larpulso3 && _dur1 < larpulso4) // Se a largura de pulso é entre 2/4000 e 2/3000 segundos
{
//startbit = 1;
_data = (_data << 1) + 1; // anexar um * 0 * para a extremidade mais à direita do buffer
}
else
{
/* força finalização do laço */
startbit=0;
}

}

if (ctr==28)
{
if (bitRead(_data,0)==1 && bitRead(_data,1)==0 && bitRead(_data,2)==1 && bitRead(_data,3)==0)
{
antcode=1;
} else {
ctr = 0;
//Serial.println(_data,BIN);
}

if (antcode==1)
{// Se foram recebidos todos os 28 Bits, o valor vai para a variavel _data e pode ser usada como exemplo abaixo.
dataok=1;
//Serial.println("Antecode OK - " + String(_data,BIN));
_data2 = _data;
ctr=0;
startbit=0;
antcode=0;
//delay(200);
}

}

}//Fim da função read_ppm()

///////em português brasil ////////////////////////////////////////
/*
ARDUINO DECODIFICADOR HT6P20B COM RESISTOR DE 2M2 NO OSC.
ESTE CÓDIGO NÃO USA TIMER, INTERRUPÇÃO EXTERNA E NEM PINO DEFINIDO DE ENTRADA.
ELE MEDE O TEMPO DO PILOT PERIOD E COMPARA SE ESTÁ DENTRO DA FAIXA DEFINIDA,
SE TIVER ELE PASSA PARA CODE PERIOD E FAZ AS MEDIDÇÕES DE TEMPO EM NIVEL ALTO
SE TIVER DENTRO DA FAIXA ACRECENTA BIT 1 OU 0 NA VARIAVEL _DATA CASO NÃO ZERA AS VARIÁVEIS E RE-INICIA
APOS RECEBER TODOS OS BITS ELE PEGA OS 4 BITS DO ANTE CODE E TESTA PARA SABER SE O CÓDIGO FOI RECEBIDO
CORRETAMENTE, CASO RECEBIDO CERTO ELE COLOCA A VARIAVEL ANTCODE EM 1.

CRIADO POR: JACQUES DANIEL MORESCO
DATA: 28/02/2012 USANDO ARDUINO 0022.
FONES:54 3324 2251 54 9206 7168
E-MAIL: ibisul@ibisul.com.br
Permitido o uso público, mas deve-se manter o nome do autor.

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
FEITA ALTERAÇÃO PARA FUNCIONAR COM INTERRUPÇÃO UTILIZANDO TIMER ATTACHINTERRUPT DEIXANDO LIBERADO O ARDUINO PARA
AS OUTRAS TAREFAS
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ALTERADO POR: MARCELO SIQUEIRA LOPES
DATA 03/07/2013 USANDO ARDUINO 1.0.5
E-MAIL: bugalaws@hotmail.com
Permitido o uso público, mas deve-se manter as autorias.
*/

int x,startbit, startbit1,ctr,dataok,t1,larpulso,larpulso1,larpulso2,larpulso3,larpulso4,bt1,bt2,antcode=0;
unsigned long _data, _data2=0; // DATA é o Código recebido do HT6p20 todos os 28 BITS onde, 22 do coodigo+2 dos botões+4 do anti-codigo;
unsigned long _dur,_dur1; // Duração do Pulso
byte _pinIn = 2; // Pino que vai receber o sinal RF digital.

#define PPM_Pin 2 //this must be 2 or 3

void setup()
{
Serial.begin(9600);
Serial.println("ready");

pinMode(PPM_Pin, INPUT);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
attachInterrupt(PPM_Pin - 2, read_ppm, CHANGE);

TCCR1A = 0; //reset timer1
TCCR1B = 0;
TCCR1B |= (1 << CS11); //set timer1 to increment every 0,5 us

}

void loop()
{
Serial.println("Conteudo _data2 - " + String(_data2,BIN));
delay(500);
}

void read_ppm(){ //leave this alone
static unsigned long counter;

//1020 ciclos equivalem a um pulso 510us
//2000 1000us
//20000 10000us
// x 12000

counter = TCNT1; //Adicion a quantidade de ciclos
TCNT1 = 0;

_dur = counter;

// Testa o tempo piloto até o Bit de inicio;
if(_dur > 20000 && _dur < 24000 && startbit==0)
{
digitalWrite(13,!digitalRead(13));
larpulso=_dur/23;
larpulso1=larpulso-100;
larpulso2=larpulso+100;
larpulso3=larpulso+larpulso-50;
larpulso4=larpulso+larpulso+50;
startbit=1;
_dur=0;
_data=0;
dataok=0;
ctr=0;
}//Fim da condição do time pilot(inicio dos bites)

if(startbit == 1)
{
startbit++;
}else if (startbit==2 && dataok==0 && ctr < 28 && digitalRead(2) == HIGH) //Se possui um bit inicial válido
{
++ctr;
_dur1 = counter;

if(_dur1 > larpulso1 && _dur1 < larpulso2) // Se a largura de pulso é entre 1/4000 e 1/3000 segundos
{
//startbit = 1;
_data = (_data << 1) ; // anexar um * 1 * para a extremidade mais à direita do buffer
}
else if(_dur1 > larpulso3 && _dur1 < larpulso4) // Se a largura de pulso é entre 2/4000 e 2/3000 segundos
{
//startbit = 1;
_data = (_data << 1) + 1; // anexar um * 0 * para a extremidade mais à direita do buffer
}
else
{
/* força finalização do laço */
startbit=0;
}

}

if (ctr==28)
{
if (bitRead(_data,0)==1 && bitRead(_data,1)==0 && bitRead(_data,2)==1 && bitRead(_data,3)==0)
{
antcode=1;
} else {
ctr = 0;
//Serial.println(_data,BIN);
}

if (antcode==1)
{// Se foram recebidos todos os 28 Bits, o valor vai para a variavel _data e pode ser usada como exemplo abaixo.
dataok=1;
//Serial.println("Antecode OK - " + String(_data,BIN));
_data2 = _data;
ctr=0;
startbit=0;
antcode=0;
//delay(200);
}

}

}//Fim da função read_ppm()

How to use this forum

Code tags, please.

And what was the question, exactly?

i was wondering for what the pin 13 is used for, and if i can change it to other pin, and if you can describe your hardware.

Hi Guys!

I developed a sketch based on the nice code above and many others..
For all "remote controls" that use the encoder HT6P20 I tested, it worked!

See the links below:
English:

Portuguese: