-
Notifications
You must be signed in to change notification settings - Fork 0
/
RC_NRF_R.c
1479 lines (1133 loc) · 36 KB
/
RC_NRF_R.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
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Tastenblinky.c
// Tastenblinky
//
// Created by Sysadmin on 03.10.07.
// Copyright Ruedi Heimlihcer 2007. All rights reserved.
//
#include <avr/io.h>
#include <avr/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <string.h>
#include <inttypes.h>
#include <math.h>
#include <avr/eeprom.h>
//#define F_CPU 4000000UL // 4 MHz
#include <avr/delay.h>
#include "lcd.c"
#include "adc.c"
//#include "onewire.c"
//#include "ds18x20.c"
//#include "crc8.c"
//#include "conio.h"
#include "defines.h"
#include "wireless.c"
#include "wl_module.h"
#include "nRF24L01.h"
//#include "RF24.h"
#define VREF 256
#define SPI_BUFSIZE 4
// von RC_PWM
#define SERVOMAX 2000
#define SERVOMIN 1000
#define TIMER0_STARTWERT 0x40
#define L_PORT 0 // index in servoportarray
#define LX_PIN 1
#define LX_PIN_HI PORTB |= (1<<LX_PIN)
#define LX_PIN_LO PORTB &= ~(1<<LX_PIN)
#define LY_PIN 2
#define LY_PIN_HI PORTB |= (1<<LY_PIN)
#define LY_PIN_LO PORTB &= ~(1<<LY_PIN)
#define R_PORT 1
#define RX_PIN 2
#define RX_PIN_HI PORTC |= (1<<RX_PIN)
#define RX_PIN_LO PORTC &= ~(1<<RX_PIN)
#define RY_PIN 3
#define RY_PIN_HI PORTC |= (1<<RY_PIN)
#define RY_PIN_LO PORTC &= ~(1<<RY_PIN)
#define LP_PIN 16
#define RP_PIN 17
#define LT_PIN 7
#define RT_PIN 4
static volatile uint8_t *servoportarray[] = { &PORTB, &PORTC, &PORTD };
volatile uint8_t servopinarray[SPI_BUFSIZE];
volatile uint8_t servowert = 0;
volatile uint8_t lastservowert = 0;
volatile uint8_t timer0startwert=TIMER0_STARTWERT;
volatile uint16_t Servo_ArrayInt[SPI_BUFSIZE] = {}; // unsigned Int
uint8_t impulscounter = 0;
uint16_t defaultwert = 800;
uint16_t loopCount0=0;
uint16_t loopCount1=0;
uint16_t loopCount2=0;
uint16_t pwmpos=0;
#define PROGRAMM_DS 0
#define GRUPPE_DS 0xC0
//#define GRUPPE_DS 0xB0
#define TWI_PORT PORTC
#define TWI_PIN PINC
#define TWI_DDR DDRC
#define SDAPIN 4
#define SCLPIN 5
#define SPI_PORT PORTB
#define SPI_PIN PINB
#define SPI_DDR DDRB
#define TASTE1 19
#define TASTE2 29
#define TASTE3 44
#define TASTE4 67
#define TASTE5 94
#define TASTE6 122
#define TASTE7 155
#define TASTE8 186
#define TASTE9 205
#define TASTEL 223
#define TASTE0 236
#define TASTER 248
#define TASTATURPORT PORTC
#define TASTATURPIN 3
#define MANUELL_PORT PORTD
#define MANUELL_DDR DDRD
#define MANUELL_PIN PIND
#define MANUELL 7 // Bit 7 von Status
#define MANUELLPIN 6 // Pin 6 von PORT D fuer Anzeige Manuell
#define MANUELLNEU 7 // Pin 7 von Status. Gesetzt wenn neue Schalterposition eingestellt
#define MANUELLTIMEOUT 100 // Loopled-counts bis Manuell zurueckgesetzt wird. 02FF: ca. 100 s
#define FOSC 1000000 /* oscillator-frequency in Hz */
#define BAUD 57600 //valid values:9600, 19200, 57600 kbits
volatile uint8_t Programmstatus=0x00;
uint8_t Tastenwert=0;
uint8_t TastaturCount=0;
volatile uint16_t Manuellcounter=0; // Countr fuer Timeout
uint16_t TastenStatus=0;
uint16_t Tastencount=0;
uint16_t Tastenprellen=0x01F;
volatile uint16_t spiwaitcounter=0;
volatile uint32_t loadcounter=0; // schaltet load fuer PowerBanl-Reset ein
//PWM-detector
volatile uint32_t pwmhi=0; // dauer des hi
volatile uint32_t pwmpuls=0; // periodendauer
volatile uint16_t pwmhicounter=0;
volatile uint16_t pwmpulscounter=0;
volatile uint8_t pwmstatus=0;
volatile uint8_t spi_status=0;
//Variablen WL
// MARK: WL Defs
volatile uint8_t wl_status=0;
volatile uint8_t PTX=0;
volatile uint8_t int0counter=0;
volatile uint8_t sendcounter=0;
volatile uint16_t wlwaitcounter=0;
volatile uint16_t firstruncounter=0x0F;
volatile uint8_t wl_spi_status;
char itoabuffer[20];
volatile uint8_t wl_data[wl_module_PAYLOAD] = {};
volatile uint8_t pipenummer = 1;
//volatile char text[] = {'*','M','a','s','t','e','r','*'};
char* text = "* Master *";
// ACD https://www.avrprogrammers.com/howto/attiny-comparator
// ACD https://www.avrprogrammers.com/howto/attiny-comparator
#define COMP_PORT PORTB
#define COMP_DDR DDRB
// Pins fuer Drive der RC
#define COMP_DRIVE_PIN_A 1
#define COMP_DRIVE_PIN_B 2
#define COMP_ADC_PORT PORTC
#define COMP_ADC_DDR DDRC
#define COMP_ADC_PIN_A 4
#define COMP_ADC_PIN_B 5
#define COMP_AIN_PORT PORTD
#define COMP_AIN_DDR DDRD
#define COMP_AIN0 6
#define COMP_AIN1 7
#define MULTIPLEX 1
volatile uint16_t captured_value;
volatile uint8_t captured;
volatile uint8_t overflow=0;
volatile uint8_t captcounter=0;
volatile uint16_t mittelwertA[4];
volatile uint16_t mittelwertB[4];
volatile uint8_t mposA=0;
volatile uint8_t mposB=0;
volatile uint8_t adckanal=0;
// end ACD
#pragma mark RC
struct PacketData
{
uint8_t lxAxisValue;
uint8_t lyAxisValue;
uint8_t rxAxisValue;
uint8_t ryAxisValue;
uint8_t lPotValue;
uint8_t rPotValue;
uint8_t switch1Value;
uint8_t switch2Value;
uint8_t switch3Value;
uint8_t switch4Value;
};
struct PacketData receiverData;
#define SIGNAL_TIMEOUT 500 // This is signal timeout in milli seconds. We will reset the data if no signal
const uint64_t pipeIn = 0xF9E8F0F0E1LL;
unsigned long lastRecvTime = 0;
volatile uint8_t wl_spi_status;
void delay_ms(unsigned int ms)
/* delay for a minimum of <ms> */
{
// we use a calibrated macro. This is more
// accurate and not so much compiler dependent
// as self made code.
while(ms){
_delay_ms(0.96);
ms--;
}
}
uint8_t Tastenwahl(uint8_t Tastaturwert)
{
//lcd_gotoxy(0,1);
//lcd_putint(Tastaturwert);
if (Tastaturwert < TASTE1)
return 1;
if (Tastaturwert < TASTE2)
return 2;
if (Tastaturwert < TASTE3)
return 3;
if (Tastaturwert < TASTE4)
return 4;
if (Tastaturwert < TASTE5)
return 5;
if (Tastaturwert < TASTE6)
return 6;
if (Tastaturwert < TASTE7)
return 7;
if (Tastaturwert < TASTE8)
return 8;
if (Tastaturwert < TASTE9)
return 9;
if (Tastaturwert < TASTEL)
return 10;
if (Tastaturwert < TASTE0)
return 0;
if (Tastaturwert < TASTER)
return 12;
return -1;
}
void SPI_Master_init (void)
{
SPCR |= (1<<MSTR);// Set as Master
// SPCR0 |= (1<<CPOL0)|(1<<CPHA0);
/*
SPI2X SPR1 SPR0 SCK Frequency
0 0 0 fosc/4
0 0 1 fosc/16
0 1 0 fosc/64
0 1 1 fosc/128
1 0 0 fosc/2
1 0 1 fosc/8
1 1 0 fosc/32
1 1 1 fosc/64
*/
//SPCR |= (1<<SPR0); // div 16 SPI2X: div 8
SPCR |= (1<<SPR1); // div 64 SPI2X: div 32
//SPCR |= (1<<SPR1) | (1<<SPR0); // div 128 SPI2X: div 64
//SPCR |= (1<<SPI2X0);
SPCR |= (1<<SPE); // Enable SPI
spi_status = SPSR; //Status loeschen
wl_spi_status = 0;
}
void deviceinit(void)
{
// MANUELL_DDR |= (1<<MANUELLPIN); //Pin 5 von PORT D als Ausgang fuer Manuell
//MANUELL_PORT &= ~(1<<MANUELLPIN);
LOADDDR |= (1<<LOADPIN);
LOADPORT |= (1<<LOADPIN);
PWM_DETECT_DDR &= ~(1<<PWM_DETECT);
PWM_DETECT_PORT |= (1<<PWM_DETECT);
LOOPLED_DDR |= (1<<LOOPLED_PIN);
//PORTD &= ~(1<<CONTROL_B);
//PORTD &= ~(1<<CONTROL_A);
OSZIDDR |= (1<<PULSA); //Pin 0 von als Ausgang fuer OSZI
OSZIPORT |= (1<<PULSA); // HI
ADCDDR &= ~(1<<PORTC2);
ADCPORT &= ~(1<<PORTC2);
ADCDDR &= ~(1<<PORTC3);
ADCPORT &= ~(1<<PORTC3);
ADCDDR &= ~(1<<PORTC4);
ADCPORT &= ~(1<<PORTC4);
ADCDDR &= ~(1<<PORTC5);
ADCPORT &= ~(1<<PORTC5);
DDRB |= (1<<PB1);
DDRB |= (1<<PB2);
PTDDR |= (1<<PT_LOAD_PIN); // Pin fuer Impuls-load von pT1000
PTPORT |= (1<<PT_LOAD_PIN);// hi
// DDRB |= (1<<PORTB0); //OC1A: Bit 1 von PORT B als Ausgang fuer PWM
// PORTB |= (1<<PORTB0); //LO
DDRB |= (1<<PORTB1); //OC1A: Bit 1 von PORT B als Ausgang fuer PWM
PORTB &= ~(1<<PORTB1); //LO
//LCD
LCD_DDR |= (1<<LCD_RSDS_PIN); //Pin als Ausgang fuer LCD
LCD_DDR |= (1<<LCD_ENABLE_PIN); //Pin als Ausgang fuer LCD
LCD_DDR |= (1<<LCD_CLOCK_PIN); //Pin als Ausgang fuer LCD
#if defined(__AVR_ATmega8__)
// Initialize external interrupt 0 (PD2)
MCUCR = ((1<<ISC11)|(0<<ISC10)|(1<<ISC01)|(0<<ISC00)); // Set external interupt on falling edge
GICR = ((1<<INT1)|(0<<INT0)); // Activate INT1
#endif // __AVR_ATmega8__
#if defined(__AVR_ATmega88A__)
EICRA = ((1<<ISC11)|(0<<ISC10)|(1<<ISC01)|(0<<ISC00)); // Set external interupt on falling edge for INT0 and INT1
EIMSK = ((0<<INT1)|(1<<INT0)); // Activate INT0
#endif // __AVR_ATmega88A__
#if defined(__AVR_ATmega168__)
// Initialize external interrupt on port PD6 (PCINT22)
DDRB &= ~(1<<PD6);
PCMSK2 = (1<<PCINT22);
PCICR = (1<<PCIE2);
#endif // __AVR_ATmega168__
#if defined(__AVR_ATmega32U4__)
// Initialize external interrupt on port PD0
INTERRUPT_DDR &= ~(1<<INT0_PIN);
INTERRUPT_PORT |= (1<<INT0_PIN);
EICRA = ((1<<ISC11)|(0<<ISC10)|(1<<ISC01)|(0<<ISC00));
// Set external interupt on falling edge for INT0 and INT1
EIMSK = ((1<<INT0));
#endif // __AVR_ATmega32U4__
}
char SPI_get_put_char(int cData)
{
//Putchar -- Master
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)))
;
/* Return data register */
return SPDR;
}
void SPI_Init(void)
{
//http://www.atmel.com/dyn/resources/prod_documents/doc2467.pdf page:165
/*
SPI_DDR |= (1<<2)|(1<<3)|(1<<5);
SPI_DDR &= ~(1<<4);
SPCR |= (1<<MSTR);
SPCR |= (1<<SPR0)|(1<<SPR1);
SPCR |= (1<<SPE);
*/
//Master init
// Set MOSI and SCK output, all others input
SPI_DDR &= ~(1<<SPI_MISO);
SPI_PORT |= (1<<SPI_MISO);
SPI_DDR |= (1<<SPI_MOSI)|(1<<SPI_CLK)|(1<<SPI_SS);
SPI_PORT |= (1<<SPI_SS);
// Enable SPI, Master, set clock rate fck/16
SPCR = (1<<SPE)|
(1<<MSTR)|
(1<<SPR0);//|
//(1<<SPR1);
/*
Slave init
// Set MISO output, all others input
DDR_SPI = (1<<DD_MISO);
// Enable SPI
SPCR = (1<<SPE);
*/
}
/*
void timer1_comp(void)
{
// Set pin for driving resistor low.
COMP_DDR |= (1<<COMP_DRIVE_PIN_A);
COMP_PORT &= ~(1<<COMP_DRIVE_PIN_A);
COMP_DDR |= (1<<COMP_DRIVE_PIN_B);
COMP_PORT &= ~(1<<COMP_DRIVE_PIN_B);
// Disable the digital input buffers.
// DIDR = (1<<AIN1D) | (1<<AIN0D);
if (MULTIPLEX)
{
// ADC-Eingaenge fuer Capt
COMP_ADC_DDR &= ~(1<<COMP_ADC_PIN_A);
COMP_ADC_PORT &= ~(1<<COMP_ADC_PIN_A);
COMP_ADC_DDR &= ~(1<<COMP_ADC_PIN_B);
COMP_ADC_PORT &= ~(1<<COMP_ADC_PIN_B);
// AIN0, AIN1 Eingang
COMP_AIN_DDR &= ~(1<<COMP_AIN0);
COMP_AIN_DDR &= ~(1<<COMP_AIN1);
SFIOR |= (1<<ACME);
//ADMUX = 3;
}
//ADCSRA =0;//| = (1<<ADEN); // disable ADC if necessary
ACSR = (1<<ACIC) | (1<<ACIS1) | (1<<ACIS0); // Comparator enabled, no bandgap, input capture.
// Timer...
TCCR1A = 0;
TCCR1B = (1<<CS10); // F_CPU / 1
//TCCR1B = (1<<ICES1); // Input capture on rising edge
TCNT1 = 0;
TIMSK1 |= (1<<TOIE1) | (1<<TICIE1); // Timer interrupts on capture and overflow.
sei();
}
*/
#pragma mark timer1
// Timer1 Servo
void timer1(void)
{
// https://www.mikrocontroller.net/topic/83609
int c=0;
//TCCR1B |= (1<<CS11); // f/8
TCCR1B |= (1<<CS11); // f
TCNT1 = 0; // reset Timer
// Impulsdauer
OCR1B = 0x500; // Impulsdauer des Kanalimpulses
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt:
// TIMSK1 |= (1 << OCIE1B); // enable timer compare interrupt:
OCR1A = Servo_ArrayInt[(impulscounter & 0x07)]; //
}
ISR(TIMER1_OVF_vect)
{
PORTB ^= (1<<PB2);
}
// MARK: TIMER1_COMPA_vect
ISR(TIMER1_COMPA_vect) // ca 4 us
{
OSZIA_TOGG;
//
loadcounter++;
if (impulscounter < SPI_BUFSIZE)
{
// Impulsdauer lesen
OCR1A = Servo_ArrayInt[(impulscounter)];
// servowert setzen
servowert = (uint8_t)servopinarray[impulscounter];// Pin & PORT>>4
//uint8_t kanalport = (servowert & 0xF0)>>4;
//uint8_t kanalpin = servowert & 0x0F;
*servoportarray[(servowert & 0xF0)>>4] |= (1<<(servowert & 0x0F));
if(impulscounter == 0) // Start Impulspaket. ersten impuls setzen
{
}
else // vorherigen Impuls beenden
{
*servoportarray[(lastservowert & 0xF0)>>4] &= ~(1<<(lastservowert & 0x0F));
}
lastservowert = servowert;
impulscounter++;
}
else
{
// letzten Impuls beenden
*servoportarray[(lastservowert & 0xF0)>>4] &= ~(1<<(lastservowert & 0x0F));
impulscounter =0; // restart from beginning
OCR1A = 15000;
}
TCNT1 = 0;
if(OCR1A < 0xFFF0)
{
//OCR1A += 100;
}
else
{
//OCR1A = 400;
}
}
ISR(TIMER1_COMPB_vect) // ca 4 us
{
//PORTB ^= (1<<PB2);
//
loadcounter++;
TCNT1 = 0;
//OSZIA_HI;
}
/*
#pragma mark INT0 WL
ISR(INT0_vect)
{
wl_spi_status |= (1<<WL_ISR_RECV);
}
*/
void timer2(void)
{
DDRB |= (1<<PB2);
TCCR2A = (1<<WGM21); // Wave Form Generation Mode 2: CTC, OC2A disconnected
TCCR2B = (1<<CS20) ; // prescaler = 256
TIMSK2 = (1<<OCIE2A); // interrupt when Compare Match with OCR2A
OCR2A = 83 ; // 10kHz
}
ISR(TIMER2_OVF_vect)
{
//PORTB ^= (1<<PB2);
}
ISR(TIMER2_COMPA_vect) // ca 4 us
{
//OSZIA_LO;
PORTB ^= (1<<PB2);
//
loadcounter++;
//OSZIA_HI;
// handle interrupt
}
#pragma mark INT1 WL
ISR(INT1_vect)
{
wl_spi_status |= (1<<WL_ISR_RECV);
}
ISR (SPI_STC_vect)
{
// SPDR = data;
}
/*
// https://www.avrfreaks.net/forum/how-can-i-make-array-ports
static volatile uint8_t *myports[] = { &PORTD, &PORTB, &PORTC };
int main() {
uint8_t i,j;
for (i=0; i < 3; i++) {
*(myports[i]) |= 0x10; // set this bit in each port
}
}
*/
// MARK: main
int main (void)
{
/* INITIALIZE */
// LCD_DDR |=(1<<LCD_RSDS_PIN);
// LCD_DDR |=(1<<LCD_ENABLE_PIN);
// LCD_DDR |=(1<<LCD_CLOCK_PIN);
uint8_t pos = 0;
for (pos=0;pos<8;pos++)
{
Servo_ArrayInt[pos] = defaultwert + pos*100;
}
Servo_ArrayInt[0] = 400;
Servo_ArrayInt[1] = 600;
Servo_ArrayInt[2] = 800;
Servo_ArrayInt[3] = 1000;
/*
Servo_ArrayInt[4] = 400;
Servo_ArrayInt[5] = 800;
Servo_ArrayInt[6] = 1200;
Servo_ArrayInt[7] = 1600;
*/
servopinarray[0] = LX_PIN + (L_PORT<<4); // linker joystick
servopinarray[1] = LY_PIN + (L_PORT<<4);
servopinarray[2] = RX_PIN + (R_PORT<<4); // rechter joystick
servopinarray[3] = RY_PIN + (R_PORT<<4);
deviceinit();
delay_ms(100);
// SPI_Init();
// SPI_Master_init();
lcd_initialize(LCD_FUNCTION_8x2, LCD_CMD_ENTRY_INC, LCD_CMD_ON);
lcd_puts("Guten Tag\0");
delay_ms(200);
lcd_cls();
lcd_gotoxy(0,0);
lcd_puts("READY\0");
// DS1820 init-stuff begin
// DS1820 init-stuff end
volatile char incoming=0;
size_t poscounter=0;
// MARK: WL main
uint8_t payload[wl_module_PAYLOAD];
uint8_t maincounter =0;
//Array for Payload
wl_module_init();
_delay_ms(50);
sei();
wl_module_rx_config();
delay_ms(50);
wl_module_CE_hi;
//
lcd_clr_line(0);
// timer1_comp();
initADC(2);
uint8_t delaycount=10;
#pragma mark while
uint8_t readstatus = wl_module_get_data((void*)&wl_data);
//lcd_puts(" los");
uint8_t eevar=13;
timer1();
// timer2();
sei();
while (1)
{
// PORTC |= (1<<0);
loopCount0 ++;
//_delay_ms(2);
//LOOPLED_PORT ^= (1<<LOOPLED_PIN);
//incoming = SPDR;
if (wl_spi_status & (1<<WL_ISR_RECV)) // in ISR gesetzt, etwas ist angekommen, Master fragt nach Daten
{
lcd_gotoxy(19,0);
lcd_putc('$');
if (int0counter < 0x2F)
{
int0counter++;
}
else
{
int0counter=0;
}
wl_spi_status &= ~(1<<WL_ISR_RECV);
/*
lcd_gotoxy(6,0);
lcd_putc('i');
lcd_puthex(int0counter);
*/
// MARK: WL Loop
/*
lcd_gotoxy(0,1);
lcd_puthex(wl_status & (1<<RX_DR));
lcd_puthex(wl_status & (1<<TX_DS));
lcd_puthex(wl_status & (1<<MAX_RT));
lcd_puthex(wl_status & (1<<TX_FULL));
*/
wl_status = wl_module_get_status();
delay_ms(3);
//lcd_gotoxy(18,0);
//lcd_puthex(wl_status);
pipenummer = wl_module_get_rx_pipe();
delay_ms(3);
if (pipenummer == WL_PIPE) // Request ist fuer uns, Data schicken
{
// lcd_gotoxy(14,0);
// lcd_puts("p ok");
//lcd_gotoxy(0,0);
// delay_ms(2);
//lcd_puts(" ");
if (wl_status & (1<<RX_DR)) // IRQ: Package has been received
{
//OSZIA_LO;
//lcd_gotoxy(16,2);
//lcd_puts("RX+");
//OSZIA_HI;
uint8_t rec = wl_module_get_rx_pw(0); //gets the RX payload width on the pipe
//lcd_gotoxy(0,3);
//lcd_puthex(rec);
//lcd_putc(' ');
// delay_ms(3);
uint8_t readstatus = wl_module_get_data((void*)&wl_data); // Reads wl_module_PAYLOAD bytes into data array
// delay_ms(3);
wl_module_config_register(STATUS, (1<<RX_DR)); //Clear Interrupt Bit
// delay_ms(3);
uint8_t i;
//lcd_gotoxy(0,0);
//lcd_puts("rs:");
//lcd_puthex(readstatus); //
/*
// pi schreiben
lcd_putc(' ');
lcd_putint1(wl_data[0]);
lcd_putc('.');
for (i=2; i<4; i++)
{
lcd_putint1(wl_data[i]);
}
*/
//lcd_putc(' ');
// Kontrolle: Data vom teensy
uint16_t temperatur = (wl_data[11]<<8);
temperatur |= wl_data[10];
//lcd_putint12(temperatur);
temperatur /=4; // *256/1024, unkalibriert
// lcd_putint2(temperatur/10);
// lcd_putc('.');
// lcd_putint1(temperatur%10);
//lcd_put_tempbis99(temperatur);
// lcd_gotoxy(18,3);
// lcd_puthex(wl_data[9]);
pwmpos = temperatur;
OCR1A = temperatur;
//OSZIA_HI;
wl_spi_status |= (1<<WL_SEND_REQUEST);
if (wl_spi_status & (1<<WL_SEND_REQUEST)) // senden starten
{
wl_spi_status &= ~(1<<WL_SEND_REQUEST);
// MARK: WL send
wl_module_tx_config(WL_PIPE); // neue Daten senden an Master auf pipe WL_PIPE
delay_ms(1);
//lcd_putc('b');
wl_module_send(payload,wl_module_PAYLOAD);
//lcd_putc('c');
uint8_t tx_status = wl_module_get_status();
//delay_ms(3);
lcd_gotoxy(0,0);
lcd_putc('s');
//lcd_puthex(tx_status);
//lcd_putc(' ');
lcd_puthex(sendcounter);
maincounter++;
PTX=0;
wl_module_rx_config(); // empfangen wieder einstellen
//delay_ms(3);
} // if
}
else
{
lcd_gotoxy(14,0);
lcd_puts("p ex");
}
} // if pipenummer
else
{
// lcd_puts("--");
}
if (wl_status & (1<<TX_DS)) // IRQ: Package has been sent
{
//OSZIA_LO; // 50 ms mit Anzeige, 140us ohne Anzeige
sendcounter++;
lcd_gotoxy(16,1);
lcd_puts("TX+");
wl_module_config_register(STATUS, (1<<TX_DS)); //Clear Interrupt Bit
PTX=0;
//OSZIA_HI;
}
else
{
// lcd_gotoxy(16,1);
// lcd_puts("***");
}
if (wl_status & (1<<MAX_RT)) // IRQ: Package has not been sent, send again
{
lcd_gotoxy(16,1);
lcd_puts("RT");
wl_module_config_register(STATUS, (1<<MAX_RT)); // Clear Interrupt Bit
}
} // end ISR abarbeiten
/*
if (wl_spi_status & (1<<WL_SEND_REQUEST)) // senden starten
{
wl_spi_status &= ~(1<<WL_SEND_REQUEST);
wl_module_tx_config(0); // neue Daten senden an device 0 (Master)
//lcd_putc('b');
wl_module_send(payload,wl_module_PAYLOAD);
//lcd_putc('c');
uint8_t tx_status = wl_module_get_status();
lcd_gotoxy(0,1);
//lcd_putc(' ');
lcd_puthex(tx_status);
lcd_putc(' ');
lcd_puthex(sendcounter);
maincounter++;
PTX=0;
wl_module_rx_config(); // empfangen wieder einstellen
} // if
*/
if (loopCount0 >=0xFE)
{
loopCount1++;
if (loopCount1 >0xAF)
{
//OSZIA_TOGG;
//LOOPLED_PORT ^= (1<<LOOPLED_PIN);
}
if ((loopCount1 >0x02AF) )//&& (!(Programmstatus & (1<<MANUELL))))
{
/*
if(OCR1A < 1000)
{
OCR1A += 10;
}
else
{
OCR1A = 400;
}
*/
lcd_gotoxy(0,0);
//lcd_puts(" OCR1A ");
//lcd_putint12(OCR1A);
LOOPLED_PORT ^= (1<<LOOPLED_PIN);
for (uint8_t pos = 0;pos < 4;pos++)
{
uint8_t tempservowert = (uint8_t)servopinarray[pos];// Pin & PORT>>4
lcd_gotoxy(pos * 3,3);
lcd_puthex(tempservowert);