/* X308Demo.c: Demo program for 308 Compiler: BC++ 3.1 Turbo C ++ 1.01(3.01) Compile mode: large Project: X308Demo.c ..\..\Lib\7188el.lib ..\..\Lib\Xboard\X308.lib Hardware: 7188EX + X308 [May/20/2005] by Liam [06/July/2005] by Liam [Dec 23, 2010] by Nicholas [Dec, 14, 2011] by Nicholas [March,16,2012] by Nicholas /********************************************************************/ /* X308: 4-Channel 12-Bit A/D (Analog input) 0V ~ +10V */ /* 6-Channel D/O */ /********************************************************************/ /********************************************************************/ /* [Software specific] */ /* Input range: 0.0V 5.0V 5.0V 10.0V */ /* Decimal integer: 0 2047 2048 4095 */ /* Hexadecimal: 000 7FF 800 FFF */ /* */ /* [A/D] */ /* sampling rate: 1800 data/sec (with floating convertion) */ /* Resolution ==> Typical: +/- 1 LSB (+/- 2.4 mV) */ /* Maximum: +/- 2 LSB (+/- 4.8 mV) */ /* [D/O] */ /* throughput: can generates 2KHz square wave signals. */ /********************************************************************/ /********************************************************************/ /* [Caution] */ /* The EEPROM block 7 on X board is used to */ /* store A/D calibration settings. When you use the */ /* EEPROM on X board, don't overwrite it. */ /********************************************************************/ #include #include "..\..\lib\7188e.h" #include "..\..\lib\XBoard\X308.h" void UserTimerFunction(void); void main(void) { int iRet, iAction, j, quit , iFlag; int iValue, iInChannel, iDIValue, iOutChannel, iDOValue, iStatus; float fValue; InitLib(); X308_Init(); X308_SetDelay(5); //Set reading exchanged A/D channel delay time 0.05ms Puts("\n\rDemo program for 7188EX + X308\n\r"); iFlag=0; while(iAction!=9) { iAction=0; quit=0; Puts("\n\r"); Puts("Single-Ended 4-Channel\n\r"); Puts("1) Reads setting from EEPROM\n\r"); Puts("2) A/D (Analog input)\n\r"); Puts("3) A/D (Analog input) read 32767 times\n\r"); Puts("4) Write DO (all channels)\n\r"); Puts("5) Write DO (one channel)\n\r"); Puts("6) DO Readback (all channels)\n\r"); Puts("7) DO Readback (one channel)\n\r"); Puts("8) Start User's Timer Function to read A/D ch0, the interval is 2 sec\n\r"); Puts("\n\r"); Puts("9) Quits demo program\n\r\n\r"); Puts("Choose an option and press [Enter]:"); Scanf("%d", &iAction); Puts("\n\r"); switch(iAction) { // Reads settings from EEPROM(on X board) case 1: iValue=X308_Init(); if((iValue&1)==0) Print("EEPROM A/D Gain ==> [%8.6f]\n\r",X308_fAD_Gain); else Print("No setting in EEPROM, A/D Gain ==> %8.6f\n\r",X308_fAD_Gain); if((iValue&2)==0) Print("EEPROM A/D Offset ==> [%8.6f]\n\r",X308_fAD_Offset); else Print("No setting in EEPROM, A/D Offset ==> %8.6f\n\r",X308_fAD_Offset); Puts("\n\r"); break; // A/D (Analog input) case 2: Puts("Please select channel 0 ~ 3: "); Scanf("%d",&iInChannel); if(iInChannel>3) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } fValue=X308_AnalogIn(iInChannel); Print("AI[%d]= %6.4fV\n\r\n\r", iInChannel, fValue); break; // A/D (Analog input) reads 32767 times case 3: Puts("Please select A/D channel(0~3): "); Scanf("%d",&iInChannel); if(iInChannel>3) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } fValue=X308_AnalogInHexToFloat(X308_AnalogIn_no(iInChannel,32767)); Print("AI[%d]= %6.4fV\n\r\n\r", iInChannel, fValue); break; // Write DO (all channels) case 4: Print("Please input Output value(0 ~ 0x3f): "); Scanf("%x", &iDOValue); X308_Write_All_DO(iDOValue); Print("Output value=> 0x%02x\r\n\r\n", iDOValue); break; // Write DO (one channel) case 5: 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; } X308_Write_One_DO(iOutChannel, iStatus); Print("Channel %d ==> %s\r\n\r\n", iOutChannel, (iStatus>=1)?"ON":"OFF"); break; // Read DO (all channels) case 6: iDOValue=X308_Read_All_DO(); Print("Digital Output ReadBack=> 0x%02x\r\n\r\n", iDOValue); break; // Read DO (one channels) case 7: Print("Please select outport channel(0~5): "); Scanf("%d", &iOutChannel); if(iOutChannel>5) { Puts("Unknow Channel Number!!\r\n\r\n"); break; } iRet=X308_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 8: if(iFlag==0) { iFlag=1; InstallUserTimerFunction_ms(2000,UserTimerFunction); } break; case 9: 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=X308_AnalogIn(iInChannel); Print("AI[%d]= %6.4fV\n\r\n\r", iInChannel, fValue); }