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