初级会员
- 积分
- 173
- 金钱
- 173
- 注册时间
- 2015-1-7
- 在线时间
- 39 小时
|
发表于 2023-1-14 08:32:50
|
显示全部楼层
SPI模式的芯片,用过,还行,以下是代码,拿走不谢谢
#include "Spi.h"
/********************************************************************
* 函数说明:初始化STM32外设,GPIO,时钟
* 输 入:无
* 返 回:无
******************************************************************/
void MCP4822_SPI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* 使用的外设时钟使能 -------------------------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
/* 配置驱动板引脚 --------------------------------------------*/
/* 配置SPI_SCK------------------------------------------------*/
GPIO_InitStructure.GPIO_Pin = SPI_SCK_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_SCK_Port, &GPIO_InitStructure);
/* 配置SPI_MOSI----------------------------------------------*/
GPIO_InitStructure.GPIO_Pin = SPI_MOSI_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_MOSI_Port, &GPIO_InitStructure);
/* 配置SPI_MISO -------------------------------------------------*/
GPIO_InitStructure.GPIO_Pin = SPI_MISO_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(SPI_MISO_Port, &GPIO_InitStructure);
/* 配置SPI_NSS(片选1)-----------------------------------------*/
GPIO_InitStructure.GPIO_Pin = SPI_NSS_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(SPI_NSS_Port, &GPIO_InitStructure);
MCP4822_NSS_OFF();
/* LDAC-----------------------------------------*/
GPIO_InitStructure.GPIO_Pin = LDAC_Pin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(LDAC_Port, &GPIO_InitStructure);
GPIO_ResetBits(LDAC_Port, LDAC_Pin);
/* 配置 SPI ------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(MCP4822_SPI, &SPI_InitStructure);
/* 使能 SPI */
SPI_Cmd(MCP4822_SPI, ENABLE);
}
/********************************************************************
* 函数说明:发送/接收一个字节到SPI
* 输 入:发送字节
* 返 回:接收字节
******************************************************************/
uint8_t MCP4822_Send_Byte(uint8_t byte)
{
// FM_NSS_ON();
/* 等待数据寄存器空 */
while(SPI_I2S_GetFlagStatus(MCP4822_SPI, SPI_I2S_FLAG_TXE) == RESET);
/* 通过SPI2接口发送数据 */
SPI_I2S_SendData(MCP4822_SPI, byte);
/* 等待接收到一个字节的数据 */
while(SPI_I2S_GetFlagStatus(MCP4822_SPI, SPI_I2S_FLAG_RXNE) == RESET);
// FM_NSS_OFF();
/* 返回接收的数据 */
return SPI_I2S_ReceiveData(MCP4822_SPI);
}
uint16_t MCP4822_Send_HalfWord(uint16 HalfWord)
{
// FM_NSS_ON();
/* 等待数据寄存器空 */
while(SPI_I2S_GetFlagStatus(MCP4822_SPI, SPI_I2S_FLAG_TXE) == RESET);
/* 通过SPI2接口发送数据 */
SPI_I2S_SendData(MCP4822_SPI, HalfWord);
/* 等待接收到一个字节的数据 */
while(SPI_I2S_GetFlagStatus(MCP4822_SPI, SPI_I2S_FLAG_RXNE) == RESET);
// FM_NSS_OFF();
/* 返回接收的数据 */
return SPI_I2S_ReceiveData(MCP4822_SPI);
}
/*
bit 15 A/B: DAC A or DAC B Selection bit
1 = Write to DAC B
0 = Write to DAC A
bit 14 — Don’t Care
bit 13 GA: Output Gain Selection bit
1 = 1x (V OUT = V REF * D/4096)
0 = 2x (V OUT = 2 * V REF * D/4096), where internal V REF = 2.048V.
bit 12 SHDN: Output Shutdown Control bit
1 = Active mode operation. V OUT is available. ?
0 = Shutdown the selected DAC channel. Analog output is not available at the channel that was shut down.
V OUT pin is connected to 500 k???typical)?
bit 11-0 D110: DAC Input Data bits. Bit x is ignored
*/
//通道和mV
void MCP4822_SetVoltage(uint8_t Index,uint16_t mVol)
{
uint16_t mDta = mVol;
//uint16_t m_value = mVol;//mVol/4.096*4095;
//mDta = mVol;
GPIO_SetBits(LDAC_Port, LDAC_Pin);
mDta &= 0x0FFF; //bit13=0则最大输出电压为4096mV
if(Index == 1)
{
mDta |= 0x8000; //通道1输出
}
mDta |= 0x1000; //打开输出
MCP4822_NSS_ON();
MCP4822_Send_HalfWord(mDta);
MCP4822_NSS_OFF();
GPIO_ResetBits(LDAC_Port, LDAC_Pin);
__NOP();__NOP();
GPIO_SetBits(LDAC_Port, LDAC_Pin);
}
|
|