/* XW310C.c: Demo program for XW310 Compiler: BC++ 3.1 Turbo C++ 1.01 (3.01) Compile mode: large Project: XW310C.c ..\..\lib\upac5000.lib ..\..\lib\XWBoard\XW310C.lib Hardware: uPAC-5000 + XW310C [Jul 02, 2014] by Liam */ /*****************************************************************/ /* XW310C: 8/4 channel 12-Bit A/D (Analog input) 0~20mA */ /* 2 channel 12-Bit D/A (Analog output) 0~20mA */ /* 3 channel D/I */ /* 3 channel D/O */ /*****************************************************************/ #include #include "..\..\lib\uPAC5000.h" #include "..\lib\XW310C.h" void main(void) { int i, iRet, iOption, iQuit=0; unsigned uLibVersion; char szLibDate[16]; int iChannel, iDOValue, iState; float fValue; InitLib(); /* Initiate the XW310 library*/ if((iRet=XW310C_Init())!=0) { Print("XW310C initiate failed [%X]\n", iRet); return; } Puts("\n"); Puts("*****************************************\n"); Puts("| Demo program for uPAC-5000 + XW310C |\n"); Puts("| |\n"); Puts("| [Jul 02, 2014] |\n"); Puts("*****************************************\n"); /* Read the version information of XW310C library */ uLibVersion=XW310C_GetLibVersion(); XW310C_GetLibDate(szLibDate); Print("\nXW310C.lib version %X.%02X, %s\r\n", uLibVersion>>8, uLibVersion&0xFF, szLibDate); while(!iQuit) { Puts("\r\n"); Puts("1) Read the Calibration Data\r\n"); Puts("2) Analog Input (20mA)\r\n"); Puts("3) Analog Output (20mA)\r\n"); 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("\r\n"); Puts("0) Quit demo program\r\n\r\n"); Puts("Choose an option and press [Enter]: "); Scanf("%d", &iOption); Puts("\r\n"); switch(iOption) { case 1: if((iRet&0x01)==0) { for(i=0; i<8; i++) { Print("Ch%d A/D Gain/Offset ==> [%8.6f/%8.6f]\r\n", i, XW310C_Read_AD_Gain(i), XW310C_Read_AD_Offset(i)); } } else Print("No setting in EEPROM\r\n"); Putch('\n'); if((iRet&0x02)==0) { for(i=0; i<2; i++) { Print("Ch%d D/A Gain/Offset ==> [%8.6f/%8.6f]\r\n", i, XW310C_Read_DA_Gain(i), XW310C_Read_DA_Offset(i)); } } else Print("No setting in EEPROM\r\n"); break; case 2: Puts("Channel (0~7): "); Scanf("%d", &iChannel); if(iChannel>7) { Puts("Unknow Channel Number!!\r\n"); break; } Print("AI%d => %6.4f mA\r\n", iChannel, XW310C_AnalogIn(iChannel)); break; case 3: Puts("Channel (0~1): "); Scanf("%d", &iChannel); if(iChannel>1) { Puts("Unknow Channel Number!!\r\n"); break; } Puts("Current (0 ~ 20.0 mA): "); Scanf("%f", &fValue); XW310C_AnalogOut(iChannel, fValue); Print("AO%d => %7.4f mA\r\n", iChannel, fValue); break; case 4: Print("Input state => 0x%02X\r\n", XW310C_Read_All_DI()); break; case 5: Print("DI Channel (0~2): "); Scanf("%d", &iChannel); if(iChannel>2) { Puts("Unknow Channel Number!!\r\n"); break; } iDOValue=XW310C_Read_One_DI(iChannel); Print("DI%d => %s\r\n", iChannel, iDOValue==0?"ON":"OFF"); break; case 6: Print("DO Output state (0~7): "); Scanf("%x", &iDOValue); XW310C_Write_All_DO(iDOValue); Print("Output state => 0x%02X\r\n", iDOValue); break; case 7: Print("DO Channel (0~2) and status (0:OFF, 1:ON): "); Scanf("%d %d", &iChannel, &iState); if(iChannel>2) { Puts("Unknow Channel Number!!\r\n"); break; } XW310C_Write_One_DO(iChannel, iState); Print("DO%d => %s\r\n", iChannel, iState==0?"OFF":"ON"); break; case 8: Print("DO ReadBack => 0x%02X\r\n", XW310C_Read_All_DO()); break; case 9: Print("DO Channel (0~2): "); Scanf("%d", &iChannel); if(iChannel>2) { Puts("Unknow Channel Number!!\r\n"); break; } iDOValue=XW310C_Read_One_DO(iChannel); Print("DO%d => %s\r\n", iChannel, iDOValue==0?"OFF":"ON"); break; case 0: iQuit=1; break; default: break; } if(!iQuit) { Puts("\r\nPress any key to continue...\r\n"); Getch(); } } }