919 |
kakl |
1 |
/**** IR Mrakomer3 ****/ |
|
|
2 |
#define VERSION "3.0" |
|
|
3 |
#define ID "$Id: irmrak3.c 931 2007-12-27 15:03:55Z kakl $" |
|
|
4 |
#include "irmrak3.h" |
|
|
5 |
|
|
|
6 |
#define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default) |
|
|
7 |
#define RAM_Access 0x00 // RAM access command |
|
|
8 |
#define RAM_Tobj1 0x07 // To1 address in the eeprom |
|
|
9 |
#define RAM_Tamb 0x06 // Ta address in the eeprom |
|
|
10 |
#define HEATING PIN_A2 // Heating for defrosting |
|
|
11 |
#define MAXHEAT 60 // Number of cycles for heating |
|
|
12 |
|
931 |
kakl |
13 |
char VER[4]=VERSION; |
|
|
14 |
char REV[50]=ID; |
919 |
kakl |
15 |
|
931 |
kakl |
16 |
int8 heat; |
|
|
17 |
|
|
|
18 |
#INT_RDA |
|
|
19 |
rs232_handler() |
|
|
20 |
{ |
|
|
21 |
char ch; |
|
|
22 |
|
|
|
23 |
if (getc()=='h') |
|
|
24 |
{ |
|
|
25 |
heat=0; // Need warmer |
|
|
26 |
} |
|
|
27 |
else |
|
|
28 |
{ |
|
|
29 |
heat=MAXHEAT; // Stop heating |
|
|
30 |
} |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
|
919 |
kakl |
34 |
unsigned char PEC_calculation(unsigned char pec[]) // CRC calculation |
|
|
35 |
{ |
|
|
36 |
unsigned char crc[6]; |
|
|
37 |
unsigned char BitPosition=47; |
|
|
38 |
unsigned char shift; |
|
|
39 |
unsigned char i; |
|
|
40 |
unsigned char j; |
|
|
41 |
unsigned char temp; |
|
|
42 |
|
|
|
43 |
do |
|
|
44 |
{ |
|
|
45 |
crc[5]=0; /* Load CRC value 0x000000000107 */ |
|
|
46 |
crc[4]=0; |
|
|
47 |
crc[3]=0; |
|
|
48 |
crc[2]=0; |
|
|
49 |
crc[1]=0x01; |
|
|
50 |
crc[0]=0x07; |
|
|
51 |
BitPosition=47; /* Set maximum bit position at 47 */ |
|
|
52 |
shift=0; |
|
|
53 |
|
|
|
54 |
//Find first 1 in the transmited message |
|
|
55 |
i=5; /* Set highest index */ |
|
|
56 |
j=0; |
|
|
57 |
while((pec[i]&(0x80>>j))==0 && i>0) |
|
|
58 |
{ |
|
|
59 |
BitPosition--; |
|
|
60 |
if(j<7) |
|
|
61 |
{ |
|
|
62 |
j++; |
|
|
63 |
} |
|
|
64 |
else |
|
|
65 |
{ |
|
|
66 |
j=0x00; |
|
|
67 |
i--; |
|
|
68 |
} |
|
|
69 |
}/*End of while */ |
|
|
70 |
|
|
|
71 |
shift=BitPosition-8; /*Get shift value for crc value*/ |
|
|
72 |
|
|
|
73 |
|
|
|
74 |
//Shift crc value |
|
|
75 |
while(shift) |
|
|
76 |
{ |
|
|
77 |
for(i=5; i<0xFF; i--) |
|
|
78 |
{ |
|
|
79 |
if((crc[i-1]&0x80) && (i>0)) |
|
|
80 |
{ |
|
|
81 |
temp=1; |
|
|
82 |
} |
|
|
83 |
else |
|
|
84 |
{ |
|
|
85 |
temp=0; |
|
|
86 |
} |
|
|
87 |
crc[i]<<=1; |
|
|
88 |
crc[i]+=temp; |
|
|
89 |
}/*End of for*/ |
|
|
90 |
shift--; |
|
|
91 |
}/*End of while*/ |
|
|
92 |
|
|
|
93 |
//Exclusive OR between pec and crc |
|
|
94 |
for(i=0; i<=5; i++) |
|
|
95 |
{ |
|
|
96 |
pec[i] ^=crc[i]; |
|
|
97 |
}/*End of for*/ |
|
|
98 |
} while(BitPosition>8);/*End of do-while*/ |
|
|
99 |
|
|
|
100 |
return pec[0]; |
|
|
101 |
}/*End of PEC_calculation*/ |
|
|
102 |
|
|
|
103 |
|
|
|
104 |
int16 ReadTemp(int8 addr, int8 select) // Read sensor RAM |
|
|
105 |
{ |
|
|
106 |
unsigned char arr[6]; // Buffer for the sent bytes |
|
|
107 |
int8 crc; // Readed CRC |
|
|
108 |
int16 temp; // Readed temperature |
|
|
109 |
|
931 |
kakl |
110 |
disable_interrupts(GLOBAL); |
919 |
kakl |
111 |
i2c_stop(); |
|
|
112 |
i2c_start(); |
|
|
113 |
i2c_write(addr); |
|
|
114 |
i2c_write(RAM_Access|select); // Select the teperature sensor in device |
|
|
115 |
i2c_start(); |
|
|
116 |
i2c_write(addr); |
|
|
117 |
arr[2]=i2c_read(1); // lo |
|
|
118 |
arr[1]=i2c_read(1); // hi |
|
|
119 |
temp=MAKE16(arr[1],arr[2]); |
|
|
120 |
crc=i2c_read(0); //crc |
|
|
121 |
i2c_stop(); |
931 |
kakl |
122 |
enable_interrupts(GLOBAL); |
919 |
kakl |
123 |
|
|
|
124 |
arr[5]=addr; |
|
|
125 |
arr[4]=RAM_Access|select; |
|
|
126 |
arr[3]=addr; |
|
|
127 |
arr[0]=0; |
|
|
128 |
if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC |
|
|
129 |
|
|
|
130 |
return temp; |
|
|
131 |
} |
|
|
132 |
|
|
|
133 |
void main() |
|
|
134 |
{ |
|
|
135 |
unsigned int16 n, temp, tempa; |
|
|
136 |
signed int16 ta, to; |
|
|
137 |
|
|
|
138 |
output_low(HEATING); // Heating off |
|
|
139 |
setup_wdt(WDT_2304MS); // Setup Watch Dog |
|
|
140 |
setup_adc_ports(NO_ANALOGS); |
|
|
141 |
setup_adc(ADC_OFF); |
|
|
142 |
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); |
|
|
143 |
setup_timer_1(T1_DISABLED); |
|
|
144 |
setup_timer_2(T2_DISABLED,0,1); |
|
|
145 |
setup_comparator(NC_NC_NC_NC); |
|
|
146 |
setup_vref(FALSE); |
|
|
147 |
setup_oscillator(OSC_4MHZ|OSC_INTRC); |
|
|
148 |
|
|
|
149 |
delay_ms(1000); |
|
|
150 |
printf("\n\r* Mrakomer %s (C) 2007 KAKL *\n\r",VER); // Welcome message |
|
|
151 |
printf("* %s *\n\r\n\r",REV); |
|
|
152 |
tempa=ReadTemp(SA, RAM_Tamb); // Dummy read |
|
|
153 |
temp=ReadTemp(SA, RAM_Tobj1); |
|
|
154 |
|
|
|
155 |
n=0; |
931 |
kakl |
156 |
heat=MAXHEAT; |
|
|
157 |
|
|
|
158 |
enable_interrupts(GLOBAL); |
|
|
159 |
enable_interrupts(INT_RDA); |
919 |
kakl |
160 |
|
|
|
161 |
while(TRUE) |
|
|
162 |
{ |
|
|
163 |
n++; |
|
|
164 |
|
|
|
165 |
tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor |
|
|
166 |
temp=ReadTemp(SA, RAM_Tobj1); |
|
|
167 |
|
|
|
168 |
if((0==tempa)||(0==temp)) // Transfer error? |
|
|
169 |
{ |
929 |
kakl |
170 |
printf("%Lu;ta:-273;ts:-273;sta:-1\n\r",n); |
919 |
kakl |
171 |
output_low(HEATING); |
|
|
172 |
} |
|
|
173 |
else |
|
|
174 |
{ |
|
|
175 |
to=(signed int16)(temp*2-27315)/100; |
|
|
176 |
ta=(signed int16)(tempa*2-27315)/100; |
|
|
177 |
|
929 |
kakl |
178 |
printf("%Lu;ta:%Ld;ts:%Ld;sta:",n,ta,to); |
931 |
kakl |
179 |
if (heat>=MAXHEAT) |
919 |
kakl |
180 |
{ |
929 |
kakl |
181 |
printf("0\n\r"); |
919 |
kakl |
182 |
output_low(HEATING); |
|
|
183 |
} |
|
|
184 |
else |
|
|
185 |
{ |
929 |
kakl |
186 |
printf("1\n\r"); |
919 |
kakl |
187 |
output_high(HEATING); |
931 |
kakl |
188 |
heat++; |
919 |
kakl |
189 |
} |
|
|
190 |
}; |
|
|
191 |
restart_wdt(); |
|
|
192 |
delay_ms(900); |
|
|
193 |
} |
|
|
194 |
} |
|
|
195 |
|