技术热线: 0755-86643915

新闻动态

芯海代理:CS1237的例程

更新时间: 2024-01-16 15:29:46
阅读量:

                    
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
",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
",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
",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
", 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)//010hz 140hz 2640hz 31280hz
 #define CS1237_PGA(x)  (x<<2)//01 12 264 3128
 #define CS1237_CH(x)  (x<<0)//0A 1 2wd 3
d  

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


 #endif
   

  
资料源自网络,侵删