/*************************************************************************** * SAMPLE2.CPP * * This sample program will read the 16 channels of an AIM-16 attached to * channel 0 of an AD12-8. The first channel is the reference junction, the * second is a type "t" thermocouple, and the rest are voltage channels. The * program uses timer-driven interrupts to perform the conversions. * * LAST MODIFICATION: FEBRUARY 16, 1994 * ****************************************************************************/ #define STRICT #include #include #include #include #include "sample2.h" #include "a12drv.h" #define BASEADDRESS 0x300 // Base address for the A/D card. BOOL conversions; HINSTANCE thisinstance; void DrawScreen(HWND hWnd); // function prototype // Call this function to print a driver error void DriverError(int error_code) { char buffer[30]; sprintf(buffer,"Driver Error Number: %d",error_code); MessageBox(NULL,buffer,"AD12-8 Driver",MB_OK | MB_ICONEXCLAMATION); } // This routine initializes the driver, and sets up the point list and // channels as described in the header. int InitDriver(void) { int error_code; // Initialize the driver and get a card number error_code = AD128_Init(MANUAL,BASEADDRESS,BIPOLAR_5); if (error_code) return error_code; // Setup channels, channel 0 is reference junction, with 0 gain code, channe1 // is type t thermocouple with gain code 5(gain = 200) and the other channels // set to measure voltage by setting the scaling facters to -5.0 and 5.0. All // temperatures are degrees C. error_code = AD128_SetPointConfig(0,0,CURVE_T,0.0,0.0,DEGREESC,0,0); if (error_code) return error_code; error_code = AD128_SetPointConfig(1,1,CURVE_t,0.0,0.0,DEGREESC,5,0); if (error_code) return error_code; error_code = AD128_SetPointConfig(2,15,NO_CURVE,-5.0,5.0,0,0,0); if (error_code) return error_code; // Setup point list, place points 0 thru 15 in the point list. error_code = AD128_AddPoints(0,15); if (error_code) return error_code; // Setup AIM-16 sample and hold settle time. AD128_SetSettleTime(50); return 0; } // This routine uninstalls the driver in preperation for exiting the program. void ShutDownDriver() { AD128_Shutdown(); } // Callback function for the Aboutbox dialog box #pragma argsused BOOL CALLBACK About(HWND hDlg,UINT message,WPARAM wParam, LPARAM lParam) { switch (message) { case WM_INITDIALOG:return TRUE; case WM_COMMAND: if (wParam == IDOK || wParam == IDCANCEL) { EndDialog(hDlg, TRUE); return TRUE; } break; } return FALSE; } //Callback function for the setup dialog box. #pragma argsused BOOL CALLBACK SetupBox(HWND Setup,UINT iMessage,WPARAM wParam, LPARAM lParam) { switch (iMessage) { case WM_INITDIALOG: return TRUE; case WM_COMMAND: if (wParam == IDOK || wParam == IDCANCEL) { EndDialog(Setup,TRUE); return TRUE; } break; } return FALSE; } // Main program callback function long CALLBACK WndProc(HWND hWnd,UINT iMessage,WPARAM wParam, LPARAM lParam) { FARPROC dlgproc; switch (iMessage) { case WM_COMMAND: switch(wParam) { case SETUP: // Do the setup dialog box dlgproc = MakeProcInstance((FARPROC)SetupBox,thisinstance); DialogBox(thisinstance,"SETUP_DIALOG",hWnd,(DLGPROC) dlgproc); FreeProcInstance(dlgproc); break; case STARTCONV: // Start conversions conversions = TRUE; break; case STOPCONV: // End conversions conversions = FALSE; break; case ABOUT: // Do the about dialog box. dlgproc = MakeProcInstance((FARPROC)About,thisinstance); DialogBox(thisinstance,"ABOUTBOX",hWnd,(DLGPROC) dlgproc); FreeProcInstance(dlgproc); break; case EXIT: conversions = FALSE; ShutDownDriver(); PostQuitMessage(0); break; } break; // End the program case WM_WINDOWPOSCHANGED: DrawScreen(hWnd); // refresh main window if altered break; case WM_DESTROY: conversions = FALSE; ShutDownDriver(); PostQuitMessage(0); break; default:return DefWindowProc(hWnd,iMessage,wParam,lParam); } return NULL; } // Draw the channel labels in the main window void DrawScreen(HWND hWnd) { HDC hdc; hdc = GetDC(hWnd); SetBkColor(hdc, RGB(192, 192, 192)); TextOut(hdc,200,40,"CHANNEL 0:",11); TextOut(hdc,200,60,"CHANNEL 1:",11); TextOut(hdc,200,80,"CHANNEL 2:",11); TextOut(hdc,200,100,"CHANNEL 3:",11); TextOut(hdc,200,120,"CHANNEL 4:",11); TextOut(hdc,200,140,"CHANNEL 5:",11); TextOut(hdc,200,160,"CHANNEL 6:",11); TextOut(hdc,200,180,"CHANNEL 7:",11); TextOut(hdc,200,200,"CHANNEL 8:",11); TextOut(hdc,200,220,"CHANNEL 9:",11); TextOut(hdc,200,240,"CHANNEL 10:",11); TextOut(hdc,200,260,"CHANNEL 11:",11); TextOut(hdc,200,280,"CHANNEL 12:",11); TextOut(hdc,200,300,"CHANNEL 13:",11); TextOut(hdc,200,320,"CHANNEL 14:",11); TextOut(hdc,200,340,"CHANNEL 15:",11); ReleaseDC(hWnd,hdc); } // This routine is called when no messages are in our program's queue. void IdleAction(HWND hWnd) { float conv[16]; char buffer[12]; HDC hdc; int error_code,scans,convs; unsigned long count; if (conversions) { // Set the counters error_code = AD128_RateGenerator(1000.0); // Perform the conversions, 1 scan of 16 points. if (!error_code) error_code = AD128_IRQScan(1,16,0,5,TIMER,NULL); if (error_code) { DriverError(error_code); conversions = FALSE; AD128_DisableCounter(1,0); // Disable counters AD128_DisableCounter(2,0); return; } // Wait for process to complete count = 0; do { count++; AD128_IRQStatus(&scans,&convs); /* Could put other instructions here to perform any required functions while waiting. A better way than this is to use the Windows message function supported by this driver function. See the reference part of the manual for details. */ } while (scans < 1 && convs < 16 && count < 100000l); if (count >= 100000l) error_code = TIMEOUT; // Shut off counters AD128_DisableCounter(1,0); AD128_DisableCounter(2,0); // Do post processing to fetch the conversions from the driver, same // parameters as PollScan, with the addition of the address of conv. if (!error_code) error_code = AD128_PostProcess(1,16,0,conv); if (error_code) { DriverError(error_code); conversions = FALSE; return; } // Update the screen with the conversion values hdc = GetDC(hWnd); SetBkColor(hdc, RGB(192, 192, 192)); for (int x = 0;x < 16;x++) { sprintf(buffer,"%10.4f",conv[x]); TextOut(hdc,325,x * 20 + 40," ", 20); TextOut(hdc,325,x * 20 + 40,buffer,strlen(buffer)); } ReleaseDC(hWnd,hdc); } } // Entry point, registers our main window and creates an instance of it, then // sets up the driver and enters the message loop. #pragma argsused int PASCAL WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpszCmdLine,int nCmdShow) { HWND hWnd; WNDCLASS wndclass; // Structure used to register Windows class. MSG msg; // If no previous instance then reigster our window class if ( !hPrevInstance ) { wndclass.cbClsExtra = 0; wndclass.cbWndExtra = 0; wndclass.hbrBackground = (HBRUSH) GetStockObject(LTGRAY_BRUSH); wndclass.hInstance = hInstance; wndclass.hIcon = LoadIcon(NULL,IDI_APPLICATION); wndclass.hCursor = LoadCursor(NULL,IDC_ARROW); wndclass.lpszMenuName = "SAMPLE2_MENU"; wndclass.lpfnWndProc = WndProc; wndclass.lpszClassName = "SAMPLE2"; wndclass.style = NULL; if (!RegisterClass(&wndclass)) return FALSE; } thisinstance = hInstance; // Create our window instance and display it hWnd = CreateWindow("Sample2","AD12-8 Sample Two Program",WS_OVERLAPPEDWINDOW, 0,0,640,480,NULL,NULL,hInstance,NULL); if (!hWnd) return FALSE; ShowWindow(hWnd,nCmdShow ); UpdateWindow(hWnd); // Setup the driver conversions = FALSE; DrawScreen(hWnd); if (InitDriver()) return 0; // This is our message loop, which will execute until GetMessage detects a WM_QUIT // message. while (TRUE) { // PeekMessage will not wait for a message for our queue, but returns // to allow us to do processing. When conversions are started we will do one // conversion pass whenever PeekMessage returns with no message in the queue. if (PeekMessage(&msg,0,0,0,PM_NOREMOVE)) { if (GetMessage(&msg,0,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } else return msg.wParam; } else IdleAction(hWnd); // Perform conversions. } }