1-Wire: Rozdiel medzi revíziami
Zo stránky SensorWiki
Vytvorená stránka „ * [http://en.wikipedia.org/wiki/1-Wire 1-Wire bus description] (wikipedia.org) * [http://www.1wire.org/Files/Articles/1-Wire-Design%20Guide%20v1.0.pdf Design guide] Ex...“ |
Bez shrnutí editace |
||
Riadok 6: | Riadok 6: | ||
<source lang="c"> | <source lang="c"> | ||
#include <LiquidCrystal.h> | |||
#include <OneWire.h> | |||
OneWire ow(12); | |||
LiquidCrystal lcd(3, 2, 1, 4, 5, 6, 7); | |||
byte addr[8]; | |||
String keyStatus=""; | |||
void setup(void) { | |||
lcd.begin(8, 2); | |||
lcd.print( "iButton-" ); | |||
lcd.setCursor(0, 1); | |||
lcd.print( "--reader" ); | |||
} | |||
void loop(void) { | |||
getKeyCode(); | |||
if ( keyStatus == "ok" ) { | |||
int i; | |||
lcd.setCursor(0, 0); | |||
for( i = 7; i > 3; i-- ) | |||
printHex( addr[i] ); | |||
lcd.setCursor(0, 1); | |||
for( i = 3; i >= 0; i-- ) | |||
printHex( addr[i] ); | |||
} | |||
else if ( keyStatus != "nothing" ) { | |||
lcd.clear(); | |||
lcd.print(keyStatus); | |||
} | |||
delay(50); | |||
} | |||
void getKeyCode(){ | |||
if ( !ow.search(addr) ) { | |||
ow.reset_search(); | |||
keyStatus = "nothing"; | |||
return; | |||
} | |||
if ( OneWire::crc8(addr, 7) != addr[7] ) { | |||
keyStatus = "CRC err"; | |||
return; | |||
} | |||
if ( addr[0] != 0x01 ) { | |||
keyStatus = "DS1990A"; | |||
return; | |||
} | |||
keyStatus = "ok"; | |||
ow.reset(); | |||
} | |||
void printHex(byte x) | |||
{ | { | ||
/ | lcd.print( x / 16, HEX ); | ||
lcd.print( x % 16, HEX ); | |||
} | } | ||
</source> | </source> |
Aktuálna revízia z 07:11, 18. október 2012
- 1-Wire bus description (wikipedia.org)
- Design guide
Example program:
#include <LiquidCrystal.h>
#include <OneWire.h>
OneWire ow(12);
LiquidCrystal lcd(3, 2, 1, 4, 5, 6, 7);
byte addr[8];
String keyStatus="";
void setup(void) {
lcd.begin(8, 2);
lcd.print( "iButton-" );
lcd.setCursor(0, 1);
lcd.print( "--reader" );
}
void loop(void) {
getKeyCode();
if ( keyStatus == "ok" ) {
int i;
lcd.setCursor(0, 0);
for( i = 7; i > 3; i-- )
printHex( addr[i] );
lcd.setCursor(0, 1);
for( i = 3; i >= 0; i-- )
printHex( addr[i] );
}
else if ( keyStatus != "nothing" ) {
lcd.clear();
lcd.print(keyStatus);
}
delay(50);
}
void getKeyCode(){
if ( !ow.search(addr) ) {
ow.reset_search();
keyStatus = "nothing";
return;
}
if ( OneWire::crc8(addr, 7) != addr[7] ) {
keyStatus = "CRC err";
return;
}
if ( addr[0] != 0x01 ) {
keyStatus = "DS1990A";
return;
}
keyStatus = "ok";
ow.reset();
}
void printHex(byte x)
{
lcd.print( x / 16, HEX );
lcd.print( x % 16, HEX );
}