DAC8554驱动--STM32F407实测通过
#include "sys.h"
#include "dac8554.h"
#include "delay.h"
void DAC8554_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC8552_RCC_GPIOClockCmd(); //使能端口引脚端口时钟
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_InitStructure.GPIO_Pin = DAC8554_SYNC_Pin ; // 端口配置
GPIO_Init(DAC8554_SYNC_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8554_SCLK_Pin;
GPIO_Init(DAC8554_SCLK_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8554_DIN_Pin;
GPIO_Init(DAC8554_DIN_Port, &GPIO_InitStructure);
}
//时序延时
void DAC8554_delay(void)
{
int x = 100; //2.7us
while(--x);
}
//16位的数据
void DAC8554_Write( unsigned int data)
{
char i=0;
unsigned int code = data;
DAC8554_SYNC = 1;
DAC8554_delay();//SYNC脚一个高脉冲
DAC8554_SYNC = 0;
for( i=0; i<24; i++ )
{
DAC8554_SCLK = 1;
if(code&0x800000)
DAC8554_DIN = 1;
else
DAC8554_DIN = 0;
DAC8554_delay();
DAC8554_SCLK = 0;
code=code<<1;
DAC8554_delay();
}
}
//CH0
void DAC8554_Updata_A(unsigned int data)
{
data = data | 0x100000; //0001 0000 xxxx xxxx xxxx xxxx
DAC8554_Write(data);
}
//CH1
void DAC8554_Updata_B(unsigned int data)
{
data = data | 0x120000; //0001 0010 xxxx xxxx xxxx xxxx
DAC8554_Write(data);
}
//CH2
void DAC8554_Updata_C(unsigned int data)
{
data = data | 0x140000; //0001 0100 xxxx xxxx xxxx xxxx
DAC8554_Write(data);
}
//CH3
void DAC8554_Updata_D(unsigned int data)
{
data = data | 0x160000; //0001 0110 xxxx xxxx xxxx xxxx
DAC8554_Write(data);
}
//data: 0---4480 对应 0---4.48V
void DAC8554_Set(unsigned int data_A,unsigned int data_B,unsigned int data_C,unsigned int data_D)
{
data_A = data_A / 1000.0 * 65535 / 4.48; //0-65535
data_B = data_B / 1000.0 * 65535 / 4.48;
data_C = data_C / 1000.0 * 65535 / 4.48;
data_D = data_D / 1000.0 * 65535 / 4.48;
DAC8554_Updata_A(data_A);
DAC8554_Updata_B(data_B);
DAC8554_Updata_C(data_C);
DAC8554_Updata_D(data_D);
}