-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvectorWindXTC.c
245 lines (182 loc) · 5.71 KB
/
vectorWindXTC.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "vectorWindXTC.h"
#define SERIAL_PREFIX 'R'
#define SERIAL_NUMBER 1036
#define LIVE_SLOT_DELAY 0 // milliseconds*10. Value 10=100 milliseconds
// 150 bytes of data with MT=3 takes ~75 ms
// so we will do 100 millisecond slots
// R1036=0 (APRS World's), R1037=10, R1038=20, R1039=30, R1040=40, R1041=50
// R1042=60, R1043=70, R1044=80, R1045=90, R1046=0
const int8 NMEA0183_TRIGGER[] = { 'G', 'P', 'G', 'G', 'A' };
typedef struct {
/* most recent valid */
int16 pulse_period; /* period for #40HC */
int16 pulse_min_period; /* period for #40HC */
int16 pulse_count;
int16 strobed_pulse_period;
int16 strobed_pulse_min_period;
int16 strobed_pulse_count;
int16 input_voltage_adc;
int16 vertical_anemometer_adc;
int16 wind_vane_adc;
int16 sequence;
int8 live_age;
int8 live_countdown;
int8 gnss_age;
int8 gnss_buff[254];
int8 gnss_length;
} struct_current;
typedef struct {
short now_nmea_raw_received;
short now_strobe_counters;
short now_gnss_trigger_start;
short now_gnss_trigger_done;
short now_10millisecond;
/* flag to see if we are timing */
short pulse_0_count;
} struct_action;
typedef struct {
int16 pulse_period;
int16 pulse_count; /* used in frequency counting interrupt to count pulses in second */
} struct_time_keep;
typedef struct {
int8 buff[254];
int8 pos;
} struct_nmea_raw;
/* global structures */
struct_current current;
struct_action action;
struct_time_keep timers;
struct_nmea_raw nmea_raw;
#include "vectorWindXTC_adc.c"
#include "vectorWindXTC_interrupts.c"
#include "vectorWindXTC_live.c"
void task_10millisecond(void) {
if ( current.live_countdown > 0 && current.live_countdown != 0xff ) {
current.live_countdown--;
}
if ( current.gnss_age < 255 ) {
current.gnss_age++;
}
/* age live data timeout */
if ( current.live_age < 255 ) {
current.live_age++;
}
if ( current.strobed_pulse_count > 0 ) {
output_high(LED_ANEMOMETER);
} else {
output_low(LED_ANEMOMETER);
}
if ( current.vertical_anemometer_adc > 782 || current.vertical_anemometer_adc < 770 ) {
output_high(LED_VERTICAL_ANEMOMETER);
} else {
output_low(LED_VERTICAL_ANEMOMETER);
}
}
void init() {
// int8 i;
// setup_oscillator(OSC_8MHZ | OSC_INTRC);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(sAN0 | sAN1 | sAN2 | VSS_VREF );
// setup_adc_ports(NO_ANALOGS);
// setup_wdt(WDT_ON);
/*
Manually set ANCON0 to 0xff and ANCON1 to 0x1f for all digital
Otherwise set high bit of ANCON1 for VbGen enable, then remaining bits are AN12 ... AN8
ANCON1 AN7 ... AN0
set bit to make input digital
*/
/* AN7 AN6 AN5 AN4 AN3 AN2 AN1 AN0 */
ANCON0 = 0b11111000;
/* VbGen x x 12 11 10 9 8 */
ANCON1 = 0b01111111;
/* one periodic interrupt @ 100uS. Generated from system 8 MHz clock */
/* prescale=4, match=49, postscale=1. Match is 49 because when match occurs, one cycle is lost */
// setup_timer_2(T2_DIV_BY_4,49,1);
/* one periodic interrupt @ 100uS. Generated from system 32 MHz clock */
/* prescale=16, match=49, postscale=1. Match is 49 because when match occurs, one cycle is lost */
setup_timer_2(T2_DIV_BY_16,49,1);
/* not sure why we need this */
delay_ms(14);
action.now_strobe_counters=0;
action.now_gnss_trigger_start=0;
action.now_gnss_trigger_done=0;
action.now_10millisecond=0;
current.pulse_period=0;
current.pulse_min_period=65535;
current.pulse_count=0;
nmea_raw.buff[0]='\0';
nmea_raw.pos=0;
current.gnss_age=255;;
current.gnss_buff[0]='\0';
current.gnss_length=0;
current.sequence=0;
current.live_countdown=0xff;
}
void main(void) {
int8 i;
// int16 l,m;
init();
output_low(LED_ORANGE);
output_low(LED_RED);
output_low(LED_GREEN);
// fprintf(SERIAL_XTC,"# vectorWindXTC (%s) on XTC\r\n",__DATE__);
/* start 100uS timer */
enable_interrupts(INT_TIMER2);
/* enable serial ports */
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
i=0;
for ( ; ; ) {
restart_wdt();
#if 0
if ( current.live_age >= 120 && 0xff == current.live_countdown ) {
/* didn't get a triger sentence from GNSS for last 1.2 seconds. Send data anyhow */
action.now_strobe_counters=1; /* triggers strobe of data */
action.now_gnss_trigger_done=1; /* triggers send of data */
// fprintf(SERIAL_XTC,"# timeout waiting for trigger\r\n");
}
#endif
/* recieving a GNSS trigger on our serial port causes counter values to be strobe. Once that
is complete, we sample ADCs */
if ( action.now_gnss_trigger_start ) {
action.now_gnss_trigger_start=0;
sample_adc();
// fprintf(SERIAL_XTC,"# got trigger sentence\r\n");
}
/* as soon as interrupt finishes a $xxHDT we are ready to send our data */
if ( action.now_gnss_trigger_done) {
action.now_gnss_trigger_done=0;
output_toggle(LED_RED);
/* start a countdown for our slot for transmit */
current.live_countdown=LIVE_SLOT_DELAY;
}
if ( 0==current.live_countdown ) {
live_send();
current.live_age=0;
current.live_countdown=0xff;
}
/* periodic tasks */
if ( action.now_10millisecond ) {
action.now_10millisecond=0;
task_10millisecond();
}
}
}
#if 0
fprintf(SERIAL_XTC,"# finished receiving trigger sentence or timeout\r\n");
fprintf(SERIAL_XTC,"# current.live_age=%u\r\n",current.live_age);
fprintf(SERIAL_XTC,"# {count=%lu, period=%lu, min_period=%lu}\r\n",
current.strobed_pulse_count,
current.strobed_pulse_period,
current.strobed_pulse_min_period
);
fprintf(SERIAL_XTC,"# {input=%lu, vertical=%lu, wind_vane=%lu}\r\n",
current.input_voltage_adc,
current.vertical_anemometer_adc,
current.wind_vane_adc
);
fprintf(SERIAL_XTC,"# current {gnss_age=%u gnss='%s'}\r\n",
current.gnss_age,
current.gnss_buff
);
#endif