![](/media/cache/profiles/8da31baaf4d91041f5182b073699ec89.jpg.50x50_q85.jpg)
うごく
main.cpp@0:9a0a89ffaa43, 2015-05-08 (annotated)
- Committer:
- maxonK
- Date:
- Fri May 08 15:44:13 2015 +0000
- Revision:
- 0:9a0a89ffaa43
- Child:
- 1:f50a49407c60
????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maxonK | 0:9a0a89ffaa43 | 1 | |
maxonK | 0:9a0a89ffaa43 | 2 | #include <mbed.h> |
maxonK | 0:9a0a89ffaa43 | 3 | #include <stdlib.h> |
maxonK | 0:9a0a89ffaa43 | 4 | #include <stdio.h> |
maxonK | 0:9a0a89ffaa43 | 5 | #include <string.h> |
maxonK | 0:9a0a89ffaa43 | 6 | |
maxonK | 0:9a0a89ffaa43 | 7 | //文字列受信関数プロトタイプ宣言 |
maxonK | 0:9a0a89ffaa43 | 8 | int stringReceive(); |
maxonK | 0:9a0a89ffaa43 | 9 | //float cramp関数プロトタイプ宣言 |
maxonK | 0:9a0a89ffaa43 | 10 | int cramp(int,int,int); |
maxonK | 0:9a0a89ffaa43 | 11 | //serialハードウェアバッファクリア関数プロトタイプ宣言 |
maxonK | 0:9a0a89ffaa43 | 12 | void bufferClear (); |
maxonK | 0:9a0a89ffaa43 | 13 | |
maxonK | 0:9a0a89ffaa43 | 14 | //dp4pwm初期化関数プロトタイプ宣言 |
maxonK | 0:9a0a89ffaa43 | 15 | void CT32B0_MAT3_PWM_registerSet(unsigned int ); |
maxonK | 0:9a0a89ffaa43 | 16 | //グローバル変数宣言 |
maxonK | 0:9a0a89ffaa43 | 17 | //dp4pwmDuty |
maxonK | 0:9a0a89ffaa43 | 18 | unsigned int dp4pwmDuty = 0; |
maxonK | 0:9a0a89ffaa43 | 19 | //dp4pwmFrequency |
maxonK | 0:9a0a89ffaa43 | 20 | unsigned int dp4pwmFrequency = 0; |
maxonK | 0:9a0a89ffaa43 | 21 | |
maxonK | 0:9a0a89ffaa43 | 22 | |
maxonK | 0:9a0a89ffaa43 | 23 | DigitalOut myled(LED1); |
maxonK | 0:9a0a89ffaa43 | 24 | |
maxonK | 0:9a0a89ffaa43 | 25 | //シリアル初期化 |
maxonK | 0:9a0a89ffaa43 | 26 | Serial sensSerial (dp16, dp15); |
maxonK | 0:9a0a89ffaa43 | 27 | /* |
maxonK | 0:9a0a89ffaa43 | 28 | //I2C初期化 |
maxonK | 0:9a0a89ffaa43 | 29 | I2C i2c(dp5 , dp27 ); //sda scl |
maxonK | 0:9a0a89ffaa43 | 30 | const int addr = 0x20 << 1; // I2C address |
maxonK | 0:9a0a89ffaa43 | 31 | */ |
maxonK | 0:9a0a89ffaa43 | 32 | |
maxonK | 0:9a0a89ffaa43 | 33 | //割り込みハンドラ(PICでいうISRのこと) |
maxonK | 0:9a0a89ffaa43 | 34 | void TIMER32_0_IRQHandler(void) |
maxonK | 0:9a0a89ffaa43 | 35 | { |
maxonK | 0:9a0a89ffaa43 | 36 | //タイマー操作 |
maxonK | 0:9a0a89ffaa43 | 37 | //タイマリセット→保持 |
maxonK | 0:9a0a89ffaa43 | 38 | //タイマ制御レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 39 | volatile unsigned int *TMR32B0TCRLocal = (unsigned int *)0x40014004; |
maxonK | 0:9a0a89ffaa43 | 40 | //1ビット目を1にしてタイマリセット |
maxonK | 0:9a0a89ffaa43 | 41 | unsigned int mask_TMR32B0TCR = 1 << 1; |
maxonK | 0:9a0a89ffaa43 | 42 | //タイマ制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 43 | *TMR32B0TCRLocal |= mask_TMR32B0TCR; |
maxonK | 0:9a0a89ffaa43 | 44 | /***********************************************************************************/ |
maxonK | 0:9a0a89ffaa43 | 45 | //マッチレジスタセット |
maxonK | 0:9a0a89ffaa43 | 46 | //新しくセットする値はグローバルで宣言しとく(割り込みハンドラは、引数、戻り値ともに意味を成さない) |
maxonK | 0:9a0a89ffaa43 | 47 | //0~1023もらって0%~100%のpwm出力する |
maxonK | 0:9a0a89ffaa43 | 48 | //CT32B0_MAT3に対応したマッチレジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 49 | volatile unsigned int *TMR32B0MR3 = (unsigned int *)(0x40014024); |
maxonK | 0:9a0a89ffaa43 | 50 | //デューティセット |
maxonK | 0:9a0a89ffaa43 | 51 | *TMR32B0MR3 = ((((48000000 / dp4pwmFrequency)/20)*18)+((((48000000 / dp4pwmFrequency)/20)/1024) * (1024 - dp4pwmDuty)));//停波(18ms)+((1ms/1024)*(1024-Duty)) |
maxonK | 0:9a0a89ffaa43 | 52 | //*TMR32B0MR2 = (((48000000 / dp24pwmFrequency) / 1024)*(1024 - dp24pwmDuty)); |
maxonK | 0:9a0a89ffaa43 | 53 | /*******************************************************************************************/ |
maxonK | 0:9a0a89ffaa43 | 54 | |
maxonK | 0:9a0a89ffaa43 | 55 | //タイマリセット解除 |
maxonK | 0:9a0a89ffaa43 | 56 | //タイマ制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 57 | *TMR32B0TCRLocal &= ~mask_TMR32B0TCR; |
maxonK | 0:9a0a89ffaa43 | 58 | /*******************************************************************************************/ |
maxonK | 0:9a0a89ffaa43 | 59 | |
maxonK | 0:9a0a89ffaa43 | 60 | //マッチチャネル0 用の割り込みフラグリセット |
maxonK | 0:9a0a89ffaa43 | 61 | //割り込みレジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 62 | volatile unsigned int *TMR32B0IRLocal = (unsigned int *)0x40014000; |
maxonK | 0:9a0a89ffaa43 | 63 | //0ビット目を1にしてMR0の割り込みフラグをリセット |
maxonK | 0:9a0a89ffaa43 | 64 | unsigned int mask_TMR32B0IR = 1; |
maxonK | 0:9a0a89ffaa43 | 65 | //割り込みレジスタセット |
maxonK | 0:9a0a89ffaa43 | 66 | *TMR32B0IRLocal |= mask_TMR32B0IR; |
maxonK | 0:9a0a89ffaa43 | 67 | |
maxonK | 0:9a0a89ffaa43 | 68 | } |
maxonK | 0:9a0a89ffaa43 | 69 | |
maxonK | 0:9a0a89ffaa43 | 70 | |
maxonK | 0:9a0a89ffaa43 | 71 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
maxonK | 0:9a0a89ffaa43 | 72 | /*****************************************************************************************************************/ |
maxonK | 0:9a0a89ffaa43 | 73 | |
maxonK | 0:9a0a89ffaa43 | 74 | int main() |
maxonK | 0:9a0a89ffaa43 | 75 | { |
maxonK | 0:9a0a89ffaa43 | 76 | //setup |
maxonK | 0:9a0a89ffaa43 | 77 | sensSerial.baud(9600);//シリアル通信開く |
maxonK | 0:9a0a89ffaa43 | 78 | //(switchmbed基板でUSB通信するときは低速に抑えること) |
maxonK | 0:9a0a89ffaa43 | 79 | myled = 0; |
maxonK | 0:9a0a89ffaa43 | 80 | |
maxonK | 0:9a0a89ffaa43 | 81 | //割り込み設定 |
maxonK | 0:9a0a89ffaa43 | 82 | //Interrupt Service Routineが書いてある、割り込み時ジャンプ先アドレス設定? |
maxonK | 0:9a0a89ffaa43 | 83 | NVIC_SetVector(TIMER_32_0_IRQn, (uint32_t)&TIMER32_0_IRQHandler); |
maxonK | 0:9a0a89ffaa43 | 84 | //割り込みハンドライネーブル |
maxonK | 0:9a0a89ffaa43 | 85 | NVIC_EnableIRQ(TIMER_32_0_IRQn); |
maxonK | 0:9a0a89ffaa43 | 86 | //割り込み優先順位設定 |
maxonK | 0:9a0a89ffaa43 | 87 | NVIC_SetPriority(TIMER_32_0_IRQn, 1); |
maxonK | 0:9a0a89ffaa43 | 88 | //PWM周波数 |
maxonK | 0:9a0a89ffaa43 | 89 | dp4pwmFrequency = 50; |
maxonK | 0:9a0a89ffaa43 | 90 | //dp4pwm初期化 |
maxonK | 0:9a0a89ffaa43 | 91 | CT32B0_MAT3_PWM_registerSet(dp4pwmFrequency); |
maxonK | 0:9a0a89ffaa43 | 92 | |
maxonK | 0:9a0a89ffaa43 | 93 | //ESCのセンター感知のため中立サーボ信号を5秒間出力 |
maxonK | 0:9a0a89ffaa43 | 94 | //pwm関係 |
maxonK | 0:9a0a89ffaa43 | 95 | //グローバルdp4pwmDutyセット |
maxonK | 0:9a0a89ffaa43 | 96 | dp4pwmDuty = (1024 / 2); |
maxonK | 0:9a0a89ffaa43 | 97 | //pwm周波数設定用マッチレジスタの動作をタイマークリアから割り込み発生へ変更 |
maxonK | 0:9a0a89ffaa43 | 98 | //マッチ制御レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 99 | volatile unsigned int *TMR32B0MCR = (unsigned int *)(0x40014014); |
maxonK | 0:9a0a89ffaa43 | 100 | //マッチ制御レジスタマスク |
maxonK | 0:9a0a89ffaa43 | 101 | //マッチレジスタMR0とのマッチで割り込みが発生するように機能切り替え//マッチでのクリアは停止される |
maxonK | 0:9a0a89ffaa43 | 102 | unsigned int mask_TMR32B0MCR = 1; |
maxonK | 0:9a0a89ffaa43 | 103 | //マッチ制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 104 | *TMR32B0MCR = mask_TMR32B0MCR; |
maxonK | 0:9a0a89ffaa43 | 105 | //待ち |
maxonK | 0:9a0a89ffaa43 | 106 | wait(5); |
maxonK | 0:9a0a89ffaa43 | 107 | |
maxonK | 0:9a0a89ffaa43 | 108 | /***************************************************************************/ |
maxonK | 0:9a0a89ffaa43 | 109 | //mainloop |
maxonK | 0:9a0a89ffaa43 | 110 | while(1) { |
maxonK | 0:9a0a89ffaa43 | 111 | int receiveVal = 0; |
maxonK | 0:9a0a89ffaa43 | 112 | static int sReceiveVal = 0; |
maxonK | 0:9a0a89ffaa43 | 113 | int gain = 10; |
maxonK | 0:9a0a89ffaa43 | 114 | receiveVal = stringReceive(); |
maxonK | 0:9a0a89ffaa43 | 115 | |
maxonK | 0:9a0a89ffaa43 | 116 | |
maxonK | 0:9a0a89ffaa43 | 117 | if(receiveVal >= 0) { |
maxonK | 0:9a0a89ffaa43 | 118 | myled = !myled; |
maxonK | 0:9a0a89ffaa43 | 119 | |
maxonK | 0:9a0a89ffaa43 | 120 | //pwm関係 |
maxonK | 0:9a0a89ffaa43 | 121 | //グローバルdp4pwmDutyセット |
maxonK | 0:9a0a89ffaa43 | 122 | dp4pwmDuty = cramp((( receiveVal*gain ) - (((1023*gain)/2)-(1023/2)) ),0,1023); |
maxonK | 0:9a0a89ffaa43 | 123 | //dp24pwmDuty = cramp((( receiveVal*gain ) - (((1023*gain)/2)-(1023/2)) - (512 - 76) ),51,102); |
maxonK | 0:9a0a89ffaa43 | 124 | //dp24pwmDuty = receiveVal; |
maxonK | 0:9a0a89ffaa43 | 125 | |
maxonK | 0:9a0a89ffaa43 | 126 | if(receiveVal != sReceiveVal) { |
maxonK | 0:9a0a89ffaa43 | 127 | sReceiveVal = receiveVal; |
maxonK | 0:9a0a89ffaa43 | 128 | //pwm周波数設定用マッチレジスタの動作をタイマークリアから割り込み発生へ変更 |
maxonK | 0:9a0a89ffaa43 | 129 | //マッチ制御レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 130 | volatile unsigned int *TMR32B0MCR = (unsigned int *)(0x40014014); |
maxonK | 0:9a0a89ffaa43 | 131 | //マッチ制御レジスタマスク |
maxonK | 0:9a0a89ffaa43 | 132 | //マッチレジスタMR0とのマッチで割り込みが発生するように機能切り替え//マッチでのクリアは停止される |
maxonK | 0:9a0a89ffaa43 | 133 | unsigned int mask_TMR32B0MCR = 1; |
maxonK | 0:9a0a89ffaa43 | 134 | //マッチ制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 135 | *TMR32B0MCR = mask_TMR32B0MCR; |
maxonK | 0:9a0a89ffaa43 | 136 | } |
maxonK | 0:9a0a89ffaa43 | 137 | /* |
maxonK | 0:9a0a89ffaa43 | 138 | //I2C送信 |
maxonK | 0:9a0a89ffaa43 | 139 | char cmd[10] = {'\0'};//I2C送信バッファ |
maxonK | 0:9a0a89ffaa43 | 140 | //int to asic2(sprintf関数) |
maxonK | 0:9a0a89ffaa43 | 141 | sprintf(cmd ,"%4d",dp4pwmDuty);//格納する配列アドレス , 書式文字列 |
maxonK | 0:9a0a89ffaa43 | 142 | //null終端 |
maxonK | 0:9a0a89ffaa43 | 143 | cmd[sizeof(cmd) - 1] = '\0'; // NULL 終端する |
maxonK | 0:9a0a89ffaa43 | 144 | //書き出し |
maxonK | 0:9a0a89ffaa43 | 145 | i2c.write(addr, cmd, 7); |
maxonK | 0:9a0a89ffaa43 | 146 | */ |
maxonK | 0:9a0a89ffaa43 | 147 | } |
maxonK | 0:9a0a89ffaa43 | 148 | |
maxonK | 0:9a0a89ffaa43 | 149 | } |
maxonK | 0:9a0a89ffaa43 | 150 | /****************************************************************************/ |
maxonK | 0:9a0a89ffaa43 | 151 | |
maxonK | 0:9a0a89ffaa43 | 152 | } |
maxonK | 0:9a0a89ffaa43 | 153 | |
maxonK | 0:9a0a89ffaa43 | 154 | /*****************************************************************************************************************/ |
maxonK | 0:9a0a89ffaa43 | 155 | /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
maxonK | 0:9a0a89ffaa43 | 156 | |
maxonK | 0:9a0a89ffaa43 | 157 | |
maxonK | 0:9a0a89ffaa43 | 158 | //受信関数 |
maxonK | 0:9a0a89ffaa43 | 159 | //受信完了時0~4095 |
maxonK | 0:9a0a89ffaa43 | 160 | int stringReceive() |
maxonK | 0:9a0a89ffaa43 | 161 | { |
maxonK | 0:9a0a89ffaa43 | 162 | //各種定数 |
maxonK | 0:9a0a89ffaa43 | 163 | //正常なデータ長,書き込み位置大きくなりすぎていないかチェック用 |
maxonK | 0:9a0a89ffaa43 | 164 | const int dataLength = 7;//7個(6個のデータ+null終端)(添字は0~ 宣言時の大きさは1~)(添字の位置確認時は-1する) |
maxonK | 0:9a0a89ffaa43 | 165 | //startbitに使用する文字 |
maxonK | 0:9a0a89ffaa43 | 166 | char startbitChar = '*'; |
maxonK | 0:9a0a89ffaa43 | 167 | //stopbitに使用する文字 |
maxonK | 0:9a0a89ffaa43 | 168 | char stopbitChar = '#'; |
maxonK | 0:9a0a89ffaa43 | 169 | //各種変数 |
maxonK | 0:9a0a89ffaa43 | 170 | //現在書き込むべきバッファの位置変数 |
maxonK | 0:9a0a89ffaa43 | 171 | static int stringPosition = 0; |
maxonK | 0:9a0a89ffaa43 | 172 | //受信値一時格納変数 |
maxonK | 0:9a0a89ffaa43 | 173 | char receiveChar = '\0'; |
maxonK | 0:9a0a89ffaa43 | 174 | //受信データ格納バッファ文字列 |
maxonK | 0:9a0a89ffaa43 | 175 | static char receiveDataString [dataLength] = {'\0'};//7個 |
maxonK | 0:9a0a89ffaa43 | 176 | |
maxonK | 0:9a0a89ffaa43 | 177 | |
maxonK | 0:9a0a89ffaa43 | 178 | //データ送信要求 |
maxonK | 0:9a0a89ffaa43 | 179 | sensSerial.putc('C'); |
maxonK | 0:9a0a89ffaa43 | 180 | //ハードウェアバッファに到着した情報があるか |
maxonK | 0:9a0a89ffaa43 | 181 | //空ならリターン、あるなら受信値格納用変数にうつす |
maxonK | 0:9a0a89ffaa43 | 182 | if(!sensSerial.readable()) { |
maxonK | 0:9a0a89ffaa43 | 183 | //空のとき |
maxonK | 0:9a0a89ffaa43 | 184 | return -1; |
maxonK | 0:9a0a89ffaa43 | 185 | } else { //あるとき |
maxonK | 0:9a0a89ffaa43 | 186 | receiveChar = sensSerial.getc(); |
maxonK | 0:9a0a89ffaa43 | 187 | } |
maxonK | 0:9a0a89ffaa43 | 188 | //バッファ書き込み位置変数が大きくなりすぎている |
maxonK | 0:9a0a89ffaa43 | 189 | //(ストップビットがくるはずがきてない)ときリターン |
maxonK | 0:9a0a89ffaa43 | 190 | if(stringPosition > (dataLength - 2)) { |
maxonK | 0:9a0a89ffaa43 | 191 | stringPosition = 0; |
maxonK | 0:9a0a89ffaa43 | 192 | //ハードウェアバッファクリア |
maxonK | 0:9a0a89ffaa43 | 193 | bufferClear(); |
maxonK | 0:9a0a89ffaa43 | 194 | return -2; |
maxonK | 0:9a0a89ffaa43 | 195 | } |
maxonK | 0:9a0a89ffaa43 | 196 | |
maxonK | 0:9a0a89ffaa43 | 197 | //到着していたデータがスタートビットだった場合 |
maxonK | 0:9a0a89ffaa43 | 198 | if(receiveChar == startbitChar) { |
maxonK | 0:9a0a89ffaa43 | 199 | |
maxonK | 0:9a0a89ffaa43 | 200 | //バッファクリア |
maxonK | 0:9a0a89ffaa43 | 201 | for (int i = 0; i < dataLength; i++) { //配列の要素は0~ 大きさは1~ |
maxonK | 0:9a0a89ffaa43 | 202 | receiveDataString[i] = '\0'; |
maxonK | 0:9a0a89ffaa43 | 203 | } |
maxonK | 0:9a0a89ffaa43 | 204 | //書き込み位置変数クリア |
maxonK | 0:9a0a89ffaa43 | 205 | stringPosition = 0; |
maxonK | 0:9a0a89ffaa43 | 206 | //バッファへ書き込み |
maxonK | 0:9a0a89ffaa43 | 207 | receiveDataString[stringPosition] = receiveChar ; |
maxonK | 0:9a0a89ffaa43 | 208 | |
maxonK | 0:9a0a89ffaa43 | 209 | |
maxonK | 0:9a0a89ffaa43 | 210 | //書き込み位置進め |
maxonK | 0:9a0a89ffaa43 | 211 | stringPosition ++; |
maxonK | 0:9a0a89ffaa43 | 212 | //-1リターン |
maxonK | 0:9a0a89ffaa43 | 213 | return -3; |
maxonK | 0:9a0a89ffaa43 | 214 | } |
maxonK | 0:9a0a89ffaa43 | 215 | |
maxonK | 0:9a0a89ffaa43 | 216 | //到着していたデータが数字だった場合 |
maxonK | 0:9a0a89ffaa43 | 217 | if(receiveChar=='0'||receiveChar=='1'||receiveChar=='2'||receiveChar=='3'||receiveChar=='4'||receiveChar=='5'||receiveChar=='6'||receiveChar=='7'||receiveChar=='8'||receiveChar=='9') { |
maxonK | 0:9a0a89ffaa43 | 218 | //バッファへ書き込み |
maxonK | 0:9a0a89ffaa43 | 219 | receiveDataString[stringPosition] = receiveChar ; |
maxonK | 0:9a0a89ffaa43 | 220 | |
maxonK | 0:9a0a89ffaa43 | 221 | //書き込み位置進め |
maxonK | 0:9a0a89ffaa43 | 222 | stringPosition++; |
maxonK | 0:9a0a89ffaa43 | 223 | //-1リターン |
maxonK | 0:9a0a89ffaa43 | 224 | return -4; |
maxonK | 0:9a0a89ffaa43 | 225 | } |
maxonK | 0:9a0a89ffaa43 | 226 | |
maxonK | 0:9a0a89ffaa43 | 227 | //到着していたデータがストップビットだった場合 |
maxonK | 0:9a0a89ffaa43 | 228 | if(receiveChar == stopbitChar) { |
maxonK | 0:9a0a89ffaa43 | 229 | //使う変数宣言 |
maxonK | 0:9a0a89ffaa43 | 230 | //バッファ内容をコピーするための文字列 |
maxonK | 0:9a0a89ffaa43 | 231 | char copyString [dataLength] = {'\0'}; |
maxonK | 0:9a0a89ffaa43 | 232 | //スタート、ストップビットをトリムしたデータだけの文字列 |
maxonK | 0:9a0a89ffaa43 | 233 | char trimString [dataLength] = {'\0'}; |
maxonK | 0:9a0a89ffaa43 | 234 | //文字列→数値変換後の値を格納する変数 |
maxonK | 0:9a0a89ffaa43 | 235 | int intVal = 0; |
maxonK | 0:9a0a89ffaa43 | 236 | |
maxonK | 0:9a0a89ffaa43 | 237 | //バッファへ書き込み |
maxonK | 0:9a0a89ffaa43 | 238 | receiveDataString[stringPosition] = receiveChar ; |
maxonK | 0:9a0a89ffaa43 | 239 | //null終端 |
maxonK | 0:9a0a89ffaa43 | 240 | copyString[sizeof(copyString) - 1] = '\0'; /* NULL 終端する */ |
maxonK | 0:9a0a89ffaa43 | 241 | receiveDataString[sizeof(receiveDataString) - 1] = '\0';//null終端 |
maxonK | 0:9a0a89ffaa43 | 242 | |
maxonK | 0:9a0a89ffaa43 | 243 | |
maxonK | 0:9a0a89ffaa43 | 244 | //バッファの内容をうつす |
maxonK | 0:9a0a89ffaa43 | 245 | strlcpy(copyString,receiveDataString,sizeof(copyString)); |
maxonK | 0:9a0a89ffaa43 | 246 | //バッファクリア |
maxonK | 0:9a0a89ffaa43 | 247 | for (int i = 0; i < dataLength; i++) { //配列の要素は0~ 大きさは1~ |
maxonK | 0:9a0a89ffaa43 | 248 | receiveDataString[i] = '\0'; |
maxonK | 0:9a0a89ffaa43 | 249 | } |
maxonK | 0:9a0a89ffaa43 | 250 | //ハードウェアバッファクリア |
maxonK | 0:9a0a89ffaa43 | 251 | bufferClear(); |
maxonK | 0:9a0a89ffaa43 | 252 | //書き込み位置クリア |
maxonK | 0:9a0a89ffaa43 | 253 | stringPosition = 0; |
maxonK | 0:9a0a89ffaa43 | 254 | |
maxonK | 0:9a0a89ffaa43 | 255 | //以下チェックとAtoI |
maxonK | 0:9a0a89ffaa43 | 256 | //先頭がスタートビットになってるか |
maxonK | 0:9a0a89ffaa43 | 257 | if(!(copyString[0] == startbitChar)) { |
maxonK | 0:9a0a89ffaa43 | 258 | return -5; |
maxonK | 0:9a0a89ffaa43 | 259 | } |
maxonK | 0:9a0a89ffaa43 | 260 | //末尾がストップビットになっているか(配列の最後はnull) |
maxonK | 0:9a0a89ffaa43 | 261 | if(!(copyString[(dataLength - 2)] == stopbitChar)) { |
maxonK | 0:9a0a89ffaa43 | 262 | return -6; |
maxonK | 0:9a0a89ffaa43 | 263 | } |
maxonK | 0:9a0a89ffaa43 | 264 | //データだけをトリミング |
maxonK | 0:9a0a89ffaa43 | 265 | //1個目から、後ろから2個目まで*xxxx#(\n) |
maxonK | 0:9a0a89ffaa43 | 266 | for(int Idx = 1; Idx < (dataLength - 2) ; Idx++) { |
maxonK | 0:9a0a89ffaa43 | 267 | trimString[(Idx - 1) ] = copyString[Idx]; |
maxonK | 0:9a0a89ffaa43 | 268 | } |
maxonK | 0:9a0a89ffaa43 | 269 | //null終端 |
maxonK | 0:9a0a89ffaa43 | 270 | trimString[sizeof(trimString) - 1] = '\0'; /* NULL 終端する */ |
maxonK | 0:9a0a89ffaa43 | 271 | //文字列→数値 |
maxonK | 0:9a0a89ffaa43 | 272 | intVal = atoi(trimString); |
maxonK | 0:9a0a89ffaa43 | 273 | //数値リターン |
maxonK | 0:9a0a89ffaa43 | 274 | return intVal; |
maxonK | 0:9a0a89ffaa43 | 275 | |
maxonK | 0:9a0a89ffaa43 | 276 | } |
maxonK | 0:9a0a89ffaa43 | 277 | |
maxonK | 0:9a0a89ffaa43 | 278 | |
maxonK | 0:9a0a89ffaa43 | 279 | //以上にひっかからなかった異常時 |
maxonK | 0:9a0a89ffaa43 | 280 | //バッファクリア |
maxonK | 0:9a0a89ffaa43 | 281 | for (int i = 0; i <dataLength; i++) { //配列の要素は0~ 大きさは1~ |
maxonK | 0:9a0a89ffaa43 | 282 | receiveDataString[i] = '\0'; |
maxonK | 0:9a0a89ffaa43 | 283 | } |
maxonK | 0:9a0a89ffaa43 | 284 | //ハードウェアバッファクリア |
maxonK | 0:9a0a89ffaa43 | 285 | bufferClear(); |
maxonK | 0:9a0a89ffaa43 | 286 | //書き込み位置クリア |
maxonK | 0:9a0a89ffaa43 | 287 | stringPosition = 0; |
maxonK | 0:9a0a89ffaa43 | 288 | //-3リターン |
maxonK | 0:9a0a89ffaa43 | 289 | return -7; |
maxonK | 0:9a0a89ffaa43 | 290 | } |
maxonK | 0:9a0a89ffaa43 | 291 | |
maxonK | 0:9a0a89ffaa43 | 292 | |
maxonK | 0:9a0a89ffaa43 | 293 | //cramp関数 |
maxonK | 0:9a0a89ffaa43 | 294 | int cramp(int val , int min , int max) |
maxonK | 0:9a0a89ffaa43 | 295 | { |
maxonK | 0:9a0a89ffaa43 | 296 | if(val < min) { |
maxonK | 0:9a0a89ffaa43 | 297 | return min; |
maxonK | 0:9a0a89ffaa43 | 298 | } |
maxonK | 0:9a0a89ffaa43 | 299 | if(val > max) { |
maxonK | 0:9a0a89ffaa43 | 300 | return max; |
maxonK | 0:9a0a89ffaa43 | 301 | } |
maxonK | 0:9a0a89ffaa43 | 302 | |
maxonK | 0:9a0a89ffaa43 | 303 | return val; |
maxonK | 0:9a0a89ffaa43 | 304 | } |
maxonK | 0:9a0a89ffaa43 | 305 | |
maxonK | 0:9a0a89ffaa43 | 306 | //serialハードウェアバッファクリア関数 |
maxonK | 0:9a0a89ffaa43 | 307 | void bufferClear() |
maxonK | 0:9a0a89ffaa43 | 308 | { |
maxonK | 0:9a0a89ffaa43 | 309 | char temp = 0; |
maxonK | 0:9a0a89ffaa43 | 310 | while(sensSerial.readable()) { |
maxonK | 0:9a0a89ffaa43 | 311 | temp = sensSerial.getc(); |
maxonK | 0:9a0a89ffaa43 | 312 | } |
maxonK | 0:9a0a89ffaa43 | 313 | return; |
maxonK | 0:9a0a89ffaa43 | 314 | } |
maxonK | 0:9a0a89ffaa43 | 315 | |
maxonK | 0:9a0a89ffaa43 | 316 | |
maxonK | 0:9a0a89ffaa43 | 317 | |
maxonK | 0:9a0a89ffaa43 | 318 | |
maxonK | 0:9a0a89ffaa43 | 319 | |
maxonK | 0:9a0a89ffaa43 | 320 | |
maxonK | 0:9a0a89ffaa43 | 321 | void CT32B0_MAT3_PWM_registerSet(unsigned int pwmFrequency) |
maxonK | 0:9a0a89ffaa43 | 322 | { |
maxonK | 0:9a0a89ffaa43 | 323 | //PWM機能設定用 |
maxonK | 0:9a0a89ffaa43 | 324 | //pwmピンのピン機能選択をmbedで行う |
maxonK | 0:9a0a89ffaa43 | 325 | //PwmOut MAT2(dp24);//CT32B0_MAT2 |
maxonK | 0:9a0a89ffaa43 | 326 | //PWM周波数 |
maxonK | 0:9a0a89ffaa43 | 327 | //unsigned int pwmFrequency = 4; |
maxonK | 0:9a0a89ffaa43 | 328 | //各種レジスタ設定用ビットマスク |
maxonK | 0:9a0a89ffaa43 | 329 | //システムAHBクロック制御レジスタマスク |
maxonK | 0:9a0a89ffaa43 | 330 | unsigned int mask_SYSAHBCLKCTRL = 0x3 << 9; |
maxonK | 0:9a0a89ffaa43 | 331 | //IO設定レジスタレジスタマスク |
maxonK | 0:9a0a89ffaa43 | 332 | unsigned int mask_IOCON_PIO0_11 = 0x3; |
maxonK | 0:9a0a89ffaa43 | 333 | //タイマ制御レジスタマスク |
maxonK | 0:9a0a89ffaa43 | 334 | //0ビット目を1にしてタイマ、プリスケールカウンタをイネーブルへ |
maxonK | 0:9a0a89ffaa43 | 335 | unsigned int mask_TMR32B0TCR = 1; |
maxonK | 0:9a0a89ffaa43 | 336 | //プリスケールレジスタマスク |
maxonK | 0:9a0a89ffaa43 | 337 | unsigned int val_TMR32B0PR = 0; |
maxonK | 0:9a0a89ffaa43 | 338 | //マッチ制御レジスタマスク |
maxonK | 0:9a0a89ffaa43 | 339 | //0~3番のマッチレジスタの、PWM出力ピンにしたい番号以外のどれか1つをタイマ・カウンタ(TC)リセット用に設定する |
maxonK | 0:9a0a89ffaa43 | 340 | //残りのマッチレジスタはディスエーブルにする |
maxonK | 0:9a0a89ffaa43 | 341 | //mbedのピン配列ではタイマ0は0番をTCリセットに使うといいかも |
maxonK | 0:9a0a89ffaa43 | 342 | unsigned int mask_TMR32B0MCR = 1 << 1; |
maxonK | 0:9a0a89ffaa43 | 343 | //PWM制御レジスタマスク |
maxonK | 0:9a0a89ffaa43 | 344 | //1つのタイマブロックで同時に、異なるデューティで動作できるPWMは3つまで |
maxonK | 0:9a0a89ffaa43 | 345 | //CT32Bn_MATxピンの、TCリセットに使っていない番号のどれをPWMピンにするか選択して、ビットを1にする |
maxonK | 0:9a0a89ffaa43 | 346 | //mbedではMAT2がpwmOutになってるがISPモードに使用しているので、dp4-MAT3を試す |
maxonK | 0:9a0a89ffaa43 | 347 | unsigned int mask_TMR32B0PWMC = 1 << 3; |
maxonK | 0:9a0a89ffaa43 | 348 | //マッチレジスタ値 |
maxonK | 0:9a0a89ffaa43 | 349 | //・マッチ制御レジスタでTCリセット用に設定したマッチレジスタに、周期を設定する |
maxonK | 0:9a0a89ffaa43 | 350 | // ((48*10^6)/nHz)で任意の周波数になる、はずである |
maxonK | 0:9a0a89ffaa43 | 351 | //・マッチ制御レジスタでTCリセット用に設定しなかったマッチレジスタで、PWM出力ピンにする番号のマッチレジスタにパルス長を設定する |
maxonK | 0:9a0a89ffaa43 | 352 | // ここでいうパルス長は、LOWレベルの長さになる。 0で常にHIGH,TCリセット値より大きいと常にLOW,TCリセット値と等しい場合は完全にはLOWにならない |
maxonK | 0:9a0a89ffaa43 | 353 | unsigned int val_TMR32B0MR0 = /*10;*/((48000000) / (pwmFrequency)); |
maxonK | 0:9a0a89ffaa43 | 354 | unsigned int val_TMR32B0MR3 = /*8;*/((48000000) / (pwmFrequency)) + 1; |
maxonK | 0:9a0a89ffaa43 | 355 | |
maxonK | 0:9a0a89ffaa43 | 356 | //システムAHBクロック制御レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 357 | volatile unsigned int *SYSAHBCLKCTRLLocal = (unsigned int *)0x40048080; |
maxonK | 0:9a0a89ffaa43 | 358 | //IO設定レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 359 | volatile unsigned int *IOCON_PIO0_11Local = (unsigned int *)0x40044074; |
maxonK | 0:9a0a89ffaa43 | 360 | //プリスケールレジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 361 | volatile unsigned int *TMR32B0PRLocal = (unsigned int *)0x4001400C; |
maxonK | 0:9a0a89ffaa43 | 362 | //タイマ制御レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 363 | volatile unsigned int *TMR32B0TCR = (unsigned int *)(0x40014004); |
maxonK | 0:9a0a89ffaa43 | 364 | //マッチ制御レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 365 | volatile unsigned int *TMR32B0MCR = (unsigned int *)(0x40014014); |
maxonK | 0:9a0a89ffaa43 | 366 | //マッチレジスタ0アドレス |
maxonK | 0:9a0a89ffaa43 | 367 | volatile unsigned int *TMR32B0MR0 = (unsigned int *)(0x40014018); |
maxonK | 0:9a0a89ffaa43 | 368 | //マッチレジスタ3アドレス |
maxonK | 0:9a0a89ffaa43 | 369 | volatile unsigned int *TMR32B0MR3 = (unsigned int *)(0x40014024); |
maxonK | 0:9a0a89ffaa43 | 370 | //PWM制御レジスタアドレス |
maxonK | 0:9a0a89ffaa43 | 371 | volatile unsigned int *TMR32B0PWMC = (unsigned int *)(0x40014074); |
maxonK | 0:9a0a89ffaa43 | 372 | |
maxonK | 0:9a0a89ffaa43 | 373 | //PWM機能制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 374 | //システムAHBクロック制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 375 | *SYSAHBCLKCTRLLocal |= mask_SYSAHBCLKCTRL; |
maxonK | 0:9a0a89ffaa43 | 376 | //IO設定レジスタセット |
maxonK | 0:9a0a89ffaa43 | 377 | *IOCON_PIO0_11Local |= mask_IOCON_PIO0_11; |
maxonK | 0:9a0a89ffaa43 | 378 | //プリスケーラレジスタセット |
maxonK | 0:9a0a89ffaa43 | 379 | *TMR32B0PRLocal = val_TMR32B0PR; |
maxonK | 0:9a0a89ffaa43 | 380 | //PWM制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 381 | *TMR32B0PWMC |= mask_TMR32B0PWMC; |
maxonK | 0:9a0a89ffaa43 | 382 | //マッチレジスタセット |
maxonK | 0:9a0a89ffaa43 | 383 | //デフォルト値設定操作 |
maxonK | 0:9a0a89ffaa43 | 384 | //MR0_周波数 |
maxonK | 0:9a0a89ffaa43 | 385 | *TMR32B0MR0 = val_TMR32B0MR0; |
maxonK | 0:9a0a89ffaa43 | 386 | //MR3_LOWパルス長 |
maxonK | 0:9a0a89ffaa43 | 387 | *TMR32B0MR3 = val_TMR32B0MR3; |
maxonK | 0:9a0a89ffaa43 | 388 | //マッチ制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 389 | *TMR32B0MCR |= mask_TMR32B0MCR; |
maxonK | 0:9a0a89ffaa43 | 390 | //タイマ制御レジスタセット |
maxonK | 0:9a0a89ffaa43 | 391 | *TMR32B0TCR |= mask_TMR32B0TCR; |
maxonK | 0:9a0a89ffaa43 | 392 | } |