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