質問編集履歴

9

現在変更したコードをアップしました。

2019/09/06 16:23

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -504,6 +504,24 @@
504
504
 
505
505
  i2c_write16_8(vl6180x_addr, 0x0030, 0x00);
506
506
 
507
+ i2c_write16_8(vl6180x_addr, 0x0011, 0x10);
508
+
509
+ i2c_write16_8(vl6180x_addr, 0x010a, 0x30);
510
+
511
+ i2c_write16_8(vl6180x_addr, 0x003f, 0x46);
512
+
513
+ i2c_write16_8(vl6180x_addr, 0x0031, 0xFF);
514
+
515
+ i2c_write16_8(vl6180x_addr, 0x0040, 0x63);
516
+
517
+ i2c_write16_8(vl6180x_addr, 0x002e, 0x01);
518
+
519
+ i2c_write16_8(vl6180x_addr, 0x001b, 0x09);
520
+
521
+ i2c_write16_8(vl6180x_addr, 0x003e, 0x31);
522
+
523
+ i2c_write16_8(vl6180x_addr, 0x0014, 0x24);
524
+
507
525
 
508
526
 
509
527
 
@@ -568,7 +586,7 @@
568
586
 
569
587
  //start single-shot mode
570
588
 
571
- if((i2c_write16_8(vl6180x_addr, 0x18,0x01)) < 0) {
589
+ if((i2c_write16_8(vl6180x_addr, 0x018,0x001)) < 0) {
572
590
 
573
591
  printf("error: power on\n");
574
592
 
@@ -584,7 +602,7 @@
584
602
 
585
603
 
586
604
 
587
- ret = i2c_read16_8(vl6180x_addr, 0x4f, &data);
605
+ ret = i2c_read16_8(vl6180x_addr, 0x04f, &data);
588
606
 
589
607
  usleep(500*1000);
590
608
 
@@ -634,6 +652,8 @@
634
652
 
635
653
 
636
654
 
655
+
656
+
637
657
  ```
638
658
 
639
659
  修正済み
@@ -642,11 +662,13 @@
642
662
 
643
663
  ・(i2c_rw()関数の部分で "ioctl(I2C_RDWR) in i2c_rw: Remote I/O error"というエラーが出ています。)
644
664
 
665
+ ・(main()関数の" wait to {0x4f} register till bit 2 is set to 1"の部分で常に"data:00"と出力され、計測が開始されない状態です。改定のアドバイスよろしくお願いいたします。)
666
+
645
667
 
646
668
 
647
669
  現在
648
670
 
649
- main()関数の" wait to {0x4f} register till bit 2 is set to 1"部分で常に"data:00"と出力され、計測が開始されない状態です。改定のアドバイスよろくお願いたします。
671
+  {0x62}の値が0と出力されいます。
650
672
 
651
673
 
652
674
 
@@ -664,33 +686,33 @@
664
686
 
665
687
  ```
666
688
 
667
- data:0x00
689
+ data:0xb4
668
-
690
+
669
- 0000:00 00 03 00 00 70 4c b1 84 b5 56 10 00 00 00 00
691
+ 0000:b4 01 03 01 02 72 4c b1 84 b5 56 10 00 00 00 00
670
-
692
+
671
- 0010:60 20 00 00 00 07 00 00 00 ff 00 ff 31 00 00 00
693
+ 0010:60 20 00 00 00 00 00 00 00 ff 00 ff 31 00 00 00
672
-
694
+
673
- 0020:00 14 00 00 00 00 00 cb 03 fb 32 8f a0 11 00 74
695
+ 0020:00 14 00 00 0a 00 00 cb 03 fb 32 8f a0 11 00 74
674
-
696
+
675
- 0030:00 00 0a 28 00 00 00 ff 00 00 ff ff 00 00 00 06
697
+ 0030:00 00 0a 28 00 00 00 ff 00 00 ff ff 00 00 ff 06
676
-
698
+
677
- 0040:00 00 00 00 00 00 00 00 00 00 00 00 00 b3 01 00
699
+ 0040:00 00 00 00 00 00 00 00 00 00 00 00 00 73 01 00
678
700
 
679
701
  0050:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
680
702
 
681
- 0060:00 00 ff ff ff 00 00 07 00 00 00 00 00 00 0b 26
703
+ 0060:00 00 ff ff ff 00 00 0c 28 fa 00 00 00 00 12 c2
682
-
704
+
683
- 0070:00 00 00 00 00 00 42 c7 00 00 00 00 00 00 bf 6c
705
+ 0070:00 3d 40 9d 00 00 04 ec 00 00 0a 57 00 00 bf 52
684
-
706
+
685
- 0080:00 00 bf 6c 00 01 02 09 0d 0b 00 00 00 00 00 00
707
+ 0080:00 00 bf 52 00 01 02 09 0d 0b 00 00 00 00 00 00
686
708
 
687
709
  0090:00 00 00 00 00 00 00 fd 00 00 00 00 00 00 00 00
688
710
 
689
- 00a0:00 00 00 3c 00 00 00 00 00 00 00 00 00 00 00 00
711
+ 00a0:00 00 00 3c 00 00 0a 57 00 3d 7e a7 00 3d 40 9d
690
-
712
+
691
- 00b0:09 0f 09 06 00 00 00 00 00 00 00 3c 00 00 42 c7
713
+ 00b0:09 0f 09 06 00 00 00 00 00 00 00 3c 00 00 04 ec
692
-
714
+
693
- 00c0:00 01 9b d0 00 00 0b 26 09 0f 09 06 01 01 0b 00
715
+ 00c0:00 00 30 4a 00 00 12 c2 09 0f 09 06 03 01 0b 00
694
716
 
695
717
  00d0:01 03 01 01 00 00 00 00 00 05 00 ce 03 f8 06 00
696
718
 
@@ -700,47 +722,25 @@
700
722
 
701
723
  data:00
702
724
 
725
+ data:04
726
+
727
+ mesurement start
728
+
729
+ distance:0
730
+
703
731
  data:00
704
732
 
733
+ data:04
734
+
735
+ mesurement start
736
+
737
+ distance:0
738
+
705
739
  data:00
706
740
 
707
- data:00
741
+ data:04
708
-
709
- data:00
742
+
710
-
711
- data:00
743
+
712
-
713
- data:00
714
-
715
- data:00
716
-
717
- data:00
718
-
719
- data:00
720
-
721
- data:00
722
-
723
- data:00
724
-
725
- data:00
726
-
727
- data:00
728
-
729
- data:00
730
-
731
- data:00
732
-
733
- data:00
734
-
735
- data:00
736
-
737
- data:00
738
-
739
- data:00
740
-
741
- data:00
742
-
743
- data:00
744
744
 
745
745
 
746
746
 

8

現在変更したコードをアップしました。

2019/09/06 16:23

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -396,6 +396,44 @@
396
396
 
397
397
 
398
398
 
399
+ int i2c_dump16(u8 slave_addr, u16 reg, u32 in_len)
400
+
401
+ {
402
+
403
+ u8 out[2] = { (u8)(reg >> 8), (u8)reg};
404
+
405
+ u8 in[256];
406
+
407
+ u32 i;
408
+
409
+ int retval;
410
+
411
+ if(in_len > sizeof(in)) { in_len = sizeof(in); }
412
+
413
+ for(i = 0;i < in_len;i++) { in[i] = 0xaa; }
414
+
415
+ retval = i2c_rw(slave_addr, out, sizeof(out), in , in_len);
416
+
417
+ if(retval >= 0){
418
+
419
+ for(i = 0;i < in_len;i++){
420
+
421
+ if ((i & 0xf) ==0) { printf("%04x:", reg + i); }
422
+
423
+ printf("%02x%c", in[i], ((i & 0xf) == 0xf) ? '\n' : ' ');
424
+
425
+ }
426
+
427
+ if ((i & 0xf) !=0) { printf("\n"); }
428
+
429
+ }
430
+
431
+ return retval;
432
+
433
+ }
434
+
435
+
436
+
399
437
  int vl6180x_initialisation(void){
400
438
 
401
439
 
@@ -512,6 +550,10 @@
512
550
 
513
551
 
514
552
 
553
+ i2c_dump16(vl6180x_addr,0,256);
554
+
555
+
556
+
515
557
  vl6180x_initialisation();
516
558
 
517
559
 
@@ -592,8 +634,6 @@
592
634
 
593
635
 
594
636
 
595
-
596
-
597
637
  ```
598
638
 
599
639
  修正済み
@@ -620,6 +660,94 @@
620
660
 
621
661
 
622
662
 
663
+ ###現段階での出力
664
+
665
+ ```
666
+
667
+ data:0x00
668
+
669
+ 0000:00 00 03 00 00 70 4c b1 84 b5 56 10 00 00 00 00
670
+
671
+ 0010:60 20 00 00 00 07 00 00 00 ff 00 ff 31 00 00 00
672
+
673
+ 0020:00 14 00 00 00 00 00 cb 03 fb 32 8f a0 11 00 74
674
+
675
+ 0030:00 00 0a 28 00 00 00 ff 00 00 ff ff 00 00 00 06
676
+
677
+ 0040:00 00 00 00 00 00 00 00 00 00 00 00 00 b3 01 00
678
+
679
+ 0050:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
680
+
681
+ 0060:00 00 ff ff ff 00 00 07 00 00 00 00 00 00 0b 26
682
+
683
+ 0070:00 00 00 00 00 00 42 c7 00 00 00 00 00 00 bf 6c
684
+
685
+ 0080:00 00 bf 6c 00 01 02 09 0d 0b 00 00 00 00 00 00
686
+
687
+ 0090:00 00 00 00 00 00 00 fd 00 00 00 00 00 00 00 00
688
+
689
+ 00a0:00 00 00 3c 00 00 00 00 00 00 00 00 00 00 00 00
690
+
691
+ 00b0:09 0f 09 06 00 00 00 00 00 00 00 3c 00 00 42 c7
692
+
693
+ 00c0:00 01 9b d0 00 00 0b 26 09 0f 09 06 01 01 0b 00
694
+
695
+ 00d0:01 03 01 01 00 00 00 00 00 05 00 ce 03 f8 06 00
696
+
697
+ 00e0:00 00 03 00 04 02 01 03 00 00 00 00 00 00 06 00
698
+
699
+ 00f0:00 00 01 01 00 02 01 03 00 00 00 00 00 00 02 05
700
+
701
+ data:00
702
+
703
+ data:00
704
+
705
+ data:00
706
+
707
+ data:00
708
+
709
+ data:00
710
+
711
+ data:00
712
+
713
+ data:00
714
+
715
+ data:00
716
+
717
+ data:00
718
+
719
+ data:00
720
+
721
+ data:00
722
+
723
+ data:00
724
+
725
+ data:00
726
+
727
+ data:00
728
+
729
+ data:00
730
+
731
+ data:00
732
+
733
+ data:00
734
+
735
+ data:00
736
+
737
+ data:00
738
+
739
+ data:00
740
+
741
+ data:00
742
+
743
+ data:00
744
+
745
+
746
+
747
+ ```
748
+
749
+
750
+
623
751
  ### 試したこと
624
752
 
625
753
 

7

現在変更したコードをアップしました。

2019/09/06 13:09

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -308,7 +308,7 @@
308
308
 
309
309
  msgs[1].addr = slave_addr;
310
310
 
311
- msgs[1].flags = I2C_M_RD | I2C_M_NOSTART;
311
+ msgs[1].flags = I2C_M_RD;
312
312
 
313
313
  msgs[1].len = in_len;
314
314
 
@@ -396,15 +396,15 @@
396
396
 
397
397
 
398
398
 
399
- int vl6180x_initialisation(void){
399
+ int vl6180x_initialisation(void){
400
-
401
-
402
-
403
-
404
-
405
-
406
-
400
+
401
+
402
+
403
+
404
+
405
+
406
+
407
- //センサを使用するため設定
407
+ //センサの初期化
408
408
 
409
409
  i2c_write16_8(vl6180x_addr, 0x0207, 0x01);
410
410
 
@@ -466,35 +466,35 @@
466
466
 
467
467
  i2c_write16_8(vl6180x_addr, 0x0030, 0x00);
468
468
 
469
-
470
-
471
-
469
+
470
+
471
+
472
472
 
473
473
  i2c_write16_8(vl6180x_addr, 0x16, 0x00);
474
474
 
475
-
475
+
476
476
 
477
477
  }
478
478
 
479
479
  int main(){
480
480
 
481
-
481
+
482
482
 
483
483
  int ret, result;
484
484
 
485
-
485
+
486
486
 
487
487
  u8 data;
488
488
 
489
-
489
+
490
490
 
491
491
  i2c_init();
492
492
 
493
-
493
+
494
494
 
495
495
  if (ioctl(i2c_fd, I2C_SLAVE, vl6180x_addr) < 0) {
496
496
 
497
-
497
+
498
498
 
499
499
  perror("ioctl(I2C_SLAVE) in i2c_rw");
500
500
 
@@ -502,27 +502,27 @@
502
502
 
503
503
  }
504
504
 
505
-
505
+
506
506
 
507
507
  ret = i2c_read16_8(vl6180x_addr, 0x000, &data);
508
508
 
509
-
509
+
510
510
 
511
511
  printf("data:0x%02x\n",data);
512
512
 
513
-
513
+
514
514
 
515
515
  vl6180x_initialisation();
516
516
 
517
-
518
-
519
-
517
+
518
+
519
+
520
520
 
521
521
  // repeat rang mesurement
522
522
 
523
- while(1){
523
+ while(1){
524
-
525
-
524
+
525
+
526
526
 
527
527
  //start single-shot mode
528
528
 
@@ -532,15 +532,15 @@
532
532
 
533
533
  }
534
534
 
535
-
536
-
537
-
535
+
536
+
537
+
538
538
 
539
539
  // wait to {0x4f} register till bit 2 is set to 1
540
540
 
541
541
  while(1){
542
542
 
543
-
543
+
544
544
 
545
545
  ret = i2c_read16_8(vl6180x_addr, 0x4f, &data);
546
546
 
@@ -548,43 +548,43 @@
548
548
 
549
549
  printf("data:%02x\n",data);
550
550
 
551
-
551
+
552
552
 
553
553
  if(data == 0x04){
554
554
 
555
-
555
+
556
556
 
557
557
  printf("mesurement start\n");
558
558
 
559
559
  break;
560
560
 
561
-
561
+
562
562
 
563
563
  }
564
564
 
565
565
  }
566
566
 
567
-
567
+
568
568
 
569
569
  // get value
570
570
 
571
571
  ret = i2c_read16_8(vl6180x_addr, 0x62, &data);
572
572
 
573
-
573
+
574
574
 
575
575
  // clear interruput status
576
576
 
577
577
  i2c_write16_8(vl6180x_addr, 0x15, 0x07);
578
578
 
579
-
579
+
580
580
 
581
581
  printf("distance:%d\n", result);
582
582
 
583
-
583
+
584
584
 
585
585
  }
586
586
 
587
-
587
+
588
588
 
589
589
  i2c_close();
590
590
 
@@ -592,6 +592,8 @@
592
592
 
593
593
 
594
594
 
595
+
596
+
595
597
  ```
596
598
 
597
599
  修正済み

6

現在のコードの修正

2019/09/06 06:30

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -396,13 +396,13 @@
396
396
 
397
397
 
398
398
 
399
- int vl6180x_initialisation(void){
399
+ int vl6180x_initialisation(void){
400
-
401
-
402
-
403
-
404
-
405
-
400
+
401
+
402
+
403
+
404
+
405
+
406
406
 
407
407
  //センサを使用するための設定
408
408
 
@@ -466,35 +466,35 @@
466
466
 
467
467
  i2c_write16_8(vl6180x_addr, 0x0030, 0x00);
468
468
 
469
-
470
-
471
-
469
+
470
+
471
+
472
472
 
473
473
  i2c_write16_8(vl6180x_addr, 0x16, 0x00);
474
474
 
475
-
475
+
476
476
 
477
477
  }
478
478
 
479
479
  int main(){
480
480
 
481
-
481
+
482
482
 
483
483
  int ret, result;
484
484
 
485
-
485
+
486
486
 
487
487
  u8 data;
488
488
 
489
-
489
+
490
490
 
491
491
  i2c_init();
492
492
 
493
-
493
+
494
494
 
495
495
  if (ioctl(i2c_fd, I2C_SLAVE, vl6180x_addr) < 0) {
496
496
 
497
-
497
+
498
498
 
499
499
  perror("ioctl(I2C_SLAVE) in i2c_rw");
500
500
 
@@ -502,19 +502,27 @@
502
502
 
503
503
  }
504
504
 
505
+
506
+
505
-
507
+ ret = i2c_read16_8(vl6180x_addr, 0x000, &data);
508
+
509
+
510
+
511
+ printf("data:0x%02x\n",data);
512
+
513
+
506
514
 
507
515
  vl6180x_initialisation();
508
516
 
509
-
510
-
511
-
517
+
518
+
519
+
512
520
 
513
521
  // repeat rang mesurement
514
522
 
515
- while(1){
523
+ while(1){
516
-
517
-
524
+
525
+
518
526
 
519
527
  //start single-shot mode
520
528
 
@@ -524,15 +532,15 @@
524
532
 
525
533
  }
526
534
 
527
-
528
-
529
-
535
+
536
+
537
+
530
538
 
531
539
  // wait to {0x4f} register till bit 2 is set to 1
532
540
 
533
541
  while(1){
534
542
 
535
-
543
+
536
544
 
537
545
  ret = i2c_read16_8(vl6180x_addr, 0x4f, &data);
538
546
 
@@ -540,43 +548,43 @@
540
548
 
541
549
  printf("data:%02x\n",data);
542
550
 
543
-
551
+
544
552
 
545
553
  if(data == 0x04){
546
554
 
547
-
555
+
548
556
 
549
557
  printf("mesurement start\n");
550
558
 
551
559
  break;
552
560
 
553
-
561
+
554
562
 
555
563
  }
556
564
 
557
565
  }
558
566
 
559
-
567
+
560
568
 
561
569
  // get value
562
570
 
563
571
  ret = i2c_read16_8(vl6180x_addr, 0x62, &data);
564
572
 
565
-
573
+
566
574
 
567
575
  // clear interruput status
568
576
 
569
577
  i2c_write16_8(vl6180x_addr, 0x15, 0x07);
570
578
 
571
-
579
+
572
580
 
573
581
  printf("distance:%d\n", result);
574
582
 
575
-
583
+
576
584
 
577
585
  }
578
586
 
579
-
587
+
580
588
 
581
589
  i2c_close();
582
590
 

5

現在のコードの修正

2019/09/05 19:06

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -398,79 +398,83 @@
398
398
 
399
399
  int vl6180x_initialisation(void){
400
400
 
401
+
402
+
403
+
404
+
405
+
406
+
401
407
  //センサを使用するための設定
402
408
 
403
- i2c_write16_16(i2c_fd, 0x0207, 0x01);
404
-
405
- i2c_write16_16(i2c_fd, 0x0208, 0x01);
406
-
407
- i2c_write16_16(i2c_fd, 0x0096, 0x00);
408
-
409
- i2c_write16_16(i2c_fd, 0x0097, 0xfd);
410
-
411
- i2c_write16_16(i2c_fd, 0x00e3, 0x00);
412
-
413
- i2c_write16_16(i2c_fd, 0x00e4, 0x04);
414
-
415
- i2c_write16_16(i2c_fd, 0x00e5, 0x02);
416
-
417
- i2c_write16_16(i2c_fd, 0x00e6, 0x01);
418
-
419
- i2c_write16_16(i2c_fd, 0x00e7, 0x03);
420
-
421
- i2c_write16_16(i2c_fd, 0x00f5, 0x02);
422
-
423
- i2c_write16_16(i2c_fd, 0x00d9, 0x05);
424
-
425
- i2c_write16_16(i2c_fd, 0x00db, 0xce);
426
-
427
- i2c_write16_16(i2c_fd, 0x00dc, 0x03);
428
-
429
- i2c_write16_16(i2c_fd, 0x00dd, 0xf8);
430
-
431
- i2c_write16_16(i2c_fd, 0x009f, 0x00);
432
-
433
- i2c_write16_16(i2c_fd, 0x00a3, 0x3c);
434
-
435
- i2c_write16_16(i2c_fd, 0x00b7, 0x00);
436
-
437
- i2c_write16_16(i2c_fd, 0x00bb, 0x3c);
438
-
439
- i2c_write16_16(i2c_fd, 0x00b2, 0x09);
440
-
441
- i2c_write16_16(i2c_fd, 0x00ca, 0x09);
442
-
443
- i2c_write16_16(i2c_fd, 0x0198, 0x01);
444
-
445
- i2c_write16_16(i2c_fd, 0x01b0, 0x17);
446
-
447
- i2c_write16_16(i2c_fd, 0x01ad, 0x00);
448
-
449
- i2c_write16_16(i2c_fd, 0x00ff, 0x05);
450
-
451
- i2c_write16_16(i2c_fd, 0x0100, 0x05);
452
-
453
- i2c_write16_16(i2c_fd, 0x0199, 0x05);
454
-
455
- i2c_write16_16(i2c_fd, 0x01a6, 0x1b);
456
-
457
- i2c_write16_16(i2c_fd, 0x01ac, 0x3e);
458
-
459
- i2c_write16_16(i2c_fd, 0x01a7, 0x1f);
460
-
461
- i2c_write16_16(i2c_fd, 0x0030, 0x00);
462
-
463
-
464
-
465
-
466
-
467
- i2c_write16_8(i2c_fd, 0x16, 0x00);
468
-
469
-
470
-
471
- }
472
-
473
-
409
+ i2c_write16_8(vl6180x_addr, 0x0207, 0x01);
410
+
411
+ i2c_write16_8(vl6180x_addr, 0x0208, 0x01);
412
+
413
+ i2c_write16_8(vl6180x_addr, 0x0096, 0x00);
414
+
415
+ i2c_write16_8(vl6180x_addr, 0x0097, 0xfd);
416
+
417
+ i2c_write16_8(vl6180x_addr, 0x00e3, 0x00);
418
+
419
+ i2c_write16_8(vl6180x_addr, 0x00e4, 0x04);
420
+
421
+ i2c_write16_8(vl6180x_addr, 0x00e5, 0x02);
422
+
423
+ i2c_write16_8(vl6180x_addr, 0x00e6, 0x01);
424
+
425
+ i2c_write16_8(vl6180x_addr, 0x00e7, 0x03);
426
+
427
+ i2c_write16_8(vl6180x_addr, 0x00f5, 0x02);
428
+
429
+ i2c_write16_8(vl6180x_addr, 0x00d9, 0x05);
430
+
431
+ i2c_write16_8(vl6180x_addr, 0x00db, 0xce);
432
+
433
+ i2c_write16_8(vl6180x_addr, 0x00dc, 0x03);
434
+
435
+ i2c_write16_8(vl6180x_addr, 0x00dd, 0xf8);
436
+
437
+ i2c_write16_8(vl6180x_addr, 0x009f, 0x00);
438
+
439
+ i2c_write16_8(vl6180x_addr, 0x00a3, 0x3c);
440
+
441
+ i2c_write16_8(vl6180x_addr, 0x00b7, 0x00);
442
+
443
+ i2c_write16_8(vl6180x_addr, 0x00bb, 0x3c);
444
+
445
+ i2c_write16_8(vl6180x_addr, 0x00b2, 0x09);
446
+
447
+ i2c_write16_8(vl6180x_addr, 0x00ca, 0x09);
448
+
449
+ i2c_write16_8(vl6180x_addr, 0x0198, 0x01);
450
+
451
+ i2c_write16_8(vl6180x_addr, 0x01b0, 0x17);
452
+
453
+ i2c_write16_8(vl6180x_addr, 0x01ad, 0x00);
454
+
455
+ i2c_write16_8(vl6180x_addr, 0x00ff, 0x05);
456
+
457
+ i2c_write16_8(vl6180x_addr, 0x0100, 0x05);
458
+
459
+ i2c_write16_8(vl6180x_addr, 0x0199, 0x05);
460
+
461
+ i2c_write16_8(vl6180x_addr, 0x01a6, 0x1b);
462
+
463
+ i2c_write16_8(vl6180x_addr, 0x01ac, 0x3e);
464
+
465
+ i2c_write16_8(vl6180x_addr, 0x01a7, 0x1f);
466
+
467
+ i2c_write16_8(vl6180x_addr, 0x0030, 0x00);
468
+
469
+
470
+
471
+
472
+
473
+ i2c_write16_8(vl6180x_addr, 0x16, 0x00);
474
+
475
+
476
+
477
+ }
474
478
 
475
479
  int main(){
476
480
 
@@ -490,7 +494,9 @@
490
494
 
491
495
  if (ioctl(i2c_fd, I2C_SLAVE, vl6180x_addr) < 0) {
492
496
 
497
+
498
+
493
- perror("ioctl(I2C_RDWR) in i2c_rw");
499
+ perror("ioctl(I2C_SLAVE) in i2c_rw");
494
500
 
495
501
  return -1;
496
502
 
@@ -508,64 +514,66 @@
508
514
 
509
515
  while(1){
510
516
 
517
+
518
+
519
+ //start single-shot mode
520
+
521
+ if((i2c_write16_8(vl6180x_addr, 0x18,0x01)) < 0) {
522
+
523
+ printf("error: power on\n");
524
+
525
+ }
526
+
511
527
 
512
528
 
529
+
530
+
513
- //start single-shot mode
531
+ // wait to {0x4f} register till bit 2 is set to 1
532
+
514
-
533
+ while(1){
534
+
535
+
536
+
515
- if((i2c_write16_8(i2c_fd, 0x18,0x01)) < 0) {
537
+ ret = i2c_read16_8(vl6180x_addr, 0x4f, &data);
538
+
516
-
539
+ usleep(500*1000);
540
+
541
+ printf("data:%02x\n",data);
542
+
543
+
544
+
545
+ if(data == 0x04){
546
+
547
+
548
+
517
- printf("error: power on\n");
549
+ printf("mesurement start\n");
550
+
551
+ break;
552
+
553
+
554
+
555
+ }
518
556
 
519
557
  }
520
558
 
521
559
 
522
560
 
561
+ // get value
562
+
563
+ ret = i2c_read16_8(vl6180x_addr, 0x62, &data);
564
+
523
565
 
524
566
 
525
- // wait to {0x4f} register till bit 2 is set to 1
567
+ // clear interruput status
526
-
527
- while(1){
568
+
528
-
529
-
530
-
531
- ret = i2c_read16_8(i2c_fd, 0x4f, &data);
569
+ i2c_write16_8(vl6180x_addr, 0x15, 0x07);
532
-
533
- printf("ret:%d\n",ret);
534
-
535
-
536
-
537
- if(ret == 0x04){
538
-
539
-
540
-
541
- printf("mesurement start\n");
542
-
543
- break;
544
-
545
-
546
-
547
- }
548
-
549
- }
550
570
 
551
571
 
552
572
 
553
- // get value
554
-
555
- ret = i2c_read16_8(i2c_fd, 0x62, &data);
573
+ printf("distance:%d\n", result);
556
574
 
557
575
 
558
576
 
559
- // clear interruput status
560
-
561
- i2c_write16_8(i2c_fd, 0x15, 0x07);
562
-
563
-
564
-
565
- printf("distance:%d\n", result);
566
-
567
-
568
-
569
577
  }
570
578
 
571
579
 
@@ -580,13 +588,15 @@
580
588
 
581
589
  修正済み
582
590
 
583
- (現在のコードではmain関数のiotclの部分で"ioctl(I2C_RDWR) in i2c_rw: Bad address"というエラーが出ている状態です。)
591
+ (現在のコードではmain関数のiotclの部分で"ioctl(I2C_RDWR) in i2c_rw: Bad address"というエラーが出ている状態です。)
592
+
593
+ ・(i2c_rw()関数の部分で "ioctl(I2C_RDWR) in i2c_rw: Remote I/O error"というエラーが出ています。)
584
594
 
585
595
 
586
596
 
587
597
  現在
588
598
 
589
- i2c_rw()関数の部分で "ioctl(I2C_RDWR) in i2c_rw: Remote I/O error"というエラー出てす。改定のアドバイスよろしくお願いいたします。
599
+ main()関数の" wait to {0x4f} register till bit 2 is set to 1"の部分で常に"data:00"出力され、計測開始されな状態です。改定のアドバイスよろしくお願いいたします。
590
600
 
591
601
 
592
602
 

4

現在のソースコードの変更

2019/09/05 18:20

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -488,7 +488,7 @@
488
488
 
489
489
 
490
490
 
491
- if (ioctl(i2c_fd, I2C_RDWR, vl6180x_addr) < 0) {
491
+ if (ioctl(i2c_fd, I2C_SLAVE, vl6180x_addr) < 0) {
492
492
 
493
493
  perror("ioctl(I2C_RDWR) in i2c_rw");
494
494
 
@@ -578,7 +578,15 @@
578
578
 
579
579
  ```
580
580
 
581
+ 修正済み
582
+
581
- 現在のコードではmain関数のiotclの部分で"ioctl(I2C_RDWR) in i2c_rw: Bad address"というエラーが出ている状態です。改定のアドバイスよろしくお願い致します。
583
+ 現在のコードではmain関数のiotclの部分で"ioctl(I2C_RDWR) in i2c_rw: Bad address"というエラーが出ている状態です。
584
+
585
+
586
+
587
+ 現在
588
+
589
+ i2c_rw()関数の部分で "ioctl(I2C_RDWR) in i2c_rw: Remote I/O error"というエラーが出ています。改定のアドバイスよろしくお願いいたします。
582
590
 
583
591
 
584
592
 

3

コードの若干の修正

2019/09/05 17:12

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -222,7 +222,7 @@
222
222
 
223
223
 
224
224
 
225
- #define vl6180x_addr 0x29; //vl6180xのI2Cアドレス
225
+ #define vl6180x_addr 0x29 //vl6180xのI2Cアドレス
226
226
 
227
227
 
228
228
 

2

現在のコードの状態と作成にあたり参考にしたサイトを載せました。

2019/09/05 16:51

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -582,6 +582,16 @@
582
582
 
583
583
 
584
584
 
585
+ こちらのサイトとデータシートを参考に作成しました。
586
+
587
+ サイト:https://qiita.com/shigeru-yokochi/items/ac2138feb74a7ef7ffc6
588
+
589
+
590
+
591
+ データシート:https://cdn.sparkfun.com/datasheets/Sensors/Proximity/VL6180_ApplicationNote.pdf
592
+
593
+
594
+
585
595
  ### 試したこと
586
596
 
587
597
 

1

現在変更したコードをアップしました。

2019/09/05 16:19

投稿

man_
man_

スコア45

test CHANGED
File without changes
test CHANGED
@@ -196,6 +196,392 @@
196
196
 
197
197
 
198
198
 
199
+ ###現在のコード
200
+
201
+ ```
202
+
203
+ #include <stdio.h>
204
+
205
+ #include <string.h>
206
+
207
+ #include <fcntl.h>
208
+
209
+ #include <unistd.h>
210
+
211
+ #include <sys/ioctl.h>
212
+
213
+
214
+
215
+ #include <linux/i2c-dev.h>
216
+
217
+ #ifndef I2C_M_RD
218
+
219
+ #include <linux/i2c.h>
220
+
221
+ #endif
222
+
223
+
224
+
225
+ #define vl6180x_addr 0x29; //vl6180xのI2Cアドレス
226
+
227
+
228
+
229
+ typedef unsigned char u8;
230
+
231
+ typedef unsigned short u16;
232
+
233
+ typedef unsigned int u32;
234
+
235
+
236
+
237
+ int i2c_fd = -1;
238
+
239
+ const char *i2c_fname = "/dev/i2c-1";
240
+
241
+
242
+
243
+ int i2c_init(void)
244
+
245
+ {
246
+
247
+ if ((i2c_fd = open(i2c_fname, O_RDWR)) < 0) {
248
+
249
+ char err[200];
250
+
251
+ sprintf(err, "open('%s') in i2c_init", i2c_fname);
252
+
253
+ perror(err);
254
+
255
+ return -1;
256
+
257
+ }
258
+
259
+
260
+
261
+ return i2c_fd;
262
+
263
+ }
264
+
265
+
266
+
267
+ void i2c_close(void)
268
+
269
+ {
270
+
271
+ close(i2c_fd);
272
+
273
+ i2c_fd = -1;
274
+
275
+ }
276
+
277
+
278
+
279
+ int i2c_rw(u8 slave_addr, u8 *out, u32 out_len, u8 *in, u32 in_len)
280
+
281
+ {
282
+
283
+ int retval;
284
+
285
+ struct i2c_msg msgs[2];
286
+
287
+ struct i2c_rdwr_ioctl_data msgset;
288
+
289
+
290
+
291
+ msgset.msgs = msgs;
292
+
293
+ msgset.nmsgs = 1;
294
+
295
+
296
+
297
+ msgs[0].addr = slave_addr;
298
+
299
+ msgs[0].flags = 0;
300
+
301
+ msgs[0].len = out_len;
302
+
303
+ msgs[0].buf = out;
304
+
305
+
306
+
307
+ if(in_len > 0U){
308
+
309
+ msgs[1].addr = slave_addr;
310
+
311
+ msgs[1].flags = I2C_M_RD | I2C_M_NOSTART;
312
+
313
+ msgs[1].len = in_len;
314
+
315
+ msgs[1].buf = in;
316
+
317
+
318
+
319
+ msgset.nmsgs = 2;
320
+
321
+ }
322
+
323
+
324
+
325
+ if (ioctl(i2c_fd, I2C_RDWR, &msgset) < 0) {
326
+
327
+ perror("ioctl(I2C_RDWR) in i2c_rw");
328
+
329
+ return -1;
330
+
331
+ }
332
+
333
+
334
+
335
+ return 0;
336
+
337
+ }
338
+
339
+
340
+
341
+ int i2c_write16_8(u8 slave_addr, u16 reg, u8 data)
342
+
343
+ {
344
+
345
+ u8 out[3] = { (u8)(reg >> 8), (u8)reg, data };
346
+
347
+ return i2c_rw(slave_addr, out, sizeof(out), NULL, 0);
348
+
349
+ }
350
+
351
+
352
+
353
+ int i2c_write16_16(u8 slave_addr, u16 reg, u16 data)
354
+
355
+ {
356
+
357
+ u8 out[4] = { (u8)(reg >> 8), (u8)reg, (u8)(data >> 8), (u8)data };
358
+
359
+ return i2c_rw(slave_addr, out, sizeof(out), NULL, 0);
360
+
361
+ }
362
+
363
+
364
+
365
+ int i2c_read16_8(u8 slave_addr, u16 reg, u8 *data)
366
+
367
+ {
368
+
369
+ u8 out[2] = { (u8)(reg >> 8), (u8)reg};
370
+
371
+ return i2c_rw(slave_addr, out, sizeof(out), data, 1);
372
+
373
+ }
374
+
375
+
376
+
377
+ int i2c_read16_16(u8 slave_addr, u16 reg, u16 *data)
378
+
379
+ {
380
+
381
+ u8 out[2] = { (u8)(reg >> 8), (u8)reg};
382
+
383
+ u8 in[2];
384
+
385
+ int retval = i2c_rw(slave_addr, out, sizeof(out), in, sizeof(in));
386
+
387
+ if(retval >= 0){
388
+
389
+ *data = (((u16)in[0]) << 8) | in[1];
390
+
391
+ }
392
+
393
+ return retval;
394
+
395
+ }
396
+
397
+
398
+
399
+ int vl6180x_initialisation(void){
400
+
401
+ //センサを使用するための設定
402
+
403
+ i2c_write16_16(i2c_fd, 0x0207, 0x01);
404
+
405
+ i2c_write16_16(i2c_fd, 0x0208, 0x01);
406
+
407
+ i2c_write16_16(i2c_fd, 0x0096, 0x00);
408
+
409
+ i2c_write16_16(i2c_fd, 0x0097, 0xfd);
410
+
411
+ i2c_write16_16(i2c_fd, 0x00e3, 0x00);
412
+
413
+ i2c_write16_16(i2c_fd, 0x00e4, 0x04);
414
+
415
+ i2c_write16_16(i2c_fd, 0x00e5, 0x02);
416
+
417
+ i2c_write16_16(i2c_fd, 0x00e6, 0x01);
418
+
419
+ i2c_write16_16(i2c_fd, 0x00e7, 0x03);
420
+
421
+ i2c_write16_16(i2c_fd, 0x00f5, 0x02);
422
+
423
+ i2c_write16_16(i2c_fd, 0x00d9, 0x05);
424
+
425
+ i2c_write16_16(i2c_fd, 0x00db, 0xce);
426
+
427
+ i2c_write16_16(i2c_fd, 0x00dc, 0x03);
428
+
429
+ i2c_write16_16(i2c_fd, 0x00dd, 0xf8);
430
+
431
+ i2c_write16_16(i2c_fd, 0x009f, 0x00);
432
+
433
+ i2c_write16_16(i2c_fd, 0x00a3, 0x3c);
434
+
435
+ i2c_write16_16(i2c_fd, 0x00b7, 0x00);
436
+
437
+ i2c_write16_16(i2c_fd, 0x00bb, 0x3c);
438
+
439
+ i2c_write16_16(i2c_fd, 0x00b2, 0x09);
440
+
441
+ i2c_write16_16(i2c_fd, 0x00ca, 0x09);
442
+
443
+ i2c_write16_16(i2c_fd, 0x0198, 0x01);
444
+
445
+ i2c_write16_16(i2c_fd, 0x01b0, 0x17);
446
+
447
+ i2c_write16_16(i2c_fd, 0x01ad, 0x00);
448
+
449
+ i2c_write16_16(i2c_fd, 0x00ff, 0x05);
450
+
451
+ i2c_write16_16(i2c_fd, 0x0100, 0x05);
452
+
453
+ i2c_write16_16(i2c_fd, 0x0199, 0x05);
454
+
455
+ i2c_write16_16(i2c_fd, 0x01a6, 0x1b);
456
+
457
+ i2c_write16_16(i2c_fd, 0x01ac, 0x3e);
458
+
459
+ i2c_write16_16(i2c_fd, 0x01a7, 0x1f);
460
+
461
+ i2c_write16_16(i2c_fd, 0x0030, 0x00);
462
+
463
+
464
+
465
+
466
+
467
+ i2c_write16_8(i2c_fd, 0x16, 0x00);
468
+
469
+
470
+
471
+ }
472
+
473
+
474
+
475
+ int main(){
476
+
477
+
478
+
479
+ int ret, result;
480
+
481
+
482
+
483
+ u8 data;
484
+
485
+
486
+
487
+ i2c_init();
488
+
489
+
490
+
491
+ if (ioctl(i2c_fd, I2C_RDWR, vl6180x_addr) < 0) {
492
+
493
+ perror("ioctl(I2C_RDWR) in i2c_rw");
494
+
495
+ return -1;
496
+
497
+ }
498
+
499
+
500
+
501
+ vl6180x_initialisation();
502
+
503
+
504
+
505
+
506
+
507
+ // repeat rang mesurement
508
+
509
+ while(1){
510
+
511
+
512
+
513
+ //start single-shot mode
514
+
515
+ if((i2c_write16_8(i2c_fd, 0x18,0x01)) < 0) {
516
+
517
+ printf("error: power on\n");
518
+
519
+ }
520
+
521
+
522
+
523
+
524
+
525
+ // wait to {0x4f} register till bit 2 is set to 1
526
+
527
+ while(1){
528
+
529
+
530
+
531
+ ret = i2c_read16_8(i2c_fd, 0x4f, &data);
532
+
533
+ printf("ret:%d\n",ret);
534
+
535
+
536
+
537
+ if(ret == 0x04){
538
+
539
+
540
+
541
+ printf("mesurement start\n");
542
+
543
+ break;
544
+
545
+
546
+
547
+ }
548
+
549
+ }
550
+
551
+
552
+
553
+ // get value
554
+
555
+ ret = i2c_read16_8(i2c_fd, 0x62, &data);
556
+
557
+
558
+
559
+ // clear interruput status
560
+
561
+ i2c_write16_8(i2c_fd, 0x15, 0x07);
562
+
563
+
564
+
565
+ printf("distance:%d\n", result);
566
+
567
+
568
+
569
+ }
570
+
571
+
572
+
573
+ i2c_close();
574
+
575
+ }
576
+
577
+
578
+
579
+ ```
580
+
581
+ 現在のコードではmain関数のiotclの部分で"ioctl(I2C_RDWR) in i2c_rw: Bad address"というエラーが出ている状態です。改定のアドバイスよろしくお願い致します。
582
+
583
+
584
+
199
585
  ### 試したこと
200
586
 
201
587