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