新手入门
- 积分
- 18
- 金钱
- 18
- 注册时间
- 2019-12-29
- 在线时间
- 7 小时
|
发表于 2020-3-14 23:09:02
|
显示全部楼层
/* Includes ------------------------------------------------------------------*/
#include "DAC856x.h"
/** @addtogroup Template_Project
* @{
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/**
* @brief Initializes the I2S.
* @param None
* @retval None
*/
void SPI1_Init( void )
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* GPIO */
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7;
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_PinAFConfig( GPIOA, GPIO_PinSource5, GPIO_AF_SPI1 ); // PA5 -> SPI1_SCK - SCLK
GPIO_PinAFConfig( GPIOA, GPIO_PinSource7, GPIO_AF_SPI1 ); // PA7 -> SPI1_MOSI - DIN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init( GPIOA, &GPIO_InitStructure );
GPIO_SetBits(GPIOA, GPIO_Pin_4); // PA4 -> SYNC
/* SPI */
//Only for initializing the SPI port
RCC_APB2PeriphResetCmd( RCC_APB2Periph_SPI1, ENABLE ); // Reset SPI1
RCC_APB2PeriphResetCmd( RCC_APB2Periph_SPI1, DISABLE ); // Stop reset SPI1
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
/*
* The DAC856x write sequence begins by bringing the SYNC line low.
* If the CPHA (clock phase) bit is 1, the second edge on the SCK pin
* (falling edge if the CPOL bit is 0) is the MSBit capture strobe.
*/
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // SCLK = APB2 / 16 = 84MHz / 16 = 5.25MHz
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init( SPI1, &SPI_InitStructure );
SPI_Cmd( SPI1, ENABLE );
}
/**
* @brief Write one byte of data to SPI1.
* @param TxData: Data to be written
* @retval None
*/
void SPI1_Write_Byte( uint8_t TxData )
{
while( RESET == SPI_I2S_GetFlagStatus( SPI1, SPI_I2S_FLAG_TXE ) ); // Wait buffer empty
SPI_I2S_SendData( SPI1, TxData );
}
/**
* @brief Write the DAC856x.
* @param None
* @retval None
*/
void DAC856x_Write( uint8_t cmd, uint16_t data )
{
GPIO_ResetBits(GPIOA, GPIO_Pin_4);
SPI1_Write_Byte( cmd );
SPI1_Write_Byte( ( uint8_t ) ( data >> 8 ) );
SPI1_Write_Byte( ( uint8_t ) ( data & 0xFF ) );
while ( SET == SPI_I2S_GetFlagStatus( SPI1, SPI_I2S_FLAG_BSY ) ); // Wait finish sending
GPIO_SetBits(GPIOA, GPIO_Pin_4);
}
/**
* @brief Initializes the DAC856x.
* @param None
* @retval None
*/
void DAC856x_Init(void)
{
SPI1_Init();
// 0x28, 0x0001
DAC856x_Write( CMD_RESET_REG, DATA_RESET_ALL_REG ); // Reset
// 0x30, 0x0003
DAC856x_Write( CMD_LDAC, DATA_LDAC_DIS ); // LDAC pin inactive for DAC-B and DAC-A
// 0x38, 0x0001
DAC856x_Write( CMD_INTERNAL_REF, DATA_INTERNAL_REF_EN ); // Enable internal reference and reset DACs to gain = 2
// 0x02, 0x0000
DAC856x_Write( CMD_GAIN, DATA_GAIN_B2_A2 ); // DAC-B gain = 2, DAC-A gain = 2 (default with internal VREF)
// 0x20, 0x0003
DAC856x_Write( CMD_PWR_A_B, DATA_PWR_UP_A_B ); // Power up DAC-A and DAC-B
}
/**
* @brief Write DAC856x data buffer A.
* @param data: Written data.
* @retval None
*/
void DAC856x_Write_Buffer_A( uint16_t data )
{
// 0x18, data
DAC856x_Write( CMD_WRITE_UPDATE_A, data );
}
/**
* @brief Write DAC856x data buffer B.
* @param data: Written data.
* @retval None
*/
void DAC856x_Write_Buffer_B( uint16_t data )
{
// 0x19, data
DAC856x_Write( CMD_WRITE_UPDATE_B, data );
}
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
|