STM32F407+DAC8568


DAC8568是一款16 位、8 通道、SPI接口的數模轉換芯片。

 

DAC8568的時序圖如圖所示:

 

 

DAC8568數據手冊中寄存器配置,重點是下圖中紅色框的命令:

1)重啟

 2)將相應位設置為1,為A、B、C、D、E、F、G、H通道通電

  3)給內部基准電壓上電-靜態模式

 4)寫相應的通道,並更新

 程序實現:

一些宏定義:

/************************DA8568寄存器SR的值*****************************/
#define PrefixControlbyte 0x03
#define AddressOutA 0x0
#define AddressOutB 0x1
#define AddressOutC 0x2
#define AddressOutD 0x3
#define AddressOutE 0x4
#define AddressOutF 0x5
#define AddressOutG 0x6
#define AddressOutH 0x7
#define Featurebyte 0x0

/*****DA8568寄存器SR的值-結尾*************************************/

/****************************DAC8568命令************************/
#define SETUP_INTERNAL_REGISTER 0
#define POWER_UP 1
#define RESET 2
/*****DAC8568命令-結尾*******************************************/

#define DAC8568_SYNC_H GPIO_SetBits(GPIOA,GPIO_Pin_15)
#define DAC8568_SYNC_L GPIO_ResetBits(GPIOA,GPIO_Pin_15)
#define DAC8568_SCLK_H GPIO_SetBits(GPIOC,GPIO_Pin_10)
#define DAC8568_SCLK_L GPIO_ResetBits(GPIOC,GPIO_Pin_10)
#define DAC8568_DIN_H GPIO_SetBits(GPIOC,GPIO_Pin_12)
#define DAC8568_DIN_L GPIO_ResetBits(GPIOC,GPIO_Pin_12)

 

 

 

 

 GPIO初始化:

///*******************************************
// 函數名稱DAC8568_GPIO_Init
// 功 能:初始化DAC8568IO
// 參 數:無
// 返回值 :無
//********************************************/
static void DAC8568_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_10;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽輸出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

模擬SPI通信,發送8位

//-----------------------------------------------------------------//
// 功 能: 模擬8位SPI通信,發送控制命令
// 入口參數: / 發送的SPI數據
// 出口參數: / 接收的SPI數據
// 全局變量: /
// 備 注: 發送函數
//-----------------------------------------------------------------//
void SPI_SendByte(u8 m)
{
u8 i;

for(i=0;i<8;i++)
{
DAC8568_SCLK_H; //clk上升沿讀取dout數據
if(m & 0x80)
{
DAC8568_DIN_H;
}
else
{
DAC8568_DIN_L;
}
m = m<<1;
DAC8568_SCLK_L; //clk下降沿把din上的數據傳到ad
Mcpdely();
}
}

 

DAC856832bit寄存器包括PreConbyte(8bit)+Addressbyte(4bit)+Datashort(16bit)+Featurebyte(4bit)

/**
* @brief DAC8568寄存器的32bit數據
* @param PreConbyte: 4bit-Prefix bits + 4bit-control bits
* @param Addressbyte:4bit-Address bits
* @param Datashort: 16bit-Data bits
* @param Featurebyte:4bit-Feature bits
*/
static uint32_t ChatToInt(uint8_t PreConbyte, uint8_t Addressbyte, uint16_t Datashort, uint8_t Featurebits)
{
uint32_t ret_val = 0;

Addressbyte &= 0x0f;
Featurebits &= 0x0f;
ret_val = PreConbyte;
ret_val <<= 4;
ret_val |= Addressbyte;
ret_val <<= 16;
ret_val |= Datashort;
ret_val <<= 4;
ret_val |= Featurebits;

return ret_val;
}

 

 

 

 根據DA8568時序圖可以看出,為高位先行。

