Rev Author Line No. Line
1580 kakl 1 /**** IR Mrakomer - special version for BART ****/
2 #define VERSION "2.2"
3 #define ID "$Id: irmrak4.c 1306 2009-01-17 12:25:40Z kakl $"
4  
5 #include "irmrak4.h"
6  
7 #bit CREN = 0x18.4 // USART registers
8 #bit SPEN = 0x18.7
9 #bit OERR = 0x18.1
10 #bit FERR = 0x18.2
11  
12 #include <string.h>
13  
14 #CASE // Case sensitive compiler
15  
16 #define MAXHEAT 30 // Doba po kterou se topi v [s]
17 #define HEATING PIN_B3 // Heating for defrosting
18  
19 char VER[4]=VERSION; // Buffer for concatenate of a version string
20  
21 int8 heat; // Status variables
22  
23  
24 void delay(int16 cycles) // Vlastni pauza, zbylo to z ovladani fototranzistoru z MM4
25 {
26 int16 i;
27  
28 for(i=0; i<cycles; i++) {delay_us(100);}
29 }
30  
31 void welcome(void) // Welcome message
32 {
33 char REV[50]=ID; // Buffer for concatenate of a version string
34  
35 if (REV[strlen(REV)-1]=='$') REV[strlen(REV)-1]=0;
36 printf("\n\r\n\r# Mrakomer %s (C) 2007 KAKL\n\r",VER); // Welcome message
37 printf("#%s\n\r",&REV[4]);
38 printf("#\n\r");
39 printf("# h - Switch On Heating for 30s.\n\r");
40 printf("# f - Freezing. Switch Off Heating.\n\r");
41 printf("# i - Print this Information.\n\r");
42 printf("# 0..9 - Single measure at given angle.\n\r");
43 printf("# m - Measure at three space points.\n\r");
44 printf("#\n\r");
45 printf("$<Angle> <Ambient Temperature> <Space Temperature> ...");
46 printf("\n\r\n\r");
47 //---WDT
48 restart_wdt();
49 }
50  
51  
52 #include "smb.c" // System Management Bus driver
53  
54  
55 // Read sensor's RAM
56 // Returns temperature in °K
57 int16 ReadTemp(int8 addr, int8 select)
58 {
59 unsigned char arr[6]; // Buffer for the sent bytes
60 int8 crc; // Readed CRC
61 int16 temp; // Readed temperature
62  
63 addr<<=1;
64  
65 SMB_STOP_bit(); //If slave send NACK stop comunication
66 SMB_START_bit(); //Start condition
67 SMB_TX_byte(addr);
68 SMB_TX_byte(RAM_Access|select);
69 SMB_START_bit(); //Repeated Start condition
70 SMB_TX_byte(addr);
71 arr[2]=SMB_RX_byte(ACK); //Read low data,master must send ACK
72 arr[1]=SMB_RX_byte(ACK); //Read high data,master must send ACK
73 temp=make16(arr[1],arr[2]);
74 crc=SMB_RX_byte(NACK); //Read PEC byte, master must send NACK
75 SMB_STOP_bit(); //Stop condition
76  
77 arr[5]=addr;
78 arr[4]=RAM_Access|select;
79 arr[3]=addr;
80 arr[0]=0;
81 if (crc != PEC_calculation(arr)) temp=0; // Calculate and check CRC
82  
83 return temp;
84 }
85  
86  
87 /*-------------------------------- MAIN --------------------------------------*/
88 void main()
89 {
90 unsigned int16 timer, temp, tempa;
91 signed int16 ta, to;
92  
93 output_low(HEATING); // Heating off
94  
95 delay_ms(500);
96 restart_wdt();
97  
98 heat=0;
99 timer=0;
100  
101 welcome();
102  
103 tempa=ReadTemp(SA, RAM_Tamb); // Dummy read
104 temp=ReadTemp(SA, RAM_Tobj1);
105  
106 delay_ms(500);
107  
108 while(TRUE) // Main Loop
109 {
110 char ch;
111  
112 //---WDT
113 restart_wdt();
114  
115 if(kbhit())
116 {
117 ch=getc(); // Precti znak od radice motoru
118 CREN=0; CREN=1; // Reinitialise USART
119  
120 tempa=ReadTemp(SA, RAM_Tamb); // Read temperatures from sensor
121 temp=ReadTemp(SA, RAM_Tobj1);
122  
123 ta=tempa*2-27315; // °K -> °C
124 to=temp*2-27315;
125  
126 switch (ch)
127 {
128 case 'H':
129 heat=MAXHEAT; // Need heating
130 break;
131  
132 case 'F':
133 heat=0; // Freeze
134 break;
135  
136 case 'I':
137 welcome(); // Information about version, etc...
138 break;
139 }
140 printf("%c %Ld %Ld ", ch, ta, to);
141 if (('A'!=ch)&&('B'!=ch)&&('C'!=ch)&&('S'!=ch)) printf("H %u\r\n", heat); // Vzdycky se konci natocenim na Ground
142 }
143 delay_ms(1);
144 if (timer>0) {timer--;} else {timer=1000;}
145 if (heat>0) {if(1000==timer) heat--; output_high(HEATING);} else { output_low(HEATING); }
146 }
147 }
148