Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of circle_war_ver_A_NUCLEO_ by
spi_nucleo.h@14:3403ce49a37a, 2016-04-05 (annotated)
- Committer:
- baba2357
- Date:
- Tue Apr 05 17:25:09 2016 +0000
- Revision:
- 14:3403ce49a37a
- Parent:
- 8:554e3d643b2d
??????;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
baba2357 | 8:554e3d643b2d | 1 | #include "mbed.h" |
baba2357 | 8:554e3d643b2d | 2 | #include "pin_file.h" |
baba2357 | 8:554e3d643b2d | 3 | #include <string.h> |
baba2357 | 1:86d25c1c4bd5 | 4 | #ifndef SPI_NUCLEO_H |
baba2357 | 1:86d25c1c4bd5 | 5 | #define SPI_NUCLEO_H |
baba2357 | 14:3403ce49a37a | 6 | //SPI mbed |
baba2357 | 14:3403ce49a37a | 7 | SPISlave from_mbed(PA_7, PA_6, PA_5, PA_4); |
baba2357 | 14:3403ce49a37a | 8 | //SPI NUCLEO |
baba2357 | 14:3403ce49a37a | 9 | SPISlave from_gyro(PB_15, PB_14, PB_13, PB_12); |
baba2357 | 1:86d25c1c4bd5 | 10 | //SPI mbed |
baba2357 | 8:554e3d643b2d | 11 | bool RR=0,LR=0,Xpm=0,cilinder=0,onoff=0,updown=0,Ypm=0; |
baba2357 | 8:554e3d643b2d | 12 | int speed_X,speed_Y; |
baba2357 | 1:86d25c1c4bd5 | 13 | //SPI NUCLEO |
baba2357 | 1:86d25c1c4bd5 | 14 | void spi_mbed(); |
baba2357 | 1:86d25c1c4bd5 | 15 | void spi_nucleo(); |
baba2357 | 2:bed6b204e64c | 16 | void getGyro(); |
baba2357 | 2:bed6b204e64c | 17 | void spiInit(); |
baba2357 | 4:ef6f18eda3e2 | 18 | typedef unsigned u16; |
baba2357 | 4:ef6f18eda3e2 | 19 | u16 FM; |
baba2357 | 2:bed6b204e64c | 20 | long int angle; |
baba2357 | 2:bed6b204e64c | 21 | double now_angle; |
baba2357 | 2:bed6b204e64c | 22 | Serial pc(USBTX,USBRX); |
baba2357 | 4:ef6f18eda3e2 | 23 | void spiInit(); |
baba2357 | 4:ef6f18eda3e2 | 24 | void spi_mbed(); |
baba2357 | 4:ef6f18eda3e2 | 25 | void getGyro(); |
baba2357 | 8:554e3d643b2d | 26 | #endif |
baba2357 | 4:ef6f18eda3e2 | 27 | |
baba2357 | 4:ef6f18eda3e2 | 28 | void spiInit() |
baba2357 | 4:ef6f18eda3e2 | 29 | { |
baba2357 | 1:86d25c1c4bd5 | 30 | from_mbed.format(16,3); |
baba2357 | 1:86d25c1c4bd5 | 31 | from_gyro.format(16,3); |
baba2357 | 1:86d25c1c4bd5 | 32 | from_mbed.frequency(1000000); |
baba2357 | 1:86d25c1c4bd5 | 33 | from_gyro.frequency(1000000); |
baba2357 | 1:86d25c1c4bd5 | 34 | } |
baba2357 | 2:bed6b204e64c | 35 | |
baba2357 | 3:6ab4e8e3aa02 | 36 | void spi_mbed() |
baba2357 | 4:ef6f18eda3e2 | 37 | { |
baba2357 | 3:6ab4e8e3aa02 | 38 | if(from_mbed.receive()) { |
baba2357 | 4:ef6f18eda3e2 | 39 | RR=0; |
baba2357 | 4:ef6f18eda3e2 | 40 | LR=0; |
baba2357 | 3:6ab4e8e3aa02 | 41 | FM=from_mbed.read(); |
baba2357 | 3:6ab4e8e3aa02 | 42 | Xpm=FM>>13; |
baba2357 | 8:554e3d643b2d | 43 | Ypm=(FM&0x10)>>4; |
baba2357 | 8:554e3d643b2d | 44 | cilinder=(FM&0x0100)>>8; |
baba2357 | 8:554e3d643b2d | 45 | onoff=(FM&0x0080)>>7; |
baba2357 | 8:554e3d643b2d | 46 | updown=(FM&0x0040)>>6; |
baba2357 | 8:554e3d643b2d | 47 | if((FM>>15)==1) { |
baba2357 | 4:ef6f18eda3e2 | 48 | RR=1; |
baba2357 | 3:6ab4e8e3aa02 | 49 | } //右旋回の関数へ |
baba2357 | 8:554e3d643b2d | 50 | else if((FM>>14)==1) { |
baba2357 | 4:ef6f18eda3e2 | 51 | LR=1; |
baba2357 | 3:6ab4e8e3aa02 | 52 | } //左旋回の関数へ |
baba2357 | 3:6ab4e8e3aa02 | 53 | else if(Xpm==1) { //コントローラーのX方向成分入力 |
baba2357 | 8:554e3d643b2d | 54 | speed_X=FM&0x1e00; |
baba2357 | 8:554e3d643b2d | 55 | speed_X=speed_X>>9; |
baba2357 | 8:554e3d643b2d | 56 | if(Ypm==0) speed_Y=FM&0xf; |
baba2357 | 3:6ab4e8e3aa02 | 57 | else { |
baba2357 | 8:554e3d643b2d | 58 | speed_Y=FM&0xf; |
baba2357 | 4:ef6f18eda3e2 | 59 | speed_Y=(-1)*speed_Y; |
baba2357 | 3:6ab4e8e3aa02 | 60 | } |
baba2357 | 3:6ab4e8e3aa02 | 61 | } |
baba2357 | 3:6ab4e8e3aa02 | 62 | else { |
baba2357 | 8:554e3d643b2d | 63 | speed_X=FM&0x1e00; |
baba2357 | 8:554e3d643b2d | 64 | speed_X=speed_X>>9; |
baba2357 | 3:6ab4e8e3aa02 | 65 | speed_X=(-1)*speed_X; |
baba2357 | 8:554e3d643b2d | 66 | if(Ypm==0) speed_Y=FM&0xf; |
baba2357 | 3:6ab4e8e3aa02 | 67 | else { |
baba2357 | 8:554e3d643b2d | 68 | speed_Y=FM&0xf; |
baba2357 | 4:ef6f18eda3e2 | 69 | speed_Y=(-1)*speed_Y; |
baba2357 | 2:bed6b204e64c | 70 | } |
baba2357 | 1:86d25c1c4bd5 | 71 | } |
baba2357 | 2:bed6b204e64c | 72 | } |
baba2357 | 3:6ab4e8e3aa02 | 73 | } |
baba2357 | 4:ef6f18eda3e2 | 74 | |
baba2357 | 4:ef6f18eda3e2 | 75 | void getGyro() |
baba2357 | 4:ef6f18eda3e2 | 76 | { |
baba2357 | 4:ef6f18eda3e2 | 77 | int data; |
baba2357 | 4:ef6f18eda3e2 | 78 | if(from_gyro.receive()) { |
baba2357 | 2:bed6b204e64c | 79 | data = from_gyro.read(); |
baba2357 | 4:ef6f18eda3e2 | 80 | if(data >> 15) angle = -(data & 0x7fff); |
baba2357 | 1:86d25c1c4bd5 | 81 | else angle = data & 0x7fff; |
baba2357 | 1:86d25c1c4bd5 | 82 | now_angle = (double)angle / 10; |
baba2357 | 4:ef6f18eda3e2 | 83 | // pc.printf("%d\r\n",data); |
baba2357 | 1:86d25c1c4bd5 | 84 | } |
baba2357 | 4:ef6f18eda3e2 | 85 | } |