-
Notifications
You must be signed in to change notification settings - Fork 0
/
1wire.c
125 lines (98 loc) · 3.56 KB
/
1wire.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
#include <delay.h>
#include <mega16.h>
#define OW_DDR DDRB
#define OW_PIN PINB
#define OW_BUS 4
//------------------------------------------------------------------------------
// Èíèöèàëèçàöèÿ
//------------------------------------------------------------------------------
char iB_Presence(void)
{
char nalichie_ustroystva = 0;
OW_DDR |= (1<<OW_BUS);
delay_us(500);
OW_DDR &= ~(1<<OW_BUS);
delay_us(90);
if((OW_PIN&0x01)== 0)
{
nalichie_ustroystva = 1;
}
delay_us(200);
return nalichie_ustroystva;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Çàïèñü íà øèíó 1
//------------------------------------------------------------------------------
void Wire1_WriteBit1(void)
{
OW_DDR |= (1<<OW_BUS); // óñòàíàâëèâàåì øèíó â 0;
delay_us(3); // âûæèäàåì íå ìåíåå ÷åì 1 ìêñ;
OW_DDR &= ~(1<<OW_BUS); // îñâîáîæäàåì øèíó;
delay_us(60); // çàäåðæêà îêîí÷àíèÿ òàéìñëîòà;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Çàïèñü íà øèíó 0
//------------------------------------------------------------------------------
void Wire1_WriteBit0(void)
{
OW_DDR |= (1<<OW_BUS); // óñòàíàâëèâàåì øèíó â 0;
delay_us(60); // óäåðæèâàå ìøèíó â 0 60 ìêñ;
OW_DDR &= ~(1<<OW_BUS); // îñâîáîæäàåì øèíó;
delay_us(3); // çàäåðæêà îêîí÷àíèÿ òàéìñëîòà;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Ïîñûëêà áàéòà íà øèíó
//------------------------------------------------------------------------------
void Wire1_SendByte(char byte)
{
char count = 0;
for( count = 0; count < 8; count ++)
{
// âûâîäèì î÷åðåäíîé áèò íà÷èíàÿ ñ ìëàäøåãî;
if(byte & 0x01)
{
Wire1_WriteBit1();
}
else
{
Wire1_WriteBit0();
}
byte >>= 1; //ñäâèãàåì äàííûå âïðàâî;
}
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Ïðè¸ì áèòà ñ øèíû
//------------------------------------------------------------------------------
char OW_ReadBit(void)
{
char Bit;
OW_DDR |= (1<<OW_BUS); // óñòàíàâëèâàåì øèíó â 0;
delay_us(3); // âûæèäàåì íå ìåíåå ÷åì 1 ìêñ;
OW_DDR &= ~(1<<OW_BUS); // îñâîáîæäàåì øèíó;
delay_us(11); // îæèäàíèå óäåðæàíèÿ ñèãíàëà äàò÷èêîì;
Bit = (OW_PIN&0x01); // ñ÷èòûâàíèå ñîñòîÿíèÿ øèíû;
delay_us(55); // çàäåðæêà îêîí÷àíèÿ òàéìñëîòà;
return Bit;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Ïðè¸ì áàéòà ñ øèíû
//------------------------------------------------------------------------------
char Wire1_ReadByte(void)
{
char Byte=0x00, count;
for( count = 0; count < 8; count ++)
{
Byte >>= 1;
if(OW_ReadBit())
{
Byte |= 0x80;
}
}
return Byte;
}
//------------------------------------------------------------------------------