/* X203Demo.c: Demo program for X203 Compiler: BC++ 3.1 Turbo C ++ 1.01(3.01) Compile mode: large Project: X203Demo.c ..\..\lib\7188el.lib ..\..\lib\XBoard\X203.lib Hardware: 7188EX + X203 [05/July/2005] by Liam [Dec 14, 2011] by Nicholas [March 16, 2012] by Nicholas */ /********************************************************************/ /* X203: 2 channel 12-Bit A/D (Analog input) 0-20mA */ /* 2 channel D/I */ /* 6 channel D/O */ /********************************************************************/ /********************************************************************/ /* [Software specific] */ /* Input/Output range: 0.0mA 10mA 20mA */ /* Decimal integer: 000 2047 4095 */ /* Hexadecimal: 0x000 0x800 0xFFF */ /* */ /* [A/D] */ /* sampling rate: 1800 data/sec (with floating convertion) */ /* Accuracy==> Maximum: +/- 0.05mA */ /* [D/O] */ /* throughput: can generates 2KHz square wave signals. */ /********************************************************************/ /********************************************************************/ /* [Caution] */ /* The block 7 first 16 bytes of EEPROM on X board is used to */ /* store A/D & D/A calibration settings. When you use the */ /* EEPROM on X board, don't overwrite it. */ /********************************************************************/ #include #include "..\..\lib\7188e.h" #include "..\..\lib\xboard\X203.h" void UserTimerFunction(void); void main(void) { int iRet, iAction, j, quit; int iValue, iInChannel, iDIValue, iOutChannel, iDOValue, iStatus; float fValue; int iFlag; InitLib(); X203_Init(); X203_SetDelay(5); //Set reading exchanged A/D channel delay time 0.05ms Puts("\r\nDemo program for 7188EX + X203\r\n"); iFlag=0; while(iAction!=11) { iAction=0; quit=0; Puts("\r\n"); Puts("1) Reads setting from EEPROM\r\n"); Puts("2) A/D (Analog input)\r\n"); Puts("3) A/D (Analog input) read 32767 times\n\r"); Puts("4) Read DI (all channels)\r\n"); Puts("5) Read DI (one channel)\r\n"); Puts("6) Write DO (all channels)\r\n"); Puts("7) Write DO (one channel)\r\n"); Puts("8) DO Readback (all channels)\r\n"); Puts("9) DO Readback (one channel)\r\n"); Puts("10) Start User's Timer Function to read A/D ch0, the interval is 2 sec\n\r"); Puts("\r\n"); Puts("11) Quits demo program\r\n\r\n"); Puts("Choose an option and press [Enter]:"); Scanf("%d", &iAction); Puts("\r\n"); switch(iAction) { // Reads settings from EEPROM(on X board) case 1: iValue=X203_Init(); if((iValue&1)==0) Print("EEPROM A/D Gain ==> [%8.6f]\r\n",X203_fAD_Gain); else Print("No setting in EEPROM, A/D Gain ==> %8.6f\r\n",X203_fAD_Gain); if((iValue&2)==0) Print("EEPROM A/D Offset ==> [%8.6f]\r\n\r\n",X203_fAD_Offset); else Print("No setting in EEPROM, A/D Offset ==> %8.6f\r\n\r\n",X203_fAD_Offset); break; // A/D (Analog input) case 2: Puts("Please select channel 0 ~ 1: "); Scanf("%d",&iInChannel); if(iInChannel>1) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } fValue=X203_AnalogIn(iInChannel); Print("AI[%d]= %6.4fmA\n\r\n\r", iInChannel, fValue); break; // A/D (Analog input) reads 32767 times case 3: Puts("Please select A/D channel(0~1): "); Scanf("%d",&iInChannel); if(iInChannel>1) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } fValue=X203_AnalogInHexToFloat(X203_AnalogIn_no(iInChannel,32767)); Print("AI[%d]= %6.4fmA\n\r\n\r", iInChannel, fValue); break; // Read DI (all channels) case 4: iDIValue=X203_Read_All_DI(); Print("Input value=> 0x%02x\r\n\r\n", iDIValue); break; // Read DI (one channel) case 5: Print("Please select inport channel(0~1): "); Scanf("%d", &iInChannel); if(iInChannel>1) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } iDIValue=X203_Read_One_DI(iInChannel); if(iDIValue) Print("Channel %d ==> OFF\r\n\r\n", iInChannel); else Print("Channel %d ==> ON\r\n\r\n", iInChannel); break; // Write DO (all channels) case 6: Print("Please input Output value(0 ~ 0x3f): "); Scanf("%x", &iDOValue); X203_Write_All_DO(iDOValue); Print("Output value=> 0x%02x\r\n\r\n", iDOValue); break; // Write DO (one channel) case 7: Print("Please select outport channel(0~5) and status(0:OFF, 1:ON)\n\r"); Print("Channel number and status:"); Scanf("%d %d", &iOutChannel, &iStatus); if(iOutChannel>5) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } X203_Write_One_DO(iOutChannel, iStatus); Print("Channel %d ==> %s\r\n\r\n", iOutChannel, (iStatus>=1)?"ON":"OFF"); break; // Read DO (all channels) case 8: iDOValue=X203_Read_All_DO(); Print("Digital Output ReadBack=> 0x%02x\r\n\r\n", iDOValue); break; // Read DO (one channels) case 9: Print("Please select outport channel(0~5): "); Scanf("%d", &iOutChannel); if(iOutChannel>5) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } iRet=X203_Read_One_DO(iOutChannel); if(iRet) Print("Channel %d ==> ON\r\n\r\n", iOutChannel); else Print("Channel %d ==> OFF\r\n\r\n", iOutChannel); break; //Start User's Timer Function, the interval is 2 sec case 10: if(iFlag==0) { iFlag=1; InstallUserTimerFunction_ms(2000,UserTimerFunction); } break; case 11: if(iFlag==1) StopUserTimerFun(); default: quit=1; break; } if(!quit) { Puts("Press any key to continue...\n\r"); Getch(); } } } void UserTimerFunction(void) { int iInChannel=0; float fValue; fValue=X203_AnalogIn(iInChannel); Print("AI[%d]= %6.4fmA\n\r\n\r", iInChannel, fValue); }