UART to Labview

Dependencies:   mbed

Committer:
hoting
Date:
Wed Mar 16 13:28:07 2016 +0000
Revision:
0:bb5e43ae643e
UART to Labview

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hoting 0:bb5e43ae643e 1 #include "mbed.h"
hoting 0:bb5e43ae643e 2
hoting 0:bb5e43ae643e 3 Ticker timer1;
hoting 0:bb5e43ae643e 4 Serial pc(USBTX, USBRX);
hoting 0:bb5e43ae643e 5 //Serial pc(D10, D2);
hoting 0:bb5e43ae643e 6
hoting 0:bb5e43ae643e 7 union splitter {
hoting 0:bb5e43ae643e 8 short j;
hoting 0:bb5e43ae643e 9 char C[2];
hoting 0:bb5e43ae643e 10 // C[0]就會等於j的lowbyte, C[1]就會等於j的hibyte
hoting 0:bb5e43ae643e 11 };
hoting 0:bb5e43ae643e 12 char T[4] = {255,255,0,0};
hoting 0:bb5e43ae643e 13 int i = 0;
hoting 0:bb5e43ae643e 14 int k = 0;
hoting 0:bb5e43ae643e 15
hoting 0:bb5e43ae643e 16 //函式宣告
hoting 0:bb5e43ae643e 17 void init_TIMER();
hoting 0:bb5e43ae643e 18 void timer1_ITR();
hoting 0:bb5e43ae643e 19 void init_UART();
hoting 0:bb5e43ae643e 20 void UART_TX();
hoting 0:bb5e43ae643e 21
hoting 0:bb5e43ae643e 22 int main()
hoting 0:bb5e43ae643e 23 {
hoting 0:bb5e43ae643e 24 init_TIMER();
hoting 0:bb5e43ae643e 25 init_UART();
hoting 0:bb5e43ae643e 26 while(1) {
hoting 0:bb5e43ae643e 27 }
hoting 0:bb5e43ae643e 28 }
hoting 0:bb5e43ae643e 29
hoting 0:bb5e43ae643e 30 void init_TIMER()
hoting 0:bb5e43ae643e 31 {
hoting 0:bb5e43ae643e 32 timer1.attach_us(&timer1_ITR, 1000); // 1ms進一次timer1中斷
hoting 0:bb5e43ae643e 33 }
hoting 0:bb5e43ae643e 34
hoting 0:bb5e43ae643e 35 void timer1_ITR()
hoting 0:bb5e43ae643e 36 {
hoting 0:bb5e43ae643e 37 UART_TX();
hoting 0:bb5e43ae643e 38 }
hoting 0:bb5e43ae643e 39
hoting 0:bb5e43ae643e 40 void init_UART()
hoting 0:bb5e43ae643e 41 {
hoting 0:bb5e43ae643e 42 pc.baud(115200); // baud rate設為115200
hoting 0:bb5e43ae643e 43 }
hoting 0:bb5e43ae643e 44
hoting 0:bb5e43ae643e 45 void UART_TX()
hoting 0:bb5e43ae643e 46 {
hoting 0:bb5e43ae643e 47 splitter splitter1;
hoting 0:bb5e43ae643e 48
hoting 0:bb5e43ae643e 49 //數值範圍:-32768~32768
hoting 0:bb5e43ae643e 50 splitter1.j = -30000;
hoting 0:bb5e43ae643e 51
hoting 0:bb5e43ae643e 52 T[2] = splitter1.C[0];
hoting 0:bb5e43ae643e 53 T[3] = splitter1.C[1];
hoting 0:bb5e43ae643e 54
hoting 0:bb5e43ae643e 55 while (1) {
hoting 0:bb5e43ae643e 56 if (pc.writeable() == 1) {
hoting 0:bb5e43ae643e 57 pc.putc(T[i]);
hoting 0:bb5e43ae643e 58 i++;
hoting 0:bb5e43ae643e 59 k++;
hoting 0:bb5e43ae643e 60 }
hoting 0:bb5e43ae643e 61 if (k>1) {
hoting 0:bb5e43ae643e 62 k = 0;
hoting 0:bb5e43ae643e 63 break;
hoting 0:bb5e43ae643e 64 }
hoting 0:bb5e43ae643e 65 if (i>3) {
hoting 0:bb5e43ae643e 66 i = 0;
hoting 0:bb5e43ae643e 67 k = 0;
hoting 0:bb5e43ae643e 68 break;
hoting 0:bb5e43ae643e 69 }
hoting 0:bb5e43ae643e 70 }
hoting 0:bb5e43ae643e 71 }