项目要求输出0-5V电压;对比不同的电压生成方案,使用一块DAC8552来实现相应功能。
IO引脚初始化
void spi_io_Initl(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA
| RCC_AHB1Periph_GPIOB
| RCC_AHB1Periph_GPIOC
| RCC_AHB1Periph_GPIOD
| RCC_AHB1Periph_GPIOE
| RCC_AHB1Periph_GPIOF
| RCC_AHB1Periph_GPIOG
| RCC_AHB1Periph_GPIOH
| RCC_AHB1Periph_GPIOI,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3
| GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SYNC = 1;
SCK = 1;
SDI = 1;
}
io模拟spi写入数据:
void spi_Write_Byte(unsigned char _1Byte)
{
unsigned char x = 0x00;
for(x = 0;x < 8;x ++)
{
if((_1Byte & 0x80) == 0x80)
SDI = 1;
else SDI = 0;
SCK = 0; delay_us(1);
SCK = 1; delay_us(1);
_1Byte = _1Byte << 1;
}
}
输出部分代码:
void dac_write_data_reg(unsigned char nCH,unsigned int nData)
{
unsigned char CMD = 0x00;
CMD = CMD | nCH;
CMD = CMD << 2;
if(nCH == OUT1_V1 || nCH == OUT3_V3)
CMD = CMD | Buffer_A;
else CMD = CMD | Buffer_B;
CMD = (CMD << 2) | DAC_MODE;
SCK = 1;
SYNC = 0;
spi_Write_Byte(CMD);
spi_Write_Byte(nData >> 8);
spi_Write_Byte(nData >> 0);
SYNC = 1;
}
void dac_v1_v2_output(unsigned char ch,float Votage)
{
unsigned int REG_Value = 0x00;
if(Votage >= 0 && Votage <= DAC_VREF)
{
REG_Value = (unsigned int)((float)Votage * CODE_MAX / DAC_VREF);
dac_write_data_reg(ch,REG_Value);
}
}
|