-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.c
1183 lines (995 loc) · 25.9 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
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
/*
10-5-11
Copyright Spark Fun Electronics© 2011
Aaron Weiss
aaron at sparkfun dot com
9DOF-Razor-IMU-AHRS compatible
[email protected] w/ external 8MHz resonator
High fuse 0xDA
Low fuse 0xFF
EXT fust 0xF8
Default Baud: 57600bps
Revision Notes:
Hardware v22
Firmware:
v18 took self test off of menu, explain how to use it in help menu
v19 added baud rate selection, default to 57600bps, various bug fixes
v19i fixed baud menu return bugs
v20 using ITG3200 gyro
v21 added auto self test upon startup (see notes)
v22 corrected HMC output, x and y registers are different in the HMC5883
ADXL345: Accelerometer
HMC5883: Magnetometer
ITG3200: Pitch, Roll, and Yaw Gyro
Notes:
-To get out of autorun, hit ctrl-z
-max baud rate @8MHz is 57600bps
-self-test startup: LED blinks 5 times then OFF = GOOD, LED ON = BAD
*/
#include <stdlib.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include "types.h"
#include "defs.h"
#include "i2c.h"
#define STATUS_LED 5 //stat LED is on PB5
#define sbi(var, mask) ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask) ((var) &= (uint8_t)~(1 << mask))
#define ITG3200_R 0xD1 // ADD pin is pulled low
#define ITG3200_W 0xD0
///============Initialize Prototypes=====//////////////////
void init(void);
unsigned int UART_Init(unsigned int ubrr);
uint8_t uart_getchar(void);
static int uart_putchar(char c, FILE *stream);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
void i2cInit(void);
///============Function Prototypes=========/////////////////
void accelerometer_init(void);
void auto_raw(void);
void baud_menu(void);
void check_baud(void);
void config_menu(void);
void config_read(void);
void gyro_init(void);
void help(void);
void magnetometer(void);
void magnetometer_init(void);
void print_adxl345(void);
void print_hmc5883(void);
void print_itg3200(void);
void raw(void);
void self_test(void);
uint16_t x_accel(void);
uint16_t y_accel(void);
uint16_t z_accel(void);
uint16_t x_gyro(void);
uint16_t y_gyro(void);
uint16_t z_gyro(void);
///============EEPROM Protoypes============//////////////////
void write_to_EEPROM(unsigned int Address, unsigned char Data);
unsigned char read_from_EEPROM(unsigned int Address);
///============Display Strings============//////////////////
const char wlcm_str[] PROGMEM = "\n\n\r9DOF IMU Firmware v22 \n\r==========================";
const char accel[] PROGMEM = "\n\r[1]Accelerometer: ADXL345 \n\r";
const char mag[] PROGMEM = "[2]Magnetometer: HMC5883 \n\r";
const char gyro[] PROGMEM = "[3]Gyroscope: ITG-3200 \n\r";
const char raw_out[] PROGMEM = "[4]Raw Output\n\r";
const char baud_change[] PROGMEM = "[5]Change Baud Rate: ";
const char autorun[] PROGMEM = "[Ctrl+z]Toggle Autorun\n\r";
const char help_[] PROGMEM = "[?]Help\n\r";
///============Global Vars=========/////////////////
uint16_t x_mag, y_mag, z_mag; //x, y, and z magnetometer values
long baud;
/////===========MAIN=====================/////////////////////
int main(void)
{
init();
self_test();
while(1)
{
//check to see if autorun is set, if it is don't print the menu
if(read_from_EEPROM(1) == 48) config_read();
else config_menu();
}
}
void accelerometer_init(void)
{
//initialize
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA6); //write to ADXL
i2cWaitForComplete();
i2cSendByte(0x2D); //power register
i2cWaitForComplete();
i2cSendByte(0x08); //measurement mode
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA6); //write to ADXL
i2cWaitForComplete();
i2cSendByte(0x31); //data format
i2cWaitForComplete();
i2cSendByte(0x08); //full resolution
i2cWaitForComplete();
i2cSendStop();
}
void print_adxl345(void)
{
printf("x=%4d, ", x_accel());
printf("y=%4d, ", y_accel());
printf("z=%4d \n\r", z_accel());
delay_ms(20);
}
void auto_raw(void)
{
unsigned int ticks = 0;
//while there is not a button pressed
while(!(UCSR0A & (1 << RXC0)))
{
//prints the raw vaues with a '$' start and '#\n\r' end
printf("$");
printf("%d,", x_accel());
printf("%d,", y_accel());
printf("%d,", z_accel());
printf("%d,", x_gyro());
printf("%d,", y_gyro());
printf("%d,", z_gyro());
if (ticks++ % 20 == 0) // Only once each 20 ticks, i.e. 400ms
magnetometer();
printf("%d,", x_mag);
printf("%d,", y_mag);
printf("%d", z_mag);
printf("#\n\r");
delay_ms(20);
}
//if a button is pressed and that button is ctrl-z, reset autorun, display menu
if(uart_getchar() == 0x1A)
{
write_to_EEPROM(1,0);
config_menu();
}
auto_raw();
}
void baud_menu(void)
{
printf("\n\rBaud Rate Select Menu\n\r");
printf("[1] 4800\n\r");
printf("[2] 9600\n\r");
printf("[3] 19200\n\r");
printf("[4] 38400\n\r");
printf("[5] 57600\n\r");
uint8_t choicer=0;
while(1)
{
choicer = uart_getchar();
putchar('\n');
putchar('\r');
if(choicer=='1') //4800
{
//outside of default flag: used to notify init not to run default baud value
write_to_EEPROM(2, 99);
//clear other EEPROM values
write_to_EEPROM(4, 0);
write_to_EEPROM(5, 0);
write_to_EEPROM(6, 0);
write_to_EEPROM(7, 0);
write_to_EEPROM(3, 4); //baud change flag
printf("!change baud rate to 4800bps, reset board!");
delay_ms(50);
UART_Init(207);
while(1);
}
if(choicer=='2') //9600
{
write_to_EEPROM(2, 99); //outside of default flag
//clear other EEPROM values
write_to_EEPROM(3, 0);
write_to_EEPROM(5, 0);
write_to_EEPROM(6, 0);
write_to_EEPROM(7, 0);
write_to_EEPROM(4, 9); //baud change flag
printf("!change baud rate to 9600bps, reset board!");
delay_ms(50);
UART_Init(103);
while(1);
}
if(choicer=='3') //19200
{
write_to_EEPROM(2, 99); //outside of default flag
//clear other EEPROM values
write_to_EEPROM(3, 0);
write_to_EEPROM(4, 0);
write_to_EEPROM(6, 0);
write_to_EEPROM(7, 0);
write_to_EEPROM(5, 19); //baud change flag
printf("!change baud rate to 19200bps, reset board!");
delay_ms(50);
UART_Init(51);
while(1);
}
if(choicer=='4') //38400
{
write_to_EEPROM(2, 99); //outside of default flag
//clear other EEPROM values
write_to_EEPROM(3, 0);
write_to_EEPROM(4, 0);
write_to_EEPROM(5, 0);
write_to_EEPROM(7, 0);
write_to_EEPROM(6, 38); //baud change flag
printf("!change baud rate to 38400bps, reset board!");
delay_ms(50);
UART_Init(25);
while(1);
}
if(choicer=='5') //57600
{
write_to_EEPROM(2, 99); //outside of default flag
//clear other EEPROM values
write_to_EEPROM(3, 0);
write_to_EEPROM(4, 0);
write_to_EEPROM(5, 0);
write_to_EEPROM(6, 0);
write_to_EEPROM(7, 57); //baud change flag
printf("!change baud rate to 57600bps, reset board!");
delay_ms(50);
UART_Init(16);
while(1);
}
if((choicer < 0X31) || (choicer > 0x35))config_menu(); //if choice is not #s 1-5 goto conig menu
}
config_menu();
}
void check_baud(void)
{
if(read_from_EEPROM(2) == 99) //check to see if the baud rate has been changed by user
{
//read baud rate selection
if(read_from_EEPROM(3) == 4)
{
baud = 4800;
UART_Init(207);
}
if(read_from_EEPROM(4) == 9)
{
baud = 9600;
UART_Init(103);
}
if(read_from_EEPROM(5) == 19)
{
baud = 19200;
UART_Init(51);
}
if(read_from_EEPROM(6) == 38)
{
baud = 38400;
UART_Init(25);
}
if(read_from_EEPROM(7) == 57)
{
baud = 57600;
UART_Init(16);
}
}
else
{
//57600bps Default UART
UART_Init(16);
baud = 57600;
}
}
void config_menu(void)
{
i2cInit();
accelerometer_init();
magnetometer_init();
printf_P(wlcm_str);
printf_P(accel);
printf_P(mag);
printf_P(gyro);
printf_P(raw_out);
printf_P(baud_change);
printf("%ldbps\n\r", baud);
printf_P(autorun);
printf_P(help_);
config_read();
}
void config_read(void)
{
uint8_t choice=0;
if(read_from_EEPROM(1) != 48)
{
while(1)
{
choice = uart_getchar();
putchar('\n');
putchar('\r');
if(choice=='1')
{
while(!(UCSR0A & (1 << RXC0)))print_adxl345();
config_menu();
}
if(choice=='2')
{
while(!(UCSR0A & (1 << RXC0)))
{
print_hmc5883();
delay_ms(350);//at least 100ms interval between measurements
}
config_menu();
}
if(choice=='3')
{
while(!(UCSR0A & (1 << RXC0)))print_itg3200();
config_menu();
}
if(choice=='4')
{
while(!(UCSR0A & (1 << RXC0)))raw();
config_menu();
}
if(choice=='5')
{
baud_menu();
config_menu();
}
if(choice==0x10) //if ctrl-p
{
self_test();
}
if(choice==0x1A) //if ctrl-z
{
write_to_EEPROM(1,48);
auto_raw();
}
if(choice==0x3F) //if ?
{
help();
while(!(UCSR0A & (1 << RXC0)));
config_menu();
}
if(choice==0xFF) config_read();
}
}else auto_raw();
}
void gyro_init(void)
{
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write 0xB4
i2cWaitForComplete();
i2cSendByte(0x3E); // write register address
i2cWaitForComplete();
i2cSendByte(0x80);
i2cWaitForComplete();
i2cSendStop();
delay_ms(10);
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write 0xB4
i2cWaitForComplete();
i2cSendByte(0x16); // write register address
i2cWaitForComplete();
i2cSendByte(0x18); // DLPF_CFG = 0, FS_SEL = 3
i2cWaitForComplete();
i2cSendStop();
delay_ms(10);
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write 0xB4
i2cWaitForComplete();
i2cSendByte(0x3E); // write register address
i2cWaitForComplete();
i2cSendByte(0x00);
i2cWaitForComplete();
i2cSendStop();
}
void help(void)
{
printf("HELP MENU\n\r");
printf("[1] send ascii 1 to get output from the accelerometer(x,y,z). Hit any key to return to menu.\n\r");
printf("[2] send ascii 2 to get output from the magnetometer(x,y,z). Hit any key to return to menu.\n\r");
printf("[3] send ascii 3 to get output from the gyroscope(x,y,z). Hit any key to return to menu.\n\r");
printf("[4] send ascii 4 to get the raw output from all of the sensors. Hit any key to return to menu.\n\r");
printf("*** Raw format '$accelx,accely,accelz,gyrox,gyroy,gyroz,magx,magy,magz#\n\r");
printf("[5] send ascii 5 to get the 5 choices for baud rates. Reset terminal and board after change. Hit any key to return to menu.\n\r");
printf("[ctrl-p] ctrl-p tests the accelerometer and magnetometer.\n\r");
printf("[ctrl-z] ctrl+z at anytime will toggle between raw output and the menu\n\r");
}
void print_hmc5883(void)
{
magnetometer();
printf("x=%4d, ", x_mag);
printf("y=%4d, ", y_mag);
printf("z=%d\n\r", z_mag);
}
void magnetometer(void)
{
/*
The magnetometer values must be read consecutively
in order to move the magnetometer pointer. Therefore the x, y, and z
outputs need to be kept in this function. To read the magnetometer
values, call the function magnetometer(), then global vars
x_mag, y_mag, z_mag.
*/
magnetometer_init();
uint8_t xh, xl, yh, yl, zh, zl;
//must read all six registers plus one to move the pointer back to 0x03
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x3D); //write to HMC
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xh = i2cGetReceivedByte(); //x high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); //x low byte
i2cWaitForComplete();
x_mag = xl|(xh << 8);
i2cReceiveByte(TRUE);
i2cWaitForComplete();
zh = i2cGetReceivedByte();
i2cWaitForComplete(); //z high byte
i2cReceiveByte(TRUE);
i2cWaitForComplete();
zl = i2cGetReceivedByte(); //z low byte
i2cWaitForComplete();
z_mag = zl|(zh << 8);
i2cReceiveByte(TRUE);
i2cWaitForComplete();
yh = i2cGetReceivedByte(); //y high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
yl = i2cGetReceivedByte(); //y low byte
i2cWaitForComplete();
y_mag = yl|(yh << 8);
i2cSendByte(0x3D); //must reach 0x09 to go back to 0x03
i2cWaitForComplete();
i2cSendStop();
}
void magnetometer_init(void)
{
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x3C); //write to HMC
i2cWaitForComplete();
i2cSendByte(0x00); //mode register
i2cWaitForComplete();
i2cSendByte(0x70); //8 average, 15Hz, normal measurement
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x3C); //write to HMC
i2cWaitForComplete();
i2cSendByte(0x01); //mode register
i2cWaitForComplete();
i2cSendByte(0xA0); //gain = 5
i2cWaitForComplete();
i2cSendStop();
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x3C); //write to HMC
i2cWaitForComplete();
i2cSendByte(0x02); //mode register
i2cWaitForComplete();
i2cSendByte(0x00); //continuous measurement mode
i2cWaitForComplete();
i2cSendStop();
}
void raw(void)
{
//prints the raw vaues with a '$' start and '#\n\r' end
printf("$");
printf("%d,", x_accel());
printf("%d,", y_accel());
printf("%d,", z_accel());
printf("%d,", x_gyro());
printf("%d,", y_gyro());
printf("%d,", z_gyro());
magnetometer();
printf("%d,", x_mag);
printf("%d,", y_mag);
printf("%d", z_mag);
printf("#\n\r");
delay_ms(350);//at least 100ms interval between mag measurements
}
void self_test(void)
{
//MAGNETOMETER
uint8_t xh, xl, yh, yl, zh, zl, hmc_flag = 0;
x_mag = 0;
y_mag = 0;
z_mag = 0;
magnetometer_init();
//must read all six registers plus one to move the pointer back to 0x03
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0x3D); //write to HMC
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xh = i2cGetReceivedByte(); //x high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); //x low byte
i2cWaitForComplete();
x_mag = xl|(xh << 8);
i2cReceiveByte(TRUE);
i2cWaitForComplete();
zh = i2cGetReceivedByte();
i2cWaitForComplete(); //z high byte
i2cReceiveByte(TRUE);
i2cWaitForComplete();
zl = i2cGetReceivedByte(); //z low byte
i2cWaitForComplete();
z_mag = zl|(zh << 8);
i2cReceiveByte(TRUE);
i2cWaitForComplete();
yh = i2cGetReceivedByte(); //y high byte
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
yl = i2cGetReceivedByte(); //y low byte
i2cWaitForComplete();
y_mag = yl|(yh << 8);
i2cSendByte(0x3D); //must reach 0x09 to go back to 0x03
i2cWaitForComplete();
i2cSendStop();
//if it gets to this point and there are values in x,y,z_mag, we can assume part is responding correctly
if((x_mag == y_mag) && (y_mag == z_mag)) hmc_flag = 0xFF;
else hmc_flag = 0;
//ACCELEROMETER
uint8_t x, dummy;
//0x32 data registers
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA6); //write to ADXL
i2cWaitForComplete();
i2cSendByte(0x00); //X0 data register
i2cWaitForComplete();
i2cSendStop(); //repeat start
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA7); //read from ADXL
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
x = i2cGetReceivedByte();
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
dummy = i2cGetReceivedByte();
i2cWaitForComplete();
i2cSendStop();
///////////////////////////////////
// Gyro////////////////////////////
///////////////////////////////////
char data;
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write 0xD2
i2cWaitForComplete();
i2cSendByte(0x00); // who am i
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(ITG3200_R); // write 0xD3
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
data = i2cGetReceivedByte(); // Get MSB result
i2cWaitForComplete();
i2cSendStop();
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
int gyro_flag = 0;
int mag_flag = 0;
int accel_flag = 0;
if(data == 0x69)
{
//printf("ITG: GOOD\n\r");
gyro_flag = 1;
}//else printf("ITG: BAD\n\r");
if(hmc_flag == 0)
{
//printf("HMC: GOOD\n\r");
mag_flag = 1;
}//else printf("HMC: BAD\n\r");
if(x == 0xE5)
{
//printf("ADXL: GOOD\n\r");
accel_flag = 1;
}//else printf("ADXL: BAD\n\r");
if(gyro_flag ==1 && mag_flag == 1 && accel_flag == 1)
{
sbi(PORTB, 5);
delay_ms(1000);
cbi(PORTB, 5);
delay_ms(1000);
sbi(PORTB, 5);
delay_ms(1000);
cbi(PORTB, 5);
delay_ms(1000);
sbi(PORTB, 5);
delay_ms(1000);
cbi(PORTB, 5);
delay_ms(1000);
sbi(PORTB, 5);
delay_ms(1000);
cbi(PORTB, 5);
delay_ms(1000);
sbi(PORTB, 5);
delay_ms(1000);
cbi(PORTB, 5);
gyro_flag = 0;
mag_flag = 0;
accel_flag = 0;
}else sbi(PORTB, 5);
//while(!(UCSR0A & (1 << RXC0)));
config_menu();
}
void print_itg3200(void)
{
printf("x= %4d, ", x_gyro());
printf("y= %4d, ", y_gyro());
printf("z= %4d\n\r", z_gyro());
delay_ms(20);
}
uint16_t x_gyro(void)
{
uint16_t xh, xl, data;
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write
i2cWaitForComplete();
i2cSendByte(0x1D); // x high address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(ITG3200_R); // read
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
xh = i2cGetReceivedByte(); // Get MSB result
i2cWaitForComplete();
i2cSendStop();
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write
i2cWaitForComplete();
i2cSendByte(0x1E); // x low address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(ITG3200_R); // read
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); // Get LSB result
i2cWaitForComplete();
i2cSendStop();
data = xl|(xh << 8);
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
return data;
}
uint16_t y_gyro(void)
{
uint16_t xh, xl, data;
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write
i2cWaitForComplete();
i2cSendByte(0x1F); // y high address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(ITG3200_R); // read
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
xh = i2cGetReceivedByte(); // Get MSB result
i2cWaitForComplete();
i2cSendStop();
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write
i2cWaitForComplete();
i2cSendByte(0x20); // y low address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(ITG3200_R); // read
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); // Get LSB result
i2cWaitForComplete();
i2cSendStop();
data = xl|(xh << 8);
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
return data;
}
uint16_t z_gyro(void)
{
uint16_t xh, xl, data;
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write
i2cWaitForComplete();
i2cSendByte(0x21); // z high address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(ITG3200_R); // read
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
xh = i2cGetReceivedByte(); // Get MSB result
i2cWaitForComplete();
i2cSendStop();
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(ITG3200_W); // write
i2cWaitForComplete();
i2cSendByte(0x22); // z low address
i2cWaitForComplete();
i2cSendStart();
i2cSendByte(ITG3200_R); // read
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); // Get LSB result
i2cWaitForComplete();
i2cSendStop();
data = xl|(xh << 8);
cbi(TWCR, TWEN); // Disable TWI
sbi(TWCR, TWEN); // Enable TWI
return data;
}
uint16_t x_accel(void)
{
//0xA6 for a write
//0xA7 for a read
uint8_t dummy, xh, xl;
uint16_t xo;
//0x32 data registers
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA6); //write to ADXL
i2cWaitForComplete();
i2cSendByte(0x32); //X0 data register
i2cWaitForComplete();
i2cSendStop(); //repeat start
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA7); //read from ADXL
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xl = i2cGetReceivedByte(); //x low byte
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
dummy = i2cGetReceivedByte(); //must do a multiple byte read?
i2cWaitForComplete();
i2cSendStop();
//0x33 data registers
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA6); //write to ADXL
i2cWaitForComplete();
i2cSendByte(0x33); //X1 data register
i2cWaitForComplete();
i2cSendStop(); //repeat start
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA7); //read from ADXL
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
xh = i2cGetReceivedByte(); //x high byte
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
dummy = i2cGetReceivedByte(); //must do a multiple byte read?
i2cWaitForComplete();
i2cSendStop();
xo = xl|(xh << 8);
return xo;
}
uint16_t y_accel(void)
{
//0xA6 for a write
//0xA7 for a read
uint8_t dummy, yh, yl;
uint16_t yo;
//0x34 data registers
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA6); //write to ADXL
i2cWaitForComplete();
i2cSendByte(0x34); //Y0 data register
i2cWaitForComplete();
i2cSendStop(); //repeat start
i2cSendStart();
i2cWaitForComplete();
i2cSendByte(0xA7); //read from ADXL
i2cWaitForComplete();
i2cReceiveByte(TRUE);
i2cWaitForComplete();
yl = i2cGetReceivedByte(); //x low byte
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
dummy = i2cGetReceivedByte(); //must do a multiple byte read?
i2cWaitForComplete();