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