|
楼主 |
发表于 2008-11-11 11:17
|
显示全部楼层
2.vc的demo程序
//
#include "stdafx.h"
#include "resource.h"
#include "adt620.h"
#include "stdlib.h"
#define ID_TIMER 1//设定一个定时器的ID值
HANDLE hDevice=0;//加载设备驱动程序的句柄
unsigned int count=0; // 计数值
unsigned int BaseAddr=0x300; // 基地址
unsigned char irq=7; // 中断号
unsigned char IOport0=1,IOport1=1,IOport2=1; // 三个数字I/O口都为输入状态
unsigned char IOport; // 数字I/O口
unsigned char outbyte,inbyte; // 数字I/O输入输出数值
float result; // 模拟输入电压值
int ADValue; // A/D变换的数据
int channel=0; // 模拟输入通道
HANDLE hEvent=0; // AD采样事件句柄
HWND hAdDlg; // 窗口句柄
char buffer[512];
//应用程序窗口的消息处理函数
BOOL CALLBACK DlgProc(HWND, WPARAM, WPARAM,LPARAM);
void ftoa(float source,char destination[10],char precision);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
DialogBox(hInstance, (LPCTSTR)IDD_DIALOG1,0, DlgProc);
return 0;
}
// AD采样事件的线程被驱动程序唤醒,用来通知应用程序中断到来并且显示中断记录
DWORD WINAPI ServiceThread(PVOID hEvent)
{
while (TRUE)
{
WaitForSingleObject(hEvent, INFINITE);
while (ADT620_ConversionDone(hDevice) == 0); // 监测A/D变换是否结束
ADValue = ADT620_ReadData(hDevice); // 读取A/D变换的数据
result=ADT620_DigitToAnalog(hDevice,ADValue); // 转换为模拟输入电压值
ftoa(result,buffer,2);
SetDlgItemText(hAdDlg,IDC_ADVALUE,buffer);
ADT620_ClearIRQ(hDevice); // 清除中断
}
return 0;
}
void hevent() // AD采样事件
{
DWORD Tid;
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
ADT620_SetEvent(hDevice,hEvent); // 产生一个事件并且发送到驱动程序
CreateThread(0, 0x1000, ServiceThread, (PVOID)hEvent, 0, &Tid); // 产生一个等待AD采样事件的线程
}
BOOL CALLBACK DlgProc(HWND hDlg, WPARAM uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CLOSE://关闭窗口消息
if(hDevice)
{
if(hEvent) CloseHandle(hEvent);
KillTimer (hDlg, ID_TIMER);
ADT620_DevClose(hDevice);//卸载设备驱动程序
}
EndDialog(hDlg,TRUE);//关闭对话框
break;
case WM_TIMER://定时器消息
if(!hDevice)
break;
ADT620_StartConversion(hDevice); // 启动A/D变换
break;
case WM_COMMAND:
switch (wParam)
{
case Load_Driver: //加载设备驱动程序
if(hDevice)
break;
hDevice=ADT620_DevLoad();
if(hDevice==INVALID_HANDLE_VALUE)
{
MessageBox(hDlg,"Load driver Error","error",MB_OK);
ExitProcess(1);
}
EnableWindow(GetDlgItem(hDlg,Load_Driver),FALSE);
break;
case Initial: // 初始化
if(!hDevice)
break;
ADT620_InitBoard(hDevice,BaseAddr,irq);
if(!hEvent)
{
ADT620_SetIRQStatus(hDevice,1); // 设置中断使能
hevent(); // AD采样事件
hAdDlg=hDlg;
}
EnableWindow(GetDlgItem(hDlg,Initial),FALSE);
break;
case Read_IO: // 读数字I/O
if(!hDevice)
break;
IOport = (unsigned char)GetDlgItemInt(hDlg,IDC_RPORT,NULL,FALSE);
if(IOport == 0) IOport0=1;
else if(IOport == 1) IOport1=1;
else if(IOport == 2) IOport2=1;
ADT620_ConfigIOPorts(hDevice,IOport0,IOport1,IOport2);
inbyte = ADT620_ReadDigitIO(hDevice,IOport);
SetDlgItemInt(hDlg,IDC_RVALUE,inbyte,FALSE);
break;
case Write_IO: // 写数字I/O
if(!hDevice)
break;
IOport = (unsigned char)GetDlgItemInt(hDlg,IDC_WPORT,NULL,FALSE);
outbyte = (unsigned char)GetDlgItemInt(hDlg,IDC_WVALUE,NULL,FALSE);
if(IOport == 0) IOport0=0;
else if(IOport == 1) IOport1=0;
else if(IOport == 2) IOport2=0;
ADT620_ConfigIOPorts(hDevice,IOport0,IOport1,IOport2);
ADT620_WriteDigitIO(hDevice,IOport,outbyte);
break;
case Start_AD: // 启动A/D变换
ADT620_ADSettings(hDevice,10,1); // 设置模拟输入电压量程和极性
channel= (unsigned char)GetDlgItemInt(hDlg,IDC_CHANNEL,NULL,FALSE);
ADT620_SetChannel(hDevice,channel); //设置模拟输入通道
if (!SetTimer (hDlg, ID_TIMER, 100, NULL)) // 启动定时器
{
MessageBox (hDlg, "定时器太多!","error", MB_OK) ;
return FALSE ;
}
break;
case Start_Counter: // 启动计数器
ADT620_ClockMode(hDevice,0, 2);
ADT620_ClockDivisor(hDevice,0, 800);
ADT620_ClockMode(hDevice,1, 2);
ADT620_ClockDivisor(hDevice,1, 2000);
ADT620_ClockMode(hDevice,2, 2);
ADT620_ClockDivisor(hDevice,2, 5000);
break;
case ReadBackCountV: // 读回计数值
count=ADT620_ClockReadBack(hDevice,2);
SetDlgItemInt(hDlg,IDC_RBCOUNTV,count,FALSE);
break;
}
break;
}
return FALSE;
}
//将浮点数source转为字符串destination,设定小数部分为precision位
void ftoa(float source,char destination[10],char precision)
{
char i=0,j=0,k,a[20];
int temp;
float sample1;
if(source<0)
{
sample1=-source;
destination[0]='-';
j=1;
}
else
{
sample1=source;
}
while(sample1/10>=1)
{
temp=(int)(sample1/10);
a=(char)((sample1-temp*10)+48);
i=i+1;
sample1=sample1/10;
}
a=(int)(sample1+48);
for(k=j;k<=j+i;k++)
{
destination[k]=a[i-(k-j)];
}
destination[k]='.';
if(source<0)
{
sample1=-source;
}
else
{
sample1=source;
}
temp=(int)sample1;
for(i=1;i<=precision;i++)
{
a=(char)((sample1*10-temp*10)+48);
destination[k+i]=a;
sample1=sample1*10;
temp=(int)sample1;
}
destination[k+i]=0;
} |
|