![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
TVZ test
main.cpp@7:08f81f5b4760, 2014-03-06 (annotated)
- Committer:
- cvitas
- Date:
- Thu Mar 06 12:08:42 2014 +0000
- Revision:
- 7:08f81f5b4760
- Parent:
- 6:be9abe81bbd1
verzija 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
cvitas | 0:5918d7a4ad04 | 1 | #include "mbed.h" |
cvitas | 0:5918d7a4ad04 | 2 | |
cvitas | 0:5918d7a4ad04 | 3 | Serial async_port(p9, p10); //set up TX and RX on pins 9 and 10 |
cvitas | 0:5918d7a4ad04 | 4 | DigitalOut red_led(LED4); //red led |
cvitas | 0:5918d7a4ad04 | 5 | DigitalOut green_led(LED3); //green led |
cvitas | 0:5918d7a4ad04 | 6 | DigitalOut RXDIN (LED1); |
cvitas | 4:4620184bbb1f | 7 | |
cvitas | 0:5918d7a4ad04 | 8 | DigitalIn switch_ip1(p5); |
cvitas | 0:5918d7a4ad04 | 9 | DigitalIn switch_ip2(p6); |
cvitas | 4:4620184bbb1f | 10 | DigitalIn switch_ip3(p7); |
cvitas | 4:4620184bbb1f | 11 | DigitalIn switch_ip4(p8); |
cvitas | 4:4620184bbb1f | 12 | |
cvitas | 7:08f81f5b4760 | 13 | Serial pc(USBTX, USBRX); |
cvitas | 0:5918d7a4ad04 | 14 | |
cvitas | 3:a4a5d9c0bc13 | 15 | char header_read ; //first byte we will send |
cvitas | 3:a4a5d9c0bc13 | 16 | char param_ads; // parameter address |
cvitas | 4:4620184bbb1f | 17 | char recd_val; //the received value |
cvitas | 0:5918d7a4ad04 | 18 | int valueADC; |
cvitas | 3:a4a5d9c0bc13 | 19 | char Outbuff[12]; |
cvitas | 3:a4a5d9c0bc13 | 20 | char Inbuff[12]; |
cvitas | 4:4620184bbb1f | 21 | int valueSensA, valueSensB, valueSensC, valueSensD; |
cvitas | 4:4620184bbb1f | 22 | float fAzimuthErr; |
cvitas | 4:4620184bbb1f | 23 | float fElevationErr; |
cvitas | 5:77ff08b129c0 | 24 | float fSensACalib; |
cvitas | 4:4620184bbb1f | 25 | unsigned char *pointnavar; |
cvitas | 4:4620184bbb1f | 26 | |
cvitas | 4:4620184bbb1f | 27 | int main() |
cvitas | 4:4620184bbb1f | 28 | { |
cvitas | 4:4620184bbb1f | 29 | async_port.baud(9600); //set baud rate to 9600 (ie default) |
cvitas | 4:4620184bbb1f | 30 | switch_ip1.mode(PullUp); // Pull up internal resistor |
cvitas | 4:4620184bbb1f | 31 | switch_ip2.mode(PullUp); |
cvitas | 4:4620184bbb1f | 32 | switch_ip3.mode(PullUp); |
cvitas | 4:4620184bbb1f | 33 | switch_ip4.mode(PullUp); |
cvitas | 5:77ff08b129c0 | 34 | fSensACalib= 35.77; |
cvitas | 4:4620184bbb1f | 35 | |
cvitas | 4:4620184bbb1f | 36 | //accept default format, of 8 bits, no parity |
cvitas | 4:4620184bbb1f | 37 | while (1) { |
cvitas | 4:4620184bbb1f | 38 | |
cvitas | 4:4620184bbb1f | 39 | // read or write mode ? |
cvitas | 4:4620184bbb1f | 40 | if (switch_ip1==0) { |
cvitas | 4:4620184bbb1f | 41 | header_read=0x10; // read output pattern |
cvitas | 4:4620184bbb1f | 42 | if (switch_ip2==0) { |
cvitas | 4:4620184bbb1f | 43 | param_ads = 0; |
cvitas | 4:4620184bbb1f | 44 | } else { |
cvitas | 4:4620184bbb1f | 45 | param_ads = 1; |
cvitas | 4:4620184bbb1f | 46 | } |
cvitas | 4:4620184bbb1f | 47 | |
cvitas | 4:4620184bbb1f | 48 | /* |
cvitas | 4:4620184bbb1f | 49 | valueADC = 0x0302; |
cvitas | 4:4620184bbb1f | 50 | Outbuff[0] = valueADC; |
cvitas | 4:4620184bbb1f | 51 | Outbuff[1] = valueADC >> 8; */ |
cvitas | 4:4620184bbb1f | 52 | |
cvitas | 4:4620184bbb1f | 53 | while (async_port.writeable()==0); //is there a place to write? |
cvitas | 4:4620184bbb1f | 54 | async_port.putc(header_read); //transmit haeder |
cvitas | 4:4620184bbb1f | 55 | while (async_port.writeable()==0); //is there a place to write? |
cvitas | 4:4620184bbb1f | 56 | async_port.putc(param_ads); //transmit parameter address |
cvitas | 4:4620184bbb1f | 57 | |
cvitas | 4:4620184bbb1f | 58 | |
cvitas | 4:4620184bbb1f | 59 | |
cvitas | 4:4620184bbb1f | 60 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 61 | Inbuff[0] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 62 | } |
cvitas | 4:4620184bbb1f | 63 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 64 | Inbuff[1] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 65 | } |
cvitas | 4:4620184bbb1f | 66 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 67 | Inbuff[2] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 68 | } |
cvitas | 4:4620184bbb1f | 69 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 70 | Inbuff[3] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 71 | } |
cvitas | 4:4620184bbb1f | 72 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 73 | Inbuff[4] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 74 | } |
cvitas | 4:4620184bbb1f | 75 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 76 | Inbuff[5] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 77 | } |
cvitas | 4:4620184bbb1f | 78 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 79 | Inbuff[6] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 80 | } |
cvitas | 4:4620184bbb1f | 81 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 82 | Inbuff[7] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 83 | } |
cvitas | 4:4620184bbb1f | 84 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 85 | Inbuff[8] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 86 | } |
cvitas | 4:4620184bbb1f | 87 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 88 | Inbuff[9] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 89 | } |
cvitas | 4:4620184bbb1f | 90 | if (async_port.readable()==1) { //is there a character to be read? |
cvitas | 4:4620184bbb1f | 91 | Inbuff[10] = async_port.getc(); //if yes, t |
cvitas | 4:4620184bbb1f | 92 | } |
cvitas | 4:4620184bbb1f | 93 | |
cvitas | 4:4620184bbb1f | 94 | RXDIN = 1; |
cvitas | 4:4620184bbb1f | 95 | wait(1); |
cvitas | 4:4620184bbb1f | 96 | RXDIN = 0; |
cvitas | 4:4620184bbb1f | 97 | if (param_ads==0x00) { |
cvitas | 4:4620184bbb1f | 98 | valueSensA = Inbuff[2]+ Inbuff[3]*256; |
cvitas | 4:4620184bbb1f | 99 | valueSensB = Inbuff[4]+ Inbuff[5]*256; |
cvitas | 4:4620184bbb1f | 100 | valueSensC = Inbuff[6]+ Inbuff[7]*256; |
cvitas | 4:4620184bbb1f | 101 | valueSensD = Inbuff[8]+ Inbuff[9]*256; |
cvitas | 4:4620184bbb1f | 102 | pc.printf("SensA: %d SensB: %d SensC: %d SensD: %d \n\r", valueSensA, valueSensB, valueSensC, valueSensD); |
cvitas | 4:4620184bbb1f | 103 | } |
cvitas | 4:4620184bbb1f | 104 | if (param_ads==0x01) { |
cvitas | 4:4620184bbb1f | 105 | pointnavar = (unsigned char*) &fAzimuthErr; |
cvitas | 4:4620184bbb1f | 106 | for (int i=2; i<6; i++) { |
cvitas | 4:4620184bbb1f | 107 | *pointnavar = Inbuff[i]; |
cvitas | 4:4620184bbb1f | 108 | pointnavar++; |
cvitas | 4:4620184bbb1f | 109 | } |
cvitas | 4:4620184bbb1f | 110 | pointnavar = (unsigned char*) &fElevationErr; |
cvitas | 4:4620184bbb1f | 111 | for (int i=6; i<10; i++) { |
cvitas | 4:4620184bbb1f | 112 | *pointnavar = Inbuff[i]; |
cvitas | 4:4620184bbb1f | 113 | pointnavar++; |
cvitas | 4:4620184bbb1f | 114 | } |
cvitas | 4:4620184bbb1f | 115 | pc.printf("AZIMUT: %+3.0f ELEVATION; %+3.0f \n\r", fAzimuthErr, fElevationErr); |
cvitas | 4:4620184bbb1f | 116 | } |
cvitas | 4:4620184bbb1f | 117 | } // if (switch_ip1==0) |
cvitas | 5:77ff08b129c0 | 118 | |
cvitas | 4:4620184bbb1f | 119 | else { |
cvitas | 5:77ff08b129c0 | 120 | header_read=0x20; // write output pattern |
cvitas | 5:77ff08b129c0 | 121 | Outbuff[0] = header_read; |
cvitas | 4:4620184bbb1f | 122 | param_ads = 2; // parameter to write |
cvitas | 5:77ff08b129c0 | 123 | Outbuff[1] = param_ads; |
cvitas | 5:77ff08b129c0 | 124 | pointnavar = (unsigned char*) &fSensACalib; |
cvitas | 5:77ff08b129c0 | 125 | for (int i=2; i<6; i++) { |
cvitas | 5:77ff08b129c0 | 126 | Outbuff[i] = *pointnavar ; |
cvitas | 5:77ff08b129c0 | 127 | pointnavar++; |
cvitas | 5:77ff08b129c0 | 128 | } |
cvitas | 6:be9abe81bbd1 | 129 | Outbuff[6]=0; // checksum |
cvitas | 6:be9abe81bbd1 | 130 | for (int i=0; i<6; i++) |
cvitas | 6:be9abe81bbd1 | 131 | { Outbuff[6] += Outbuff[i]; |
cvitas | 6:be9abe81bbd1 | 132 | } |
cvitas | 6:be9abe81bbd1 | 133 | |
cvitas | 5:77ff08b129c0 | 134 | |
cvitas | 5:77ff08b129c0 | 135 | for (int i=0; i<7; i++) { |
cvitas | 5:77ff08b129c0 | 136 | while (async_port.writeable()==0); //is there a place to write? |
cvitas | 5:77ff08b129c0 | 137 | async_port.putc(Outbuff[i]); //transmit byte |
cvitas | 5:77ff08b129c0 | 138 | } |
cvitas | 6:be9abe81bbd1 | 139 | RXDIN = 1; |
cvitas | 6:be9abe81bbd1 | 140 | wait(1); |
cvitas | 6:be9abe81bbd1 | 141 | RXDIN = 0; |
cvitas | 5:77ff08b129c0 | 142 | |
cvitas | 5:77ff08b129c0 | 143 | |
cvitas | 5:77ff08b129c0 | 144 | } // else |
cvitas | 4:4620184bbb1f | 145 | } // while(1) |
cvitas | 4:4620184bbb1f | 146 | } //main |