Function to get the ID of iButtons via the 1-wire bus

Dependencies:   mbed

Committer:
StijnS
Date:
Sat Feb 12 11:00:05 2011 +0000
Revision:
0:39cce56e485b

        

Who changed what in which revision?

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