Hideto Niwa / Mbed 2 deprecated Cap-Sensor

Dependencies:   mbed

Committer:
HidetoN
Date:
Thu Jan 23 08:04:00 2020 +0000
Revision:
2:e0c00fd3c351
Parent:
1:632bf9a889cc
OutPut Data 100Hz,Get data 1000Hz but OutPut Data is that average.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
HidetoN 0:1025ad5aabba 1 #include "mbed.h"
HidetoN 0:1025ad5aabba 2
HidetoN 0:1025ad5aabba 3 #define ID 0x00
HidetoN 1:632bf9a889cc 4 #define SENSORNUM 2
HidetoN 0:1025ad5aabba 5
HidetoN 0:1025ad5aabba 6 DigitalOut led(LED1);
HidetoN 2:e0c00fd3c351 7 Serial pc(USBTX,USBRX,921600);
HidetoN 0:1025ad5aabba 8
HidetoN 0:1025ad5aabba 9 //cap1
HidetoN 0:1025ad5aabba 10 DigitalOut cap1Out(A1);
HidetoN 0:1025ad5aabba 11 InterruptIn cap1Check(A0);
HidetoN 0:1025ad5aabba 12 InterruptIn cap1Get(A2);
HidetoN 0:1025ad5aabba 13
HidetoN 2:e0c00fd3c351 14 //割り込み処理(参考:https://os.mbed.com/users/okini3939/notebook/ticker_jp/)
HidetoN 2:e0c00fd3c351 15 Ticker sig;
HidetoN 2:e0c00fd3c351 16
HidetoN 2:e0c00fd3c351 17 Timer t1;
HidetoN 2:e0c00fd3c351 18 Timer t2;
HidetoN 0:1025ad5aabba 19 Timer constantTimer;//マイコン起動時間計測用タイマー
HidetoN 2:e0c00fd3c351 20 uint32_t data[SENSORNUM]= {0};
HidetoN 1:632bf9a889cc 21
HidetoN 1:632bf9a889cc 22 uint32_t constantTime=0;
HidetoN 0:1025ad5aabba 23
HidetoN 2:e0c00fd3c351 24 bool outPutState=false;
HidetoN 2:e0c00fd3c351 25
HidetoN 2:e0c00fd3c351 26 void check1Out()
HidetoN 0:1025ad5aabba 27 {
HidetoN 2:e0c00fd3c351 28 t1.start();
HidetoN 0:1025ad5aabba 29 }
HidetoN 0:1025ad5aabba 30
HidetoN 2:e0c00fd3c351 31 uint32_t loop=0;
HidetoN 2:e0c00fd3c351 32 uint32_t ave=0;
HidetoN 2:e0c00fd3c351 33 uint32_t aveCount=0;
HidetoN 2:e0c00fd3c351 34 uint32_t sum=0;
HidetoN 2:e0c00fd3c351 35
HidetoN 2:e0c00fd3c351 36 void get1()
HidetoN 0:1025ad5aabba 37 {
HidetoN 2:e0c00fd3c351 38 t1.stop();
HidetoN 0:1025ad5aabba 39 cap1Out=0;
HidetoN 2:e0c00fd3c351 40 data[0]=t1.read_us();
HidetoN 2:e0c00fd3c351 41
HidetoN 2:e0c00fd3c351 42 if((data[0]<75)&&(data[0]>25)){
HidetoN 2:e0c00fd3c351 43 //pc.printf("c\t");
HidetoN 2:e0c00fd3c351 44 sum=sum+data[0];
HidetoN 2:e0c00fd3c351 45 aveCount++;
HidetoN 2:e0c00fd3c351 46 }
HidetoN 2:e0c00fd3c351 47
HidetoN 2:e0c00fd3c351 48 if(((loop%10)==0)&&(aveCount!=0)) {
HidetoN 2:e0c00fd3c351 49 ave=sum/aveCount;
HidetoN 2:e0c00fd3c351 50 for(uint8_t i=0;i<2;i++){
HidetoN 2:e0c00fd3c351 51 for(uint8_t i=0; i<1; i++) {
HidetoN 2:e0c00fd3c351 52 if(ave>=100) {
HidetoN 2:e0c00fd3c351 53 } else if(ave>=10) {
HidetoN 2:e0c00fd3c351 54 pc.printf("0");
HidetoN 2:e0c00fd3c351 55 } else {
HidetoN 2:e0c00fd3c351 56 pc.printf("00");
HidetoN 2:e0c00fd3c351 57 }
HidetoN 2:e0c00fd3c351 58 pc.printf("%u",ave);
HidetoN 2:e0c00fd3c351 59 //pc.printf("\t%d\t%d",sum,aveCount);
HidetoN 2:e0c00fd3c351 60 pc.printf("\r\n");
HidetoN 2:e0c00fd3c351 61 }
HidetoN 2:e0c00fd3c351 62 }
HidetoN 2:e0c00fd3c351 63 aveCount=0;
HidetoN 2:e0c00fd3c351 64 sum=0;
HidetoN 2:e0c00fd3c351 65 }else {
HidetoN 2:e0c00fd3c351 66 }
HidetoN 2:e0c00fd3c351 67 t1.reset();
HidetoN 2:e0c00fd3c351 68 loop++;
HidetoN 2:e0c00fd3c351 69 }
HidetoN 2:e0c00fd3c351 70
HidetoN 2:e0c00fd3c351 71 void outSig()
HidetoN 2:e0c00fd3c351 72 {
HidetoN 2:e0c00fd3c351 73 //ID表示
HidetoN 2:e0c00fd3c351 74 cap1Out=outPutState;
HidetoN 2:e0c00fd3c351 75 outPutState=!outPutState;
HidetoN 1:632bf9a889cc 76 constantTime=constantTimer.read_us();
HidetoN 2:e0c00fd3c351 77 }
HidetoN 2:e0c00fd3c351 78
HidetoN 2:e0c00fd3c351 79 void timeRset()
HidetoN 2:e0c00fd3c351 80 {
HidetoN 2:e0c00fd3c351 81 pc.getc();
HidetoN 2:e0c00fd3c351 82 constantTimer.stop();
HidetoN 2:e0c00fd3c351 83 constantTimer.reset();
HidetoN 2:e0c00fd3c351 84 constantTimer.start();
HidetoN 0:1025ad5aabba 85 }
HidetoN 0:1025ad5aabba 86
HidetoN 0:1025ad5aabba 87 int main()
HidetoN 0:1025ad5aabba 88 {
HidetoN 0:1025ad5aabba 89
HidetoN 0:1025ad5aabba 90 pc.printf("Hello!\r\n");
HidetoN 2:e0c00fd3c351 91 pc.attach(timeRset, Serial::RxIrq);
HidetoN 0:1025ad5aabba 92
HidetoN 2:e0c00fd3c351 93 cap1Check.rise(&check1Out);
HidetoN 2:e0c00fd3c351 94 cap1Get.rise(&get1);
HidetoN 1:632bf9a889cc 95 constantTimer.start();
HidetoN 2:e0c00fd3c351 96
HidetoN 2:e0c00fd3c351 97 sig.attach_us(&outSig,500);
HidetoN 2:e0c00fd3c351 98
HidetoN 2:e0c00fd3c351 99 led=1;
HidetoN 0:1025ad5aabba 100 while(1) {
HidetoN 0:1025ad5aabba 101 }
HidetoN 0:1025ad5aabba 102 }