/**
******************************************************************************
* @file bsp_dac8552.c
* @author 张超凡
* @version V1.0
* @date
* @brief DAC8552驱动程序
*
*/
/*******************************调试信息*************************************************
* 模拟了三个spi通信。一红有6个通道,编号从 0-5 。
* 为保证输出的精度,有两种办法:1、用最小二乘法拟合直线。2、用分段线性插值。
*
*
* 增加了延时
*
*
*
*******************************************************************************/
#include "stm32f4xx.h"
#include "bsp_dac8552.h"
#include "stm32f4xx_conf.h"
#include "delay.h"
static void DAC8552_0_SPI_Write( unsigned int code);
static void DAC8552_1_SPI_Write( unsigned int code);
static void DAC8552_2_SPI_Write( unsigned int code);
static void DAC8552_delay(void);
void DAC8552_Init()
{
unsigned char i = 0;
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_Pin = DAC8552_0_SYNC_Pin ; // 端口配置
GPIO_Init(DAC8552_0_SYNC_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_0_SCLK_Pin;
GPIO_Init(DAC8552_0_SCLK_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_0_DIN_Pin;
GPIO_Init(DAC8552_0_DIN_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_1_SYNC_Pin ; // 端口配置
GPIO_Init(DAC8552_1_SYNC_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_1_SCLK_Pin;
GPIO_Init(DAC8552_1_SCLK_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_1_DIN_Pin;
GPIO_Init(DAC8552_1_DIN_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_2_SYNC_Pin ; // 端口配置
GPIO_Init(DAC8552_2_SYNC_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_2_SCLK_Pin;
GPIO_Init(DAC8552_2_SCLK_Port, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DAC8552_2_DIN_Pin;
GPIO_Init(DAC8552_2_DIN_Port, &GPIO_InitStructure);
for(i = 0;i <5;i ++)
{
DAC8552_SetVoltage(i,0);
}
}
static void DAC8552_delay(void)
{
int x = 10;
while(--x);
}
void DAC8552_SetVoltage( unsigned char channel,unsigned int code) //通道一共6个(0-5),一个芯片有两个通道 data是一个24位的数据,STM32中int型是32位的数据
{
unsigned int data = 0;
unsigned char chip;
if( code > 65535 | channel > 5)
return ;
chip = channel >> 1;
(channel%2 == 0) ? (data = 0x100000 | code) : (data = 0x240000 | code) ; //通道有 chip0 : 0 1 chip1 : 2 3 chip2 : 4 5 偶数通道0 奇数通道一
switch( chip)
{
case 0 :
{
DAC8552_0_SPI_Write(data);
break;
}
case 1 :
{
DAC8552_1_SPI_Write(data);
break;
}
case 2 :
{
DAC8552_2_SPI_Write(data);
break;
}
}
}
static void DAC8552_1_SPI_Write( unsigned int code)
{
char i=0;
unsigned int data = code;
DAC8552_1_SYNC = 1;
DAC8552_delay();//SYNC脚一个高脉冲
DAC8552_1_SYNC = 0;
for( i=0; i<24; i++ )
{
if((data&0x800000) == 0x800000)
DAC8552_1_DIN = 1;
else
DAC8552_1_DIN = 0;
DAC8552_1_SCLK = 1;
DAC8552_delay();
DAC8552_1_SCLK = 0;
data=data<<1;
DAC8552_delay();
}
}
static void DAC8552_0_SPI_Write( unsigned int code)
{
char i=0;
unsigned int data = code;
DAC8552_0_SYNC = 1;
DAC8552_delay();//SYNC脚一个高脉冲
DAC8552_0_SYNC = 0;
for( i=0; i<24; i++ )
{
if((data&0x800000) == 0x800000)
DAC8552_0_DIN = 1;
else
DAC8552_0_DIN = 0;
DAC8552_0_SCLK = 1;
DAC8552_delay();
DAC8552_0_SCLK = 0;
data=data<<1;
DAC8552_delay();
}
}
static void DAC8552_2_SPI_Write( unsigned int code)
{
char i=0;
unsigned int data = code;
DAC8552_2_SYNC = 1;
DAC8552_delay();//SYNC脚一个高脉冲
DAC8552_2_SYNC = 0;
for( i=0; i<24; i++ )
{
if((data&0x800000) == 0x800000)
DAC8552_2_DIN = 1;
else
DAC8552_2_DIN = 0;
DAC8552_2_SCLK = 1;
DAC8552_delay();
DAC8552_2_SCLK = 0;
data=data<<1;
DAC8552_delay();
}
}