/***************************************** start header *****************************************/ #include <12f683.h> #fuses INTRC_IO, NOWDT,NOMCLR,NOPUT #byte TRISIO = 0x85 // GPIO tri-state register #byte GPIO = 0x05 // general purpose I/O register #byte ANSEL = 0x9F // analog select register #byte CMCON0 = 0x19 // comparator configuration register #byte IOC = 0x96 // interrupt on change register #byte INTCON = 0x0B // interrupt control register #bit GP0 = GPIO.0 #bit GP1 = GPIO.1 #bit GP2 = GPIO.2 #bit GP3 = GPIO.3 #bit SYNC = GPIO.4 #bit SYNCDIR = TRISIO.4 #bit TRX = GPIO.5 #bit TRXDIR = TRISIO.5 /***************************************** end header *****************************************/ /***************************************** start function prototypes *****************************************/ int get_byte(); void send_byte(int mybyte); void delay(int times, int16 time); /***************************************** end function prototypes *****************************************/ /***************************************** start main function *****************************************/ void main() { ANSEL = 0x00; CMCON0 = 0x07; IOC = 0x00; TRISIO = 0x00; INTCON = 0x00; while(1) { send_byte(6); } } /***************************************** end main function *****************************************/ /***************************************** start get_byte() function *****************************************/ int get_byte() { int a, mybyte; // setup 2wire comm direction SYNCDIR = 1; TRXDIR = 1; GP0 = 0x00; /* we wait for the start sequence */ while(1) { a = 0x00; if(SYNC & TRX) { a = 0x01; } while(SYNC & a) { if(SYNC & !TRX) { a = 0x00; goto shiftin; } } } /* shift in bits MSB first (128 place) */ shiftin: for(a=0;a!=8;a++) { while(!SYNC); if(SYNC) { mybyte = mybyte << 1; if(TRX) { mybyte = mybyte + 1; } } while(SYNC); } if(mybyte == 6) { GP0 ^= 0x01; delay(4,8000); } return 1; } /***************************************** end get_byte() function *****************************************/ /***************************************** start send_byte() function *****************************************/ void send_byte(int mybyte) { int i, a; // setup 2wire comm direction SYNCDIR = 0; TRXDIR = 0; /* start sequence */ SYNC = 0x01; TRX = 0x01; delay(1,500); TRX = 0x00; SYNC = 0x00; delay(1,1000); /* we start to send the eight bits MSB first (128 place) */ a = 128; for(i = 8;i != 0;i--) { if(mybyte&a) { TRX = 0x01; SYNC = 0x01; delay(1,500); SYNC = 0x00; } else { TRX = 0x00; SYNC = 0x01; delay(1,500); SYNC = 0x00; } a /= 2; } return; } /***************************************** end send_byte() function *****************************************/ /***************************************** start delay() function *****************************************/ void delay(int times, int16 time) { int16 i, a; for(a=0;a!=times;a++) { for(i=0;i!=time;i++); } return; } /***************************************** end delay() function *****************************************/