Accelerometer simulator

Dependencies:   mbed

Committer:
rschiano
Date:
Tue Nov 03 15:23:36 2020 +0000
Revision:
2:fbb699c3994f
Parent:
1:a31564d0b86c
Child:
3:b87d5296015c
Example of data transmission to and from microcontroller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rschiano 0:8ef83d7dc88d 1 #include "mbed.h"
rschiano 2:fbb699c3994f 2 #include <stdlib.h>
rschiano 0:8ef83d7dc88d 3
rschiano 2:fbb699c3994f 4 union MYFLOAT {
rschiano 2:fbb699c3994f 5 float value;
rschiano 2:fbb699c3994f 6 uint8_t byte[4];
rschiano 2:fbb699c3994f 7 } example,sampPer;
rschiano 2:fbb699c3994f 8
rschiano 2:fbb699c3994f 9 union MYINT {
rschiano 2:fbb699c3994f 10 uint16_t value;
rschiano 2:fbb699c3994f 11 uint8_t byte[2];
rschiano 2:fbb699c3994f 12 } nSamples;
rschiano 2:fbb699c3994f 13
rschiano 2:fbb699c3994f 14 Serial pc(PA_2,PA_3,921600);
rschiano 1:a31564d0b86c 15 DigitalIn myBtn(PC_13);
rschiano 1:a31564d0b86c 16 Timer timer;
rschiano 1:a31564d0b86c 17 // Clock frequency 84MHz 12ns Instruction clock
rschiano 2:fbb699c3994f 18 unsigned int cnt;
rschiano 2:fbb699c3994f 19 float elapTime,value = 17.1234567;
rschiano 0:8ef83d7dc88d 20
rschiano 1:a31564d0b86c 21 int main()
rschiano 1:a31564d0b86c 22 {
rschiano 2:fbb699c3994f 23 /* example.value = 2.343456795e1;
rschiano 2:fbb699c3994f 24 pc.printf("The value is %.9f.\r\n",example.value);
rschiano 2:fbb699c3994f 25 // pc.printf("The value is ");
rschiano 2:fbb699c3994f 26 for(int j=0;j<10;j++){
rschiano 2:fbb699c3994f 27 for(int i=0;i<4;i++){
rschiano 2:fbb699c3994f 28 pc.putc(example.byte[i]);
rschiano 2:fbb699c3994f 29 }
rschiano 2:fbb699c3994f 30 example.value = example.value + 1.0f;
rschiano 2:fbb699c3994f 31 }
rschiano 2:fbb699c3994f 32 // pc.printf("\r\n");
rschiano 2:fbb699c3994f 33 */
rschiano 1:a31564d0b86c 34 while(myBtn);
rschiano 1:a31564d0b86c 35 while(!myBtn);
rschiano 2:fbb699c3994f 36 pc.printf("Hello world!!!\r\n");
rschiano 2:fbb699c3994f 37 // timer.start();
rschiano 2:fbb699c3994f 38 while(1) {
rschiano 2:fbb699c3994f 39 for(cnt=0; cnt<2; cnt++)
rschiano 2:fbb699c3994f 40 nSamples.byte[cnt] = pc.getc();
rschiano 2:fbb699c3994f 41 // pc.scanf("%u",&nSamples);
rschiano 2:fbb699c3994f 42 pc.printf("Entered %d.\r\n",nSamples.value);
rschiano 2:fbb699c3994f 43 // pc.printf("Please, provide the sampling period [s] ");
rschiano 2:fbb699c3994f 44 for(cnt=0; cnt<4; cnt++)
rschiano 2:fbb699c3994f 45 sampPer.byte[cnt] = pc.getc();
rschiano 2:fbb699c3994f 46 // pc.scanf("%f",&sampPer);
rschiano 2:fbb699c3994f 47 pc.printf("Entered %e.\r\n",sampPer.value);
rschiano 2:fbb699c3994f 48 for(cnt=0; cnt<nSamples.value; cnt++) {
rschiano 1:a31564d0b86c 49 timer.reset();
rschiano 2:fbb699c3994f 50 example.value = value + (float)(rand()%100)/100;
rschiano 2:fbb699c3994f 51 for(int i=0; i<4; i++) {
rschiano 2:fbb699c3994f 52 pc.putc(example.byte[i]);
rschiano 2:fbb699c3994f 53 }
rschiano 1:a31564d0b86c 54 elapTime = timer.read();
rschiano 2:fbb699c3994f 55 wait(sampPer.value - elapTime);
rschiano 1:a31564d0b86c 56 }
rschiano 1:a31564d0b86c 57 }
rschiano 0:8ef83d7dc88d 58 }