Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of ibutton by
main.cpp
00001 #include "mbed.h" 00002 //#define ibuttonpin p21 00003 00004 Serial pc(USBTX, USBRX); 00005 00006 //DigitalInOut ibuttondata(ibuttonpin); 00007 DigitalInOut sensor(p21); 00008 00009 typedef struct { 00010 unsigned char family; 00011 unsigned char serial[6]; 00012 unsigned char crc; 00013 unsigned char valid; 00014 } ibuttonvalue; 00015 00016 00017 unsigned char crc8(unsigned char crc, unsigned char data) { //Calculate CRC8 00018 crc = crc ^ data; 00019 for (int i = 0; i < 8; i++) { 00020 if (crc & 0x01) { 00021 crc = (crc >> 1) ^ 0x8C; 00022 } else { 00023 crc >>= 1; 00024 } 00025 } 00026 return crc; 00027 } 00028 00029 void OneWireReset(void) { //Generates reset on 1-wire bus 00030 sensor.output(); 00031 sensor = 0; 00032 wait_us(500); 00033 sensor.input(); 00034 wait_us(500); 00035 } 00036 00037 void OneWireOutByte(unsigned char data) { //Write byte on 1-wire bus 00038 for (int n = 8;n!=0; n--) { 00039 if ((data & 0x01) == 1) { 00040 sensor.output(); 00041 sensor = 0; 00042 wait_us(5); 00043 sensor.input(); 00044 wait_us(60); 00045 } else { 00046 sensor.output(); 00047 wait_us(60); 00048 sensor.input(); 00049 } 00050 data = data >> 1; 00051 } 00052 } 00053 00054 unsigned char OneWireReadByte(void) { //Read 1 byte from 1-wire bus 00055 unsigned char d = 0; 00056 unsigned char b; 00057 for (int n = 0; n<8; n++) { 00058 sensor.output(); 00059 sensor = 0; 00060 wait_us(5); 00061 sensor.input(); 00062 wait_us(5); 00063 b = sensor; 00064 wait_us(50); 00065 d = (d >> 1) | (b << 7); 00066 } 00067 return d; 00068 } 00069 00070 ibuttonvalue DetectiButton(void) { //Function to detect an iButton en give back its contents 00071 ibuttonvalue detect; 00072 unsigned char crc = 0; 00073 OneWireReset(); 00074 OneWireOutByte(0x33); //Read Rom cmd 00075 detect.family = OneWireReadByte(); 00076 crc = crc8(crc, detect.family); 00077 if (detect.family == 0x00 || detect.family == 0xFF) { 00078 detect.valid = 0; 00079 return detect; //No iButton detected 00080 } 00081 for (int i = 0; i <6; i++) { 00082 detect.serial[i] = OneWireReadByte(); 00083 crc = crc8(crc, detect.serial[i]); 00084 } 00085 detect.crc = OneWireReadByte(); 00086 if (crc == detect.crc) { //If CRC is valid: set valid flag to 1 00087 detect.valid = 1; 00088 } 00089 return detect; 00090 } 00091 00092 //LocalFileSystem local("local"); 00093 00094 int main() { 00095 00096 //FILE *fp = fopen("/local/out.txt", "rw"); 00097 00098 ibuttonvalue detected; 00099 int i; 00100 while(1) { 00101 detected = DetectiButton(); 00102 if (detected.valid == 1) { //Test valid flag 00103 pc.printf("iButton Family: %X Serial: ", detected.family); 00104 for (i=6-1; i>=0; i--) { 00105 pc.printf("%X", detected.serial[i]); 00106 // fprintf(fp, "Serial: %X", detected.serial[i]); 00107 00108 } 00109 pc.printf(" CRC: %X\n", detected.crc); 00110 }//fclose(fp); 00111 } 00112 }
Generated on Wed Jul 13 2022 15:16:55 by
1.7.2
