STM32F030C8读取CS1237采集模拟

2023-12-13 15:59:25


Chapter1 【问题解决记录】STM32F030C8读取CS1237采集模拟

原文链接:https://blog.csdn.net/weixin_40058986/article/details/133160155

问题描述

使用STM32F030C8读取CS1237采集模拟电压时,遇到CS1237的CONFIG寄存器无法读写正常的困扰;就是配置 RefOut_OFF | SpeedSelct_1280HZ | PGA_1 | CH_A 这些参数无法正确写入,实际是我要写入0x70,但是读出来是随机值;而修改代码,上电MCU直接读取CONFIG,得到数值是0x0C(和手册默认值是对应的,说明读操作正常)。
而直接读取CS1237的ADC转换数值是和外界输入电压是一致的,只是设置CONFG寄存器读写不一致。

原因分析:

第一个想到的原因是,CS1237的操作时序问题,有几个注意点是:
(1) 设置STM32的DOUT端口为INPUT,监测来自CS1237的DOUT电平,一般芯片上电后,CS1237的DOUT会不断地输出高脉冲(示波器测量约20us的高脉冲),代码中要监测到DOUT的”下降沿“,然后才是读写配置和读取ADC转换数值的操作;
(2)参考了网络好多示例代码,尤其是”技小新“的CS1237模块,LCSC立创商城可以搜到有;我的代码基本和他的demo一样;
(3)CS1237上电后需要延时300毫秒的启动时间?这个实际测试后发现,这个时间不是必须;
在这里插入图片描述
一顿仔细检查代码,发现了STM32端口配置的”笔误“,修改后,仍然无法正确配置CONFIG;
第二个想到的原因是,硬件CS1237和STM32的电平兼容问题,我的电路CS1237是5V供电,stm32是3V3供电,它们之间SPI通过22欧姆电阻串联。修改硬件电路,将CS1237的电源电压改为3V3后,虽然仍无法正确配置CONFIG,但是采集电压变得稳定许多;
修改串联电阻为100欧姆,600欧姆,6k5欧姆,几乎没有改善;
第三个原因是,设置CS1237的转换速率是不是对于STM32F0这个MCU来说有点太快了?于是我配置了SpeedSelct_1280HZ 是配置不正常,而设置其他SpeedSelct_640HZ及以下频率,发现CONFIG寄存器正常读写 ,没错!是可以正常读写,而PGA的放大倍数也可以正常设置了。

下面是关键部分的代码:

#include "gp8211s.h"
#include "main.h"
#include "delay.h"

uint8_t Gp8211s_Channel_Sel;

void Gp8211s_Choose_Channel(uint8_t ch)
{
	if( !ch ) 
		Gp8211s_Channel_Sel = 0;
	else
		Gp8211s_Channel_Sel = 1;
}

void Gp8211s_SCL_Clr()
{
	if( !Gp8211s_Channel_Sel )
		HAL_GPIO_WritePin(GP1_SCLK_GPIO_Port, GP1_SCLK_Pin, GPIO_PIN_RESET);
	else	
		HAL_GPIO_WritePin(GP2_SCLK_GPIO_Port, GP2_SCLK_Pin, GPIO_PIN_RESET);
}
void Gp8211s_SCL_Set()
{
	if( !Gp8211s_Channel_Sel )
		HAL_GPIO_WritePin(GP1_SCLK_GPIO_Port, GP1_SCLK_Pin, GPIO_PIN_SET);
	else	
		HAL_GPIO_WritePin(GP2_SCLK_GPIO_Port, GP2_SCLK_Pin, GPIO_PIN_SET);
}
void Gp8211s_SDA_Clr()
{
	if( !Gp8211s_Channel_Sel )
		HAL_GPIO_WritePin(GP1_SDA_GPIO_Port, GP1_SDA_Pin, GPIO_PIN_RESET);
	else	
		HAL_GPIO_WritePin(GP2_SDA_GPIO_Port, GP2_SDA_Pin, GPIO_PIN_RESET);
}
void Gp8211s_SDA_Set()
{
	if( !Gp8211s_Channel_Sel )
		HAL_GPIO_WritePin(GP1_SDA_GPIO_Port, GP1_SDA_Pin, GPIO_PIN_SET);
	else	
		HAL_GPIO_WritePin(GP2_SDA_GPIO_Port, GP2_SDA_Pin, GPIO_PIN_SET);
}


void Gp8211s_IIC_SdaOut()
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	if( !Gp8211s_Channel_Sel )
	{
		GPIO_InitStruct.Pin = GP1_SDA_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		HAL_GPIO_Init( GP1_SDA_GPIO_Port, &GPIO_InitStruct );
	}
	else
	{
		GPIO_InitStruct.Pin = GP2_SDA_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		HAL_GPIO_Init( GP2_SDA_GPIO_Port, &GPIO_InitStruct );
	}
}

void Gp8211s_IIC_SdaIn()
{
	GPIO_InitTypeDef GPIO_InitStruct;
	//return;

	if( !Gp8211s_Channel_Sel )
	{
		GPIO_InitStruct.Pin = GP1_SDA_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		HAL_GPIO_Init( GP1_SDA_GPIO_Port, &GPIO_InitStruct );
	}
	else
	{
		GPIO_InitStruct.Pin = GP2_SDA_Pin;
		GPIO_InitStruct.Mode = GPIO_MODE_INPUT; 
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		HAL_GPIO_Init( GP2_SDA_GPIO_Port, &GPIO_InitStruct );
	}
}

void Gp8211s_IIC_Init()
{
	Gp8211s_IIC_SdaOut();
	Gp8211s_SDA_Set();
	Gp8211s_SCL_Set();
	delay_us(10);
}

void Gp8211s_IIC_Start(void)
{
	Gp8211s_IIC_SdaOut();
	Gp8211s_SDA_Set();
	Gp8211s_SCL_Set();
	delay_us(10);
	Gp8211s_SDA_Clr();
	delay_us(10);
}

void Gp8211s_IIC_Stop(void)
{
	Gp8211s_IIC_SdaOut();
	Gp8211s_SDA_Clr();
	Gp8211s_SCL_Set();
	delay_us(10);
	Gp8211s_SDA_Set();
	delay_us(10);
}

void Gp8211s_IIC_SendByte(uint8_t txd)
{
	uint8_t t;  
	uint8_t tdata = txd;	
	
	Gp8211s_IIC_SdaOut();
    Gp8211s_SCL_Clr();
	delay_us(10);
    for(t=0; t<8; t++)
    {              
        if( tdata & 0x80 )
			Gp8211s_SDA_Set();
		else
			Gp8211s_SDA_Clr();
        tdata <<= 1; 	  
		delay_us(10);
		Gp8211s_SCL_Set();
		delay_us(10);
		Gp8211s_SCL_Clr();
		delay_us(10);
    }	 
}



uint8_t Gp8211s_IIC_SdaState()
{
	uint8_t ret;
	
	if( !Gp8211s_Channel_Sel )
		ret = HAL_GPIO_ReadPin(GP1_SDA_GPIO_Port, GP1_SDA_Pin);
	else
		ret = HAL_GPIO_ReadPin(GP2_SDA_GPIO_Port, GP2_SDA_Pin);
	return ret;
}

uint8_t Gp8211s_IIC_ReadByte(void)
{
	uint8_t t;  
	uint8_t rdata=0;	
	
	Gp8211s_IIC_SdaIn();
    for(t=0; t<8; t++)
    {      
		Gp8211s_SCL_Set();
		delay_us(10);
		rdata <<= 1; 
		
        if( Gp8211s_IIC_SdaState() )
			rdata|=0x01;
		Gp8211s_SCL_Clr();
		delay_us(10);
    }	 
	return rdata;
}

uint8_t Gp8211s_IIC_WaitAck(void)
{
	uint32_t ucErrTime = 0;
	GPIO_PinState sda_state;
	uint8_t RetValue = 0;
	
	Gp8211s_SCL_Set();
	delay_us(20);
		
	#if 0	
	Gp8211s_IIC_SdaIn();
	while(ucErrTime++ < 10000)
	{
		delay_us(20);
		sda_state = Gp8211s_IIC_SdaState();
		if( sda_state == GPIO_PIN_RESET )
		{
			RetValue = 1;
			break;
		}
	}
	
	if( ucErrTime >= 10000 )
	{
		printf("Gp8211s_IIC_WaitAck timeout\n");
		Gp8211s_IIC_Stop();
	}
	#endif
	
	return RetValue;
}

void Gp8211s_Init(void)
{	
	//Range 0 to 5V
	Gp8211s_IIC_Start();
	Gp8211s_IIC_SendByte(0xB0);
	Gp8211s_IIC_WaitAck();
	Gp8211s_IIC_SendByte(0x01);
	Gp8211s_IIC_WaitAck();
	Gp8211s_IIC_SendByte(0x55);   //0x44, 5V; 0x55: 10V; 0x77:12V;
	Gp8211s_IIC_WaitAck();
	Gp8211s_IIC_Stop(); 
}

// data_hex max: 0x7FFF
void gp8211s_send_cmd(uint16_t data_hex )
{	
	uint8_t data8_tmp = 0;

	//Set Reg value
	Gp8211s_IIC_Start();
	Gp8211s_IIC_SendByte(0xB0);
	Gp8211s_IIC_WaitAck();
	Gp8211s_IIC_SendByte(0x02);
	Gp8211s_IIC_WaitAck();
	data8_tmp = data_hex & 0xFF;
	Gp8211s_IIC_SendByte(data8_tmp);//DATA Low
	//Gp8211s_IIC_WaitAck();   //!!! add this, not OK;
	data8_tmp = (data_hex & 0x7F00 )>> 8;
	Gp8211s_IIC_SendByte(data8_tmp);//DATA High
	Gp8211s_IIC_WaitAck();
	Gp8211s_IIC_Stop(); 
}

void gp8211s_OutPutVolt(uint16_t volt_in_mv )
{	
	uint32_t VoltRange = 10*1000; //10V
	uint16_t  RegVal = 0;
	
	if( volt_in_mv > VoltRange )
		volt_in_mv = VoltRange;
	
	RegVal = volt_in_mv * 0x7FFF / VoltRange;
	RegVal &= 0x7FFF;
	//printf("volt_in_mv: %d, RegVal: %X  \n ", volt_in_mv, RegVal);
	
	gp8211s_send_cmd(RegVal);
//		gp8211s_send_cmd(0x4000);
	//printf("volt_in_mv: %d, RegVal: %X  \n ", volt_in_mv, RegVal);
}

main部分

configData = RefOut_OFF | SpeedSelct_640HZ | PGA_1 | CH_A;
	Con_CS1237(configData);
	if( Read_CON() != configData )
		printf("config in1 error! set: %X, get:%X \r\n ", configData,  Read_CON());
	printf("> config ch1 ok! \n");

解决方案:

实物照片:外部输入1.50V,OLED的IN1显示1.472V;基本正确;
在这里插入图片描述
例如:新建一个 Message 对象,并将读取到的数据存入 Message,然后 mHandler.obtainMessage(READ_DATA, bytes, -1, buffer).sendToTarget();换成 mHandler.sendMessage()。

Chapter2 CS1237 STM32控制程序以及原理图需要注意事项

原文链接:https://blog.csdn.net/qq_34991787/article/details/111083226

CS1237供电为5V的时候STM32是3.3V这时候应该加不小于5.1K的上拉电阻,不然CS1237跟32通信不上,32设置为OD驱动,同时不需要设计通信引脚的限流电阻就可以

下面是驱动程序

----------------------------------------------------------MAIN----------------------------------------------------------

/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "mycs1237.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t mystrlen(uint8_t *tmp)
{
    uint8_t i=0;
    while(1)
    {
        if(tmp[i]!='\0')
            i++;
        else
            return i;
    }
}
    
/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
    uint8_t buff[128]={0};
    uint8_t tmp=0;
    unsigned long mydata=0;
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
  /* USER CODE BEGIN 2 */
    init_cs1237();
    tmp=CS1237_REF(1)|CS1237_SPEED(1)|CS1237_PGA(0)|CS1237_CH(0);
    sprintf(buff,"SET CF is:0x%X\n",tmp);
    HAL_UART_Transmit(&huart1,buff,mystrlen(buff),100);
    memset(buff,0,128);
    rw_cs1237_cofig(0x65,tmp);
    
    tmp=rw_cs1237_cofig(0x56,0);
    sprintf(buff,"GET CF is:0x%X\n",tmp);
    HAL_UART_Transmit(&huart1,buff,mystrlen(buff),100);
    memset(buff,0,128);
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
        mydata=read_cs1237_data();
        sprintf(buff,"read adc:0x%lX\n",mydata);
        HAL_UART_Transmit(&huart1,buff,mystrlen(buff),100);
        memset(buff,0,128);
        mydata=0;
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 4;
  RCC_OscInitStruct.PLL.PLLN = 168;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 4;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */

  /* USER CODE END Error_Handler_Debug */
}

#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{ 
  /* USER CODE BEGIN 6 */
  /* User can add his own implementation to report the file name and line number,
     tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
----------------------------------------------------------CS1237.C----------------------------------------------------------

 

#include "mycs1237.h"

static GPIO_InitTypeDef GPIO_InitStruct = {0};

#define CS1237_PINSCLK GPIO_PIN_14
#define CS1237_PINDD GPIO_PIN_13

#define CS1237_SCLK(x) HAL_GPIO_WritePin(GPIOB, CS1237_PINSCLK,x)
#define CS1237_DD(x) HAL_GPIO_WritePin(GPIOB, CS1237_PINDD,x)

#define READ_SCLK HAL_GPIO_ReadPin(GPIOB,CS1237_PINSCLK)
#define READ_DD HAL_GPIO_ReadPin(GPIOB,CS1237_PINDD)

#define CS1237_GPIO_OUT(x)  GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Pin = x;GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct)
#define CS1237_GPIO_IN(x)  GPIO_InitStruct.Pull = GPIO_NOPULL;GPIO_InitStruct.Pin = x;GPIO_InitStruct.Mode = GPIO_MODE_INPUT;GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;HAL_GPIO_Init(GPIOB, &GPIO_InitStruct)

//SCLK高100uS会关闭ADC!!!

static void cs1237_nop(void)
{
    unsigned int i=0;
    for(i=0;i<20;i++)
__asm {
    nop
}
}

void init_cs1237(void)
{
    
    CS1237_DD(GPIO_PIN_SET);
    CS1237_SCLK(GPIO_PIN_RESET);
    
  CS1237_GPIO_OUT(CS1237_PINDD);
    CS1237_GPIO_OUT(CS1237_PINSCLK);
    
    CS1237_SCLK(GPIO_PIN_SET);
    HAL_Delay(500);//ms
    CS1237_SCLK(GPIO_PIN_RESET);
    HAL_Delay(500);//ms
}

unsigned long read_cs1237_data(void)
{
    unsigned short i=0;
    unsigned long tmp=0;
    

    CS1237_GPIO_IN(CS1237_PINDD);//输入数据
    CS1237_GPIO_OUT(CS1237_PINSCLK);//输出脉冲
    
  while((GPIO_PIN_SET==READ_DD)&&(i<320))
    {
        i++;
        HAL_Delay(1);//ms
    }
    if(i<320)
    {}
    else
    {
        return 0;
    }
    for(i=0;i<24;i++)//1-24读取数据
    {    
        tmp<<=1;
        CS1237_SCLK(GPIO_PIN_SET);
        cs1237_nop();//460ns
        CS1237_SCLK(GPIO_PIN_RESET);
        cs1237_nop();//460ns        
        if(GPIO_PIN_SET==READ_DD)
            tmp++;
    }
    for(i=0;i<3;i++)//25-27拉高数据脚
    {
        CS1237_SCLK(GPIO_PIN_SET);
        cs1237_nop();//ns
        CS1237_SCLK(GPIO_PIN_RESET);
        cs1237_nop();//ns
    }
    CS1237_GPIO_IN(CS1237_PINDD);
    tmp^=0x800000;
    return tmp;
}

unsigned char rw_cs1237_cofig(unsigned char cmd,unsigned char data)
{
    unsigned char tmp=0;
    unsigned short i=0;
    unsigned char rw_flag=0;
    unsigned char cnoo = 0;
    
    if(0x65==cmd)
        rw_flag=1;
    else
        rw_flag=0;
    
    CS1237_GPIO_IN(CS1237_PINDD);
    
    while((READ_DD==1)&&(i<320))
    {
        i++;
        HAL_Delay(1);//ms
    }
    if(i<320)
    {}
    else
    {
        return 0;
    }
    for(i=1;i<25;i++)//1-24脉冲
    {
        CS1237_SCLK(GPIO_PIN_SET);
        cs1237_nop();//ns
        CS1237_SCLK(GPIO_PIN_RESET);
        cs1237_nop();//ns
    }
    for(i=25;i<27;i++)//25-26
    {
        cnoo<<=1;
        CS1237_SCLK(GPIO_PIN_SET);
        cs1237_nop();//ns        
        CS1237_SCLK(GPIO_PIN_RESET);
        cs1237_nop();//ns    
        if(1==READ_DD)
            cnoo++;
    }
    CS1237_SCLK(GPIO_PIN_SET);//27
    cs1237_nop();//ns
    CS1237_SCLK(GPIO_PIN_RESET);
    cs1237_nop();//ns
    CS1237_SCLK(GPIO_PIN_SET);//28
    cs1237_nop();//ns
    CS1237_SCLK(GPIO_PIN_RESET);
    cs1237_nop();//ns
    CS1237_SCLK(GPIO_PIN_SET);//29
    cs1237_nop();//ns
    CS1237_SCLK(GPIO_PIN_RESET);
    cs1237_nop();//ns    
  CS1237_GPIO_OUT(CS1237_PINDD);
    for(i=30;i<37;i++)//30-36
    {
        CS1237_SCLK(GPIO_PIN_SET);
        cs1237_nop();//ns
        if((cmd&0x40)==(0x40))
            CS1237_DD(1);
        else
            CS1237_DD(0);
        CS1237_SCLK(GPIO_PIN_RESET);
        cs1237_nop();//ns
        cmd<<=1;
    }
    CS1237_SCLK(GPIO_PIN_SET);//37
    cs1237_nop();//ns
    CS1237_SCLK(GPIO_PIN_RESET);
    cs1237_nop();//ns        
    if(rw_flag==1)
    {
        for(i=38;i<46;i++)//38-45
        {
            CS1237_SCLK(GPIO_PIN_SET);
            cs1237_nop();//ns
            if((data&0x80)==0x80)
                CS1237_DD(1);
            else
                CS1237_DD(0);
            CS1237_SCLK(GPIO_PIN_RESET);
            cs1237_nop();//ns    
            data<<=1;
        }
    }
    else
    {
        CS1237_GPIO_IN(CS1237_PINDD);
        for(i=38;i<46;i++)//38-45
        {
            tmp<<=1;
            CS1237_SCLK(GPIO_PIN_SET);
            cs1237_nop();//ns
            CS1237_SCLK(GPIO_PIN_RESET);
            cs1237_nop();//ns    
            if(READ_DD==1)
                tmp++;
        }
    }
    CS1237_SCLK(GPIO_PIN_SET);//46
    cs1237_nop();//ns
    CS1237_SCLK(GPIO_PIN_RESET);
    cs1237_nop();//ns    
    CS1237_GPIO_IN(CS1237_PINDD);
    return tmp;
}
----------------------------------------------------------CS1237.H----------------------------------------------------------

 

#ifndef __MYCS1237__
#define __MYCS1237__

#include "stm32f4xx_hal.h"

#define CS1237_REF(x) (x<<6)//1off
#define CS1237_SPEED(x) (x<<4)//0\10hz 1\40hz 2\640hz 3\1280hz
#define CS1237_PGA(x)  (x<<2)//0\1 1\2 2\64 3\128
#define CS1237_CH(x)  (x<<0)//0\A 1\ 2\wd 3\nd 

void init_cs1237(void);
unsigned long read_cs1237_data(void);
unsigned char rw_cs1237_cofig(unsigned char cmd,unsigned char data);


#endif

文章来源:https://blog.csdn.net/m0_46577050/article/details/134972563
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。