read encoder

Dependencies:   mbed

Committer:
JJting
Date:
Tue Jul 31 14:19:56 2018 +0000
Revision:
2:33062fc279ca
Parent:
1:2f2a74337b77
20180731ver3(encoder read with header file)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JJting 0:f2657b94ea70 1 #include "mbed.h"
JJting 1:2f2a74337b77 2 #include "encoder.h"
JJting 0:f2657b94ea70 3
JJting 0:f2657b94ea70 4 Serial uart(USBTX, USBRX);
JJting 0:f2657b94ea70 5 //Serial uart(D10,D2); // TX : D10 RX : D2 // blueteeth
JJting 0:f2657b94ea70 6 DigitalOut LED(A4); // check if the code is running
JJting 0:f2657b94ea70 7
JJting 0:f2657b94ea70 8 // timer
JJting 0:f2657b94ea70 9 Ticker timer1;
JJting 2:33062fc279ca 10 float ITR_time1 = 10000.0; // unit:us
JJting 0:f2657b94ea70 11
JJting 0:f2657b94ea70 12 // uart_tx
JJting 0:f2657b94ea70 13 union splitter {
JJting 0:f2657b94ea70 14 short j;
JJting 0:f2657b94ea70 15 char C[2];
JJting 0:f2657b94ea70 16 // C[0] is lowbyte of j, C[1] is highbyte of j
JJting 0:f2657b94ea70 17 };
JJting 0:f2657b94ea70 18 char T[16] = {255,255,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
JJting 0:f2657b94ea70 19 int i = 0;
JJting 0:f2657b94ea70 20
JJting 0:f2657b94ea70 21 // function
JJting 0:f2657b94ea70 22 void init_UART();
JJting 0:f2657b94ea70 23 void init_TIMER();
JJting 0:f2657b94ea70 24 void timer1_ITR();
JJting 0:f2657b94ea70 25 void uart_tx();
JJting 0:f2657b94ea70 26
JJting 0:f2657b94ea70 27
JJting 0:f2657b94ea70 28 int main()
JJting 0:f2657b94ea70 29 {
JJting 0:f2657b94ea70 30 LED = 1;
JJting 0:f2657b94ea70 31 wait_ms(500);
JJting 0:f2657b94ea70 32 // initial sensor
JJting 2:33062fc279ca 33 init_SPI_encoder();
JJting 0:f2657b94ea70 34 init_encoder();
JJting 2:33062fc279ca 35
JJting 0:f2657b94ea70 36 // initial uart
JJting 0:f2657b94ea70 37 init_UART();
JJting 0:f2657b94ea70 38
JJting 0:f2657b94ea70 39 wait_ms(500);
JJting 0:f2657b94ea70 40 LED = 0;
JJting 0:f2657b94ea70 41
JJting 0:f2657b94ea70 42 init_TIMER();
JJting 0:f2657b94ea70 43
JJting 0:f2657b94ea70 44 while(1) {
JJting 0:f2657b94ea70 45 }
JJting 0:f2657b94ea70 46
JJting 0:f2657b94ea70 47 }
JJting 2:33062fc279ca 48
JJting 0:f2657b94ea70 49 void init_UART()
JJting 0:f2657b94ea70 50 {
JJting 0:f2657b94ea70 51 uart.baud(115200);
JJting 0:f2657b94ea70 52 }
JJting 0:f2657b94ea70 53
JJting 0:f2657b94ea70 54 void init_TIMER()
JJting 0:f2657b94ea70 55 {
JJting 0:f2657b94ea70 56 timer1.attach_us(&timer1_ITR, ITR_time1);
JJting 0:f2657b94ea70 57 }
JJting 0:f2657b94ea70 58
JJting 0:f2657b94ea70 59 void timer1_ITR()
JJting 0:f2657b94ea70 60 {
JJting 0:f2657b94ea70 61 angle_measure();
JJting 0:f2657b94ea70 62 uart_tx();
JJting 0:f2657b94ea70 63 }
JJting 2:33062fc279ca 64
JJting 0:f2657b94ea70 65 void uart_tx()
JJting 0:f2657b94ea70 66 {
JJting 0:f2657b94ea70 67 splitter s1;
JJting 0:f2657b94ea70 68 splitter s2;
JJting 0:f2657b94ea70 69 splitter s3;
JJting 0:f2657b94ea70 70 splitter s4;
JJting 0:f2657b94ea70 71 splitter s5;
JJting 0:f2657b94ea70 72 splitter s6;
JJting 0:f2657b94ea70 73 splitter s7;
JJting 0:f2657b94ea70 74
JJting 0:f2657b94ea70 75 s1.j = angle;
JJting 0:f2657b94ea70 76 s2.j = Angle;
JJting 0:f2657b94ea70 77 s3.j = 2;
JJting 0:f2657b94ea70 78 s4.j = 3;
JJting 0:f2657b94ea70 79 s5.j = 1;
JJting 0:f2657b94ea70 80 s6.j = 2;
JJting 0:f2657b94ea70 81 s7.j = 3;
JJting 0:f2657b94ea70 82
JJting 0:f2657b94ea70 83 T[2] = s1.C[0];
JJting 0:f2657b94ea70 84 T[3] = s1.C[1];
JJting 0:f2657b94ea70 85 T[4] = s2.C[0];
JJting 0:f2657b94ea70 86 T[5] = s2.C[1];
JJting 0:f2657b94ea70 87 T[6] = s3.C[0];
JJting 0:f2657b94ea70 88 T[7] = s3.C[1];
JJting 0:f2657b94ea70 89 T[8] = s4.C[0];
JJting 0:f2657b94ea70 90 T[9] = s4.C[1];
JJting 0:f2657b94ea70 91 T[10] = s5.C[0];
JJting 0:f2657b94ea70 92 T[11] = s5.C[1];
JJting 0:f2657b94ea70 93 T[12] = s6.C[0];
JJting 0:f2657b94ea70 94 T[13] = s6.C[1];
JJting 0:f2657b94ea70 95 T[14] = s7.C[0];
JJting 0:f2657b94ea70 96 T[15] = s7.C[1];
JJting 0:f2657b94ea70 97
JJting 0:f2657b94ea70 98 while(1) {
JJting 0:f2657b94ea70 99 if (uart.writable() == 1) {
JJting 0:f2657b94ea70 100 uart.putc(T[i]);
JJting 0:f2657b94ea70 101 i++;
JJting 0:f2657b94ea70 102 }
JJting 0:f2657b94ea70 103 if (i >= (sizeof(T)-1)) {
JJting 0:f2657b94ea70 104 i = 0;
JJting 0:f2657b94ea70 105 break;
JJting 0:f2657b94ea70 106 }
JJting 0:f2657b94ea70 107 }
JJting 0:f2657b94ea70 108 }