Rev 1274 Rev 1275
Line 1... Line 1...
1 /**** IR Mrakomer 4 ****/ 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0" 2 #define VERSION "4.0"
3 #define ID "$Id: irmrak3.c 1215 2008-08-08 12:25:25Z kakl $" 3 #define ID "$Id: irmrak3.c 1215 2008-08-08 12:25:25Z kakl $"
4 #include "irmrak4.h" 4 #include "irmrak4.h"
5   5  
-   6 #define MAXHEAT 10 // Number of cycles for heating
-   7 #define MAXOPEN 10 // Number of cycles for dome open
-   8 #define MEASURE_DELAY 10000
-   9 #define SEND_DELAY 50
-   10  
6 #define DOME PIN_B4 // Dome controll port 11 #define DOME PIN_B4 // Dome controll port
7 #define SA 0x00 // Slave Address (0 for single slave / 0x5A<<1 default) -  
8 #define RAM_Access 0x00 // RAM access command -  
9 #define RAM_Tobj1 0x07 // To1 address in the eeprom -  
10 #define RAM_Tamb 0x06 // Ta address in the eeprom -  
11 #define HEATING PIN_B3 // Heating for defrosting 12 #define HEATING PIN_B3 // Heating for defrosting
12 #define MAXHEAT 10 // Number of cycles for heating -  
13 #define MAXOPEN 10 // Number of cycles for dome open -  
14   13  
15 #bit CREN = 0x18.4 // USART registers 14 #bit CREN = 0x18.4 // USART registers
16 #bit SPEN = 0x18.7 15 #bit SPEN = 0x18.7
17 #bit OERR = 0x18.1 16 #bit OERR = 0x18.1
18 #bit FERR = 0x18.2 17 #bit FERR = 0x18.2
Line 21... Line 20...
21 char REV[50]=ID; 20 char REV[50]=ID;
22   21  
23 int8 heat; 22 int8 heat;
24 int8 open; 23 int8 open;
25   24  
26   -  
27 unsigned char PEC_calculation(unsigned char pec[]) // CRC calculation 25 inline void toggle_dome(void)
28 { 26 {
29 unsigned char crc[6]; -  
30 unsigned char BitPosition=47; -  
31 unsigned char shift; -  
32 unsigned char i; -  
33 unsigned char j; -  
34 unsigned char temp; -  
35   -  
36 do -  
37 { -  
38 crc[5]=0; /* Load CRC value 0x000000000107 */ -  
39 crc[4]=0; -  
40 crc[3]=0; -  
41 crc[2]=0; -  
42 crc[1]=0x01; -  
43 crc[0]=0x07; -  
44 BitPosition=47; /* Set maximum bit position at 47 */ -  
45 shift=0; 27 if (open>0)
46   -  
47 //Find first 1 in the transmited message -  
48 i=5; /* Set highest index */ -  
49 j=0; -  
50 while((pec[i]&(0x80>>j))==0 && i>0) -  
51 { -  
52 BitPosition--; 28 {output_toggle(DOME);}
53 if(j<7) -  
54 { -  
55 j++; -  
56 } -  
57 else 29 else
58 { -  
59 j=0x00; -  
60 i--; -  
61 } -  
62 }/*End of while */ 30 {output_low(DOME);}
63   31 }
64 shift=BitPosition-8; /*Get shift value for crc value*/ -  
65   32  
-   33 void delay(int16 cycles)
-   34 {
-   35 int16 i;
-   36
-   37 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);}
66   38  
67 //Shift crc value -  
68 while(shift) 39 restart_wdt();
69 { 40 }
70 for(i=5; i<0xFF; i--) -  
71 { -  
72 if((crc[i-1]&0x80) && (i>0)) -  
73 { -  
74 temp=1; -  
75 } -  
76 else -  
77 { -  
78 temp=0; -  
79 } -  
80 crc[i]<<=1; -  
81 crc[i]+=temp; -  
82 }/*End of for*/ -  
83 shift--; -  
84 }/*End of while*/ -  
85   41  
86 //Exclusive OR between pec and crc -  
87 for(i=0; i<=5; i++) -  
88 { -  
89 pec[i] ^=crc[i]; -  
90 }/*End of for*/ -  
91 } while(BitPosition>8);/*End of do-while*/ -  
92   42  
93 return pec[0]; 43 #include "smb.c"
94 }/*End of PEC_calculation*/ -  
95   44  
96   45  
97 int16 ReadTemp(int8 addr, int8 select) // Read sensor RAM 46 int16 ReadTemp(int8 addr, int8 select) // Read sensor RAM
98 { 47 {
99 unsigned char arr[6]; // Buffer for the sent bytes 48 unsigned char arr[6]; // Buffer for the sent bytes
100 int8 crc; // Readed CRC 49 int8 crc; // Readed CRC
101 int16 temp; // Readed temperature 50 int16 temp; // Readed temperature
102   51  
103 disable_interrupts(GLOBAL); -  
104 i2c_stop(); 52 addr<<=1;
-   53  
-   54 SMB_STOP_bit(); //If slave send NACK stop comunication
105 i2c_start(); 55 SMB_START_bit(); //Start condition
106 i2c_write(addr); 56 SMB_TX_byte(addr);
107 i2c_write(RAM_Access|select); // Select the teperature sensor in device 57 SMB_TX_byte(RAM_Access|select);
108 i2c_start(); 58 SMB_START_bit(); //Repeated Start condition
109 i2c_write(addr); 59 SMB_TX_byte(addr);
110 arr[2]=i2c_read(1); // lo 60 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
111 arr[1]=i2c_read(1); // hi 61 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
112 temp=MAKE16(arr[1],arr[2]); 62 temp=MAKE16(arr[1],arr[2]);
113 crc=i2c_read(0); //crc 63 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
114 i2c_stop(); -  
115 enable_interrupts(GLOBAL); 64 SMB_STOP_bit(); //Stop condition
116   65  
117 arr[5]=addr; 66 arr[5]=addr;
118 arr[4]=RAM_Access|select; 67 arr[4]=RAM_Access|select;
119 arr[3]=addr; 68 arr[3]=addr;
120 arr[0]=0; 69 arr[0]=0;
121 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC 70 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
122   71  
123 return temp; 72 return temp;
124 } 73 }
125   74  
126 void delay(int16 cycles) -  
127 { -  
128 int16 i; -  
129 -  
130 if (open>0) -  
131 for(i=0; i<cycles; i++) {output_toggle(DOME); delay_us(100);} -  
132 else -  
133 for(i=0; i<cycles; i++) {output_low(DOME); delay_us(100);} -  
134   -  
135 restart_wdt(); -  
136 } -  
137   -  
138 void main() 75 void main()
139 { 76 {
140 unsigned int16 n, temp, tempa; 77 unsigned int16 n, temp, tempa;
141 signed int16 ta, to; 78 signed int16 ta, to;
142   79  
Line 174... Line 111...
174 while(kbhit()) getc(); // Flush USART buffer 111 while(kbhit()) getc(); // Flush USART buffer
175 CREN=0; CREN=1; // Reinitialise USART 112 CREN=0; CREN=1; // Reinitialise USART
176   113  
177 do 114 do
178 { 115 {
179 delay(10000); 116 delay(MEASURE_DELAY);
180 if (heat>0) 117 if (heat>0)
181 { 118 {
182 output_high(HEATING); 119 output_high(HEATING);
183 heat--; 120 heat--;
184 } 121 }
Line 222... Line 159...
222   159  
223 ta=tempa*2-27315; // °K -> °C 160 ta=tempa*2-27315; // °K -> °C
224 to=temp*2-27315; 161 to=temp*2-27315;
225   162  
226 { // printf 163 { // printf
227 char output[30]; 164 char output[30]; // Output buffer
228 int8 j; 165 int8 j; // Counter
229 166
230 sprintf(output,"#%Lu %Ld %Ld %u %u\n\r\0", n, ta, to, heat, open); 167 sprintf(output,"#%Lu %Ld %Ld %u %u\n\r\0", n, ta, to, heat, open);
231 168
232 j=0; 169 j=0;
233 while(output[j]!=0) 170 while(output[j]!=0)
234 { 171 {
235 delay(50); 172 delay(SEND_DELAY);
236 putc(output[j++]); 173 putc(output[j++]);
237 output_toggle(DOME); 174 output_toggle(DOME);
238 } 175 }
239 } 176 }
240 } 177 }