AD7908


6通道,自动。


#define AD7908_SCLK 13

#define AD7908_DIN 12

#define AD7908_CS 11

#define AD7908_DOUT 14


//SPI hardware fuction

#define    SPI_AD7908__Enable_CS     nrf_gpio_pin_clear(AD7908_CS) 

#define    SPI_AD7908__Disable_CS   nrf_gpio_pin_set(AD7908_CS)

#define    SPI_AD7908__Set_SCLK     nrf_gpio_pin_set(AD7908_SCLK)

#define    SPI_AD7908__Clr_SCLK     nrf_gpio_pin_clear(AD7908_SCLK)

#define    SPI_AD7908__Set_DIN       nrf_gpio_pin_set(AD7908_DIN)

#define    SPI_AD7908__Clr_DIN       nrf_gpio_pin_clear(AD7908_DIN)

#define    SPI_AD7908__Get_DOUT       nrf_gpio_pin_read(AD7908_DOUT)


#define SPI_AD7908_WRITE(n) (n<<11) //0=read 1=write

#define SPI_AD7908_ADD(n) (n<<6) //n:0~7

#define SPI_AD7908_PM(n) (n<<4) //n:0~3

#define SPI_AD7908_SEQ(n) (n<<10)

#define SPI_AD7908_SHADOW(n) (n<<3)

#define SPI_AD7908_RANGE(n) (n<<1)//0=0 V to 2*REF IN ,1=0 V to REF IN

#define SPI_AD7908_CODING(n) (n<<0)//0=twos complement ,1=binary


void SPI_AD7908_Init(void)

{

nrf_gpio_cfg_output(AD7908_SCLK);

nrf_gpio_cfg_output(AD7908_DIN);

nrf_gpio_cfg_output(AD7908_CS);

nrf_gpio_cfg_input(AD7908_DOUT,NRF_GPIO_PIN_NOPULL);

}


uint16_t SPI_AD7908_Write_Control(bool writeread)

{

uint16_t dat_16 = 0;

if(writeread == 1)//write

{

dat_16 = SPI_AD7908_WRITE(writeread) | \

SPI_AD7908_ADD(5) | \

SPI_AD7908_PM(3) | \

SPI_AD7908_SEQ(1) | \

SPI_AD7908_SHADOW(1) | \

SPI_AD7908_RANGE(1) | \

SPI_AD7908_CODING(1);

}

else//read

{

dat_16 = SPI_AD7908_WRITE(writeread) | \

SPI_AD7908_ADD(5) | \

SPI_AD7908_PM(3) | \

SPI_AD7908_SEQ(1) | \

SPI_AD7908_SHADOW(1) | \

SPI_AD7908_RANGE(1) | \

SPI_AD7908_CODING(1);

}

dat_16 <<= 4;

return dat_16;

}




uint8_t SPI_AD7908_Read(void)

{

uint8_t dat;

uint16_t dat_buf;

SPI_AD7908__Disable_CS;

SPI_AD7908__Set_SCLK;

nrf_delay_us(2);

SPI_AD7908__Enable_CS;

for(uint8_t i=0;i<16;i++)

{

nrf_delay_us(2);

dat_buf<<=1;

SPI_AD7908__Set_SCLK;

if(SPI_AD7908__Get_DOUT)

{

dat_buf |= 0x0001;

}

else

{

dat_buf &= 0xfffe;

}

SPI_AD7908__Clr_SCLK;

}

dat_buf >>= 4;

dat = (uint8_t)dat_buf;

SPI_AD7908__Disable_CS;

return dat;

}


void SPI_AD7908_Read_more(uint8_t nchannel,uint8_t* pbuff)

{

uint16_t dat_buf;

for(uint8_t j=0;j

{

SPI_AD7908__Disable_CS;

SPI_AD7908__Set_SCLK;

nrf_delay_us(2);

SPI_AD7908__Enable_CS;

for(uint8_t i=0;i<16;i++)

{

nrf_delay_us(2);

dat_buf<<=1;

SPI_AD7908__Set_SCLK;

if(SPI_AD7908__Get_DOUT)

{

dat_buf |= 0x0001;

}

else

{

dat_buf &= 0xfffe;

}

SPI_AD7908__Clr_SCLK;

}

dat_buf >>= 4;

pbuff[j] = (uint8_t)dat_buf;

SPI_AD7908__Disable_CS;

nrf_delay_us(2);

}

}

void SPI_AD7908_Write(uint16_t dat)

{

SPI_AD7908__Disable_CS;

SPI_AD7908__Set_SCLK;

nrf_delay_us(2);

SPI_AD7908__Enable_CS;

for(uint8_t i=0;i<16;i++)

{

nrf_delay_us(2);

SPI_AD7908__Set_SCLK;

if(dat & 0x8000)

{

SPI_AD7908__Set_DIN;

}

else

{

SPI_AD7908__Clr_DIN;

}

SPI_AD7908__Clr_SCLK;

dat <<= 1;

}

SPI_AD7908__Disable_CS;

nrf_delay_us(2);

}


void ADC_to_Vout(uint16_t i)

{

vout[0][i] = (uint16_t)buff[0]*2500/256;

vout[1][i] = (uint16_t)buff[1]*2500/256;

vout[2][i] = (uint16_t)buff[2]*2500/256;

vout[3][i] = (uint16_t)buff[3]*2500/256;

vout[4][i] = (uint16_t)buff[4]*2500/256;

vout[5][i] = (uint16_t)buff[5]*2500/256;

}


你可能感兴趣的:(AD)