本帖最后由 lovezichao 于 2022-3-1 10:35 编辑
使用的STM32L051C8T6的单片机。程序代码如下,用串口发出来的AD值全是FF。请大神进来看下,代码哪里有问题。
- .H文件:
- #include "stdint.h"
- #define CMD_CS30_READ_OFFSET_REG ((1 << 3) | (1 << 0))
- #define CMD_CS30_WRITE_OFFSET_REG ((0 << 3) | (1 << 0))
-
- #define CMD_CS30_READ_GAIN_REG ((1 << 3) | (1 << 1))
- #define CMD_CS30_WRITE_GAIN_REG ((0 << 3) | (1 << 1))
-
- #define CMD_CS30_READ_CFG_REG ((1 << 3) | (3 << 0))
- #define CMD_CS30_WRITE_CFG_REG ((0 << 3) | (3 << 0))
- #define CMD_CS30_SINGLE_CONVER ((1 << 7) | (0 << 6))
- #define CMD_CS30_CONTINUOUS_CONVER ((1 << 7) | (1 << 6))
- #define CMD_CS30_SYS_OFFSET_CALIB 0x85
- #define CMD_CS30_SYS_GAIN_CALIB 0x86
- #define CFG_BIT_RS_CS30 (1 << 29)
- #define CFG_BIT_RV_CS30 (1 << 28)
- #define CFG_BIT_FRS_CS30 (1 << 19)
- #define CFG_VRS_25UP_CS30 (0x00 << 25) //2.5V < Vref <= VA+ - VA-
- #define CFG_VRS_25LS_CS30 (0x01 << 25) //1V <= Vref <= 2.5V
- #define CFG_B_POLAR_CS30 (0x00 << 10)
- #define CFG_U_POLAR_CS30 (0x01 << 10)
- #define WR_CS30_120 (0x00 << 11)
- #define WR_CS30_60 (0x01 << 11)
- #define WR_CS30_30 (0x02 << 11)
- #define WR_CS30_15 (0x03 << 11)
- #define WR_CS30_7p5 (0x04 << 11)
- #define WR_CS30_3840 (0x08 << 11)
- #define WR_CS30_1920 (0x09 << 11)
- #define WR_CS30_960 (0x0A << 11)
- #define WR_CS30_480 (0x0B << 11)
- #define WR_CS30_240 (0x0C << 11)
- void CS5530_Write_Byte(unsigned char dat);
- void CS5530_Read_Data(void);
- void CS5530_Sample(void);
- void CS5530_Init(void);
- void CS5530_Read(uint8_t* Buf, uint32_t len);
- int32_t CS5530_ReadADC_Data(void);
- void CS5530_Read(uint8_t* Buf, uint32_t len);
- .C文件:
- #include "stm32l0xx_hal.h"
- #include "cs5530.h"
- #include "stm32l0xx_hal_spi.h"
- #include "main.h"
- #include "string.h "
- #define _SPI_DELAY_ 50 //
- #define SPI_CS(x) (x?HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_SET):HAL_GPIO_WritePin(GPIOB,GPIO_PIN_12,GPIO_PIN_RESET))
- #define SPI_CLK(x) (x?HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_SET):HAL_GPIO_WritePin(GPIOB,GPIO_PIN_13,GPIO_PIN_RESET))
- #define SPI_MOSI(x) (x?HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_SET):HAL_GPIO_WritePin(GPIOB,GPIO_PIN_15,GPIO_PIN_RESET))
- #define SPI_MISO HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)
- void SPI_Delay(uint16_t del){
-
- while(del--){
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- }
- uint8_t SPI_Write_Read_Byte(uint8_t TX_Dat){
- uint8_t i;
- uint8_t RX_Dat = 0;
-
- for(i = 0; i < 8; i++){
-
- if(TX_Dat & 0x80){
- SPI_MOSI(1);
- } else {
- SPI_MOSI(0);
- }
- TX_Dat <<= 1;
-
- SPI_CLK(1);
- SPI_Delay(_SPI_DELAY_);
-
- RX_Dat <<= 1;
- if(SPI_MISO != 0){
- RX_Dat |= 0x01;
- }
-
- SPI_CLK(0);
- SPI_Delay(_SPI_DELAY_);
- }
- return RX_Dat;
- }
- static void CS5530_Send(uint8_t* data, uint32_t len){
- uint32_t i;
- for(i = 0; i < len; i++){
- SPI_Write_Read_Byte(data);
- }
- }
- static void CS5530_Read(uint8_t* Buf, uint32_t len){
- uint32_t i;
-
- for(i = 0; i < len; i++){
- Buf = SPI_Write_Read_Byte(0x00);
- }
- }
- static uint8_t CS30_CMD_BUF[20];
- #define SYNC_LEN 18
- static void CS5530_Sync(void){
- memset(CS30_CMD_BUF,0xff,SYNC_LEN);
- CS30_CMD_BUF[SYNC_LEN - 1] = 0xfe;
- CS5530_Send(CS30_CMD_BUF,SYNC_LEN);
- }
- static void CS5530_WriteResByCmd(uint32_t cmd,uint32_t res){
- CS30_CMD_BUF[0] = cmd;
- CS30_CMD_BUF[1] = res >> 24;
- CS30_CMD_BUF[2] = res >> 16;
- CS30_CMD_BUF[3] = res >> 8;
- CS30_CMD_BUF[4] = res;
-
- CS5530_Send(CS30_CMD_BUF,5);
- }
- static uint32_t CS5530_ReadResByCmd(uint32_t cmd){
- CS30_CMD_BUF[0] = cmd;
- CS5530_Send(CS30_CMD_BUF,1);
- CS5530_Read(&CS30_CMD_BUF[1],4);
-
- return (uint32_t)((CS30_CMD_BUF[1] << 24) | (CS30_CMD_BUF[2] << 16) | (CS30_CMD_BUF[3] << 8) | CS30_CMD_BUF[4]);
- }
- static void CS5530_Reset(void){
- //Read Cfg , clear RV bit
- CS5530_ReadResByCmd(CMD_CS30_WRITE_CFG_REG);
-
- //write RS bit to 1, wait 8 master clock sycles wirte RS back to 0.
- CS5530_WriteResByCmd(CMD_CS30_WRITE_CFG_REG, CFG_BIT_RS_CS30);
- // osDelay(pdMS_TO_TICKS(5));
- SPI_Delay(10);
- CS5530_WriteResByCmd(CMD_CS30_WRITE_CFG_REG, 0);
-
- while(!(CS5530_ReadResByCmd(CMD_CS30_WRITE_CFG_REG) & CFG_BIT_RV_CS30)){
- // osDelay(pdMS_TO_TICKS(5));
- SPI_Delay(100);
- }
- CS5530_WriteResByCmd(CMD_CS30_WRITE_CFG_REG, 0);
- }
- static void CS5530_Start(uint32_t type){
- CS30_CMD_BUF[0] = type;
- CS5530_Send(CS30_CMD_BUF,1);
- }
- static uint32_t cfgResCS30 = 0;
- void CS5530_Init(){
- SPI_CS(0);
- CS5530_Sync();
- CS5530_Reset();
-
- cfgResCS30 = CFG_VRS_25LS_CS30 | WR_CS30_240 | CFG_U_POLAR_CS30;// | CFG_BIT_FRS_CS30;
- CS5530_WriteResByCmd(CMD_CS30_WRITE_CFG_REG, cfgResCS30);
-
- HAL_Delay(5);
- CS5530_Start(CMD_CS30_CONTINUOUS_CONVER);//
- SPI_CS(1);
- }
- int32_t CS5530_ReadADC_Data() {
- int32_t adcCode = 0;
-
- memset(CS30_CMD_BUF, 0x00, 5);
-
- SPI_CS(0);
- CS5530_Read(CS30_CMD_BUF, 5);
- SPI_CS(1);
-
- adcCode = (CS30_CMD_BUF[1] << 24) | (CS30_CMD_BUF[2] << 16) | (CS30_CMD_BUF[3] << 8) | CS30_CMD_BUF[4];
-
- return adcCode;
- }
|