-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensor.cp
186 lines (177 loc) · 2.79 KB
/
sensor.cp
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
#line 1 "C:/Users/Mario/Desktop/sensor_temperatura/sensor.c"
unsigned short i, DD0 = 0x40, DD1 = 0x40, N_Flag, valor_manual;
unsigned temp_value = 0;
unsigned short temp_minima = 10;
unsigned short temp_maxima = 99;
unsigned short temp_por_defecto = 20;
unsigned short mask(unsigned short num);
void display_temp(short DD0, short DD1);
void DS18B20();
unsigned short presionBoton(unsigned short pin);
void main()
{
CMCON |= 7;
TRISA0_bit = 0;
TRISA1_bit = 0;
TRISA2_bit = 0;
TRISA4_bit = 0;
TRISB0_bit = 0;
TRISB1_bit = 0;
TRISB2_bit = 0;
TRISB3_bit = 0;
TRISB4_bit = 0;
TRISB5_bit = 0;
TRISB6_bit = 0;
PORTB = 1;
temp_por_defecto = EEPROM_Read(0x01);
valor_manual = temp_por_defecto;
RA0_bit = 0;
RA1_bit = 0;
RA2_bit = 1;
RA4_bit = 0;
do
{
DD0 = valor_manual % 10;
DD0 = mask(DD0);
DD1 = (valor_manual / 10) % 10;
DD1 = mask(DD1);
display_temp(DD0, DD1);
if (presionBoton(3) == 3)
{
if (valor_manual < temp_maxima)
{
valor_manual++;
EEPROM_Write(0x01,valor_manual);
}
}
if (presionBoton(6) == 6)
{
if (valor_manual >= temp_minima)
{
valor_manual--;
EEPROM_Write(0x01,valor_manual);
}
}
if (presionBoton(7) == 7)
{
do
{
N_Flag = 0;
DS18B20();
DD0 = temp_value % 10;
DD0 = mask(DD0);
DD1 = (temp_value / 10) % 10;
DD1 = mask(DD1);
display_temp(DD0, DD1);
if (temp_value >= valor_manual)
{
RA2_bit = 1;
RA4_bit = 0;
}
else
{
RA2_bit = 0;
RA4_bit = 1;
}
} while (!presionBoton(7));
RA2_bit = 1;
RA4_bit = 0;
}
} while (1);
}
unsigned short mask(unsigned short num)
{
switch (num)
{
case 0:
return 0x40;
case 1:
return 0x79;
case 2:
return 0x24;
case 3:
return 0x30;
case 4:
return 0x19;
case 5:
return 0x12;
case 6:
return 0x02;
case 7:
return 0x78;
case 8:
return 0x00;
case 9:
return 0x10;
case 10:
return 0xBF;
case 11:
return 0x9E;
case 12:
return 0xFF;
}
}
void display_temp(short DD0, short DD1)
{
for (i = 0; i <= 4; i++)
{
PORTB = DD0;
RA0_bit = 0;
RA1_bit = 1;
delay_ms(2);
PORTB = DD1;
RA0_bit = 1;
RA1_bit = 0;
delay_ms(2);
}
return;
}
void DS18B20()
{
Ow_Reset(&PORTB, 7);
Ow_Write(&PORTB, 7, 0xCC);
Ow_Write(&PORTB, 7, 0x44);
Ow_Reset(&PORTB, 7);
Ow_Write(&PORTB, 7, 0xCC);
Ow_Write(&PORTB, 7, 0xBE);
temp_value = Ow_Read(&PORTB, 7);
temp_value = (Ow_Read(&PORTB, 7) << 8) + temp_value;
if (temp_value & 0x8000)
{
temp_value = ~temp_value + 1;
N_Flag = 1;
}
if (temp_value & 0x0001)
temp_value += 1;
temp_value = temp_value >> 4;
if (N_Flag == 1)
{
temp_value = 0;
}
if (temp_value >= 99)
{
temp_value = 99;
}
}
unsigned short presionBoton(unsigned short pin)
{
int pulso = 0;
int oldstate = 0;
pulso = Button(&PORTA, pin, 100, 1);
if (pulso != 0)
{
oldstate = 1;
}
while (pulso == 1)
{
pulso = Button(&PORTA, pin, 100, 1);
}
if (oldstate == 1)
{
return pin;
}
else
{
return 0;
}
}