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