Last active
June 18, 2017 16:39
-
-
Save Jacajack/0c7c0c7e66dae4b5df5736b5ee7449e3 to your computer and use it in GitHub Desktop.
Development version of DS18B20 ROM search algorithm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#define ROMCNT 20 | |
unsigned char roms[ROMCNT] = {0}; | |
unsigned char active[ROMCNT] = {1, 1, 1, 1}; | |
int bitcnt = 0; | |
char getbit( ) | |
{ | |
int i = 0; | |
int bit = 1; | |
int mybit; | |
for ( i = 0; i < ROMCNT; i++ ) | |
{ | |
mybit = ( roms[i] >> bitcnt ) & 1; | |
if ( !active[i] ) continue; | |
if ( !mybit ) bit = 0; | |
} | |
return bit; | |
} | |
char get2bit( ) | |
{ | |
int i = 0; | |
int bit = 1; | |
int mybit; | |
for ( i = 0; i < ROMCNT; i++ ) | |
{ | |
mybit = ( roms[i] >> bitcnt ) & 1; | |
if ( !active[i] ) continue; | |
if ( mybit ) bit = 0; | |
} | |
bitcnt++; | |
return bit; | |
} | |
void reset( ) | |
{ | |
bitcnt = 0; | |
int i = 0; | |
for ( i = 0; i < ROMCNT; i++ ) | |
{ | |
active[i] = 1; | |
} | |
} | |
void sendbit( int bcnt, char bit ) | |
{ | |
int i = 0; | |
for ( i = 0; i < ROMCNT; i++ ) | |
{ | |
if ( ( ( roms[i] >> bcnt ) & 1 ) != bit ) | |
active[i] = 0; | |
} | |
} | |
int main( ) | |
{ | |
int i; | |
reset( ); | |
int a, b; | |
int currom = 0; | |
//unsigned char zero = 0; | |
unsigned char recvd[ROMCNT] = {0}; | |
//unsigned char *temp = &zero; | |
unsigned char junction = 0; | |
int resp; | |
int j; | |
long seed = time(NULL); | |
srand( seed ); | |
printf( "%ld\n", seed ); | |
for ( i = 0; i < ROMCNT; i++ ) roms[i] = rand( ); | |
do | |
{ | |
//currom = j; | |
for ( i = 0; i < 8; i++ ) | |
{ | |
a = getbit( ); | |
b = get2bit( ); | |
int k; | |
for ( k = 0; k < ROMCNT; k++ ) | |
printf( "\x1b[%dm%d\x1b[0m ", active[k] ? 32 : 31, active[k] ); | |
printf( "- [%d %d]", a, b ); | |
if ( a == 0 && b == 0 ) | |
{ | |
if ( ( junction >> ( i + 1 ) ) ) | |
{ | |
resp = ( ( ~junction >> i ) & 1 ); | |
} | |
else | |
{ | |
resp = ( ( junction >> i ) & 1 ); | |
junction ^= ( 1 << i ); | |
} | |
if ( resp ) | |
{ | |
recvd[currom] |= ( 1 << i ); | |
} | |
else | |
{ | |
recvd[currom] &= ~( 1 << i ); | |
} | |
sendbit( i, resp ); | |
printf( " - send %d <%02x>", resp, junction ); | |
} | |
if ( a == 0 && b == 1 ) | |
{ | |
recvd[currom] &= ~( 1 << i ); | |
printf( " - it's 0" ); | |
sendbit( i, 0 ); | |
} | |
if ( a == 1 && b == 0 ) | |
{ | |
recvd[currom] |= ( 1 << i ); | |
printf( " - it's 1" ); | |
sendbit( i, 1 ); | |
} | |
//printf( "(%d)", !!( recvd[currom] & ( 1 << i ) ) ); | |
printf( "\n" ); | |
//temp = recvd +currom; | |
} | |
//printf( "<junction: %2x>\n", junction ); | |
printf( "----------------\n" ); | |
reset( ); | |
currom++; | |
} while ( junction ); | |
for ( i =0; i < ROMCNT; i++ ) printf( "%02x, ", recvd[i] ); | |
printf( "\n" ); | |
for ( i =0; i < ROMCNT; i++ ) printf( "%02x, ", roms[i] ); | |
printf( "\n" ); | |
for ( i =0; i < ROMCNT; i++ ) | |
{ | |
for ( j = 0; j < ROMCNT; j++ ) | |
if ( roms[i] == recvd[j] ) | |
{ | |
printf( "XX, " ); | |
break; | |
} | |
if ( j == ROMCNT ) printf( " , " ); | |
} | |
printf( "\n" ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment