Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of JTNucleo06PulsedPS_Encoder_RS232 by
main.cpp
00001 #include "mbed.h" 00002 #include "TextLCD.h" 00003 #include "CRotaryEncoder.h" 00004 00005 #define DAC_ADDR (0xc0) 00006 #define ADC_ADDR (0xD0) 00007 #define BlankString " " 00008 00009 //bool DEBUGON = 1; 00010 bool DEBUGON = 0; 00011 00012 bool TxEmpty = 0; 00013 bool SerialAvailable = 0; 00014 uint8_t r0,r1,r2,r3,r4,r5,r6,r7; 00015 uint8_t ChRead; 00016 char SerialBuffer[50]; 00017 00018 Ticker OneSecTicker; 00019 uint8_t CursorPos = 0; 00020 00021 I2C i2c(D4,D5); //I2C i2c(I2C_SDA, I2C_SCL); 00022 00023 //InterruptIn EncoderButton(PA_7); 00024 DigitalIn EncoderButton(PA_7); 00025 00026 TextLCD lcd(D12, D11, D10, D9, D8, D7,TextLCD::LCD20x4); // rs, e, d4-d7 00027 CRotaryEncoder Encoder(D3,D6); //PinA,PinB 00028 int EncoderCount = 0; 00029 00030 DigitalOut MCP4728Ldac(PA_6,1); 00031 DigitalIn MCP4728Rdy(PA_5); 00032 static uint16_t DACvalue = 0; 00033 00034 Ticker HMSec; 00035 uint8_t NowChannel=0; 00036 00037 char Buffer[21]; 00038 00039 DigitalOut myled(LED1); 00040 00041 //Serial pc(SERIAL_TX, SERIAL_RX); 00042 Serial Rs232(PA_9, PA_10); 00043 00044 /* 00045 typedef struct { 00046 __IO uint32_t FIODIR; 00047 uint32_t RESERVED0[3]; 00048 __IO uint32_t FIOMASK; 00049 __IO uint32_t FIOPIN; 00050 __IO uint32_t FIOSET; 00051 __O uint32_t FIOCLR; 00052 } LPC_GPIO_TypeDef; 00053 */ 00054 typedef struct { 00055 uint16_t Ch0; 00056 uint16_t Ch1; 00057 uint16_t Ch2; 00058 uint16_t Ch3; 00059 } StructDAC; 00060 00061 typedef struct { 00062 uint16_t HexData[4]; 00063 float RealData[4]; 00064 float CalibData[4]; 00065 } StructADC; 00066 00067 StructADC ADCData; 00068 StructDAC DACData; 00069 00070 00071 bool mcp4728_setChannel(uint8_t channel, bool UseInternalVRef, uint8_t powerDownMode, bool use2xGain, uint16_t value){ 00072 char buf[3]; 00073 buf[0] = 0x40 | ((channel & 0x3) << 1); 00074 00075 buf[1] = ((uint8_t)UseInternalVRef << 7) | ((powerDownMode & 0x3) << 5) | ((uint8_t)use2xGain << 4); 00076 buf[1] |= (value & 0x0f00)>>8; 00077 00078 buf[2] = value & 0xff; 00079 00080 return i2c.write(DAC_ADDR, buf, 3, 0) == 0; 00081 } 00082 00083 int dac_test(void){ 00084 // static uint16_t value = 0; 00085 int ret; 00086 00087 // MCP4728Ldac = 0; 00088 if(DACvalue == 0xfff) DACvalue = 0; 00089 else DACvalue = 0xfff; 00090 ret = mcp4728_setChannel(0, 1, 0, 1, DACvalue); 00091 // DACvalue += 4; 00092 // MCP4728Ldac = 1; 00093 00094 if(!ret) return -2; 00095 00096 return 1; 00097 } 00098 00099 void EncoderButtonPressed(){ 00100 int cnt; 00101 cnt = Encoder.Get(); 00102 cnt += 10; 00103 Encoder.Set(cnt); 00104 if(DEBUGON) DEBUGON = 0; 00105 else DEBUGON = 1; 00106 } 00107 void SetDAC(uint8_t ChNo,uint16_t Value){ 00108 switch(ChNo){ 00109 case 0: 00110 DACData.Ch0 = Value; 00111 mcp4728_setChannel(0, 1, 0, 1, Value); 00112 break; 00113 case 1: 00114 DACData.Ch1 = Value; 00115 mcp4728_setChannel(1, 1, 0, 1, Value); 00116 break; 00117 case 2: 00118 DACData.Ch2 = Value; 00119 mcp4728_setChannel(2, 1, 0, 1, Value); 00120 break; 00121 case 3: 00122 DACData.Ch3 = Value; 00123 mcp4728_setChannel(3, 1, 0, 1, Value); 00124 break; 00125 } 00126 } 00127 void UpdateDAC(){ 00128 mcp4728_setChannel(0, 1, 0, 1, DACData.Ch0); 00129 mcp4728_setChannel(1, 1, 0, 1, DACData.Ch1); 00130 mcp4728_setChannel(2, 1, 0, 1, DACData.Ch2); 00131 mcp4728_setChannel(3, 1, 0, 1, DACData.Ch3); 00132 } 00133 void ResetDAC(){ 00134 DACData.Ch0 = 0; 00135 DACData.Ch1 = 0; 00136 DACData.Ch2 = 0; 00137 DACData.Ch3 = 0; 00138 UpdateDAC(); 00139 } 00140 void FullDAC(){ 00141 DACData.Ch0 = 0xfff; 00142 DACData.Ch1 = 0xfff; 00143 DACData.Ch2 = 0xfff; 00144 DACData.Ch3 = 0xfff; 00145 UpdateDAC(); 00146 } 00147 bool mcp3428_writeConfig(uint8_t conf){ 00148 return i2c.write(ADC_ADDR, (char*) &conf, 1, 0) == 0; 00149 } 00150 00151 bool mcp3428_read(uint16_t* data, uint8_t* conf){ 00152 char buf[3]; 00153 int ret = i2c.read(ADC_ADDR, buf, 3, 0); 00154 if(ret != 0) 00155 return false; 00156 *data = buf[0] << 8 | buf[1]; 00157 *conf = buf[2]; 00158 return true; 00159 } 00160 00161 int ReadADC(uint8_t ChNo){ 00162 uint8_t conf = 0x98; 00163 uint16_t data = 0; 00164 00165 00166 switch(ChNo){ 00167 case 0: 00168 conf = 0x98; 00169 break; 00170 case 1: 00171 conf = 0xb8; 00172 break; 00173 case 2: 00174 conf = 0xd8; 00175 break; 00176 case 3: 00177 conf = 0xf8; 00178 break; 00179 } 00180 if(!mcp3428_writeConfig(conf)) return -1; 00181 wait(0.1); 00182 int ret = mcp3428_read(&data, &conf); 00183 //if(DEBUGON) Rs232.printf("CONF: %02x ChannelNo: %02d \n",conf,ChNo); 00184 if(!ret) return -2; 00185 return data; 00186 } 00187 void ConvertNegative(uint8_t ChNo,int d){ 00188 if(d & 0x8000) { 00189 d &= 0x7fff; 00190 d = (d ^ 0x07fff) + 1; 00191 ADCData.HexData[ChNo] = d; 00192 ADCData.RealData[ChNo] = ADCData.HexData[ChNo] * ADCData.CalibData[1] / 32768.0; 00193 ADCData.RealData[ChNo] *= (-1.0); 00194 } 00195 else{ 00196 ADCData.HexData[ChNo] = d; 00197 ADCData.RealData[ChNo] = ADCData.HexData[ChNo] * ADCData.CalibData[ChNo] / 32768.0; 00198 } 00199 } 00200 void ReadADCConverted(){ 00201 int adData=0; 00202 bool Negative=0; 00203 00204 adData = ReadADC(0); 00205 if ( (adData == -1) || (adData == -2) ) goto next1; 00206 ConvertNegative(0,adData); 00207 next1: 00208 adData = ReadADC(1); 00209 if ( (adData == -1) || (adData == -2) ) goto next2; 00210 ConvertNegative(1,adData); 00211 next2: 00212 adData = ReadADC(2); 00213 if ( (adData == -1) || (adData == -2) ) goto next3; 00214 ConvertNegative(2,adData); 00215 next3: 00216 adData = ReadADC(3); 00217 if ( (adData == -1) || (adData == -2) ) goto next4; 00218 ConvertNegative(3,adData); 00219 next4: 00220 return; 00221 } 00222 /* 00223 void ReadADCTicker(){ 00224 uint8_t conf = 0x98; 00225 uint16_t data = 0; 00226 00227 switch(NowChannel){ 00228 case 0:conf = 0x98;break; 00229 case 1:conf = 0xb8;break; 00230 case 2:conf = 0xd8;break; 00231 case 3:conf = 0xf8;break; 00232 } 00233 00234 int ret = mcp3428_read(&data, &conf); 00235 if(!ret) goto NextCh; 00236 // mcp3428_read(&data, &conf); 00237 ADCData.HexData[NowChannel] = data & 0x7fff; 00238 00239 ADCData.RealData[NowChannel] = ( (ADCData.HexData[NowChannel] * ADCData.CalibData[NowChannel] / 32768.0) ); 00240 NextCh: 00241 NowChannel++; 00242 if(NowChannel > 3) NowChannel = 0; 00243 00244 switch(NowChannel){ 00245 case 0:conf = 0x98;break; 00246 case 1:conf = 0xb8;break; 00247 case 2:conf = 0xd8;break; 00248 case 3:conf = 0xf8;break; 00249 } 00250 mcp3428_writeConfig(conf); 00251 00252 } 00253 */ 00254 int adc_test(void){ 00255 // uint8_t conf = 0x90; 00256 uint8_t conf = 0x98; 00257 uint16_t data = 0; 00258 00259 if(!mcp3428_writeConfig(conf)) return -1; 00260 00261 int ret = mcp3428_read(&data, &conf); 00262 00263 if(!ret) return -2; 00264 00265 return data; 00266 00267 } 00268 void HMSecRoutine(){ 00269 // ReadADCTicker(); 00270 } 00271 void LCDPrintDataADC(){ 00272 // lcd.cls(); 00273 sprintf(Buffer,"Ch1: %6.3f V",ADCData.RealData[0]); 00274 lcd.locate(0,0); 00275 lcd.printf(BlankString); 00276 lcd.locate(0,0); 00277 lcd.printf(Buffer); 00278 00279 sprintf(Buffer,"Ch2: %6.3f V",ADCData.RealData[1]); 00280 lcd.locate(0,1); 00281 lcd.printf(BlankString); 00282 lcd.locate(0,1); 00283 lcd.printf(Buffer); 00284 00285 sprintf(Buffer,"Ch3: %6.3f V",ADCData.RealData[2]); 00286 lcd.locate(0,2); 00287 lcd.printf(BlankString); 00288 lcd.locate(0,2); 00289 lcd.printf(Buffer); 00290 00291 sprintf(Buffer,"Ch4: %6.3f V",ADCData.RealData[3]); 00292 lcd.locate(0,3); 00293 lcd.printf(BlankString); 00294 lcd.locate(0,3); 00295 lcd.printf(Buffer); 00296 00297 // Rs232printf("adc_test: %5.3f %5.3f %5.3f %5.3f\n", ADCData.RealData[0],ADCData.RealData[1],ADCData.RealData[2],ADCData.RealData[3]); 00298 00299 00300 } 00301 void LCDPrintDataDAC(){ 00302 // lcd.cls(); 00303 sprintf(Buffer," Ch1: %4d",DACData.Ch0); 00304 lcd.locate(0,0); 00305 lcd.printf(BlankString); 00306 lcd.locate(0,0); 00307 lcd.printf(Buffer); 00308 00309 sprintf(Buffer," Ch2: %4d",DACData.Ch1); 00310 lcd.locate(0,1); 00311 lcd.printf(BlankString); 00312 lcd.locate(0,1); 00313 lcd.printf(Buffer); 00314 00315 sprintf(Buffer," Ch3: %4d",DACData.Ch2); 00316 lcd.locate(0,2); 00317 lcd.printf(BlankString); 00318 lcd.locate(0,2); 00319 lcd.printf(Buffer); 00320 00321 sprintf(Buffer," Ch4: %4d",DACData.Ch3); 00322 lcd.locate(0,3); 00323 lcd.printf(BlankString); 00324 lcd.locate(0,3); 00325 lcd.printf(Buffer); 00326 00327 while(EncoderButton == 0); 00328 00329 } 00330 00331 void DisplayCursor(){ 00332 00333 switch(CursorPos){ 00334 case 0: 00335 lcd.locate(0,0);lcd.printf(">"); 00336 lcd.locate(0,1);lcd.printf(" "); 00337 lcd.locate(0,2);lcd.printf(" "); 00338 lcd.locate(0,3);lcd.printf(" "); 00339 break; 00340 case 1: 00341 lcd.locate(0,0);lcd.printf(" "); 00342 lcd.locate(0,1);lcd.printf(">"); 00343 lcd.locate(0,2);lcd.printf(" "); 00344 lcd.locate(0,3);lcd.printf(" "); 00345 break; 00346 case 2: 00347 lcd.locate(0,0);lcd.printf(" "); 00348 lcd.locate(0,1);lcd.printf(" "); 00349 lcd.locate(0,2);lcd.printf(">"); 00350 lcd.locate(0,3);lcd.printf(" "); 00351 break; 00352 case 3: 00353 lcd.locate(0,0);lcd.printf(" "); 00354 lcd.locate(0,1);lcd.printf(" "); 00355 lcd.locate(0,2);lcd.printf(" "); 00356 lcd.locate(0,3);lcd.printf(">"); 00357 break; 00358 } 00359 } 00360 void ProcessEncoder(){ 00361 uint16_t val; 00362 switch(CursorPos){ 00363 case 0: val = DACData.Ch0;break; 00364 case 1: val = DACData.Ch1;break; 00365 case 2: val = DACData.Ch2;break; 00366 case 3: val = DACData.Ch3;break; 00367 } 00368 Encoder.Set(val); 00369 here: 00370 val = Encoder.Get(); 00371 lcd.locate(0,3); 00372 lcd.printf("%04d",val); 00373 if(Encoder.Get() < 0) Encoder.Set(0); 00374 if(Encoder.Get() > 4095) Encoder.Set(4095); 00375 if(EncoderButton == 0){ 00376 while(EncoderButton == 0); 00377 val = Encoder.Get(); 00378 switch(CursorPos) { 00379 case 0: 00380 DACData.Ch0 = val; 00381 SetDAC(0,DACData.Ch0); 00382 break; 00383 case 1: 00384 DACData.Ch1 = val; 00385 SetDAC(1,DACData.Ch1); 00386 break; 00387 case 2: 00388 DACData.Ch2 = val; 00389 SetDAC(2,DACData.Ch2); 00390 break; 00391 case 3: 00392 DACData.Ch3 = val; 00393 SetDAC(3,DACData.Ch3); 00394 break; 00395 } 00396 return; 00397 } 00398 goto here; 00399 } 00400 void SetTheDacData(){ 00401 lcd.cls(); 00402 lcd.locate(0,2); 00403 switch(CursorPos){ 00404 case 0:lcd.printf("Set CH%02d Value",CursorPos+1);break; 00405 case 1:lcd.printf("Set CH%02d Value",CursorPos+1);break; 00406 case 2:lcd.printf("Set CH%02d Value",CursorPos+1);break; 00407 case 3:lcd.printf("Set CH%02d Value",CursorPos+1);break; 00408 } 00409 ProcessEncoder(); 00410 } 00411 void SetDac(){ 00412 int ECount; 00413 CursorPos = 0; 00414 here1: 00415 Encoder.Set(CursorPos); 00416 LCDPrintDataDAC(); 00417 while(1){ 00418 ECount = Encoder.Get(); 00419 if(ECount > 3){ECount = 0;CursorPos = 0;Encoder.Set(CursorPos);return;} 00420 else { CursorPos = ECount;Encoder.Set(CursorPos);} 00421 DisplayCursor(); 00422 if(EncoderButton == 0){ 00423 while(EncoderButton == 0); 00424 SetTheDacData(); 00425 goto here1; 00426 } 00427 } 00428 } 00429 00430 void PushToBuffer(){ 00431 r0 = r1; 00432 r1 = r2; 00433 r2 = r3; 00434 r3 = r4; 00435 r4 = r5; 00436 r5 = r6; 00437 r6 = r7; 00438 r7 = ChRead; 00439 if((r0==':')&&(r7==';')) SerialAvailable = 1; 00440 } 00441 00442 void Rx_interrupt() { 00443 while(Rs232.readable()){ 00444 ChRead = Rs232.getc(); 00445 PushToBuffer(); 00446 } 00447 } 00448 00449 void Tx_interrupt() { 00450 // TxEmpty = 1; 00451 return; 00452 } 00453 00454 void SendSerial(){ 00455 uint8_t ch,i; 00456 TxEmpty = 1; 00457 for(i=0;i<=49;i++){ 00458 ch = SerialBuffer[i]; 00459 if(ch == 0){ 00460 while(!TxEmpty); 00461 TxEmpty = 0; 00462 Rs232.putc(0x0d); 00463 while(!TxEmpty); 00464 TxEmpty = 0; 00465 Rs232.putc(0x0a); 00466 return; 00467 } 00468 while(!TxEmpty); 00469 TxEmpty = 0; 00470 Rs232.putc(ch); 00471 } 00472 00473 } 00474 void SerialDataSend(){ 00475 Rs232.printf("ADCChannels: %6.3f %6.3f %6.3f %6.3f\n", ADCData.RealData[0],ADCData.RealData[1],ADCData.RealData[2],ADCData.RealData[3]); 00476 } 00477 void PrintRs232(){ 00478 lcd.cls(); 00479 lcd.locate(0,2);lcd.printf("SerialCommand"); 00480 lcd.locate(0,3);lcd.putc(r0); 00481 lcd.locate(1,3);lcd.putc(r1); 00482 lcd.locate(2,3);lcd.putc(r2); 00483 lcd.locate(3,3);lcd.putc(r3); 00484 lcd.locate(4,3);lcd.putc(r4); 00485 lcd.locate(5,3);lcd.putc(r5); 00486 lcd.locate(6,3);lcd.putc(r6); 00487 lcd.locate(7,3);lcd.putc(r7); 00488 } 00489 void SetSerialDAC(){ 00490 uint8_t ch; 00491 uint16_t val; 00492 ch = r2 - '0'; 00493 val = (r3 - '0')*1000; 00494 val += (r4 - '0')*100; 00495 val += (r5 - '0')*10; 00496 val += (r6 - '0')*1; 00497 if(val > 4095) val = 4095; 00498 if(val < 0) val = 0; 00499 if((ch >= 0) && (ch < 4)) SetDAC(ch,val); 00500 else return; 00501 } 00502 00503 00504 void ParseCom(){ 00505 if(SerialAvailable){ 00506 PrintRs232(); 00507 switch(r1){ 00508 case '3'://DAC Command Set Value; 00509 SetSerialDAC(); 00510 break; 00511 case '4'://DAC Command Set Zero; 00512 ResetDAC(); 00513 break; 00514 case '5'://DAC Command Set Full; 00515 FullDAC(); 00516 break; 00517 default:break; 00518 } 00519 } 00520 SerialAvailable = 0; 00521 } 00522 int main() { 00523 uint16_t Counter=0; 00524 // EncoderButton.fall(&EncoderButtonPressed); 00525 // EncoderButton.enable_irq (); 00526 00527 00528 Rs232.attach(&Rx_interrupt, Serial::RxIrq); 00529 Rs232.attach(&Tx_interrupt, Serial::TxIrq); 00530 00531 OneSecTicker.attach(&SerialDataSend,1.0); 00532 00533 lcd.cls(); 00534 MCP4728Ldac = 0; 00535 ResetDAC(); 00536 //ForHighVoltageModule 00537 ADCData.CalibData[0] = 10.08; 00538 ADCData.CalibData[1] = 10.079; 00539 ADCData.CalibData[2] = 10.096; 00540 ADCData.CalibData[3] = 10.095; 00541 00542 DACData.Ch0 = 0; 00543 DACData.Ch1 = 0; 00544 DACData.Ch2 = 0; 00545 DACData.Ch3 = 0; 00546 00547 lcd.locate(0,0); 00548 // lcd.printf("Test Jig Module "); 00549 lcd.printf("4 Ch HV Module "); 00550 wait(2); 00551 00552 while(1){ 00553 ReadADCConverted(); 00554 LCDPrintDataADC(); 00555 ParseCom(); 00556 if(EncoderButton == 0){ 00557 SetDac(); 00558 } 00559 wait(1); 00560 myled = !myled; 00561 } 00562 00563 } 00564 00565 00566 00567 00568 00569 00570
Generated on Thu Jul 21 2022 10:38:01 by
1.7.2
