#include <16F876.h>
#fuses XT,NOWDT,NOPUT,NOPROTECT
#use delay(clock=4000000)
#use standard_io(b)
/******************************************************************************/
/******************************************************************************/
//Definicion de variables.
int1 cambio=0,start,dato_recibido;
int longitud=17,i,j;
int16 data,address;
int32 word;
long tiempo;
/*****************************************************************************/
#INT_EXT
void ext_isr(){
if(!cambio){
set_timer1(0);
tiempo=0;
EXT_INT_EDGE(L_TO_H);
cambio=1;
}else{
tiempo= get_timer1();
EXT_INT_EDGE(H_TO_L);
cambio=0;
}
if((tiempo>4250) && (tiempo<4650)){ //"Start", si está entre 4450 y 4650 us.
start=1; //Flag start activado.
i=0; //contador de bits a 0.
}
if(start){
if((tiempo>300) && (tiempo<580)){ //Entre 355 y 550 us, es cero.
bit_clear(word,i);
i++;
}
else if((tiempo>1350) && (tiempo<1750)){ //Entre 1550 y 1650 us es uno.
bit_set(word,i);
i++;
}
if(i==longitud){
dato_recibido=1;
EXT_INT_EDGE(H_TO_L);
start=0; //para reiniciar lectura.
}
}
}
/******************************************************************************/
/******************************************************************************/
//Rutinas de separacion de comando y direccion.
void take_data(void){
i=16;
data=0;
for(j=0;j<9;j++){
data= data<<1;
data=data+bit_test(word,i);
i--;
}
}
void take_address(void){
i=8;
address=0;
for(j=0;j<8;j++){
address=address<<1;
address=address+bit_test(word,i);
i--;
}
}
/******************************************************************************/
/******************************************************************************/
void main(){
//port_b_pullups(TRUE); //Pull-up para RB0
setup_timer_1(T1_INTERNAL | T1_DIV_BY_1); //configuracion del timer0.
ext_int_edge(H_TO_L); //En canto de bajada se activa int.
//cambio=0; //control de cambio, para que comience a contar.
//longitud=12;
enable_interrupts(int_ext); //habilita la interrupcion externa (Rb0).
enable_interrupts(GLOBAL);
do{
if(dato_recibido){
DISABLE_INTERRUPTS(INT_EXT);
if(word==0x0008){
output_high(PIN_B1);
ENABLE_INTERRUPTS(INT_EXT);
}
}
}while(TRUE);
}