-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
539 lines (416 loc) · 12.3 KB
/
main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
/**
Generated Main Source File
Company:
Microchip Technology Inc.
File Name:
main.c
Summary:
This is the main file generated using PIC10 / PIC12 / PIC16 / PIC18 MCUs
Description:
This header file provides implementations for driver APIs for all modules selected in the GUI.
Generation Information :
Product Revision : PIC10 / PIC12 / PIC16 / PIC18 MCUs - 1.81.5
Device : PIC16F1507
Driver Version : 2.00
*/
/*
(c) 2018 Microchip Technology Inc. and its subsidiaries.
Subject to your compliance with these terms, you may use Microchip software and any
derivatives exclusively with Microchip products. It is your responsibility to comply with third party
license terms applicable to your use of third party software (including open source software) that
may accompany Microchip software.
THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY
IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS
FOR A PARTICULAR PURPOSE.
IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP
HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO
THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL
CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT
OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS
SOFTWARE.
*/
#include "mcc_generated_files/mcc.h"
typedef enum states
{
STATE_UNDEFINED,
STATE_NORMAL,
STATE_TEACH_LEFT,
STATE_TEACH_RIGHT
}state_t;
/*
Main application
*/
#define ADC_ABSOLUTE_LOWER_LIMIT (0)
#define ADC_ABSOLUTE_UPPER_LIMIT (1023)
static volatile bool doButtonAction = false;
static volatile uint16_t msTick = 0;
static volatile uint16_t msPressed = 0;
static bool doToggle;
static state_t currentState = STATE_UNDEFINED;
static int32_t adcValuePosition = 0;
static uint32_t lowerLimit = ADC_ABSOLUTE_LOWER_LIMIT;
static uint32_t upperLimit = ADC_ABSOLUTE_UPPER_LIMIT;
//Calibration values
static const int32_t fixedPointFactor = 1000;
static int32_t m = 1 * fixedPointFactor;
static int24_t b = 0;
void readFlash()
{
//Read lower limit
lowerLimit = 0;
lowerLimit += (uint32_t)FLASH_ReadWord(END_FLASH-4) << 16;
lowerLimit |= (uint32_t)FLASH_ReadWord(END_FLASH-3);
//Read upper limit
upperLimit = 0;
upperLimit += (uint32_t)FLASH_ReadWord(END_FLASH-2) << 16;
upperLimit += (uint32_t)FLASH_ReadWord(END_FLASH-1) ;
}
void writeFlash()
{
//Write flash
#define FLASH_ROW_ADDRESS ( END_FLASH-WRITE_FLASH_BLOCKSIZE)
FLASH_EraseBlock((uint16_t)FLASH_ROW_ADDRESS);
uint16_t wrBlockData[WRITE_FLASH_BLOCKSIZE];
wrBlockData[WRITE_FLASH_BLOCKSIZE-1] = (uint16_t)(upperLimit);
wrBlockData[WRITE_FLASH_BLOCKSIZE-2] = (uint16_t)(upperLimit >> 16);
wrBlockData[WRITE_FLASH_BLOCKSIZE-3] = (uint16_t)(lowerLimit);
wrBlockData[WRITE_FLASH_BLOCKSIZE-4] = (uint16_t)(lowerLimit >> 16);
// write to Flash memory block
FLASH_WriteBlock((uint16_t)FLASH_ROW_ADDRESS, (uint16_t*)wrBlockData);
}
void timer0CallBack()
{
msTick++;
if(msTick > 200)
{
doToggle = true;
msTick = 0;
}
}
void setLedsForPositioning(int32_t position);
void setState(state_t newState)
{
if(newState == currentState)
{
return;
}
outputLed1_SetLow();
outputLed2_SetLow();
outputLed3_SetLow();
outputLed4_SetLow();
outputLed5_SetLow();
outputLed6_SetLow();
outputLed7_SetLow();
outputLed8_SetLow();
outputLed9_SetLow();
outputLed10_SetLow();
outputLed11_SetLow();
switch(newState)
{
case STATE_NORMAL:
//Read calibration values from Flash
readFlash();
upperLimit = (upperLimit > ADC_ABSOLUTE_UPPER_LIMIT) ? ADC_ABSOLUTE_UPPER_LIMIT : upperLimit;
lowerLimit = (lowerLimit < ADC_ABSOLUTE_LOWER_LIMIT) || (lowerLimit > upperLimit) ? ADC_ABSOLUTE_LOWER_LIMIT : lowerLimit;
m = (1023*fixedPointFactor)/(upperLimit - lowerLimit);
b = lowerLimit*m;
break;
case STATE_TEACH_LEFT:
outputLed1_SetHigh();
break;
case STATE_TEACH_RIGHT:
outputLed11_SetHigh();
break;
}
msTick = 0;
currentState = newState;
}
void main(void)
{
// initialize the device
SYSTEM_Initialize();
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
setState(STATE_NORMAL);
TMR0_SetInterruptHandler(timer0CallBack);
TMR2_StartTimer();
while(1)
{
static adc_channel_t channel = channelBrightness;
//Select next adc channel
channel = (channel == channelRudder) ? channelBrightness : channelRudder;
//Get next adc value
if(channel == channelRudder)
{
adcValuePosition = ADC_GetConversion(channel);
}
else
{
//Set brightness
int16_t dimming = (int16_t)ADC_GetConversion(channel);
dimming = (dimming << 1) +24 ;
if(dimming > 1000)
{
dimming = 1000;
}
else if(dimming < ADC_ABSOLUTE_LOWER_LIMIT)
{
dimming = ADC_ABSOLUTE_LOWER_LIMIT;
}
PWM3_LoadDutyValue(dimming);
}
if(!inputTeachIn_GetValue() && !doButtonAction)
{
msPressed++;
if(msPressed > 3000)
{
doButtonAction = true;
outputLed6_SetLow();
}
else
{
outputLed6_SetHigh();
}
}
else
{
msPressed = 0;
// outputLed6_SetLow();
}
if(doButtonAction)
{
while(!inputTeachIn_GetValue())
{
}
//Change state
switch(currentState)
{
case STATE_NORMAL:
setState(STATE_TEACH_LEFT);
break;
case STATE_TEACH_LEFT:
lowerLimit = adcValuePosition;
setState(STATE_TEACH_RIGHT);
break;
case STATE_TEACH_RIGHT:
{
upperLimit = adcValuePosition;
writeFlash();
setState(STATE_NORMAL);
break;
}
}
doButtonAction = false;
}
switch(currentState)
{
case STATE_NORMAL:
if(channel == channelBrightness)
{
adcValuePosition = (adcValuePosition*m-b)/fixedPointFactor;
setLedsForPositioning(adcValuePosition);
}
break;
case STATE_TEACH_LEFT:
if(doToggle)
{
outputLed1_Toggle();
outputLed2_Toggle();
outputLed3_Toggle();
outputLed4_Toggle();
outputLed5_Toggle();
doToggle = false;
}
break;
case STATE_TEACH_RIGHT:
if(doToggle)
{
outputLed7_Toggle();
outputLed8_Toggle();
outputLed9_Toggle();
outputLed10_Toggle();
outputLed11_Toggle();
doToggle = false;
}
break;
}
};
}
void setLedsForPositioning(int32_t adcValue)
{
outputLed1_SetLow();
outputLed2_SetLow();
outputLed3_SetLow();
outputLed4_SetLow();
outputLed5_SetLow();
outputLed6_SetHigh();
outputLed7_SetLow();
outputLed8_SetLow();
outputLed9_SetLow();
outputLed10_SetLow();
outputLed11_SetLow();
if(adcValue < 93)
{
outputLed1_SetHigh();
outputLed2_SetHigh();
outputLed3_SetHigh();
outputLed4_SetHigh();
outputLed5_SetHigh();
outputLed6_SetHigh();
}
else if(adcValue < 186)
{
outputLed2_SetHigh();
outputLed3_SetHigh();
outputLed4_SetHigh();
outputLed5_SetHigh();
outputLed6_SetHigh();
}
else if(adcValue < 279)
{
outputLed3_SetHigh();
outputLed4_SetHigh();
outputLed5_SetHigh();
outputLed6_SetHigh();
}
else if(adcValue < 372)
{
outputLed4_SetHigh();
outputLed5_SetHigh();
outputLed6_SetHigh();
}
else if(adcValue < 465)
{
outputLed5_SetHigh();
outputLed6_SetHigh();
}
else if(adcValue < 558) //Center
{
outputLed6_SetHigh();
}
else if(adcValue < 651)
{
outputLed6_SetHigh();
outputLed7_SetHigh();
}
else if(adcValue < 744)
{
outputLed6_SetHigh();
outputLed7_SetHigh();
outputLed8_SetHigh();
}
else if(adcValue < 837)
{
outputLed6_SetHigh();
outputLed7_SetHigh();
outputLed8_SetHigh();
outputLed9_SetHigh();
}
else if(adcValue < 930)
{
outputLed6_SetHigh();
outputLed7_SetHigh();
outputLed8_SetHigh();
outputLed9_SetHigh();
outputLed10_SetHigh();
}
else
{
outputLed6_SetHigh();
outputLed7_SetHigh();
outputLed8_SetHigh();
outputLed9_SetHigh();
outputLed10_SetHigh();
outputLed11_SetHigh();
}
// if(adcValue < 54)
// {
// outputLed1_SetHigh();
// }
// else if(adcValue < 108)
// {
// outputLed1_SetHigh();
// outputLed2_SetHigh();
// }
// else if(adcValue < 162)
// {
// outputLed2_SetHigh();
// }
// else if(adcValue < 215)
// {
// outputLed2_SetHigh();
// outputLed3_SetHigh();
// }
// else if(adcValue < 269)
// {
// outputLed3_SetHigh();
// }
// else if(adcValue < 323)
// {
// outputLed3_SetHigh();
// outputLed4_SetHigh();
// }
// else if(adcValue < 377)
// {
// outputLed4_SetHigh();
// }
// else if(adcValue < 431)
// {
// outputLed4_SetHigh();
// outputLed5_SetHigh();
// }
// else if(adcValue < 485)
// {
// outputLed5_SetHigh();
// }
// else if(adcValue < 538) //Center
// {
//
// }
// else if(adcValue < 592)
// {
// outputLed7_SetHigh();
// }
// else if(adcValue < 646)
// {
// outputLed7_SetHigh();
// outputLed8_SetHigh();
// }
// else if(adcValue < 700)
// {
// outputLed8_SetHigh();
// }
// else if(adcValue < 754)
// {
// outputLed8_SetHigh();
// outputLed9_SetHigh();
// }
// else if(adcValue < 808)
// {
// outputLed9_SetHigh();
// }
// else if(adcValue < 861)
// {
// outputLed9_SetHigh();
// outputLed10_SetHigh();
// }
// else if(adcValue < 915)
// {
// outputLed10_SetHigh();
// }
// else if(adcValue < 969)
// {
// outputLed10_SetHigh();
// outputLed11_SetHigh();
// }
// else
// {
// outputLed11_SetHigh();
// }
}
/**
End of File
*/