📄 ad7730.c
字号:
/*
*****************************************************************************
**
** Project :
**
** Abstract : AD7730 driver--YZ
**
**
** Target :
**
** Version :
**
** Date :
**
**
*****************************************************************************
*/
/*
**===========================================================================
** INCLUDE FILES
**===========================================================================
*/
#include "proj_tgt.h" /* Target & Project specific header file */
/*
**===========================================================================
** DECLARATIONS(constant,macro)
**===========================================================================
*/
#define ADCPORT PORTB
#define NOREF_MASK 0x10
#define RDY_MASK 0x80
#define MODE_MASK 0xE0
#define POLAR_MASK 0x10
#define HIREF_MASK 0x80
#define RANGE_MASK 0x30
#define BURNOUT_MASK 0x04
#define CHANNEL_MASK 0x03
#define MODE_IDLE 0x00
#define MODE_CONT 0x20
#define MODE_SINGLE 0x40
#define MODE_STBY 0x60
#define MODE_IZSC 0x80
#define MODE_IFSC 0xA0
#define MODE_UNIPOLAR 0x10
#define MODE_BIPOLAR 0x00
#define HIREF_5V 0x80
#define HIREF_2V 0x00
#define RANGE_10 0x00
#define RANGE_20 0x10
#define RANGE_40 0x20
#define RANGE_80 0x30
#define BURNOUT_ON 0x04
#define BURNOUT_OFF 0x00
#define CHANNEL_AIN1 0x00
#define CHANNEL_AIN2 0x01
/*
**===========================================================================
** Type definitions
**===========================================================================
*/
/*
**===========================================================================
** Global variables
**===========================================================================
*/
/* Extern Var */
/*
**===========================================================================
** Internal Variables
**===========================================================================
*/
/*
**===========================================================================
** Global Function prototypes
**===========================================================================
*/
void ADC_SPI_Init (void);
U8 ADC_SPI_RW (U8 outputb);
U8 ADC_Read_Status(U8 adcno);
void ADC_Reset(U8 adcno);
void ADC_Write_Mode(U8 adcno,U8 ModeH,U8 ModeL);
void ADC_Read_Mode(U8 adcno,U8 *ModeH,U8 *ModeL);
void ADC_Write_Filter(U8 adcno,U8 FilterH,U8 FilterM,U8 FilterL);
void ADC_Read_Filter(U8 adcno,U8 *FilterH,U8 *FilterM,U8 *FilterL);
void ADC_Write_DAC(U8 adcno,U8 DACval);
void ADC_Read_DAC(U8 adcno,U8 *DACval);
void ADC_Read_Data(U8 adcno,U8 *DataH,U8 *DataL);
/* Extern Func */
/*
**===========================================================================
** Internal Function prototypes
**===========================================================================
*/
// Notes
//
// when there is no refrence voltage( < 0.3V) on Vref pins this bit goes 1
// if data is ready then RDY = 0
// After change mode (for cal ,....) wait for some time to do operation
// After define new task for adc after a delay check the RDY!
/*
**===========================================================================
** *** GLOBAL FUNCTIONS ***
**===========================================================================
*/
/*****************************************************************************
*
* Function name : ADC_SPI_Init
*
* Returns : None
*
* Parameters : None
*
* Purpose : Sets up the HW SPI in Master mode, Mode 3
* Note -> Uses the SS line to control the DF CS-line.
*
******************************************************************************/
void ADC_SPI_Init (void)
{
//Set SS high --- Set SS, MOSI and SCK as outputs
SPCR = (1<<SPE) | (1<<MSTR) | (1<<CPHA) | (1<<CPOL) | (1<<SPR0); //Enable SPI in Master mode, mode 3
}
/*****************************************************************************
*
* Function name : ADC_SPI_RW
*
* Returns : Byte read from SPI data register (any value)
*
* Parameters : Byte to be written to SPI data register (any value)
*
* Purpose : Read and writes one byte from/to SPI master
*
******************************************************************************/
U8 ADC_SPI_RW (U8 outputb)
{
U8 inputb;
SPDR = outputb; //put byte 'output' in SPI data register
while((SPSR & 0x80) != 0x80); //wait for transfer complete, poll SPIF-flag
inputb = SPDR; //read value in SPI data reg.
return inputb; //return the byte clocked in from SPI slave
}
//------------------------------------------------------------------------------
// ADC Delay
//------------------------------------------------------------------------------
void ADC_Delay(void)
{
__delay_cycles(320);
}
/*****************************************************************************
*
* Function name : ADC1_Read_Status(0 for ADC0 ,4 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
U8 ADC_Read_Status(U8 adcno)
{
U8 status;
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x10); //send comm reg to read status(opcode)
status = ADC_SPI_RW(0x00); //read status byte
ADCPORT |= (1<<adcno); //CS = 1
return status;
}
/*****************************************************************************
*
* Function name : ADC1_Reset(0 for ADC0 ,4 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Reset(U8 adcno)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0xFF);
ADC_SPI_RW(0xFF);
ADC_SPI_RW(0xFF);
ADC_SPI_RW(0xFF);
ADCPORT |= (1<<adcno); //CS = 1
}
/*****************************************************************************
*
* Function name : ADC_Write_Mode(0 for ADC0 ,1 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Write_Mode(U8 adcno,U8 ModeH,U8 ModeL)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x02); //send comm reg for write to MODE reg(opcode)
ADC_SPI_RW(ModeH); //write high byte
ADC_SPI_RW(ModeL); //write low byte
ADCPORT |= (1<<adcno); //CS = 1
ADC_Delay();
}
/*****************************************************************************
*
* Function name : ADC_Read_Mode(0 for ADC0 ,1 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Read_Mode(U8 adcno,U8 *ModeH,U8 *ModeL)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x12); //send comm reg for read from MODE reg(opcode)
*ModeH = ADC_SPI_RW(0x00); //read high byte
*ModeL = ADC_SPI_RW(0x00); //read low byte
ADCPORT |= (1<<adcno); //CS = 1
ADC_Delay();
}
/*****************************************************************************
*
* Function name : ADC_Write_Filter(0 for ADC0 ,1 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Write_Filter(U8 adcno,U8 FilterH,U8 FilterM,U8 FilterL)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x03); //send comm reg for write to FILTER reg(opcode)
ADC_SPI_RW(FilterH); //write high byte
ADC_SPI_RW(FilterM); //write middle byte
ADC_SPI_RW(FilterL); //write low byte
ADCPORT |= (1<<adcno); //CS = 1
ADC_Delay();
}
/*****************************************************************************
*
* Function name : ADC_Read_Filter(0 for ADC0 ,1 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Read_Filter(U8 adcno,U8 *FilterH,U8 *FilterM,U8 *FilterL)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x13); //send comm reg for read from FILTER reg(opcode)
*FilterH = ADC_SPI_RW(0x00); //read high byte
*FilterM = ADC_SPI_RW(0x00); //read middle byte
*FilterL = ADC_SPI_RW(0x00); //read low byte
ADCPORT |= (1<<adcno); //CS = 1
ADC_Delay();
}
/*****************************************************************************
*
* Function name : ADC_Write_DAC(0 for ADC0 ,1 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Write_DAC(U8 adcno,U8 DACval)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x04); //send comm reg for write to DAC reg(opcode)
ADC_SPI_RW(DACval); //write byte
ADCPORT |= (1<<adcno); //CS = 1
ADC_Delay();
}
/*****************************************************************************
*
* Function name : ADC_Read_DAC(0 for ADC0 ,1 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Read_DAC(U8 adcno,U8 *DACval)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x14); //send comm reg for read from DAC reg(opcode)
*DACval = ADC_SPI_RW(0x00); //read byte
ADCPORT |= (1<<adcno); //CS = 1
ADC_Delay();
}
/*****************************************************************************
*
* Function name : ADC_Read_Data(0 for ADC0 ,1 for ADC1)
*
* Returns :
*
* Parameters :
*
* Purpose :
*
******************************************************************************/
void ADC_Read_Data(U8 adcno,U8 *DataH,U8 *DataL)
{
ADCPORT |= (1<<adcno); //make sure to toggle CS signal in order
ADCPORT &= ~(1<<adcno); //CS = 0
ADC_SPI_RW(0x11); //send comm reg for read from DATA reg(opcode)
*DataH = ADC_SPI_RW(0x00); //read high byte
*DataL = ADC_SPI_RW(0x00); //read low byte
ADCPORT |= (1<<adcno); //CS = 1
ADC_Delay();
}
// ADC0,1 init
void ADC_Init_All(void)
{
U8 s;
volatile U16 k;
ADC_SPI_Init();
// first adc
ADC_Write_DAC(0,0x00);
ADC_Write_Filter(0,0x80,0x00,0x10);
ADC_Write_Mode(0,(MODE_IFSC | MODE_BIPOLAR),(HIREF_5V | RANGE_20 | BURNOUT_OFF | CHANNEL_AIN1));
do{
s = ADC_Read_Status(0);
}while( (s & RDY_MASK) == RDY_MASK);
ADC_Write_Mode(0,(MODE_IZSC | MODE_BIPOLAR),(HIREF_5V | RANGE_20 | BURNOUT_OFF | CHANNEL_AIN1));
do{
s = ADC_Read_Status(0);
}while( (s & RDY_MASK) == RDY_MASK);
ADC_Write_Mode(0,(MODE_CONT | MODE_BIPOLAR),(HIREF_5V | RANGE_20 | BURNOUT_OFF | CHANNEL_AIN1));
// second adc
ADC_Write_DAC(4,0x00);
ADC_Write_Filter(4,0x80,0x00,0x10);
ADC_Write_Mode(4,(MODE_IFSC | MODE_BIPOLAR),(HIREF_5V | RANGE_20 | BURNOUT_OFF | CHANNEL_AIN1));
do{
s = ADC_Read_Status(4);
}while( (s & RDY_MASK) == RDY_MASK);
ADC_Write_Mode(4,(MODE_IZSC | MODE_BIPOLAR),(HIREF_5V | RANGE_20 | BURNOUT_OFF | CHANNEL_AIN1));
do{
s = ADC_Read_Status(4);
}while( (s & RDY_MASK) == RDY_MASK);
ADC_Write_Mode(4,(MODE_CONT | MODE_BIPOLAR),(HIREF_5V | RANGE_20 | BURNOUT_OFF | CHANNEL_AIN1));
}
/*
**===========================================================================
** *** INTERNAL FUNCTIONS ***
**===========================================================================
*/
/*
**---------------------------------------------------------------------------
**
** Abstract:
**
**
** Parameters:
**
**
** Returns:
**
**
**---------------------------------------------------------------------------
*/
/*
**===========================================================================
** END OF FILE
**===========================================================================
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -