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