-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathSportAnalog.ino
966 lines (846 loc) · 17.9 KB
/
SportAnalog.ino
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/* ============================================================
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* ============================================================*/
// Author: Mike Blandford
// Link bit IO8
// ID bit IO3
// Sport bit IO2
#define DEBUG 0
#define DEBUG_BIT 0x10
#define DEBUG_PORT PORTB
#define DEBUG_DDR DDRB
#define DEBUG_PIN PINB
#define DEBUGA_BIT 0x08
#define SENSOR_ID 0x1B
#define A1_ID 0xF102
#define A2_ID 0xF103
#define A3_ID 0x0900
#define A4_ID 0x0910
// A3/A4 return U32 as V/100
// 3.3V = 1024
// ADC*33/8*10/128
#define TEMP2_ID 5
#define SENSOR_CONFIG_ID 0x1C
// For 8MHz CPU
//#define RXCENTRE 19
#define RXINTRA 43
#define RXSTOP 43
#define TXDELAY 41
// Clocks at 8MHz for 1 bit at 57600 baud
#define BITRATE 139
#define RXCENTRE (69-20)
#define XMIT_START_ADJUSTMENT 3
#define IDLEDELAYTIME 208
// Link for scaling option is IO8
#define LINK_BIT 0x01
#define LINK_PORT PORTB
#define LINK_DDR DDRB
#define LINK_PIN PINB
// ID option bit is IO3
#define ID_BIT 0x08
#define ID_PORT PORTD
#define ID_DDR DDRD
#define ID_PIN PIND
// A3 option bit is IO4
#define A3_BIT 0x10
#define A3_PORT PORTD
#define A3_DDR DDRD
#define A3_PIN PIND
// A4 option bit is IO5
#define A4_BIT 0x20
#define A4_PORT PORTD
#define A4_DDR DDRD
#define A4_PIN PIND
// SPORT_BIT is the one with INT0 available (IO2)
// Unless PINCHANGE is defined
// This is IO2
#define SPORT_BIT 0x04
#define SPORT_PORT PORTD
#define SPORT_DDR DDRD
#define SPORT_PIN PIND
#define FORCE_INDIRECT(ptr) __asm__ __volatile__ ("" : "=e" (ptr) : "0" (ptr))
//typedef uint8_t prog_uint8_t __attribute__((__progmem__));//,deprecated("prog_uint8_t type is deprecated.")));
#define APM __attribute__(( section(".progmem.data") ))
const uint8_t APM Indices[] = { 0x00, 0xA1, 0x22, 0x83, 0xE4, 0x45,
0xC6, 0x67, 0x48, 0xE9, 0x6A, 0xCB,
0xAC, 0x0D, 0x8E, 0x2F, 0xD0, 0x71,
0xF2, 0x53, 0x34, 0x95, 0x16, 0xB7,
0x98, 0x39, 0xBA, 0x1B } ;
struct t_anacontrol
{
uint16_t Analog ;
uint16_t AnaAve ;
uint8_t AnaCount ;
} AnalogControl[4] ;
uint8_t SensorId ;
uint8_t Crc ;
uint8_t Timer12ms ;
uint8_t SendStart ;
uint8_t Idle ;
uint8_t ReceivedAppId[2] ;
uint8_t Send32packet ;
uint8_t ReceivedPacket[8] ;
uint16_t MillisPrecount ;
uint16_t lastTimerValue ;
uint32_t TotalMicros ;
uint32_t TotalMillis ;
void init()
{
// initSportUart() ;
// Timer1
TCCR1A = 0x00 ; //Init.
TCCR1B = 0xC1 ; // I/p noise cancel, rising edge, Clock/1
EICRA = 3 ; // INT0 rising edge generates interrupt
init_from_eeprom() ;
ADCSRA = 0x86 ;
PORTC |= 2 ; // Pull up on second analog i/p
DIDR0 |= 0x0D ; // Disable digital pins 3,2,0
ID_DDR &= ~ID_BIT ; // Input
ID_PORT |= ID_BIT ; // pullup
LINK_DDR &= ~LINK_BIT ; //Input
LINK_PORT |= LINK_BIT ; // pullup
A3_DDR &= ~A3_BIT ; // Input
A3_PORT |= A3_BIT ; // pullup
A4_DDR &= ~A4_BIT ; // Input
A4_PORT |= A4_BIT ; // pullup
#if SPORT_BIT == 0x80
ADCSRB &= ~(1<<ACME) ;
ACSR = (1<<ACBG) | (1 << ACIS1) ;
DIDR1 |= (1<<AIN1D) ;
#endif
#if DEBUG
DEBUG_DDR |= DEBUG_BIT | DEBUGA_BIT ;
DEBUG_PORT &= ~DEBUG_BIT & ~DEBUGA_BIT ;
#endif
}
void setup()
{
}
static uint8_t rx_pin_read()
{
#if SPORT_BIT == 0x80
return ACSR & ( 1 << ACO ) ;
#else
return SPORT_PIN & SPORT_BIT ;
#endif
}
inline void setTX()
{
SPORT_DDR |= SPORT_BIT ;
SPORT_PORT &= ~SPORT_BIT ; // low as inverse logic
}
inline void setRX()
{
SPORT_DDR &= ~SPORT_BIT ;
SPORT_PORT &= ~SPORT_BIT ; // low so no pullup
}
void waitCompA()
{
while ( ( TIFR1 & (1 << OCF1A) ) == 0 )
{
// null body
}
TIFR1 = (1 << OCF1A) ; // Clear flag
}
void swrite(uint8_t b)
{
uint16_t lBitRate = BITRATE ;
// Write the start bit
OCR1A = TCNT1 + lBitRate ;
TIFR1 = (1 << OCF1A) ; // Clear flag
SPORT_PORT |= SPORT_BIT ; // high for start bit as inverse logic
waitCompA() ;
// Write each of the 8 bits
for ( uint8_t i = 8 ; i ; i -= 1 )
{
if (b & 1) // test bit
SPORT_PORT &= ~SPORT_BIT ; // send 1
else
SPORT_PORT |= SPORT_BIT ; // send 0
OCR1A += lBitRate ;
waitCompA() ;
b >>= 1 ;
}
SPORT_PORT &= ~SPORT_BIT ; // restore pin to natural state
OCR1A += lBitRate ;
waitCompA() ;
}
static void initSerial()
{
setRX() ;
}
void sendByte( uint8_t byte )
{
uint16_t lcrc = Crc ;
// CRC update
lcrc += byte; //0-1FF
lcrc += lcrc >> 8; //0-100
Crc = lcrc ;
if ( ( byte == 0x7E ) || ( byte == 0x7D ) )
{
swrite( 0x7D ) ;
byte &= ~0x20 ;
}
swrite(byte) ;
}
static void sendCrc()
{
sendByte(0xFF-Crc) ;
}
static void sendValue( uint8_t type, uint16_t value, uint16_t id )
{
setTX() ;
Crc = 0;
sendByte(type); // DATA_FRAME
sendByte( (uint8_t)id );
sendByte( id >> 8 );
sendByte(value);
sendByte(value>>8);
sendByte(0);
sendByte(0);
sendCrc();
setRX() ;
}
static uint16_t convertA3A4( uint16_t value )
{
// ADC*33/8*10/128, to keep inside a 16 bit value
value *= 33 ;
value /= 8 ;
value *= 10 ;
value /= 128 ;
return value ;
}
static uint8_t whichId = 0 ;
static void sendData()
{
uint16_t value ;
uint16_t id ;
// TCCR0B = 1 ; // Clock div 1
if ( whichId == 0 )
{
value = AnalogControl[0].Analog ;
id = A2_ID ;
}
else if ( whichId == 1 )
{
value = AnalogControl[1].Analog ;
id = TEMP2_ID ;
if ( (ID_PIN & ID_BIT) == 0 )
{
id = A1_ID ;
}
}
else if ( whichId == 2 )
{
value = AnalogControl[2].Analog ;
value = convertA3A4( value ) ;
id = A3_ID ;
}
else if ( whichId == 3 )
{
value = AnalogControl[3].Analog ;
value = convertA3A4( value ) ;
id = A4_ID ;
}
if ( SendStart )
{
sendValue( 0x10, value, id ) ;
}
else
{
sendValue( 0, 0, 0 ) ;
}
// Locate next value to send
if ( SendStart )
{
SendStart -= 1 ;
whichId += 1 ; // Go to next item
if ( whichId == 1 )
{
if ( (DIDR0 & 2) == 0 ) // A1 not enabled
{
if ( SendStart )
{
SendStart -= 1 ;
}
whichId = 2 ;
}
}
if ( whichId == 2 )
{
if ( A3_PIN & A3_BIT ) // A3 not enabled
{
if ( SendStart )
{
SendStart -= 1 ;
}
whichId = 3 ;
}
}
if ( whichId == 3 )
{
if ( A4_PIN & A4_BIT ) // A4 not enabled
{
if ( SendStart )
{
SendStart -= 1 ;
}
whichId = 3 ;
}
}
}
else
{
whichId = 0 ;
}
}
static void readSensors()
{
struct t_anacontrol *panalog ;
uint8_t index = 0 ;
uint16_t val ;
uint16_t x ;
panalog = &AnalogControl[0] ;
FORCE_INDIRECT( panalog ) ;
// set the reference to Vcc and the measurement to ADC1
ADMUX = 0x40 ;
do
{
ADCSRA |= _BV(ADSC); // Start conversion
while (bit_is_set(ADCSRA,ADSC)); // measuring
val = ADC ; // read the value from the sensor
if ( index == 1 )
{
x = val ;
}
if ( ( LINK_PIN & LINK_BIT ) || ( index == 1 ) )
{
val += 1 ;
val >>= 2 ;
}
panalog->AnaAve += val ;
panalog->AnaCount += 1 ;
if ( panalog->AnaCount > 3 )
{
if ( index < 2 )
{
if ( ( LINK_PIN & LINK_BIT ) || ( index == 1 ) )
{
val = panalog->AnaAve >> 2 ;
}
else
{
// Scale 15K/3.3K to 10K/3.3K
// Map 744 counts to 256
// We have 4 samples added so 2976 goes to 256
// Use 11/128, is about 0.2% out, but MUCH better than the resistor tolerance
// val = panalog->AnaAve * 11 ;
// Shifts and adds are faster and shorter than a multiply
val = panalog->AnaAve << 2 ; // *4
val += panalog->AnaAve ; // *5
val <<= 1 ; // *10
val += panalog->AnaAve ; // *11
val >>= 7 ; // divide by 128
}
panalog->Analog = (val > 255) ? 255 : val ;
}
else
{
val = panalog->AnaAve >> 2 ;
panalog->Analog = val ;
}
panalog->AnaCount = 0 ;
panalog->AnaAve = 0 ;
}
panalog += 1 ;
ADMUX += 1 ;
} while ( ++index < 4 ) ;
// x has full 10 bit input of second ADC
if ( x < 1000 )
{
// We have a real analog input on the second channel
DIDR0 |= 2 ; // Disable digital pin
PORTC &= ~2 ; // Remove pull up on second analog i/p
}
}
//
// The receive routine called by the interrupt handler
//
static uint8_t recv()
{
uint8_t d = 0 ;
// If RX line is high, then we don't see any start bit
// so interrupt is probably not for us
#if DEBUG
DEBUG_PORT &= ~DEBUGA_BIT ;
DEBUG_PORT |= DEBUG_BIT ;
#endif
// if (rx_pin_read())
// {
// Wait approximately 1/2 of a bit width to "centre" the sample
waitCompA() ;
#if DEBUG
DEBUG_PORT &= ~DEBUG_BIT ;
DEBUG_PORT |= DEBUG_BIT ;
#endif
// Read each of the 8 bits
for ( uint8_t i = 0 ; i < 8 ; i += 1 )
{
d >>= 1 ;
OCR1A += BITRATE ;
waitCompA() ;
#if DEBUG
DEBUG_PORT &= ~DEBUG_BIT ;
#endif
#if SPORT_BIT == 0x80
if (rx_pin_read())
#else
if (!rx_pin_read())
#endif
{
d |= 0x80 ;
}
#if DEBUG
DEBUG_PORT |= DEBUG_BIT ;
#endif
}
// skip the stop bit
OCR1A += BITRATE ;
waitCompA() ;
// }
#if DEBUG
DEBUG_PORT &= ~DEBUG_BIT ;
#endif
return d ;
}
void wait4msIdle()
{
// We want 4mS with NO transitions on INT0
// and INT0 (PD2) is LOW ( = idle high for inverted data)
TCNT0 = 0 ;
TCCR0B = 4 ; // Clock div 256, 32 uS per count
// 125 counts is 4mS
#if SPORT_BIT == 0x80
ACSR |= ( 1<<ACI ) ; // clear pending interrupt
#else
EIFR = (1 << INTF0) ; // CLEAR flag
#endif
do
{
#if SPORT_BIT == 0x80
if ( ( ACSR & (1 << ACI) ) || ( ( ACSR & ACO ) == 0 ) )
{
ACSR |= ( 1<<ACI ) ; // clear pending interrupt
#else
if ( ( EIFR & (1 << INTF0) ) || ( SPORT_PIN & SPORT_BIT) )
{
EIFR = (1 << INTF0) ;
#endif
TCNT0 = 0 ;
}
// wdt_reset() ;
} while ( TCNT0 < 126 ) ;
TCCR0B = 0 ; // Stop timer
}
uint8_t getBaudrate()
{
#if SPORT_BIT == 0x80
return 100 ; // dummy value not used
#else
uint8_t rx ;
TCCR0B = 2 ; // Clock div 8, 1 uS per count
EIFR = (1 << INTF0) ; // CLEAR flag
for(;;)
{
if ( EIFR & (1 << INTF0) )
{
rx = TCNT0 ; // Note timer value
EIFR = (1 << INTF0) ; // CLEAR flag
break ;
}
// wdt_reset() ;
}
// Got the start of the start bit
for(;;)
{
if ( EIFR & (1 << INTF0) )
{
EIFR = (1 << INTF0) ;
break ;
}
// wdt_reset() ;
}
EICRA &= ~1 ; // INT0 interrupt on falling edge
for(;;)
{
if ( EIFR & (1 << INTF0) )
{
rx = TCNT0 - rx ;
EIFR = (1 << INTF0) ;
break ;
}
// wdt_reset() ;
}
// rx now the time (in 1uS units) of the 8 bits
return rx ;
#endif
}
int16_t getByte()
{
uint16_t time ;
uint8_t rx ;
time = TCNT1 ;
EIFR = (1 << INTF0) ; // Clear flag
while ( TCNT1 - time < 8000 ) // 1mS timeout
{
if ( EIFR & (1 << INTF0) )
{
OCR1A = TCNT1 + RXCENTRE ;
TIFR1 = (1 << OCF1A) ; // Clear flag
EIFR = (1 << INTF0) ; // Clear flag
rx = recv() ;
return rx ;
}
}
return -1 ;
}
int16_t getPacket()
{
uint8_t count ;
uint8_t stuffing = 0 ;
int16_t value ;
uint8_t rxbyte ;
for ( count = 0 ; count < 8 ; count += 1 )
{
value = getByte() ;
if ( value == -1 )
{
return -1 ;
}
rxbyte = value ;
if ( ( rxbyte == 0x7E ) || ( rxbyte == 0x7D ) )
{
stuffing = 1 ;
}
if ( stuffing )
{
value = getByte() ;
if ( value == -1 )
{
return -1 ;
}
rxbyte = value + 0x20 ;
}
stuffing = 0 ;
ReceivedPacket[count] = rxbyte ;
}
return 0 ;
}
static uint8_t checkSportPacket()
{
uint8_t *packet = ReceivedPacket ;
uint16_t crc = 0 ;
for ( uint8_t i = 0 ; i < 8 ; i += 1 )
{
crc += packet[i]; //0-1FF
crc += crc >> 8; //0-100
crc &= 0x00ff;
}
return (crc == 0x00ff) ;
}
void loop()
{
static uint8_t lastRx = 0 ;
uint8_t rx ;
readSensors() ;
readSensors() ;
readSensors() ;
readSensors() ;
wait4msIdle() ;
getBaudrate() ; // Skip first frame (X6R problem)
wait4msIdle() ;
getBaudrate() ; // Skip second frame as well
wait4msIdle() ;
#if SPORT_BIT == 0x80
ACSR |= ( 1<<ACI ) ; // clear pending interrupt
#else
EICRA = 3 ;
EIFR = (1 << INTF0) ; // CLEAR flag
#endif
for(;;)
{
#if SPORT_BIT == 0x80
if ( ACSR & (1 << ACI) )
#else
if ( EIFR & (1 << INTF0) )
#endif
{
// TCCR0B = 0 ; // stop timer
// TCNT0 = 0 ;
// TCCR0B = 1 ; // Clock div 1
OCR1A = TCNT1 + RXCENTRE ;
TIFR1 = (1 << OCF1A) ; // Clear flag
#if SPORT_BIT == 0x80
ACSR |= ( 1<<ACI ) ; // clear pending interrupt
#else
EIFR = (1 << INTF0) ; // CLEAR flag
#endif
#if DEBUG
DEBUG_PORT |= DEBUGA_BIT ;
#endif
rx = recv() ;
if (lastRx == 0x7e )
{
Timer12ms += 1 ;
if ( Timer12ms > 16 )
{
Timer12ms = 0 ;
SendStart = 4 ;
}
if (rx == SensorId)
{
lastRx = 0 ;
// Delay around 400uS
// TCCR0B = 0 ; // stop timer
OCR1A = TCNT1 + 3200 ;
TIFR1 = (1 << OCF1A) ; // Clear flag
// TCCR0B = 3 ; // Clock div 64
waitCompA() ;
if ( Idle == 0 )
{
sendData() ;
}
else if ( Send32packet )
{
TCCR0B = 1 ; // Clock div 1
setTX() ;
Crc = 0 ;
sendByte(0x32); // DATA_FRAME (0x10) or 0
sendByte( ReceivedAppId[0] ) ;
sendByte( ReceivedAppId[1] ) ;
sendByte(1);
sendByte(SensorId & 0x1F);
sendByte(0);
sendByte(0);
sendCrc();
setRX() ;
Send32packet = 0 ;
}
readSensors() ;
}
else
{
lastRx = 0 ;
// if ( (rx & 0x1F) == SENSOR_CONFIG_ID)
// {
// receive complete packet
if ( getPacket() != -1 )
{
if ( checkSportPacket() )
{
if ( ReceivedPacket[0] == 0x21 )
{
if ( ReceivedPacket[1] == 0xFF )
{
if ( ReceivedPacket[2] == 0xFF )
{
Idle = 1 ;
}
}
}
else if ( ReceivedPacket[0] == 0x20 )
{
if ( ReceivedPacket[1] == 0xFF )
{
if ( ReceivedPacket[2] == 0xFF )
{
Idle = 0 ;
}
}
}
else if ( ReceivedPacket[2] == 0xF1 )
{
if ( ( ReceivedPacket[1] == 0x02 ) || ( ReceivedPacket[1] == 0x03 ) )
{
if ( ReceivedPacket[0] == 0x31 )
{
if ( ReceivedPacket[3] == 0x01 )
{
// New Id is in ReceivedPacket[4]
uint8_t newId ;
uint8_t id ;
newId = ReceivedPacket[4] ;
for ( uint8_t i = 0 ; i < sizeof(Indices) ; i += 1 )
{
id = pgm_read_byte( &Indices[i] ) ;
if ( ( id & 0x1F ) == ( newId & 0x1F ) )
{
SensorId = id ;
chk_wrieeprom( 0, id ) ;
break ;
}
}
}
}
else if ( ReceivedPacket[0] == 0x30 )
{
if ( ReceivedPacket[3] == 0x01 )
{
ReceivedAppId[0] = ReceivedPacket[1] ;
ReceivedAppId[1] = ReceivedPacket[2] ;
// Request to read current value
Send32packet = 1 ;
}
}
}
}
}
// }
}
// else
// {
// lastRx = rx ;
// }
}
}
else
{
lastRx = rx ;
}
#if SPORT_BIT == 0x80
ACSR |= ( 1<<ACI ) ; // clear pending interrupt
#else
EIFR = (1 << INTF0) ; // CLEAR flag
#endif
}
}
}
uint32_t micros()
{
uint16_t elapsed ;
uint8_t millisToAdd ;
uint8_t oldSREG = SREG ;
cli() ;
uint16_t time = TCNT1 ; // Read timer 1
SREG = oldSREG ;
elapsed = time - lastTimerValue ;
#if F_CPU == 20000000L // 20MHz clock
#error Unsupported clock speed
#elif F_CPU == 16000000L // 16MHz clock
#error Unsupported clock speed
// elapsed >>= 4 ;
#elif F_CPU == 8000000L // 8MHz clock
elapsed >>= 3 ;
#else
#error Unsupported clock speed
#endif
//elapsed >>= 4 ;
uint32_t ltime = TotalMicros ;
ltime += elapsed ;
cli() ;
TotalMicros = ltime ; // Done this way for RPM to work correctly
lastTimerValue = time ;
SREG = oldSREG ; // Still valid from above
elapsed += MillisPrecount;
millisToAdd = 0 ;
if ( elapsed > 3999 )
{
millisToAdd = 4 ;
elapsed -= 4000 ;
}
else if ( elapsed > 2999 )
{
millisToAdd = 3 ;
elapsed -= 3000 ;
}
else if ( elapsed > 1999 )
{
millisToAdd = 2 ;
elapsed -= 2000 ;
}
else if ( elapsed > 999 )
{
millisToAdd = 1 ;
elapsed -= 1000 ;
}
TotalMillis += millisToAdd ;
MillisPrecount = elapsed ;
return TotalMicros ;
}
uint32_t millis()
{
micros() ;
return TotalMillis ;
}
static void init_from_eeprom()
{
uint8_t value ;
uint8_t newValue ;
uint8_t i ;
// SensorId = SENSOR_ID ; // Set default
value = eeprom_read( 0 ) ;
if ( value == 0xFF )
{
newValue = SENSOR_ID ; // Set default
}
else
{
for ( i = 0 ; i < sizeof(Indices) ; i += 1 )
{
newValue = pgm_read_byte( &Indices[i] ) ;
if ( ( value & 0x1F ) == ( newValue & 0x1F ) )
{
break ;
}
}
if ( i >= sizeof(Indices) )
{
// Not found
newValue = SENSOR_ID ; // Set default
}
}
if ( newValue != value )
{
chk_wrieeprom( 0, newValue ) ;
}
SensorId = newValue ;
}
// Write an 8 bit byte to EEPROM, Address->byte
static void chk_wrieeprom( uint8_t uiAddress, uint8_t ucData )
{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE))
{
// null body
}
/* Set up address and data registers */
EEAR = uiAddress;
EEDR = ucData;
/* Write logical one to EEMWE */
EECR |= (1<<EEMPE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEPE);
}
// Read an 8 bit byte from EEPROM, Address->byte
static uint8_t eeprom_read( uint8_t address )
{
/* Wait for completion of previous write */
while(EECR & (1<<EEPE))
{
// null body
}
/* Set up address register */
EEAR = address ;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE) ;
/* Return data from data register */
return EEDR ;
}