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