📄 adc.c
字号:
//****************************************************************************
//
// ADC.C - Routines to enable the synchronous serial interface and use it to
// sample data from the ADC. On the EP7312 evaluation board
// the ADC is a Burr Brown ADS7846E.
//
// Copyright (c) 2001 Cirrus Logic, Inc.
//
//****************************************************************************
#include "ep7312.h"
#include "lib7312.h"
//****************************************************************************
//
// The following variables are used to indicate when a SSI transfer is in
// progress and to hold the value read via the SSI transfer.
//
//****************************************************************************
static volatile int bDone = 1;
static long lADCData;
//****************************************************************************
//
// The following variable is 0 if the ADC is used in a polling mode, 1 if it
// is interrupt driven.
//
//****************************************************************************
static int ADCIRQEnable = 1;
//****************************************************************************
//
// ADCISR is the interrupt handler for the ADC interrupt. It wil read the
// data from the SSI interface and indicate that the SSI transfer has
// completed.
//
//****************************************************************************
static void
ADCISR(void)
{
unsigned long * volatile pulPtr = (unsigned long *)HwBaseAddress;
//
// Read the data which was shifted in from the ADC.
//
lADCData = pulPtr[HwSpiData >> 2] & 0xFFFF;
//
// Indicate that the SSI transfer is done.
//
bDone = 1;
}
//****************************************************************************
//
// ADCEnable configures the synchronous serial interface.
//
//****************************************************************************
void
ADCEnable(void)
{
unsigned long * volatile pulPtr = (unsigned long *)HwBaseAddress;
//
// Set the interface rate to 128kHz. This gives us 7.8usec per clock.
//
pulPtr[HwControl >> 2] |= HwControlSpiClock128KHz;
//
// If ADCEnable is true, install the ISR so that we can process the ADC interrupt.
// Otherwise we are using polled mode.
//
if (ADCIRQEnable)
{
InterruptInstallIRQ();
//
// Install the ADC interrupt handler.
//
InterruptSetADCHandler(ADCISR);
//
// Enable the ADC interrupt.
//
pulPtr[HwIntMask >> 2] |= HwIrqSpi;
}
}
//****************************************************************************
//
// ADCDisable powers down the synchronous serial interface.
//
//****************************************************************************
void
ADCDisable(void)
{
unsigned long * volatile pulPtr = (unsigned long *)HwBaseAddress;
if (ADCIRQEnable)
{
//
// Disable the ADC interrupt.
//
pulPtr[HwIntMask >> 2] &= ~HwIrqSpi;
//
// Remove the ADC interrupt handler.
//
InterruptSetADCHandler(0);
//
// Remove the interrupt handler.
//
InterruptRemoveIRQ();
}
}
//****************************************************************************
//
// ADCGetData reads data from the ADC accross the synchronous serial
// interface. On the EP7312 evaluation board the ADC is a Burr Brown
// ADS7846 chip.
//
//****************************************************************************
long
ADCGetData(unsigned long ulADCctl)
{
unsigned long * volatile pulPtr = (unsigned long *)HwBaseAddress;
unsigned long ulMask;
//
// Tell the synchronous serial interface to read a word of data from the
// ADC. The Burr-Brown ADS7846E is configured for a 24-bit frame consisting
// of the 8-bit control word and a 16-bit result. Note that we pass the
// control word in directly using the ADCctl variable. This allows the
// code calling us to control the operation exactly, including returned
// data length (i.e. 8-bit or 12-bit) as well as conversion modes such
// as single-ended and differential.
bDone = 0;
pulPtr[HwSpiData >> 2] = HwSpiDataTxFrame |
(24 << HwSpiDataFrameLengthShift) |
ulADCctl;
//
// Wait until the synchronous serial interface has read the ADC data.
//
if (ADCIRQEnable)
{
while(!bDone)
{
}
}
//
// Poll the SSI End of Transfer Interrupt
//
else
{
ulMask = pulPtr[HwIntStatus >> 2] & HwIrqSpi;
while (!(pulPtr[HwIntStatus >> 2 ] & HwIrqSpi))
{
}
}
//
// Return the data shifted in from the ADC.
//
if (!ADCIRQEnable)
{
lADCData = pulPtr[HwSpiData >> 2] & 0xFFFF;
}
return(lADCData);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -