📄 adlib.cpp
字号:
/*
Science & Technology CO.,LTD.
Rm814-815,Shangda Plaza, Zhenhua,ShenZhen,GuangDong,P.R.C.
Tel: 86-755-3224200
Fax: 86-755-3343187
Zip: 518031
Board: SF93A-01
Compiler: Turbo C (r) version 1.0, 1.5, 2.0
Turbo C++ (r) version 1.0
Borland C++ (r) version 2.0, 3.0
Last update: August 19, 1993
This file contains procedure and functions used by the sample programs
provided with your ADT600 The procedures are well documented and should
be helpful in learning how to program your ADT600
*/
#include <ucore/i386.h>
#include <stdlib.h>
#include <stdio.h>
#include "ad.h"
unsigned char Channel_IRQ;
unsigned BaseAddress;
float VoltageRange,
DACSlope,
ConversionFactor,
Baseline ;
int DACOffset;
/**************************
InitializeBoardSettings
The InitializeBoardSettings procedure is used to set the Base address
variable and calculate the conversion factor for converting between digital
values and volts. Since the base address variable is not set until this
procedure is called, make certain that you call this procedure before
any others in this file.
*******************/
void InitializeBoardSettings(unsigned BA, float Range, char Polarity)
{
BaseAddress = BA;
Channel_IRQ = 0;
VoltageRange = Range;
ConversionFactor = VoltageRange / 4096.0;
if (Polarity == BIPOLAR)
{
Baseline = 0;
}
else
{
Baseline = 5.0;
}
}
/***********
The Function DigitalToSBS converts a digitized value to a SBS value.
In these sample programs the conversion factor and baseline
correspond to converting between digitized values and volts.
***********/
float DigitalToSBS(int DigitalValue)
{
return(DigitalValue * ConversionFactor + Baseline);
}
/*******************
ResetBoard
The ResetBoard procedure is used to reset the ADT600. The UPD71055 PPI is
configured so that ports 0,1 and 2 are input, a dummy A-D conversion is
performed and then the clear board command is sent.
The channel bits of the Channel Select register are set for channel 0
The external trigger is disabled, the IRQ source is set to Timer 2 out.
*******************/
void ResetBoard(void)
{
unsigned char B;
//char a ;
sysOutByte(BaseAddress + PPI_CTRL, 0x9B); /* Set PPI Port 0,1,and 2 for input */
sysOutByte(BaseAddress + CHANNEL_SLCT, 0x00); /* Not select channel,IRQ disabled */
Channel_IRQ = 0x00;
sysOutByte(BaseAddress + START_CONVERSION, 0);/* Wait 'til conversion done */
while((sysInByte(BaseAddress + STATUS_BYTE) & 1) == 0)
{
}
/* Wait 'til conversion done */
B=sysInByte(BaseAddress + READ_DATA_LSB);
B=sysInByte(BaseAddress + READ_DATA_MSB);
}
/*******************
SetChannel
The SetChannel procedure is used to set the channel bits, in the CHANNEL
SELECT register (BA + 12).
Note how this procedure sets only the channel select bits;
it does not change the other bits in this register.
This is important because if you unintentionally clear the other bits it
can cause unexpected behavior of the ADT600.
*******************/
void SetChannel(unsigned char ChannelNumber)
{
unsigned char B;
B = Channel_IRQ; /* read current byte */
B = B & 0x08;
B = B | ( ChannelNumber & 0x07 ); /* set channel bits */
if(ChannelNumber <= 7)
B = B | 0x10;
else
B = B | 0x20;
sysOutByte(BaseAddress + CHANNEL_SLCT, B); /* write new byte */
Channel_IRQ = B;
}
/*******************
SetIRQStatus
The SetIRQStatus procedure is used to set or clear the IRQ Enable bit
on the ADT600. A value of 1 passed to this procedure enables interrupts
a value of 0 disables interrupts.
*******************/
void SetIRQStatus(char IRQStatus)
{
unsigned char B;
B = Channel_IRQ; /* read current byte */
B = B & 0xF7; /* clear IRQ bit */
B = B | IRQStatus * 8; /* set IRQ select bit */
sysOutByte(BaseAddress + IRQ_SLCT, B); /* write new byte */
Channel_IRQ = B; /* store current channel and IRQ select byte */
}
/*******************
StartConversion
The StartConversion procedure is used to start conversions.
*******************/
void StartConversion(void)
{
sysOutByte(BaseAddress + START_CONVERSION, 0);
}
/*******************
ConversionDone
The ConversionDone function returns TRUE if a conversion is complete, FALSE
if a conversion is in progress.
*******************/
char ConversionDone(void)
{
unsigned char Status;
int i=100;
Status=sysInByte(BaseAddress + STATUS_BYTE); /* read board status */
i--;
if ( (Status & 1) == 1|i==0)
{
return(TRUE);
} /* if B0 is set return TRUE */
else
{
return(FALSE); /* if B0 is not set return FALSE */
}
}
/*******************
ReadData
The ReadData function retrieves two bytes from the converter and combines
them into an integer value.
*******************/
int ReadData(void)
{
int MSB, LSB;
MSB=sysInByte(BaseAddress + READ_DATA_MSB)*16;
LSB=sysInByte(BaseAddress + READ_DATA_LSB)/16;
return(MSB + LSB - 2048);
}
/*AD采集程序,其中 n表示通道号,AcquireTimes表示采集次数,
* Buffer[]表示存储用数组
*/
int ADtest(int n,int AcquireTimes,float Buffer[]) {
int Index,i;
for(Index=0; Index < AcquireTimes; Index++)
{
SetChannel(n);
StartConversion();
while(ConversionDone() == 0);
Buffer[i]=DigitalToSBS(ReadData());
}
return 0;
}
/***********
Read DigitalIO
The ReadDigitalIO function returns the value of the specified digital
input port. Each digital input line is represented by a bit of the
return value. Digital in 0 is bit 0, digital in 1 is bit 1, and so on.
***********/
unsigned char ReadDigitalIO(unsigned char InputPort)
{
return(sysInByte(BaseAddress + PPI_0 + InputPort));
}
/***********
WriteDigitalIO
The WriteDigitalIO function sets the value of the digital output port to
equal the value passed as parameter v. Each digital output line is
represented by a bit of v. Digital out 0 is bit 0, digital out 1 is bit 1,
and so on.
***********/
void WriteDigitalIO(unsigned char OutputPort, unsigned char v)
{
sysOutByte(BaseAddress + PPI_0 + OutputPort, v);
}
/*****************
ConfigureIOPorts
The ConfigureIOPorts procedure is used to configure the ports 0 and 2 on
the UPD71055 PPI for either input or output.
A value of 1 means input, a value of 0 is for output.
It is advisable to use the INPUT and OUTPUT constants defined in this file.
*****************/
void ConfigureIOPorts(unsigned char Port0, unsigned char Port1,unsigned char Port2)
{
unsigned char ControlByte;
ControlByte = 128 + (Port0 * 16) + (Port1 * 2)+(Port2 * 8)+ Port2;
sysOutByte(BaseAddress+PPI_CTRL, ControlByte);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -