• 驱动开发:STM32F7控制AD5663模拟量输出


    AD5663是ADI公司的一款DAC模块,用以实现两路模拟量信号输出。该芯片通过SPI通信来驱动。下面讲解使用STM32F7主控芯片来控制AD5663模拟量输出的流程。

    配置STM32F7 SPI通信管脚

    STM32CubeMX生成SPI驱动代码

    1. /* SPI3 init function */
    2. void MX_SPI3_Init(void)
    3. {
    4. /* USER CODE BEGIN SPI3_Init 0 */
    5. /* USER CODE END SPI3_Init 0 */
    6. /* USER CODE BEGIN SPI3_Init 1 */
    7. /* USER CODE END SPI3_Init 1 */
    8. hspi3.Instance = SPI3;
    9. hspi3.Init.Mode = SPI_MODE_MASTER;
    10. hspi3.Init.Direction = SPI_DIRECTION_2LINES;
    11. hspi3.Init.DataSize = SPI_DATASIZE_8BIT;
    12. hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
    13. hspi3.Init.CLKPhase = SPI_PHASE_2EDGE;
    14. hspi3.Init.NSS = SPI_NSS_SOFT;
    15. hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
    16. hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
    17. hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
    18. hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
    19. hspi3.Init.CRCPolynomial = 7;
    20. hspi3.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
    21. hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
    22. if (HAL_SPI_Init(&hspi3) != HAL_OK)
    23. {
    24. Error_Handler();
    25. }
    26. /* USER CODE BEGIN SPI3_Init 2 */
    27. /* USER CODE END SPI3_Init 2 */
    28. }

    AD5663驱动代码开发

    头文件io.h

    1. #ifndef __IO_H__
    2. #define __IO_H__
    3. #ifdef __cplusplus
    4. extern "C" {
    5. #endif
    6. /* Includes ------------------------------------------------------------------*/
    7. #include "gpio.h"
    8. /* USER CODE BEGIN Includes */
    9. /* USER CODE END Includes */
    10. /* USER CODE BEGIN Private defines */
    11. /* USER CODE END Private defines */
    12. /* USER CODE BEGIN Prototypes */
    13. /*ÃüÁÒå*/
    14. #define Write_to_Input_Register 0x000000
    15. #define Update_DAC_Register 0x080000
    16. #define Software_LDAC 0x100000
    17. #define WriteTo_Update_DAC_Channel 0x180000
    18. #define Power_Down 0x200000
    19. #define RESET 0x280000
    20. #define LDAC_Register_Setup 0x300000
    21. #define Reference_Set 0x380000
    22. /*µØַģʽ,Ñ¡ÔñDACͨµÀ*/
    23. #define DAC_Channel_A 0x000000
    24. #define DAC_Channel_B 0x010000
    25. #define DAC_Channel_All 0X070000
    26. //FOR POWER DOWN
    27. #define NORMAL 0X00
    28. #define K1Ohm 0X10
    29. #define K100Ohm 0X20
    30. #define ThreeState 0X30
    31. #define PDACA 0X01
    32. #define PDACB 0X02
    33. typedef enum
    34. {
    35. AOUT1 = 0,
    36. AOUT2 = 1,
    37. } AOUT_NAME;
    38. extern void IO_Initialize(void);
    39. extern void Set_AOUT_Level(AOUT_NAME aout, uint16_t level);
    40. extern void IO_Function_Test(void);

    源文件io.c

    1. #include
    2. #include "io.h"
    3. #include "spi.h"
    4. #include "usart.h"
    5. /*Global variables*/
    6. uint16_t Aout1, Aout2;
    7. static void AD5663_PowerDown(void)
    8. {
    9. uint32_t inputShiftData = 0;
    10. uint8_t SendSPIByte[3];
    11. inputShiftData = Power_Down| NORMAL | PDACA | PDACB;
    12. SendSPIByte[0] = inputShiftData>>16;
    13. SendSPIByte[1] = inputShiftData>>8;
    14. SendSPIByte[2] = inputShiftData;
    15. HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_RESET);
    16. HAL_SPI_Transmit(&hspi3, SendSPIByte, sizeof(SendSPIByte), 100);
    17. HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_SET);
    18. }
    19. void IO_Initialize(void)
    20. {
    21. AD5663_PowerDown();
    22. }
    23. void Set_AOUT_Level(AOUT_NAME aout, uint16_t level)
    24. {
    25. uint32_t inputShiftData = 0;
    26. uint8_t SendSPIByte[3];
    27. if(aout == AOUT1)
    28. {
    29. Aout1 = level;
    30. inputShiftData = WriteTo_Update_DAC_Channel | DAC_Channel_A | level;
    31. }
    32. else if(aout == AOUT2)
    33. {
    34. Aout2 = level;
    35. inputShiftData = WriteTo_Update_DAC_Channel | DAC_Channel_B | level;
    36. }
    37. SendSPIByte[0] = inputShiftData>>16;
    38. SendSPIByte[1] = inputShiftData>>8;
    39. SendSPIByte[2] = inputShiftData;
    40. HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_RESET);
    41. HAL_SPI_Transmit(&hspi3, SendSPIByte, sizeof(SendSPIByte), 100);
    42. HAL_GPIO_WritePin(DA_SYNC_GPIO_Port, DA_SYNC_Pin, GPIO_PIN_SET);
    43. }
    44. void IO_Function_Test(void)
    45. {
    46. Set_AOUT_Level(AOUT1, 0x8000);
    47. Set_AOUT_Level(AOUT2, 0xEFFF);
    48. }

  • 相关阅读:
    第七章Redis_Jedis_测试
    uniapp<button>按钮去掉边框
    Mybatis做批量操作
    Vue3使用Svgaplayer进行.svga动画的播放
    java计算机毕业设计服装批发进销存系统MyBatis+系统+LW文档+源码+调试部署
    贝塞尔曲线的一些资料收集
    StoneDB顺利通过中科院软件所 2023 开源之夏 结项审核
    endnote设置
    从0开始python学习-54.python中flask创建MD5和base64加密校验的接口
    变量赋值中 + 号 - 号 = 号的用法
  • 原文地址:https://blog.csdn.net/xikangsoon/article/details/133278308