r_test1_2017_10_11_Wed_A

Dependencies:   QEI mbed

Committer:
shobonwarrior
Date:
Wed Oct 11 08:14:54 2017 +0000
Revision:
0:996353c69d55
r_test1_2017_10_11_Wed_A

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shobonwarrior 0:996353c69d55 1 /*
shobonwarrior 0:996353c69d55 2 *
shobonwarrior 0:996353c69d55 3 *
shobonwarrior 0:996353c69d55 4 *
shobonwarrior 0:996353c69d55 5 * PS3をSBDBTで使うときにでも使ってください(mbed LPC1768用、UART使用)
shobonwarrior 0:996353c69d55 6 *
shobonwarrior 0:996353c69d55 7 * =====使い方=====
shobonwarrior 0:996353c69d55 8 * 動作クロックは20MHz(それ以外でやる時は通信速度が2500bpsになるように調整してちょ)
shobonwarrior 0:996353c69d55 9 * まずPS3DefaultSetを一回実行してください。USARTレジスタの設定と構造体の初期化を行います。
shobonwarrior 0:996353c69d55 10 *
shobonwarrior 0:996353c69d55 11 * メインループでCheckInputData関数を一回実行してください。ボタンからのデータがpsdata配列(グローバル)に格納されます。
shobonwarrior 0:996353c69d55 12 * if文の条件式に、BTTRUE(***)またはBTFALSE(***)をぶち込んでください。
shobonwarrior 0:996353c69d55 13 * ***には各ボタンに対応した構造体の名前が入ります。
shobonwarrior 0:996353c69d55 14 *
shobonwarrior 0:996353c69d55 15 * (例)□ボタンが押されたときに処理したいとき
shobonwarrior 0:996353c69d55 16 * if(BTTRUE(SQUARE)){
shobonwarrior 0:996353c69d55 17 * 処理;
shobonwarrior 0:996353c69d55 18 * }
shobonwarrior 0:996353c69d55 19 *
shobonwarrior 0:996353c69d55 20 *
shobonwarrior 0:996353c69d55 21 * スティックに関しては、psdata配列から該当するバイトを丸ごと読んでください。
shobonwarrior 0:996353c69d55 22 * 64がニュートラル、0が下端、127が上端です。
shobonwarrior 0:996353c69d55 23 *
shobonwarrior 0:996353c69d55 24 * (例)右スティック縦軸が半分以上前に倒されている時に処理したいとき
shobonwarrior 0:996353c69d55 25 * if(RIGHT_VERTICAL > 80){
shobonwarrior 0:996353c69d55 26 * 処理;
shobonwarrior 0:996353c69d55 27 * }
shobonwarrior 0:996353c69d55 28 *
shobonwarrior 0:996353c69d55 29 * BTTOGGE関数は「ボタンが押されたら一回だけ実行したい処理」を処理するときに使ってください。
shobonwarrior 0:996353c69d55 30 * 押されるたびに変形するときとか
shobonwarrior 0:996353c69d55 31 *
shobonwarrior 0:996353c69d55 32 *
shobonwarrior 0:996353c69d55 33 */
shobonwarrior 0:996353c69d55 34
shobonwarrior 0:996353c69d55 35 /*
shobonwarrior 0:996353c69d55 36 * ボタン対応一覧
shobonwarrior 0:996353c69d55 37 *
shobonwarrior 0:996353c69d55 38 * ○ボタン CIRCLE
shobonwarrior 0:996353c69d55 39 * ×ボタン CROSS
shobonwarrior 0:996353c69d55 40 * □ボタン SQUARE
shobonwarrior 0:996353c69d55 41 * △ボタン TRIANGLE
shobonwarrior 0:996353c69d55 42 * 十字キー上 UP
shobonwarrior 0:996353c69d55 43 * 十字キー下 DOWN
shobonwarrior 0:996353c69d55 44 * 十字キー右 RIGHT
shobonwarrior 0:996353c69d55 45 * 十字キー左 LEFT
shobonwarrior 0:996353c69d55 46 * L1ボタン L1
shobonwarrior 0:996353c69d55 47 * L2ボタン L2
shobonwarrior 0:996353c69d55 48 * R1ボタン R1
shobonwarrior 0:996353c69d55 49 * R2ボタン R2
shobonwarrior 0:996353c69d55 50 * 右スティック縦軸 RIGHT_VERTICAL
shobonwarrior 0:996353c69d55 51 * 右スティック横軸 RIGHT_WYDE
shobonwarrior 0:996353c69d55 52 * 左スティック縦軸 LEFT_VERTICAL
shobonwarrior 0:996353c69d55 53 * 左スティック横軸 LEFT_WYDE
shobonwarrior 0:996353c69d55 54 *
shobonwarrior 0:996353c69d55 55 */
shobonwarrior 0:996353c69d55 56
shobonwarrior 0:996353c69d55 57
shobonwarrior 0:996353c69d55 58
shobonwarrior 0:996353c69d55 59 /************************************************************************/
shobonwarrior 0:996353c69d55 60 /* 構造体宣言 */
shobonwarrior 0:996353c69d55 61 /************************************************************************/
shobonwarrior 0:996353c69d55 62 typedef struct{
shobonwarrior 0:996353c69d55 63 int storebyte; //何バイト目か
shobonwarrior 0:996353c69d55 64 uint8_t data; //データ
shobonwarrior 0:996353c69d55 65 int toggleflag; //BTTOGGLEで使うフラグ
shobonwarrior 0:996353c69d55 66 } ps3;
shobonwarrior 0:996353c69d55 67
shobonwarrior 0:996353c69d55 68
shobonwarrior 0:996353c69d55 69 /************************************************************************/
shobonwarrior 0:996353c69d55 70 /* define スティック(何バイト目か) */
shobonwarrior 0:996353c69d55 71 /************************************************************************/
shobonwarrior 0:996353c69d55 72 #define LEFT_WYDE psdata[3] //左横軸
shobonwarrior 0:996353c69d55 73 #define LEFT_VERTICAL psdata[4] //左縦軸
shobonwarrior 0:996353c69d55 74 #define RIGHT_WYDE psdata[5] //右横軸
shobonwarrior 0:996353c69d55 75 #define RIGHT_VERTICAL psdata[6] //右縦軸
shobonwarrior 0:996353c69d55 76
shobonwarrior 0:996353c69d55 77 /************************************************************************/
shobonwarrior 0:996353c69d55 78 /* define 処理 */
shobonwarrior 0:996353c69d55 79 /************************************************************************/
shobonwarrior 0:996353c69d55 80 #define BTTRUE(Target) CheckBT(Target) == 1 //Targetで指定されたボタンが押されているか判定するifの条件式
shobonwarrior 0:996353c69d55 81 #define BTFALSE(Target) CheckBT(Target) == 0
shobonwarrior 0:996353c69d55 82 #define BTTOGGLE(Target) ToggleBT(&Target) == 1 //Targetが押されたら一回だけ実行する
shobonwarrior 0:996353c69d55 83
shobonwarrior 0:996353c69d55 84 /************************************************************************/
shobonwarrior 0:996353c69d55 85 /* define その他 */
shobonwarrior 0:996353c69d55 86 /************************************************************************/
shobonwarrior 0:996353c69d55 87 #define ERROR 1 //受信エラー
shobonwarrior 0:996353c69d55 88 #define SUCCESS 0 //正常に受信
shobonwarrior 0:996353c69d55 89
shobonwarrior 0:996353c69d55 90 /************************************************************************/
shobonwarrior 0:996353c69d55 91 /* グローバル変数宣言 */
shobonwarrior 0:996353c69d55 92 /************************************************************************/
shobonwarrior 0:996353c69d55 93 volatile uint8_t psdata[8]; //コントローラーからのデータを格納
shobonwarrior 0:996353c69d55 94 ps3 SQUARE,TRIANGLE,CIRCLE,CROSS,UP,DOWN,LEFT,RIGHT,L1,L2,R1,R2; //各ボタンに対応したデータが入ってる構造体×12
shobonwarrior 0:996353c69d55 95
shobonwarrior 0:996353c69d55 96 /************************************************************************/
shobonwarrior 0:996353c69d55 97 /* プロトタイプ宣言 */
shobonwarrior 0:996353c69d55 98 /************************************************************************/
shobonwarrior 0:996353c69d55 99 int CheckBT(ps3); //ボタン入力判定
shobonwarrior 0:996353c69d55 100 void CheckInputData(void); //受信用関数
shobonwarrior 0:996353c69d55 101 int ToggleBT(ps3 *TargetBT); //ボタンが押されたら一回だけ1を返す関数
shobonwarrior 0:996353c69d55 102 void PS3DefaultSet(void); //設定初期化
shobonwarrior 0:996353c69d55 103 int CheckError(void); //エラーチェック
shobonwarrior 0:996353c69d55 104
shobonwarrior 0:996353c69d55 105 /************************************************************************/
shobonwarrior 0:996353c69d55 106 /* レジスタ初期設定関数 */
shobonwarrior 0:996353c69d55 107 /************************************************************************/
shobonwarrior 0:996353c69d55 108
shobonwarrior 0:996353c69d55 109 Serial ps3com(p28,p27); //シリアルピン27,28使用
shobonwarrior 0:996353c69d55 110 void PS3DefaultSet(void){
shobonwarrior 0:996353c69d55 111 /*
shobonwarrior 0:996353c69d55 112 *レジスタの初期設定を行う関数
shobonwarrior 0:996353c69d55 113 *プログラムの最初に一回実行してください
shobonwarrior 0:996353c69d55 114 *mbed LPC以外を使う場合や20MHz以外の動作クロックでやる場合はこの関数をいじってください
shobonwarrior 0:996353c69d55 115 */
shobonwarrior 0:996353c69d55 116
shobonwarrior 0:996353c69d55 117 /**********UARTレジスタ設定**********/
shobonwarrior 0:996353c69d55 118
shobonwarrior 0:996353c69d55 119 //受信完了割り込み禁止、送信割り込み禁止、受信許可、送信禁止
shobonwarrior 0:996353c69d55 120 ps3com.format(8,Serial::None,1);
shobonwarrior 0:996353c69d55 121 //UART、パリティなし、停止ビット1、データビット8
shobonwarrior 0:996353c69d55 122 ps3com.baud(2400);//通信速度2400bps
shobonwarrior 0:996353c69d55 123
shobonwarrior 0:996353c69d55 124 /**********構造体の初期化**********/
shobonwarrior 0:996353c69d55 125
shobonwarrior 0:996353c69d55 126 SQUARE.storebyte = 1;
shobonwarrior 0:996353c69d55 127 TRIANGLE.storebyte = 2;
shobonwarrior 0:996353c69d55 128 CIRCLE.storebyte = 2;
shobonwarrior 0:996353c69d55 129 CROSS.storebyte = 2;
shobonwarrior 0:996353c69d55 130 UP.storebyte = 2;
shobonwarrior 0:996353c69d55 131 DOWN.storebyte = 2;
shobonwarrior 0:996353c69d55 132 LEFT.storebyte = 2;
shobonwarrior 0:996353c69d55 133 RIGHT.storebyte = 2;
shobonwarrior 0:996353c69d55 134 L1.storebyte = 1;
shobonwarrior 0:996353c69d55 135 L2.storebyte = 1;
shobonwarrior 0:996353c69d55 136 R1.storebyte = 1;
shobonwarrior 0:996353c69d55 137 R2.storebyte = 1;
shobonwarrior 0:996353c69d55 138
shobonwarrior 0:996353c69d55 139 UP.data = 0b00000001; //十字キー上
shobonwarrior 0:996353c69d55 140 DOWN.data = 0b00000010; //十字キー下
shobonwarrior 0:996353c69d55 141 RIGHT.data = 0b00000100; //十字キー右
shobonwarrior 0:996353c69d55 142 LEFT.data = 0b00001000; //十字キー左
shobonwarrior 0:996353c69d55 143 TRIANGLE.data = 0b00010000; //△ボタン
shobonwarrior 0:996353c69d55 144 CROSS.data = 0b00100000; //×ボタン
shobonwarrior 0:996353c69d55 145 CIRCLE.data = 0b01000000; //○ボタン
shobonwarrior 0:996353c69d55 146 SQUARE.data = 0b00000001; //□ボタン
shobonwarrior 0:996353c69d55 147 L1.data = 0b00000010; //L1ボタン
shobonwarrior 0:996353c69d55 148 L2.data = 0b00000100; //L2ボタン
shobonwarrior 0:996353c69d55 149 R1.data = 0b00001000; //R1ボタン
shobonwarrior 0:996353c69d55 150 R2.data = 0b00010000; //R2ボタン
shobonwarrior 0:996353c69d55 151
shobonwarrior 0:996353c69d55 152 SQUARE.toggleflag = 0;
shobonwarrior 0:996353c69d55 153 TRIANGLE.toggleflag = 0;
shobonwarrior 0:996353c69d55 154 CIRCLE.toggleflag = 0;
shobonwarrior 0:996353c69d55 155 CROSS.toggleflag = 0;
shobonwarrior 0:996353c69d55 156 UP.toggleflag = 0;
shobonwarrior 0:996353c69d55 157 DOWN.toggleflag = 0;
shobonwarrior 0:996353c69d55 158 LEFT.toggleflag = 0;
shobonwarrior 0:996353c69d55 159 RIGHT.toggleflag = 0;
shobonwarrior 0:996353c69d55 160 L1.toggleflag = 0;
shobonwarrior 0:996353c69d55 161 L2.toggleflag = 0;
shobonwarrior 0:996353c69d55 162 R1.toggleflag = 0;
shobonwarrior 0:996353c69d55 163 R2.toggleflag = 0;
shobonwarrior 0:996353c69d55 164
shobonwarrior 0:996353c69d55 165 }
shobonwarrior 0:996353c69d55 166
shobonwarrior 0:996353c69d55 167
shobonwarrior 0:996353c69d55 168 /************************************************************************/
shobonwarrior 0:996353c69d55 169 /* ボタンチェック関数 */
shobonwarrior 0:996353c69d55 170 /************************************************************************/
shobonwarrior 0:996353c69d55 171 int CheckBT(ps3 TargetBT){ //構造体でデータぶっこむ
shobonwarrior 0:996353c69d55 172
shobonwarrior 0:996353c69d55 173 /*****指定されたボタンが押されているか判定して0か1を返す関数*****/
shobonwarrior 0:996353c69d55 174
shobonwarrior 0:996353c69d55 175 uint8_t ConpareData;
shobonwarrior 0:996353c69d55 176
shobonwarrior 0:996353c69d55 177 ConpareData = psdata[TargetBT.storebyte] & TargetBT.data;
shobonwarrior 0:996353c69d55 178
shobonwarrior 0:996353c69d55 179 if(ConpareData == TargetBT.data){
shobonwarrior 0:996353c69d55 180 return 1; //押されていたら
shobonwarrior 0:996353c69d55 181 }else{
shobonwarrior 0:996353c69d55 182 return 0; //押されていなかったら
shobonwarrior 0:996353c69d55 183 }
shobonwarrior 0:996353c69d55 184
shobonwarrior 0:996353c69d55 185 }
shobonwarrior 0:996353c69d55 186
shobonwarrior 0:996353c69d55 187
shobonwarrior 0:996353c69d55 188 /************************************************************************/
shobonwarrior 0:996353c69d55 189 /* ボタントグル判定関数 */
shobonwarrior 0:996353c69d55 190 /************************************************************************/
shobonwarrior 0:996353c69d55 191 int ToggleBT(ps3 *TargetBT){
shobonwarrior 0:996353c69d55 192
shobonwarrior 0:996353c69d55 193 if(BTTRUE(*TargetBT)){
shobonwarrior 0:996353c69d55 194 if(TargetBT->toggleflag == 0){
shobonwarrior 0:996353c69d55 195 TargetBT->toggleflag = 1;
shobonwarrior 0:996353c69d55 196 return 1;
shobonwarrior 0:996353c69d55 197 }
shobonwarrior 0:996353c69d55 198
shobonwarrior 0:996353c69d55 199 }else{
shobonwarrior 0:996353c69d55 200 TargetBT->toggleflag = 0;
shobonwarrior 0:996353c69d55 201 }
shobonwarrior 0:996353c69d55 202
shobonwarrior 0:996353c69d55 203 return 0;
shobonwarrior 0:996353c69d55 204 }
shobonwarrior 0:996353c69d55 205
shobonwarrior 0:996353c69d55 206 /************************************************************************/
shobonwarrior 0:996353c69d55 207 /* 受信データ処理関数 */
shobonwarrior 0:996353c69d55 208 /************************************************************************/
shobonwarrior 0:996353c69d55 209 void CheckInputData(){
shobonwarrior 0:996353c69d55 210 //ps3からのデータをpsdata配列に格納する関数
shobonwarrior 0:996353c69d55 211 int i;
shobonwarrior 0:996353c69d55 212 int flg;
shobonwarrior 0:996353c69d55 213
shobonwarrior 0:996353c69d55 214
shobonwarrior 0:996353c69d55 215
shobonwarrior 0:996353c69d55 216 // do{
shobonwarrior 0:996353c69d55 217 while(LPC_UART2->RBR!= 0x80); //第一バイトが来るまで待つ
shobonwarrior 0:996353c69d55 218
shobonwarrior 0:996353c69d55 219 for(i=0;i<=7;i++){
shobonwarrior 0:996353c69d55 220 psdata[i] =LPC_UART2->RBR;
shobonwarrior 0:996353c69d55 221 flg=LPC_UART2->LSR & 0x01;
shobonwarrior 0:996353c69d55 222 while(flg!=0x01){
shobonwarrior 0:996353c69d55 223 flg=LPC_UART2->LSR & 0x01;
shobonwarrior 0:996353c69d55 224 } //受信完了フラグが立つまで待つ
shobonwarrior 0:996353c69d55 225 }
shobonwarrior 0:996353c69d55 226
shobonwarrior 0:996353c69d55 227
shobonwarrior 0:996353c69d55 228 // chechsum = CheckError(); //エラーチェック
shobonwarrior 0:996353c69d55 229 //}while(chechsum == ERROR); //受信失敗したらリトライ
shobonwarrior 0:996353c69d55 230 }