generic_io/EAOH/DAC161S997/dac161s997.c

207 lines
3.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "dac161s997.h"
/************************************************
函数名称 SPI_Delay_us
功 能 软件毫秒延时
参 数 Count ---- 次数
返 回 值
*************************************************/
static void spi_delay_us( uint32_t Count )
{
delay_us(Count);
}
/************************************************
函数名称 Write_SPI_Byte
功 能 SPI写一个字节
参 数 Byte ---- 数据
返 回 值 Byte ---- 数据
*************************************************/
uint8_t write_spi_byte( uint8_t Byte )
{
uint8_t i;
for(i = 0;i < 8;i++)
{
DAC_SPI_SCK(0);
spi_delay_us(5); // 空等待
if(Byte & 0x80)
{
DAC_SPI_MOSI(1);
}
else
{
DAC_SPI_MOSI(0);
}
Byte <<= 1;
spi_delay_us(5); // 空等待
DAC_SPI_SCK(1);
spi_delay_us(10); // 空等待
Byte |= DAC_SPI_MISO;
}
DAC_SPI_SCK(0);
return Byte;
}
/************************************************
函数名称 Read_SPI_Byte
功 能 SPI只读一个字节
参 数
返 回 值 temp ---- 数据
*************************************************/
uint8_t read_spi_byte(void)
{
uint8_t i;
uint8_t temp = 0;
for(i = 0;i < 8;i++)
{
DAC_SPI_SCK(0);
spi_delay_us(2); // 空等待
temp <<= 1;
temp |= DAC_SPI_MISO;
DAC_SPI_SCK(1);
spi_delay_us(2); // 空等待
}
DAC_SPI_SCK(0);
return temp;
}
void spi_nss_on(uint8_t dac_num)
{
switch(dac_num)
{
case 1:
{
DAC_NSS1(0);
DAC_NSS2(1);
DAC_NSS3(1);
DAC_NSS4(1);
}
break;
case 2:
{
DAC_NSS1(1);
DAC_NSS2(0);
DAC_NSS3(1);
DAC_NSS4(1);
}
break;
case 3:
{
DAC_NSS1(1);
DAC_NSS2(1);
DAC_NSS3(0);
DAC_NSS4(1);
}
break;
case 4:
{
DAC_NSS1(1);
DAC_NSS2(1);
DAC_NSS3(1);
DAC_NSS4(0);
}
break;
}
}
void spi_nss_off(void)
{
DAC_NSS1(1);
DAC_NSS2(1);
DAC_NSS3(1);
DAC_NSS4(1);
}
void dac161_spi_write(uint8_t * pData,uint8_t size)
{
int i = 0;
for(i = 0;i < size;i++)
{
write_spi_byte(*pData++);
}
}
/**
* @brief 向DAC161S997芯片写入寄存器值
*
* 该函数通过SPI接口向DAC161S997芯片写入指定的寄存器值。
*
* @param dac_num DAC芯片编号
* @param reg 要写入的寄存器地址
* @param data 要写入的数据16位
*/
void dac161s997_write_reg(uint8_t dac_num, uint8_t reg, uint16_t data)
{
uint8_t data_buffer[3] = {0, 0, 0};
data_buffer[0] = reg;
data_buffer[1] = data >> 8;
data_buffer[2] = data;
spi_nss_on(dac_num);
spi_delay_us(20);
dac161_spi_write(data_buffer, 3);
spi_delay_us(20);
spi_nss_off();
}
/*
* 重置DAC161S997寄存器数据
*/
void dac161s997_reset(uint8_t dac_num)
{
dac161s997_write_reg(dac_num, DAC161S997_RESET_REG, DAC161S997_RESET_CODE);
dac161s997_write_reg(dac_num, DAC161S997_NOP_REG, DAC161S997_RESET_CODE);
}
/**
* @brief 初始化DAC161S997 DAC设备
*/
void dac161s997_init(void)
{
int i = 1;
for(i = 1;i < 5;i++)
{
dac161s997_reset(i);
delay_ms(1);
dac161s997_write_reg(i, DAC161S997_ERR_LOW_REG, 0xFFFF);
dac161s997_write_reg(i, DAC161S997_ERR_CONFIG_REG, 0x070E);
}
}
/**
* @brief DAC161S997芯片输出函数
*
* 此函数用于设置DAC161S997芯片的输出电流。
*
* @param dac_num DAC芯片类型
* @param current 需要设置的电流值,单位:毫安
*/
void dac161s997_output(uint8_t dac_num, float current)
{
uint32_t dac_code = (uint32_t)((current * 65536.0f) / 24.0f);
uint8_t data_buffer[3] = {0, 0, 0};
data_buffer[0] = DAC161S997_DACCODE_REG;
data_buffer[1] = dac_code >> 8;
data_buffer[2] = dac_code;
spi_nss_on(dac_num);
spi_delay_us(20);
dac161_spi_write(data_buffer, 3);
spi_delay_us(20);
spi_nss_off();
}