/**
* @brief DAC8568指定通道寫數據
* @param Addressbyte: 0-7對應通道A到通道H
* @param Datashort:寄存器SR的32 bit數據
*/
static void DAC8568_Write_passageway(uint8_t Addressbyte, uint16_t Datashort)
{
uint8_t abc24, abc16, abc8, abc0;
uint32_t SRData; //發送給DA8568移位寄存器SR的值
SRData = ChatToInt(PrefixControlbyte, Addressbyte, Datashort, Featurebyte);
DAC8568_SYNC_L;
Mcpdely();
SPI_SendByte((SRData & 0xFF000000) >> 24); //發送DB31-DB24位
abc24 = (SRData & 0xFF000000) >> 24;
SPI_SendByte((SRData & 0xFF0000) >> 16); //發送DB23-DB16位
abc16 = (SRData & 0xFF0000) >> 16;
SPI_SendByte((SRData & 0xFF00) >> 8); //發送DB15-DB8位
abc8 = (SRData & 0xFF00) >> 8;
SPI_SendByte(SRData & 0xFF); //發送DB7-DB0位
abc0 = SRData & 0xFF;
DAC8568_SYNC_H;
Mcpdely();
}

 

 

 

/**
* @brief DAC8568寫指定命令
* @param Addressbyte: 0-7對應通道A到通道H
* @param Datashort:寄存器SR的32 bit數據
*/
static void DAC8568_Write_Command(uint8_t command)
{
switch(command)
{
//給內部基准電壓上電 - 靜態模式
//注意:當所有 DAC 掉電時,基准電壓會掉電;當任何DAC上電時,基准電壓會上電
case SETUP_INTERNAL_REGISTER:
{
DAC8568_SYNC_L;
SPI_SendByte(0x08); //發送DB31-DB24位
SPI_SendByte(0); //發送DB23-DB16位
SPI_SendByte(0); //發送DB15-DB8位
SPI_SendByte(0x01); //發送DB7-DB0位
DAC8568_SYNC_H;
break;
}

//通過將相應位設置為“1”,為DAC A、B、C、D、E、F、G、H通電
case POWER_UP:
{
DAC8568_SYNC_L;
SPI_SendByte(0x04); //發送DB31-DB24位
SPI_SendByte(0); //發送DB23-DB16位
SPI_SendByte(0); //發送DB15-DB8位
SPI_SendByte(0xff); //發送DB7-DB0位
DAC8568_SYNC_H;
break;
}
//重啟
case RESET:
{
DAC8568_SYNC_L;
SPI_SendByte(0x07); //發送DB31-DB24位
SPI_SendByte(0); //發送DB23-DB16位
SPI_SendByte(0); //發送DB15-DB8位
SPI_SendByte(0); //發送DB7-DB0位
DAC8568_SYNC_H;
break;
}
}

}

 

/**
* @brief 設置DAC8568通道 電壓
* @param mCH:通道,0-7
* @param mVol:設置的電壓值
*/
void DAC8568_SetVoltage(unsigned char mCh,float mVol)
{
float mDatafloat;
uint16_t mDtashort;
mDatafloat = mVol * (65536/(5*1.91));
mDtashort = (uint16_t)mDatafloat;
mDtashort &= 0xffff;
if(mDtashort > 65535)
mDtashort = 65535;
DAC8568_SYNC_H;
Mcpdely();
switch(mCh)
{
case 0: //DA8568的A通道
{
DAC8568_Write_passageway(AddressOutA, mDtashort);
break;
}
case 1: //DA8568的B通道
{
DAC8568_Write_passageway(AddressOutB, mDtashort);
break;
}
case 2: //DA8568的C通道
{
DAC8568_Write_passageway(AddressOutC, mDtashort);
break;
}
case 3: //DA8568的D通道
{
DAC8568_Write_passageway(AddressOutD, mDtashort);
break;
}
case 4: //DA8568的E通道
{
DAC8568_Write_passageway(AddressOutE, mDtashort);
break;
}
case 5: //DA8568的F通道
{
DAC8568_Write_passageway(AddressOutF, mDtashort);
break;
}
case 6: //DA8568的G通道
{
DAC8568_Write_passageway(AddressOutG, mDtashort);
break;
}
case 7: //DA8568的H通道
{
DAC8568_Write_passageway(AddressOutH, mDtashort);
break;
}
}
}

最后DA8568初始化:

 

//DA8568初始化
void DAC8568_Init(void)
{
DAC8568_GPIO_Init();
DAC8568_Write_Command(RESET); //重啟
DAC8568_Write_Command(POWER_UP); //通過將相應位設置為“1”,為DAC A、B、C、D、E、F、G、H通電
DAC8568_Write_Command(SETUP_INTERNAL_REGISTER); //給內部基准電壓上電 - 靜態模式
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM