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