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