Ljubivoj Cvitas
/
SerialMBEDtoMSP430
TVZ test
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 00003 Serial async_port(p9, p10); //set up TX and RX on pins 9 and 10 00004 DigitalOut red_led(LED4); //red led 00005 DigitalOut green_led(LED3); //green led 00006 DigitalOut RXDIN (LED1); 00007 00008 DigitalIn switch_ip1(p5); 00009 DigitalIn switch_ip2(p6); 00010 DigitalIn switch_ip3(p7); 00011 DigitalIn switch_ip4(p8); 00012 00013 Serial pc(USBTX, USBRX); 00014 00015 char header_read ; //first byte we will send 00016 char param_ads; // parameter address 00017 char recd_val; //the received value 00018 int valueADC; 00019 char Outbuff[12]; 00020 char Inbuff[12]; 00021 int valueSensA, valueSensB, valueSensC, valueSensD; 00022 float fAzimuthErr; 00023 float fElevationErr; 00024 float fSensACalib; 00025 unsigned char *pointnavar; 00026 00027 int main() 00028 { 00029 async_port.baud(9600); //set baud rate to 9600 (ie default) 00030 switch_ip1.mode(PullUp); // Pull up internal resistor 00031 switch_ip2.mode(PullUp); 00032 switch_ip3.mode(PullUp); 00033 switch_ip4.mode(PullUp); 00034 fSensACalib= 35.77; 00035 00036 //accept default format, of 8 bits, no parity 00037 while (1) { 00038 00039 // read or write mode ? 00040 if (switch_ip1==0) { 00041 header_read=0x10; // read output pattern 00042 if (switch_ip2==0) { 00043 param_ads = 0; 00044 } else { 00045 param_ads = 1; 00046 } 00047 00048 /* 00049 valueADC = 0x0302; 00050 Outbuff[0] = valueADC; 00051 Outbuff[1] = valueADC >> 8; */ 00052 00053 while (async_port.writeable()==0); //is there a place to write? 00054 async_port.putc(header_read); //transmit haeder 00055 while (async_port.writeable()==0); //is there a place to write? 00056 async_port.putc(param_ads); //transmit parameter address 00057 00058 00059 00060 if (async_port.readable()==1) { //is there a character to be read? 00061 Inbuff[0] = async_port.getc(); //if yes, t 00062 } 00063 if (async_port.readable()==1) { //is there a character to be read? 00064 Inbuff[1] = async_port.getc(); //if yes, t 00065 } 00066 if (async_port.readable()==1) { //is there a character to be read? 00067 Inbuff[2] = async_port.getc(); //if yes, t 00068 } 00069 if (async_port.readable()==1) { //is there a character to be read? 00070 Inbuff[3] = async_port.getc(); //if yes, t 00071 } 00072 if (async_port.readable()==1) { //is there a character to be read? 00073 Inbuff[4] = async_port.getc(); //if yes, t 00074 } 00075 if (async_port.readable()==1) { //is there a character to be read? 00076 Inbuff[5] = async_port.getc(); //if yes, t 00077 } 00078 if (async_port.readable()==1) { //is there a character to be read? 00079 Inbuff[6] = async_port.getc(); //if yes, t 00080 } 00081 if (async_port.readable()==1) { //is there a character to be read? 00082 Inbuff[7] = async_port.getc(); //if yes, t 00083 } 00084 if (async_port.readable()==1) { //is there a character to be read? 00085 Inbuff[8] = async_port.getc(); //if yes, t 00086 } 00087 if (async_port.readable()==1) { //is there a character to be read? 00088 Inbuff[9] = async_port.getc(); //if yes, t 00089 } 00090 if (async_port.readable()==1) { //is there a character to be read? 00091 Inbuff[10] = async_port.getc(); //if yes, t 00092 } 00093 00094 RXDIN = 1; 00095 wait(1); 00096 RXDIN = 0; 00097 if (param_ads==0x00) { 00098 valueSensA = Inbuff[2]+ Inbuff[3]*256; 00099 valueSensB = Inbuff[4]+ Inbuff[5]*256; 00100 valueSensC = Inbuff[6]+ Inbuff[7]*256; 00101 valueSensD = Inbuff[8]+ Inbuff[9]*256; 00102 pc.printf("SensA: %d SensB: %d SensC: %d SensD: %d \n\r", valueSensA, valueSensB, valueSensC, valueSensD); 00103 } 00104 if (param_ads==0x01) { 00105 pointnavar = (unsigned char*) &fAzimuthErr; 00106 for (int i=2; i<6; i++) { 00107 *pointnavar = Inbuff[i]; 00108 pointnavar++; 00109 } 00110 pointnavar = (unsigned char*) &fElevationErr; 00111 for (int i=6; i<10; i++) { 00112 *pointnavar = Inbuff[i]; 00113 pointnavar++; 00114 } 00115 pc.printf("AZIMUT: %+3.0f ELEVATION; %+3.0f \n\r", fAzimuthErr, fElevationErr); 00116 } 00117 } // if (switch_ip1==0) 00118 00119 else { 00120 header_read=0x20; // write output pattern 00121 Outbuff[0] = header_read; 00122 param_ads = 2; // parameter to write 00123 Outbuff[1] = param_ads; 00124 pointnavar = (unsigned char*) &fSensACalib; 00125 for (int i=2; i<6; i++) { 00126 Outbuff[i] = *pointnavar ; 00127 pointnavar++; 00128 } 00129 Outbuff[6]=0; // checksum 00130 for (int i=0; i<6; i++) 00131 { Outbuff[6] += Outbuff[i]; 00132 } 00133 00134 00135 for (int i=0; i<7; i++) { 00136 while (async_port.writeable()==0); //is there a place to write? 00137 async_port.putc(Outbuff[i]); //transmit byte 00138 } 00139 RXDIN = 1; 00140 wait(1); 00141 RXDIN = 0; 00142 00143 00144 } // else 00145 } // while(1) 00146 } //main
Generated on Mon Jul 18 2022 22:47:04 by 1.7.2