test

Dependencies:   MINI8I8O5A000 mbed EEPROM_P4 IICLCD_LOWCOST

Committer:
tsuparit
Date:
Thu Oct 08 10:44:42 2015 +0000
Revision:
0:5e60f85d6c26
Child:
1:d9ab13cdf377
Add test code for serial  in out

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsuparit 0:5e60f85d6c26 1 float firmware_version = 1.00;
tsuparit 0:5e60f85d6c26 2
tsuparit 0:5e60f85d6c26 3 #include "mbed.h"
tsuparit 0:5e60f85d6c26 4 #include "LiquidCrystal_I2C.h"
tsuparit 0:5e60f85d6c26 5 #include "mini8i8o5a.h"
tsuparit 0:5e60f85d6c26 6 #define EEPROMBUFFERSIZE 16
tsuparit 0:5e60f85d6c26 7 #include "eepromlib.h"
tsuparit 0:5e60f85d6c26 8
tsuparit 0:5e60f85d6c26 9 #ifdef USBSerial
tsuparit 0:5e60f85d6c26 10 #include "USBSerial.h"
tsuparit 0:5e60f85d6c26 11 USBSerial pc;
tsuparit 0:5e60f85d6c26 12
tsuparit 0:5e60f85d6c26 13 #else
tsuparit 0:5e60f85d6c26 14 Serial pc(USBTX, USBRX);
tsuparit 0:5e60f85d6c26 15 #endif
tsuparit 0:5e60f85d6c26 16
tsuparit 0:5e60f85d6c26 17
tsuparit 0:5e60f85d6c26 18 DigitalOut myled(LED1);
tsuparit 0:5e60f85d6c26 19
tsuparit 0:5e60f85d6c26 20
tsuparit 0:5e60f85d6c26 21
tsuparit 0:5e60f85d6c26 22 char str[50];
tsuparit 0:5e60f85d6c26 23 char strbuf[50];
tsuparit 0:5e60f85d6c26 24
tsuparit 0:5e60f85d6c26 25
tsuparit 0:5e60f85d6c26 26 int rxchar_count=0;
tsuparit 0:5e60f85d6c26 27 #define RXBUFFERSIZE 100
tsuparit 0:5e60f85d6c26 28 char rx_buffer[RXBUFFERSIZE];
tsuparit 0:5e60f85d6c26 29 char rx_buffer_CMD[RXBUFFERSIZE];
tsuparit 0:5e60f85d6c26 30 char rx_buffer_ARG[RXBUFFERSIZE];
tsuparit 0:5e60f85d6c26 31 char rx_pointer=0;
tsuparit 0:5e60f85d6c26 32 char inchar=0;
tsuparit 0:5e60f85d6c26 33
tsuparit 0:5e60f85d6c26 34 char remote_control=0;
tsuparit 0:5e60f85d6c26 35
tsuparit 0:5e60f85d6c26 36 float ser_para_reset=0;
tsuparit 0:5e60f85d6c26 37 //float ser_para_stab=3.25;
tsuparit 0:5e60f85d6c26 38 float ser_para_time=1.19;
tsuparit 0:5e60f85d6c26 39 float ser_para_result=0.0;
tsuparit 0:5e60f85d6c26 40
tsuparit 0:5e60f85d6c26 41 char ser_para_state=0;
tsuparit 0:5e60f85d6c26 42 char ser_para_err=0;
tsuparit 0:5e60f85d6c26 43 char ser_para_range=0;
tsuparit 0:5e60f85d6c26 44
tsuparit 0:5e60f85d6c26 45 char ser_trigger=0;
tsuparit 0:5e60f85d6c26 46 char ser_para_cont_mode=1; //serial continue print out
tsuparit 0:5e60f85d6c26 47
tsuparit 0:5e60f85d6c26 48 Timer com_timer;
tsuparit 0:5e60f85d6c26 49
tsuparit 0:5e60f85d6c26 50
tsuparit 0:5e60f85d6c26 51 char boardaddress =1;
tsuparit 0:5e60f85d6c26 52
tsuparit 0:5e60f85d6c26 53 void serial_pc_rx_services()
tsuparit 0:5e60f85d6c26 54 {
tsuparit 0:5e60f85d6c26 55 int i,j;
tsuparit 0:5e60f85d6c26 56
tsuparit 0:5e60f85d6c26 57 while(pc.readable())
tsuparit 0:5e60f85d6c26 58 {
tsuparit 0:5e60f85d6c26 59 myled = 0;
tsuparit 0:5e60f85d6c26 60 inchar =pc.getc();
tsuparit 0:5e60f85d6c26 61 wait(0.1);
tsuparit 0:5e60f85d6c26 62 myled = 1;
tsuparit 0:5e60f85d6c26 63
tsuparit 0:5e60f85d6c26 64 if ( rx_pointer==0) //first incoming character
tsuparit 0:5e60f85d6c26 65 {
tsuparit 0:5e60f85d6c26 66 for (i=0;i<RXBUFFERSIZE;i++) rx_buffer[i]=0; //clear old buffer
tsuparit 0:5e60f85d6c26 67 com_timer.reset();
tsuparit 0:5e60f85d6c26 68 com_timer.start();
tsuparit 0:5e60f85d6c26 69 }
tsuparit 0:5e60f85d6c26 70
tsuparit 0:5e60f85d6c26 71 rx_buffer[rx_pointer]=inchar; //keep char in buffer...
tsuparit 0:5e60f85d6c26 72
tsuparit 0:5e60f85d6c26 73 rx_pointer++;
tsuparit 0:5e60f85d6c26 74
tsuparit 0:5e60f85d6c26 75
tsuparit 0:5e60f85d6c26 76 if (rx_pointer>=(RXBUFFERSIZE-10))
tsuparit 0:5e60f85d6c26 77 {
tsuparit 0:5e60f85d6c26 78 //rx_pointer=200;
tsuparit 0:5e60f85d6c26 79 //rx_buffer[rx_pointer]=0;
tsuparit 0:5e60f85d6c26 80 rx_pointer=0;//Error buffer overflow
tsuparit 0:5e60f85d6c26 81 pc.printf("ERR:buffer overflow\n",firmware_version);
tsuparit 0:5e60f85d6c26 82 }
tsuparit 0:5e60f85d6c26 83
tsuparit 0:5e60f85d6c26 84 if ((inchar=='\r')|(inchar=='\n'))
tsuparit 0:5e60f85d6c26 85 {
tsuparit 0:5e60f85d6c26 86 remote_control=1;
tsuparit 0:5e60f85d6c26 87 rx_buffer[rx_pointer-1]=0;//mark end of string, remove terminate char
tsuparit 0:5e60f85d6c26 88
tsuparit 0:5e60f85d6c26 89 i=0;
tsuparit 0:5e60f85d6c26 90 j=0;
tsuparit 0:5e60f85d6c26 91 char cmd_done=0;
tsuparit 0:5e60f85d6c26 92 while (rx_buffer[i]!=0)
tsuparit 0:5e60f85d6c26 93 {
tsuparit 0:5e60f85d6c26 94 if (cmd_done==0)
tsuparit 0:5e60f85d6c26 95 {
tsuparit 0:5e60f85d6c26 96 rx_buffer_CMD[i]=rx_buffer[i];//copy cmd part
tsuparit 0:5e60f85d6c26 97 if (rx_buffer_CMD[i]==' ')
tsuparit 0:5e60f85d6c26 98 {
tsuparit 0:5e60f85d6c26 99 cmd_done=1;
tsuparit 0:5e60f85d6c26 100 rx_buffer_CMD[i+1]=0;
tsuparit 0:5e60f85d6c26 101 }
tsuparit 0:5e60f85d6c26 102 }
tsuparit 0:5e60f85d6c26 103 else
tsuparit 0:5e60f85d6c26 104 {
tsuparit 0:5e60f85d6c26 105 rx_buffer_ARG[j]=rx_buffer[i];//copy cmd part
tsuparit 0:5e60f85d6c26 106 j++;
tsuparit 0:5e60f85d6c26 107 }
tsuparit 0:5e60f85d6c26 108 i++;
tsuparit 0:5e60f85d6c26 109
tsuparit 0:5e60f85d6c26 110 }
tsuparit 0:5e60f85d6c26 111 rx_buffer_ARG[j]=0; //end of string
tsuparit 0:5e60f85d6c26 112
tsuparit 0:5e60f85d6c26 113 //pc.printf("rxbuffer: %s",rx_buffer); //test direct echo
tsuparit 0:5e60f85d6c26 114
tsuparit 0:5e60f85d6c26 115 if (strcmp(rx_buffer,"*IDN?")==0)
tsuparit 0:5e60f85d6c26 116 {
tsuparit 0:5e60f85d6c26 117 pc.printf("Schaffner SerIO, ver %.1f\n",firmware_version);
tsuparit 0:5e60f85d6c26 118 }
tsuparit 0:5e60f85d6c26 119 else if (strcmp(rx_buffer,"*RST")==0)
tsuparit 0:5e60f85d6c26 120 {
tsuparit 0:5e60f85d6c26 121 pc.printf("Instrumnet Reset\n");
tsuparit 0:5e60f85d6c26 122 ser_para_reset=1; //pass flag stop all operation
tsuparit 0:5e60f85d6c26 123 //LCDI2C4Bit_init();
tsuparit 0:5e60f85d6c26 124 LiquidCrystal_I2Cinit();
tsuparit 0:5e60f85d6c26 125 LCDI2C4Bit_clear();
tsuparit 0:5e60f85d6c26 126 //LCDI2C4Bit_backLight(true);
tsuparit 0:5e60f85d6c26 127 LiquidCrystal_I2Cbacklight();
tsuparit 0:5e60f85d6c26 128
tsuparit 0:5e60f85d6c26 129 LCDI2C4Bit_cursorTo(0,0);
tsuparit 0:5e60f85d6c26 130 sprintf (str," --- RESET ----");
tsuparit 0:5e60f85d6c26 131 LCDI2C4Bit_printIn(str);
tsuparit 0:5e60f85d6c26 132 wait(0.25);
tsuparit 0:5e60f85d6c26 133 NVIC_SystemReset();//softreser
tsuparit 0:5e60f85d6c26 134 }
tsuparit 0:5e60f85d6c26 135 else if (strcmp(rx_buffer,":SYS:VER")==0)
tsuparit 0:5e60f85d6c26 136 {
tsuparit 0:5e60f85d6c26 137 pc.printf("%.1f\n",firmware_version);
tsuparit 0:5e60f85d6c26 138 }
tsuparit 0:5e60f85d6c26 139 else if (strcmp(rx_buffer,":SYST:ERR")==0)
tsuparit 0:5e60f85d6c26 140 {
tsuparit 0:5e60f85d6c26 141 pc.printf("0\n");
tsuparit 0:5e60f85d6c26 142 }
tsuparit 0:5e60f85d6c26 143 //else if (strcmp(rx_buffer,"a11000")==0) //Read Input "aX1"
tsuparit 0:5e60f85d6c26 144 else if ( (rx_buffer[0]=='a') & (rx_buffer[1]==('0'+boardaddress)) & (rx_buffer[2]=='1')) // Write Output "aX1"
tsuparit 0:5e60f85d6c26 145 {
tsuparit 0:5e60f85d6c26 146 pc.printf("OK%d\n",11);
tsuparit 0:5e60f85d6c26 147 }
tsuparit 0:5e60f85d6c26 148 else if ( (rx_buffer[0]=='a') & (rx_buffer[1]==('0'+boardaddress)) & (rx_buffer[2]=='2')) // Write Output "aX2"
tsuparit 0:5e60f85d6c26 149 {
tsuparit 0:5e60f85d6c26 150 pc.printf("OK%d\n",22);
tsuparit 0:5e60f85d6c26 151 }
tsuparit 0:5e60f85d6c26 152 else if ( (rx_buffer[0]=='a') & (rx_buffer[1]==('0'+boardaddress)) & (rx_buffer[2]=='3')) // Write Output "aX2"
tsuparit 0:5e60f85d6c26 153 {
tsuparit 0:5e60f85d6c26 154 pc.printf("OK%d\n",33);
tsuparit 0:5e60f85d6c26 155 }
tsuparit 0:5e60f85d6c26 156 else
tsuparit 0:5e60f85d6c26 157 {//unknow / handle comand
tsuparit 0:5e60f85d6c26 158 pc.printf( "UNKNOWN %s=>%s,%s\n",rx_buffer,rx_buffer_CMD,rx_buffer_ARG);
tsuparit 0:5e60f85d6c26 159 }
tsuparit 0:5e60f85d6c26 160
tsuparit 0:5e60f85d6c26 161 rx_pointer=0;//Reset at the end
tsuparit 0:5e60f85d6c26 162 com_timer.stop();
tsuparit 0:5e60f85d6c26 163 }
tsuparit 0:5e60f85d6c26 164
tsuparit 0:5e60f85d6c26 165 }
tsuparit 0:5e60f85d6c26 166
tsuparit 0:5e60f85d6c26 167
tsuparit 0:5e60f85d6c26 168 }
tsuparit 0:5e60f85d6c26 169
tsuparit 0:5e60f85d6c26 170
tsuparit 0:5e60f85d6c26 171 int main()
tsuparit 0:5e60f85d6c26 172 {
tsuparit 0:5e60f85d6c26 173
tsuparit 0:5e60f85d6c26 174
tsuparit 0:5e60f85d6c26 175
tsuparit 0:5e60f85d6c26 176
tsuparit 0:5e60f85d6c26 177 LiquidCrystal_I2C_SETUP(0x27,16,2);
tsuparit 0:5e60f85d6c26 178 LiquidCrystal_I2Cinit();
tsuparit 0:5e60f85d6c26 179 LiquidCrystal_I2Cbacklight();
tsuparit 0:5e60f85d6c26 180
tsuparit 0:5e60f85d6c26 181 LiquidCrystal_I2CsetCursor(0,1);
tsuparit 0:5e60f85d6c26 182 sprintf (str, " SCHAFFNER ");
tsuparit 0:5e60f85d6c26 183 LCDI2C4Bit_printIn(str);
tsuparit 0:5e60f85d6c26 184 LiquidCrystal_I2CsetCursor(0,0);
tsuparit 0:5e60f85d6c26 185 sprintf (str, "SerailDIO v:%.2f ",1.00) ;
tsuparit 0:5e60f85d6c26 186 LCDI2C4Bit_printIn(str);
tsuparit 0:5e60f85d6c26 187
tsuparit 0:5e60f85d6c26 188
tsuparit 0:5e60f85d6c26 189 pc.baud(9600); // set what you want here depending on your terminal program speed
tsuparit 0:5e60f85d6c26 190
tsuparit 0:5e60f85d6c26 191 //pc.printf( "RST\n");
tsuparit 0:5e60f85d6c26 192 com_timer.start();
tsuparit 0:5e60f85d6c26 193 com_timer.stop();
tsuparit 0:5e60f85d6c26 194 com_timer.reset();
tsuparit 0:5e60f85d6c26 195
tsuparit 0:5e60f85d6c26 196 wait(1);
tsuparit 0:5e60f85d6c26 197
tsuparit 0:5e60f85d6c26 198
tsuparit 0:5e60f85d6c26 199 while(1)
tsuparit 0:5e60f85d6c26 200 {
tsuparit 0:5e60f85d6c26 201 serial_pc_rx_services();
tsuparit 0:5e60f85d6c26 202
tsuparit 0:5e60f85d6c26 203 LiquidCrystal_I2CsetCursor(0,0);
tsuparit 0:5e60f85d6c26 204 sprintf (str, " I:%d O:%d ",255,255);
tsuparit 0:5e60f85d6c26 205 LCDI2C4Bit_printIn(str);
tsuparit 0:5e60f85d6c26 206
tsuparit 0:5e60f85d6c26 207
tsuparit 0:5e60f85d6c26 208 LiquidCrystal_I2CsetCursor(0,1);
tsuparit 0:5e60f85d6c26 209 sprintf (str, "%.2fs:%s ",com_timer.read(),rx_buffer);
tsuparit 0:5e60f85d6c26 210 LCDI2C4Bit_printIn(str);
tsuparit 0:5e60f85d6c26 211 if (com_timer.read()>5.0)
tsuparit 0:5e60f85d6c26 212 {
tsuparit 0:5e60f85d6c26 213 rx_pointer=0;//Reset at the end
tsuparit 0:5e60f85d6c26 214 com_timer.stop();
tsuparit 0:5e60f85d6c26 215 }
tsuparit 0:5e60f85d6c26 216
tsuparit 0:5e60f85d6c26 217 }
tsuparit 0:5e60f85d6c26 218
tsuparit 0:5e60f85d6c26 219
tsuparit 0:5e60f85d6c26 220 }