ibutton function for mbed working!

Dependencies:   OneWire mbed

Fork of ibutton by Stijn Sontrop

Committer:
Renato
Date:
Tue Aug 27 16:20:09 2013 +0000
Revision:
1:d6732fa1e5f8
Parent:
0:39cce56e485b
ibutton function working!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
StijnS 0:39cce56e485b 1 #include "mbed.h"
Renato 1:d6732fa1e5f8 2 //#define ibuttonpin p21
StijnS 0:39cce56e485b 3
StijnS 0:39cce56e485b 4 Serial pc(USBTX, USBRX);
StijnS 0:39cce56e485b 5
Renato 1:d6732fa1e5f8 6 //DigitalInOut ibuttondata(ibuttonpin);
Renato 1:d6732fa1e5f8 7 DigitalInOut sensor(p21);
StijnS 0:39cce56e485b 8
StijnS 0:39cce56e485b 9 typedef struct {
StijnS 0:39cce56e485b 10 unsigned char family;
StijnS 0:39cce56e485b 11 unsigned char serial[6];
StijnS 0:39cce56e485b 12 unsigned char crc;
StijnS 0:39cce56e485b 13 unsigned char valid;
StijnS 0:39cce56e485b 14 } ibuttonvalue;
StijnS 0:39cce56e485b 15
StijnS 0:39cce56e485b 16
StijnS 0:39cce56e485b 17 unsigned char crc8(unsigned char crc, unsigned char data) { //Calculate CRC8
StijnS 0:39cce56e485b 18 crc = crc ^ data;
StijnS 0:39cce56e485b 19 for (int i = 0; i < 8; i++) {
StijnS 0:39cce56e485b 20 if (crc & 0x01) {
StijnS 0:39cce56e485b 21 crc = (crc >> 1) ^ 0x8C;
StijnS 0:39cce56e485b 22 } else {
StijnS 0:39cce56e485b 23 crc >>= 1;
StijnS 0:39cce56e485b 24 }
StijnS 0:39cce56e485b 25 }
StijnS 0:39cce56e485b 26 return crc;
StijnS 0:39cce56e485b 27 }
StijnS 0:39cce56e485b 28
StijnS 0:39cce56e485b 29 void OneWireReset(void) { //Generates reset on 1-wire bus
Renato 1:d6732fa1e5f8 30 sensor.output();
Renato 1:d6732fa1e5f8 31 sensor = 0;
StijnS 0:39cce56e485b 32 wait_us(500);
Renato 1:d6732fa1e5f8 33 sensor.input();
StijnS 0:39cce56e485b 34 wait_us(500);
StijnS 0:39cce56e485b 35 }
StijnS 0:39cce56e485b 36
StijnS 0:39cce56e485b 37 void OneWireOutByte(unsigned char data) { //Write byte on 1-wire bus
StijnS 0:39cce56e485b 38 for (int n = 8;n!=0; n--) {
StijnS 0:39cce56e485b 39 if ((data & 0x01) == 1) {
Renato 1:d6732fa1e5f8 40 sensor.output();
Renato 1:d6732fa1e5f8 41 sensor = 0;
StijnS 0:39cce56e485b 42 wait_us(5);
Renato 1:d6732fa1e5f8 43 sensor.input();
StijnS 0:39cce56e485b 44 wait_us(60);
StijnS 0:39cce56e485b 45 } else {
Renato 1:d6732fa1e5f8 46 sensor.output();
StijnS 0:39cce56e485b 47 wait_us(60);
Renato 1:d6732fa1e5f8 48 sensor.input();
StijnS 0:39cce56e485b 49 }
StijnS 0:39cce56e485b 50 data = data >> 1;
StijnS 0:39cce56e485b 51 }
StijnS 0:39cce56e485b 52 }
StijnS 0:39cce56e485b 53
StijnS 0:39cce56e485b 54 unsigned char OneWireReadByte(void) { //Read 1 byte from 1-wire bus
StijnS 0:39cce56e485b 55 unsigned char d = 0;
StijnS 0:39cce56e485b 56 unsigned char b;
StijnS 0:39cce56e485b 57 for (int n = 0; n<8; n++) {
Renato 1:d6732fa1e5f8 58 sensor.output();
Renato 1:d6732fa1e5f8 59 sensor = 0;
StijnS 0:39cce56e485b 60 wait_us(5);
Renato 1:d6732fa1e5f8 61 sensor.input();
StijnS 0:39cce56e485b 62 wait_us(5);
Renato 1:d6732fa1e5f8 63 b = sensor;
StijnS 0:39cce56e485b 64 wait_us(50);
StijnS 0:39cce56e485b 65 d = (d >> 1) | (b << 7);
StijnS 0:39cce56e485b 66 }
StijnS 0:39cce56e485b 67 return d;
StijnS 0:39cce56e485b 68 }
StijnS 0:39cce56e485b 69
StijnS 0:39cce56e485b 70 ibuttonvalue DetectiButton(void) { //Function to detect an iButton en give back its contents
StijnS 0:39cce56e485b 71 ibuttonvalue detect;
StijnS 0:39cce56e485b 72 unsigned char crc = 0;
StijnS 0:39cce56e485b 73 OneWireReset();
StijnS 0:39cce56e485b 74 OneWireOutByte(0x33); //Read Rom cmd
StijnS 0:39cce56e485b 75 detect.family = OneWireReadByte();
StijnS 0:39cce56e485b 76 crc = crc8(crc, detect.family);
StijnS 0:39cce56e485b 77 if (detect.family == 0x00 || detect.family == 0xFF) {
StijnS 0:39cce56e485b 78 detect.valid = 0;
StijnS 0:39cce56e485b 79 return detect; //No iButton detected
StijnS 0:39cce56e485b 80 }
StijnS 0:39cce56e485b 81 for (int i = 0; i <6; i++) {
StijnS 0:39cce56e485b 82 detect.serial[i] = OneWireReadByte();
StijnS 0:39cce56e485b 83 crc = crc8(crc, detect.serial[i]);
StijnS 0:39cce56e485b 84 }
StijnS 0:39cce56e485b 85 detect.crc = OneWireReadByte();
StijnS 0:39cce56e485b 86 if (crc == detect.crc) { //If CRC is valid: set valid flag to 1
StijnS 0:39cce56e485b 87 detect.valid = 1;
StijnS 0:39cce56e485b 88 }
StijnS 0:39cce56e485b 89 return detect;
StijnS 0:39cce56e485b 90 }
Renato 1:d6732fa1e5f8 91
Renato 1:d6732fa1e5f8 92 //LocalFileSystem local("local");
Renato 1:d6732fa1e5f8 93
StijnS 0:39cce56e485b 94 int main() {
Renato 1:d6732fa1e5f8 95
Renato 1:d6732fa1e5f8 96 //FILE *fp = fopen("/local/out.txt", "rw");
Renato 1:d6732fa1e5f8 97
Renato 1:d6732fa1e5f8 98 ibuttonvalue detected;
Renato 1:d6732fa1e5f8 99 int i;
StijnS 0:39cce56e485b 100 while(1) {
StijnS 0:39cce56e485b 101 detected = DetectiButton();
StijnS 0:39cce56e485b 102 if (detected.valid == 1) { //Test valid flag
StijnS 0:39cce56e485b 103 pc.printf("iButton Family: %X Serial: ", detected.family);
Renato 1:d6732fa1e5f8 104 for (i=6-1; i>=0; i--) {
StijnS 0:39cce56e485b 105 pc.printf("%X", detected.serial[i]);
Renato 1:d6732fa1e5f8 106 // fprintf(fp, "Serial: %X", detected.serial[i]);
Renato 1:d6732fa1e5f8 107
StijnS 0:39cce56e485b 108 }
StijnS 0:39cce56e485b 109 pc.printf(" CRC: %X\n", detected.crc);
Renato 1:d6732fa1e5f8 110 }//fclose(fp);
StijnS 0:39cce56e485b 111 }
StijnS 0:39cce56e485b 112 }