Rev 1280 Rev 1281
1 /**** IR Mrakomer 4 ****/ 1 /**** IR Mrakomer 4 ****/
2 #define VERSION "4.0" 2 #define VERSION "4.0"
3 #define ID "$Id: irmrak4.c 1280 2009-01-07 12:23:26Z kakl $" 3 #define ID "$Id: irmrak4.c 1281 2009-01-07 12:31:20Z kakl $"
4 #include "irmrak4.h" 4 #include "irmrak4.h"
5   5  
6 #define MAXHEAT 20 // Number of cycles for heating 6 #define MAXHEAT 20 // Number of cycles for heating
7 #define MAXOPEN 20 // Number of cycles for dome open 7 #define MAXOPEN 20 // Number of cycles for dome open
8 #define MEASURE_DELAY 10000 // Delay to a next measurement 8 #define MEASURE_DELAY 10000 // Delay to a next measurement
9 #define RESPONSE_DELAY 100 // Reaction time after receiving a command 9 #define RESPONSE_DELAY 100 // Reaction time after receiving a command
10 #define SAFETY_COUNT 100 // Time of one emergency cycle 10 #define SAFETY_COUNT 100 // Time of one emergency cycle
11 #define SEND_DELAY 50 // Time between two characters on RS232 11 #define SEND_DELAY 50 // Time between two characters on RS232
12   12  
13 #define DOME PIN_B4 // Dome controll port 13 #define DOME PIN_B4 // Dome controll port
14 #define HEATING PIN_B3 // Heating for defrosting 14 #define HEATING PIN_B3 // Heating for defrosting
15   15  
16 #bit CREN = 0x18.4 // USART registers 16 #bit CREN = 0x18.4 // USART registers
17 #bit SPEN = 0x18.7 17 #bit SPEN = 0x18.7
18 #bit OERR = 0x18.1 18 #bit OERR = 0x18.1
19 #bit FERR = 0x18.2 19 #bit FERR = 0x18.2
20   20  
21 char VER[4]=VERSION; // Buffer for concatenate of a version string 21 char VER[4]=VERSION; // Buffer for concatenate of a version string
22 char REV[50]=ID; 22 char REV[50]=ID;
23   23  
24 int8 heat; // Status variables 24 int8 heat; // Status variables
25 int8 open; 25 int8 open;
26   26  
27 inline void toggle_dome(void) 27 inline void toggle_dome(void)
28 { 28 {
29 if (open>0) 29 if (open>0)
30 {output_toggle(DOME);} 30 {output_toggle(DOME);}
31 else 31 else
32 {output_low(DOME);} 32 {output_low(DOME);}
33 } 33 }
34   34  
35 void delay(int16 cycles) 35 void delay(int16 cycles)
36 { 36 {
37 int16 i; 37 int16 i;
38   38  
39 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);} 39 for(i=0; i<cycles; i++) {toggle_dome(); delay_us(100);}
40 } 40 }
41   41  
42   42  
43 #include "smb.c" 43 #include "smb.c"
44   44  
45   45  
46 // Read sensor RAM 46 // Read sensor RAM
47 // Returns temperature in °K 47 // Returns temperature in °K
48 int16 ReadTemp(int8 addr, int8 select) 48 int16 ReadTemp(int8 addr, int8 select)
49 { 49 {
50 unsigned char arr[6]; // Buffer for the sent bytes 50 unsigned char arr[6]; // Buffer for the sent bytes
51 int8 crc; // Readed CRC 51 int8 crc; // Readed CRC
52 int16 temp; // Readed temperature 52 int16 temp; // Readed temperature
53   53  
54 addr<<=1; 54 addr<<=1;
55   55  
56 SMB_STOP_bit(); //If slave send NACK stop comunication 56 SMB_STOP_bit(); //If slave send NACK stop comunication
57 SMB_START_bit(); //Start condition 57 SMB_START_bit(); //Start condition
58 SMB_TX_byte(addr); 58 SMB_TX_byte(addr);
59 SMB_TX_byte(RAM_Access|select); 59 SMB_TX_byte(RAM_Access|select);
60 SMB_START_bit(); //Repeated Start condition 60 SMB_START_bit(); //Repeated Start condition
61 SMB_TX_byte(addr); 61 SMB_TX_byte(addr);
62 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK 62 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
63 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK 63 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
64 temp=MAKE16(arr[1],arr[2]); 64 temp=MAKE16(arr[1],arr[2]);
65 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK 65 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
66 SMB_STOP_bit(); //Stop condition 66 SMB_STOP_bit(); //Stop condition
67   67  
68 arr[5]=addr; 68 arr[5]=addr;
69 arr[4]=RAM_Access|select; 69 arr[4]=RAM_Access|select;
70 arr[3]=addr; 70 arr[3]=addr;
71 arr[0]=0; 71 arr[0]=0;
72 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC 72 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
73   73  
74 return temp; 74 return temp;
75 } 75 }
76   76  
77 void main() 77 void main()
78 { 78 {
79 unsigned int16 seq, temp, tempa; 79 unsigned int16 seq, temp, tempa;
80 signed int16 ta, to; 80 signed int16 ta, to;
81 int8 safety_counter; 81 int8 safety_counter;
82   82  
83 output_low(HEATING); // Heating off 83 output_low(HEATING); // Heating off
84 setup_wdt(WDT_2304MS); // Setup Watch Dog 84 setup_wdt(WDT_2304MS); // Setup Watch Dog
85 setup_adc_ports(NO_ANALOGS); 85 setup_adc_ports(NO_ANALOGS);
86 setup_adc(ADC_OFF); 86 setup_adc(ADC_OFF);
87 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1); 87 setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
88 setup_timer_1(T1_DISABLED); 88 setup_timer_1(T1_DISABLED);
89 setup_timer_2(T2_DISABLED,0,1); 89 setup_timer_2(T2_DISABLED,0,1);
90 setup_comparator(NC_NC_NC_NC); 90 setup_comparator(NC_NC_NC_NC);
91 setup_vref(FALSE); 91 setup_vref(FALSE);
92 // setup_oscillator(OSC_4MHZ|OSC_INTRC,+2); // Pokud je nutna kalibrace RCosc 92 // setup_oscillator(OSC_4MHZ|OSC_INTRC,+2); // Pokud je nutna kalibrace RCosc
93 setup_oscillator(OSC_4MHZ|OSC_INTRC); 93 setup_oscillator(OSC_4MHZ|OSC_INTRC);
94   94  
95 delay_ms(1000); 95 delay_ms(1000);
96 restart_wdt(); 96 restart_wdt();
97 printf("\n\r* Mrakomer %s (C) 2007 KAKL *\n\r",VER); // Welcome message 97 printf("\n\r* Mrakomer %s (C) 2007 KAKL *\n\r",VER); // Welcome message
98 printf("* %s *\n\r",REV); 98 printf("* %s *\n\r",REV);
99 printf("<#sequence> <ambient [1/100 °C]> <sky [1/100 °C]> "); 99 printf("<#sequence> <ambient [1/100 °C]> <sky [1/100 °C]> ");
100 printf("<heating [s]> <dome [s]>\n\r\n\r"); 100 printf("<heating [s]> <dome [s]>\n\r\n\r");
101 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read 101 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
102 temp=ReadTemp(SA, RAM_Tobj1); 102 temp=ReadTemp(SA, RAM_Tobj1);
103   103  
104 seq=0; 104 seq=0;
105 heat=0; 105 heat=0;
106 open=0; 106 open=0;
107   107  
108 // enable_interrupts(GLOBAL); 108 // enable_interrupts(GLOBAL);
109 // enable_interrupts(INT_RDA); 109 // enable_interrupts(INT_RDA);
110   110  
111 //---WDT 111 //---WDT
112 restart_wdt(); 112 restart_wdt();
113   113  
114 while(TRUE) 114 while(TRUE)
115 { 115 {
116 while(kbhit()) getc(); // Flush USART buffer 116 while(kbhit()) getc(); // Flush USART buffer
117 CREN=0; CREN=1; // Reinitialise USART 117 CREN=0; CREN=1; // Reinitialise USART
118   118  
119 safety_counter=0; 119 safety_counter=0;
120   120  
121 do 121 do
122 { 122 {
123 if (safety_counter<SAFETY_COUNT) safety_counter++; 123 if (safety_counter<SAFETY_COUNT) safety_counter++;
124   124  
125 delay(RESPONSE_DELAY); 125 delay(RESPONSE_DELAY);
126   126  
127 if (safety_counter>=SAFETY_COUNT) 127 if (safety_counter>=SAFETY_COUNT)
128 { 128 {
129 if (heat>0) 129 if (heat>0)
130 { 130 {
131 output_high(HEATING); 131 output_high(HEATING);
132 heat--; 132 heat--;
133 } 133 }
134 else 134 else
135 { 135 {
136 output_low(HEATING); 136 output_low(HEATING);
137 } 137 }
138 138  
139 if (open>0) open--; 139 if (open>0) open--;
140   140  
141 safety_counter=0; 141 safety_counter=0;
142 //---WDT 142 //---WDT
143 restart_wdt(); 143 restart_wdt();
144 } 144 }
145 } while (!kbhit()); 145 } while (!kbhit());
146   146  
147 //---WDT 147 //---WDT
148 restart_wdt(); 148 restart_wdt();
149 { 149 {
150 char ch; 150 char ch;
151   151  
152 ch=getc(); 152 ch=getc();
153   153  
154 switch (ch) 154 switch (ch)
155 { 155 {
156 case 'h': 156 case 'h':
157 heat=MAXHEAT; // Need heating 157 heat=MAXHEAT; // Need heating
158 break; 158 break;
159   159  
160 case 'c': 160 case 'c':
161 heat=0; // Need colder 161 heat=0; // Need colder
162 break; 162 break;
163   163  
164 case 'o': 164 case 'o':
165 open=MAXOPEN; // Open the dome 165 open=MAXOPEN; // Open the dome
166 break; 166 break;
167   167  
168 case 'l': 168 case 'l':
169 open=0; // Lock the dome 169 open=0; // Lock the dome
170 break; 170 break;
171 } 171 }
172 } 172 }
173   173  
174 seq++; // Increment the number of measurement 174 seq++; // Increment the number of measurement
175   175  
176 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor 176 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
177 temp=ReadTemp(SA, RAM_Tobj1); 177 temp=ReadTemp(SA, RAM_Tobj1);
178   178  
179 ta=tempa*2-27315; // °K -> °C 179 ta=tempa*2-27315; // °K -> °C
180 to=temp*2-27315; 180 to=temp*2-27315;
181   181  
182 { // printf 182 { // printf
183 char output[30]; // Output buffer 183 char output[30]; // Output buffer
184 int8 j; // Counter 184 int8 j; // Counter
185   185  
186 sprintf(output,"#%Lu %Ld %Ld %u %u\n\r\0", seq, ta, to, heat, open); 186 sprintf(output,"#%Lu %Ld %Ld %u %u\n\r\0", seq, ta, to, heat, open);
187   187  
188 j=0; 188 j=0;
189 while(output[j]!=0) 189 while(output[j]!=0)
190 { 190 {
191 delay(SEND_DELAY); 191 delay(SEND_DELAY);
192 putc(output[j++]); 192 putc(output[j++]);
193 output_toggle(DOME); -  
194 } 193 }
195 } 194 }
196   195  
197 delay(MEASURE_DELAY); // Delay to a next measurement 196 delay(MEASURE_DELAY); // Delay to a next measurement
198 //---WDT 197 //---WDT
199 restart_wdt(); 198 restart_wdt();
200 } 199 }
201 } 200 }
202   201