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