-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathOneWire.c
1678 lines (1338 loc) · 43.2 KB
/
OneWire.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ************************************************************************
*
* OneWire communication and tools
*
* (c) 2018-2023 by Markus Reschke
*
* ************************************************************************ */
/*
* hints:
* - DQ (data line) requires a 4.7kOhms pull-up resistor to Vcc
* - pin assignment for probes (ONEWIRE_PROBES)
* probe #1 Gnd
* probe #2 DQ (requires 4.7kOhms pull-up resistor to Vcc)
* probe #3 Vcc (via Rl to limit current)
* - port and pins for dedicated MCU pin (ONEWIRE_IO_PIN)
* ONEWIRE_PORT port data register
* ONEWIRE_DDR port data direction register
* ONEWIRE_PIN port input pins register
* ONEWIRE_DQ pin for DQ
* - standard-speed (accurate 1µs delay)
* - external power for clients (no parasite power)
*/
/* local includes */
#include "config.h" /* global configuration */
#if defined (ONEWIRE_IO_PIN) || defined (ONEWIRE_PROBES)
/*
* local constants
*/
/* source management */
#define ONEWIRE_C
/* operation modes */
#define MODE_MANUAL 0 /* manual mode */
#define MODE_AUTO 1 /* automatic mode */
/*
* include header files
*/
/* local includes */
#include "common.h" /* common header file */
#include "variables.h" /* global variables */
#include "functions.h" /* external functions */
#include "OneWire.h" /* OneWire specifics */
/*
* local variables
*/
/* CRC */
uint8_t CRC8; /* current CRC-8 */
#if defined (ONEWIRE_READ_ROM) || defined (SW_ONEWIRE_SCAN)
/* ROM code */
uint8_t ROM_Code[8]; /* ROM code */
uint8_t LastCon; /* bit position of last code conflict */
#endif
/* ************************************************************************
* low level functions
* ************************************************************************ */
#ifdef ONEWIRE_IO_PIN
/*
* set up OneWire bus
* - DQ line
*/
void OneWire_Setup(void)
{
/*
* DQ is driven as open-drain output
* and pulled up by external 4.7kOhm resistor
*/
/* set DQ to input mode */
ONEWIRE_DDR &= ~(1 << ONEWIRE_DQ); /* clear bit */
/* preset DQ to low for output mode */
ONEWIRE_PORT &= ~(1 << ONEWIRE_DQ); /* clear bit */
}
#endif
#ifdef ONEWIRE_PROBES
/*
* set up probes for OneWire bus
* - probe-1: Gnd
* - probe-2: DQ (pull-up resistor required)
* - probe-3: Vcc (current limited by Rl)
*
* requires:
* - String: string stored in EEPROM for first display line
*
* returns:
* - 1 on success
* - 0 on any problem
*/
uint8_t OneWire_Probes(const unsigned char *String)
{
uint8_t Flag = 0; /* return value */
uint8_t Run = 1; /* loop control */
/* inform user in line #1 */
ShortCircuit(0); /* make sure probes are not shorted */
LCD_Clear();
#ifdef UI_COLORED_TITLES
Display_ColoredEEString(String, COLOR_TITLE); /* display String */
#else
Display_EEString(String); /* display String */
#endif
/* display module pinout (1: Gnd / 2: Data / 3: Vcc) */
Display_NextLine();
Show_SimplePinout('-', 'd', '+');
/* set probes: probe-1 -- Gnd / probe-3 -- Rl -- Vcc / probe-2 (HiZ) -- Rh -- Gnd */
/* pull up probe-3 via Rl, pull down probe-2 via Rh */
R_DDR = (1 << R_RL_3) | (1 << R_RH_2); /* enable resistors */
R_PORT = (1 << R_RL_3); /* pull up probe-3, pull down probe-2 */
ADC_PORT = 0; /* pull down directly: */
ADC_DDR = (1 << TP1); /* probe-1 */
wait20ms(); /* time to settle */
/* wait for external pull-up resistor or key press */
while (Run)
{
if (ADC_PIN & (1 << TP2)) /* check for high level of probe-2 */
{
Flag = 1; /* signal "bus ok" */
Run = 0; /* end loop */
}
else /* check test key */
{
/* wait 100ms for key press */
Flag = TestKey(100, CHECK_BAT);
if (Flag) /* key pressed */
{
Flag = 0; /* signal "skipped" */
Run = 0; /* end loop */
}
}
}
/* remove pull-down via Rh for probe-2 */
R_DDR = (1 << R_RL_3); /* disable Rh for probe-2 */
return Flag;
}
#endif
/*
* reset bus and check for presence pulse from client(s)
*
* returns:
* - 0: no presence pulse from client
* - 1: presence pulse from client
*/
uint8_t OneWire_ResetBus(void)
{
uint8_t Flag = 0; /* return value */
/*
* pull down DQ for >=480µs and release it again
*/
#ifdef ONEWIRE_IO_PIN
/* change DQ to output mode (port pin is low) */
ONEWIRE_DDR |= (1 << ONEWIRE_DQ); /* set bit */
#endif
#ifdef ONEWIRE_PROBES
/* change probe-2 (DQ) to output mode (port pin is low) */
ADC_DDR |= (1 << TP2); /* set bit */
#endif
wait500us(); /* delay of 500µs */
#ifdef ONEWIRE_IO_PIN
/* change DQ back to input mode */
ONEWIRE_DDR &= ~(1 << ONEWIRE_DQ); /* clear bit */
#endif
#ifdef ONEWIRE_PROBES
/* change probe-2 (DQ) back to input mode */
ADC_DDR &= ~(1 << TP2); /* clear bit */
#endif
/*
* check for presence pulse from client(s)
* - time slot >=480µs
* - client responds after 15-60µs with a low pulse of 60-240µs
* - we check DQ after 70µs
*/
wait50us(); /* read delay of 70µs */
wait20us();
#ifdef ONEWIRE_IO_PIN
/* read DQ */
if (! (ONEWIRE_PIN & (1 << ONEWIRE_DQ))) /* low */
{
Flag = 1; /* signal ok */
}
#endif
#ifdef ONEWIRE_PROBES
/* read DQ (probe-2) */
if (! (ADC_PIN & (1 << TP2))) /* low */
{
Flag = 1; /* signal ok */
}
#endif
/* end the time slot */
wait400us(); /* delay of 430µs (500µs - 70µs) */
wait30us();
return Flag;
}
/*
* send bit
*
* requires:
* - Bit: 0/1
*/
void OneWire_SendBit(uint8_t Bit)
{
/*
* - write time slot: 60-120µs
* - recovery time after write: >=1µs
* - write "1":
* - 1-15µs low pulse
* - release DQ and wait until end of time slot
* - write "0":
* - 60-120µs low pulse (complete time slot)
* - release DQ
*/
/*
* pull down DQ
*/
#ifdef ONEWIRE_IO_PIN
/* change DQ to output mode (port pin is low) */
ONEWIRE_DDR |= (1 << ONEWIRE_DQ); /* set bit */
#endif
#ifdef ONEWIRE_PROBES
/* change probe-2 (DQ) to output mode (port pin is low) */
ADC_DDR |= (1 << TP2); /* set bit */
#endif
if (Bit) /* 1 */
{
/*
* pull down DQ for 5µs and release it again
*/
wait5us(); /* pulse delay of 5µs */
#ifdef ONEWIRE_IO_PIN
/* change DQ back to input mode */
ONEWIRE_DDR &= ~(1 << ONEWIRE_DQ); /* clear bit */
#endif
#ifdef ONEWIRE_PROBES
/* change probe-2 (DQ) back to input mode */
ADC_DDR &= ~(1 << TP2); /* clear bit */
#endif
/* end the time slot + recovery time */
wait50us(); /* delay of 65µs */
wait5us();
wait10us();
}
else /* 0 */
{
/*
* pull down DQ for 60µs and release it again
*/
wait50us(); /* pulse delay of 60µs */
wait10us();
#ifdef ONEWIRE_IO_PIN
/* change DQ back to input mode */
ONEWIRE_DDR &= ~(1 << ONEWIRE_DQ); /* clear bit */
#endif
#ifdef ONEWIRE_PROBES
/* change probe-2 (DQ) back to input mode */
ADC_DDR &= ~(1 << TP2); /* clear bit */
#endif
/* recovery time */
wait10us(); /* delay of 10µs */
}
}
/*
* read bit
*
* returns:
* - Bit: 0/1
*/
uint8_t OneWire_ReadBit(void)
{
uint8_t Bit = 0; /* return value */
/*
* - read time slot: 60-120µs
* - recovery time after read: >=1µs
* - master starts read process by a low pulse >=1µs
* - client's response is valid for 15µs after falling edge of
* the master's low pulse
* - we read DQ after 13µs
*/
/*
* pull down DQ for 5µs and release it again
*/
#ifdef ONEWIRE_IO_PIN
/* change DQ to output mode (port pin is low) */
ONEWIRE_DDR |= (1 << ONEWIRE_DQ); /* set bit */
#endif
#ifdef ONEWIRE_PROBES
/* change probe-2 (DQ) to output mode (port pin is low) */
ADC_DDR |= (1 << TP2); /* set bit */
#endif
wait5us(); /* pulse delay of 5µs */
#ifdef ONEWIRE_IO_PIN
/* change DQ back to input mode */
ONEWIRE_DDR &= ~(1 << ONEWIRE_DQ); /* clear bit */
#endif
#ifdef ONEWIRE_PROBES
/* change probe-2 (DQ) back to input mode */
ADC_DDR &= ~(1 << TP2); /* clear bit */
#endif
/*
* read client's response
* - data bit valid for 15µs starting with master's read pulse
* - high -> 1
* - low -> 0
*/
wait5us(); /* read delay of 8µs */
wait3us();
#ifdef ONEWIRE_IO_PIN
/* read DQ */
if (ONEWIRE_PIN & (1 << ONEWIRE_DQ)) /* high */
{
Bit = 1; /* 1 */
}
#endif
#ifdef ONEWIRE_PROBES
/* read DQ (probe-2) */
if (ADC_PIN & (1 << TP2)) /* high */
{
Bit = 1; /* 1 */
}
#endif
/* end the time slot + recovery time */
wait50us(); /* delay of 57µs */
wait5us();
wait2us();
return Bit;
}
/*
* send byte
* - sending bit order: LSB
*
* requires:
* - Byte: data byte
*/
void OneWire_SendByte(uint8_t Byte)
{
uint8_t n = 0; /* counter */
while (n < 8) /* 8 bits */
{
OneWire_SendBit(Byte & 0x01); /* send LSB */
Byte >>= 1; /* shift to right */
n++; /* next bit */
}
}
/*
* read byte
* - reading bit order: LSB
*
* returns:
* - Byte: data byte
*/
uint8_t OneWire_ReadByte(void)
{
uint8_t Byte = 0; /* return value */
uint8_t n = 0; /* counter */
while (n < 8) /* 8 bits */
{
Byte >>= 1; /* shift to right */
if (OneWire_ReadBit()) /* read "1" */
{
Byte |= 0b10000000; /* set bit */
}
n++; /* next bit */
}
return Byte;
}
/* ************************************************************************
* high level functions
* ************************************************************************ */
#if 0
/*
* address client
* - includes reset of the bus
* - transaction steps: initialization and ROM command
*
* requires:
* - ROM_Code: pointer to ROM code stored in an array of 8 bytes
*
* returns:
* - 1
* - 0
*/
uint8_t OneWire_AddressClient(uint8_t *ROM_Code)
{
uint8_t Flag = 0; /* return value */
uint8_t n = 0; /* counter */
/* transaction: initialization */
/* reset bus and check for presence pulse */
Flag = OneWire_ResetBus();
if (Flag) /* detected client(s) */
{
/* transaction: ROM command */
if (ROM_Code) /* valid pointer */
{
/* address specific client on the bus */
OneWire_SendByte(CMD_MATCH_ROM); /* select client with ROM code */
/* send client's ROM code */
while (n < 8) /* 8 bytes */
{
OneWire_SendByte(*ROM_Code); /* send one byte of ROM code */
ROM_Code++; /* next byte */
n++; /* next byte */
}
}
else /* NULL pointer */
{
/* assumes a single client on the bus */
OneWire_SendByte(CMD_SKIP_ROM); /* select all clients */
}
}
return Flag;
}
#endif
/*
* calculate CRC-8
* - CRC = X^8 + X^5 + X^4 + 1
* - start value: 0x00
* - uses variable CRC8 to track current CRC
*
* requires:
* - Byte: new input byte
*/
void OneWire_CRC8(uint8_t Byte)
{
uint8_t n = 0; /* counter */
uint8_t Bit; /* LSB */
while (n < 8) /* 8 bits */
{
/* XOR current LSB of input with CRC8's current X^8 */
Bit = CRC8 ^ Byte; /* XOR */
Bit &= 0b00000001; /* filter LSB */
/* shift CRC right */
CRC8 >>= 1; /* for next bit */
if (Bit) /* XORed LSB is 1 */
{
/*
* XOR CRC's X^5 and X^4 with 1
* - XOR with 0b00011000
* - since CRC is already shifted right: XOR with 0b00001100
* - since we have to feed the XORed LSB back into the CRC
* and the MSB is 0 after shifting: XOR with 0b10001100
*/
CRC8 ^= 0b10001100; /* XOR */
}
/* when 0:
* - XOR would keep the original bits
* - MSB will be 0 after a right shift anyway
*/
/* shift input right */
Byte >>= 1; /* for next input bit */
n++; /* next bit */
}
}
/* ************************************************************************
* Search ROM
* ************************************************************************ */
#ifdef SW_ONEWIRE_SCAN
/*
* search for next device (ROM code)
*
* returns:
* - 2 found last device
* - 1 found next device
* - 0 on any problem
*/
uint8_t OneWire_Next_ROM_Code(void)
{
uint8_t Flag; /* return value */
uint8_t BitPos = 1; /* bit position of ROM code */
uint8_t NewCon = 0; /* bit position of code conflict */
uint8_t Bit1; /* bit */
uint8_t Bit2; /* bit complement */
uint8_t NewBit; /* bit to send */
uint8_t CodeByte = 0; /* byte counter for ROM code */
uint8_t CodeBit = 1; /* bitmask for ROM code */
uint8_t Temp; /* temporary value */
wdt_reset(); /* reset watchdog */
/* transaction: initialization */
/* reset bus and check for presence pulse */
Flag = OneWire_ResetBus();
if (Flag) /* detected client */
{
/* transaction: ROM command */
OneWire_SendByte(CMD_SEARCH_ROM); /* search ROM */
/*
* processing loop
*/
while (BitPos < 65) /* 64 bits */
{
NewBit = 2; /* default: invalid */
/* read bit and its complement */
Bit1 = OneWire_ReadBit(); /* read bit */
Bit2 = OneWire_ReadBit(); /* read complement */
/* process bits */
if (Bit1 != Bit2) /* valid bit (01 or 10) */
{
NewBit = Bit1; /* copy bit */
}
else if (Bit1 & Bit2) /* no response (11) */
{
BitPos = 64; /* end loop */
Flag = 0; /* signal bus error */
}
else /* code conflict (00) */
{
if (LastCon < BitPos) /* code conflict further up */
{
/* start branch with 0 */
NewBit = 0; /* set bit to 0 */
NewCon = BitPos; /* update bit position */
}
else if (LastCon == BitPos) /* hit pos of last code conflict */
{
/* change branch to 1 */
NewBit = 1; /* set bit to 1 */
}
else /* LastCon > BitPos */ /* code conflict further down */
{
/* get bit from ROM code */
Temp = ROM_Code[CodeByte] & CodeBit;
if (Temp == 0) /* bit is 0 */
{
NewCon = BitPos; /* update bit position */
/* this is going to be 1 in the next search run */
}
NewBit = Temp; /* use bit from ROM code */
}
}
/* process bit to send */
if (NewBit < 2) /* valid bit */
{
/* update ROM code */
if (NewBit == 0) /* bit is 0 */
{
ROM_Code[CodeByte] &= ~CodeBit; /* clear bit in ROM code */
}
else /* bit is 1 */
{
ROM_Code[CodeByte] |= CodeBit; /* set bit in ROM code */
}
/* send bit to select branch */
OneWire_SendBit(NewBit);
}
/* loop management */
if (CodeBit & 0b10000000) /* bitmask overflow */
{
CodeBit = 0b00000001; /* reset bitmask to first bit */
CodeByte++; /* next byte */
}
else /* no overflow */
{
CodeBit <<= 1; /* shift left by one */
}
BitPos++; /* next bit */
}
/* manage branching for next run */
LastCon = NewCon; /* update bit position of code conflict */
if (LastCon == 0) /* no devices left */
{
if (Flag) /* don't overwrite error */
{
Flag = 2; /* signal last device */
}
}
/* special case: all zero (pull-up resistor suddenly missing) */
if (ROM_Code[0] == 0) /* family code 00 */
{
Flag = 0; /* signal bus error */
}
}
return Flag;
}
/*
* read ROM codes of connected devices
*
* returns:
* - 1 on success
* - 0 on any problem
*/
uint8_t OneWire_Scan_Tool(void)
{
uint8_t Flag; /* return value */
uint8_t Run; /* loop control */
uint8_t Test; /* key / feedback */
uint8_t n; /* counter */
/* local constants */
#define RUN_FLAG 0b00000001 /* run */
#define RESET_FLAG 0b00000010 /* reset values */
#define OUTPUT_FLAG 0b00000100 /* output ROM code */
#define DONE_FLAG 0b00001000 /* last device */
#ifdef ONEWIRE_IO_PIN
Flag = 1; /* set default */
#endif
#ifdef ONEWIRE_PROBES
/* inform user about pinout and check for external pull-up resistor */
Flag = OneWire_Probes(OneWire_Scan_str);
if (Flag == 0) /* bus error */
{
return Flag; /* exit tool and signal error */
}
#endif
LCD_ClearLine2(); /* clear line #2 */
Display_EEString(Start_str); /* display: Start */
UI.LineMode = LINE_STD | LINE_KEEP; /* next-line mode: keep first line */
/*
* processing loop
*/
Run = RUN_FLAG | RESET_FLAG;
while (Run)
{
n = UI.CharPos_Y; /* get current line number */
/* user input */
/* wait for user input */
Test = TestKey(0, CURSOR_BLINK | CHECK_KEY_TWICE | CHECK_BAT);
if (Test == KEY_TWICE) /* two short key presses */
{
break; /* end loop */
}
/* reset values */
if (Run & RESET_FLAG) /* reset requested */
{
/* clear ROM code */
for (n = 0; n < 8; n++) /* 8 bytes */
{
ROM_Code[n] = 0;
}
LastCon = 0; /* reset bit position of last code conflict */
Run &= ~RESET_FLAG; /* clear flag */
}
else /* consecutive run */
{
LCD_CharPos(1, n); /* move to beginning of former line */
}
/* manage screen */
Display_NextLine(); /* move to next line */
/* search for next device */
if (Run) /* ok to proceed */
{
Test = OneWire_Next_ROM_Code(); /* get next device */
if (Test >= 1) /* got ROM code */
{
/* check CRC */
CRC8 = 0x00; /* reset CRC to start value */
n = 0;
while (n < 7) /* 7 data bytes */
{
OneWire_CRC8(ROM_Code[n]); /* process byte */
n++; /* next byte */
}
if (ROM_Code[7] == CRC8) /* CRC matches */
{
Run |= OUTPUT_FLAG; /* output ROM code */
}
else /* mismatch */
{
Display_EEString_Space(CRC_str); /* display: CRC */
Display_EEString(Error_str); /* display: error */
}
if (Test == 2) /* no devices left */
{
Run |= DONE_FLAG; /* last ROM code */
}
}
else /* error */
{
Display_EEString_Space(Bus_str); /* display: Bus */
Display_EEString(Error_str); /* display: error */
Run |= RESET_FLAG; /* reset values for next scan */
}
}
/* display ROM code */
if (Run & OUTPUT_FLAG) /* output requested */
{
/* display family code */
Display_HexByte(ROM_Code[0]); /* display family */
Display_Space(); /* display space */
/* display serial number (MSB left) */
for (n = 6; n > 0; n--) /* 6 bytes */
{
Display_HexByte(ROM_Code[n]); /* display S/N */
}
Run &= ~OUTPUT_FLAG; /* clear flag */
}
/* indicate end of scan */
if (Run & DONE_FLAG) /* no devices left */
{
Display_NL_EEString(Done_str); /* display: done */
Run |= RESET_FLAG; /* reset values for next scan */
Run &= ~DONE_FLAG; /* clear flag */
}
}
return Flag;
/* clean up */
#undef RUN_FLAG
#undef RESET_FLAG
#undef OUTPUT_FLAG
#undef DONE_FLAG
}
#endif
/* ************************************************************************
* Read ROM
* ************************************************************************ */
#ifdef ONEWIRE_READ_ROM
/*
* read ROM code of connected device
* - single client on the bus
* - also display ROM code
*/
void OneWire_Read_ROM_Code(void)
{
uint8_t Flag = 0; /* control flag */
uint8_t n; /* counter */
wdt_reset(); /* reset watchdog */
/* transaction: initialization */
/* reset bus and check for presence pulse */
Flag = OneWire_ResetBus();
if (Flag) /* detected client */
{
/* transaction: ROM command */
OneWire_SendByte(CMD_READ_ROM); /* read ROM */
/* read 8 bytes */
n = 0;
while (n < 8) /* 8 bytes */
{
ROM_Code[n] = OneWire_ReadByte(); /* read byte */
n++; /* next byte */
}
/* check CRC */
CRC8 = 0x00; /* reset CRC to start value */
n = 0;
while (n < 7) /* 7 data bytes */
{
OneWire_CRC8(ROM_Code[n]); /* process byte */
n++; /* next byte */
}
if (ROM_Code[7] == CRC8) /* CRC matches */
{
/* output ROM code */
/* display family code */
Display_HexByte(ROM_Code[0]); /* display family */
Display_Space(); /* display space */
/* display serial number (MSB left) */
for (n = 6; n > 0; n--) /* 6 bytes */
{
Display_HexByte(ROM_Code[n]); /* display S/N */
}
}
else /* mismatch */
{
Display_Minus(); /* display n/a */
}
}
}
#endif
/* ************************************************************************
* DS18B20
* ************************************************************************ */
#ifdef SW_DS18B20
/*
* DS18B20: read temperature
* - single client on the bus
*
* requires:
* - Value: pointer to temperature in °C
* - Scale: pointer to scale factor (*10^x)
*
* returns:
* - 1 on success
* - 0 on any problem
*/
uint8_t DS18B20_ReadTemperature(int32_t *Value, int8_t *Scale)
{
uint8_t Flag = 0; /* return value / control flag */
uint8_t Run = 0; /* loop control */
uint8_t n; /* counter */
uint8_t ScratchPad[9]; /* scratchpad */
uint8_t Sign; /* sign flag */
int16_t Temp; /* temperature */
wdt_reset(); /* reset watchdog */
/* transaction: initialization */
/* reset bus and check for presence pulse */
Flag = OneWire_ResetBus();
if (Flag) /* detected client */
{
/* transaction: ROM command */
OneWire_SendByte(CMD_SKIP_ROM); /* select all clients */
/* transaction: function command */
/* start conversion */
OneWire_SendByte(CMD_DS18B20_CONVERT_T);
/*
* maximum conversion time
* - 9 bits 93.75ms (t_conv/8)
* - 10 bits 187.5ms (t_conv/4)
* - 11 bits 375ms (t_conv/2)
* - 12 bits 750ms (t_conv)
*/