找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5543|回复: 7
收起左侧

基于stm32采集ads1255这款24位ad的源代码

  [复制链接]
ID:394304 发表于 2018-9-10 13:20 | 显示全部楼层 |阅读模式
次代码为stm32f系列单片机采集ads1255这款24位ad的源代码,通讯采用spi方式,24位ad的精度是可20ms采集一次,效率很高

单片机源程序:
  1. #include "main.h"
  2. #include "stm32f1xx_hal.h"
  3. #include "dma.h"
  4. #include "iwdg.h"
  5. #include "spi.h"
  6. #include "tim.h"
  7. #include "usart.h"
  8. #include "gpio.h"

  9. /* USER CODE BEGIN Includes */
  10. #include "common.h"
  11. #include "ads1255.h"
  12. #include "Protocol.h"
  13. /* USER CODE END Includes */

  14. /* Private variables ---------------------------------------------------------*/

  15. /* USER CODE BEGIN PV */

  16. /* Buffer used for reception */
  17. #define BUFFER_SIZE 64  //宏定义缓冲区大小

  18. /* Private variables ---------------------------------------------------------*/
  19. uint16_t rx_len1 = 0;//定义一个接收长度变量
  20. uint8_t recv_end_flag1 = 0;//定义一个接收结束标变量
  21. uint8_t rx_buffer1[64];//定义一个接收缓冲数组,128个元素
  22. uint8_t sendcommand_flag=0x03;
  23. /* USER CODE END PV */

  24. /* Private function prototypes -----------------------------------------------*/
  25. void SystemClock_Config(void);

  26. /* USER CODE BEGIN PFP */
  27. /* Private function prototypes -----------------------------------------------*/

  28. /* USER CODE END PFP */

  29. /* USER CODE BEGIN 0 */

  30. /* USER CODE END 0 */

  31. int main(void)
  32. {

  33.   /* USER CODE BEGIN 1 */

  34.   /* USER CODE END 1 */

  35.   /* MCU Configuration----------------------------------------------------------*/

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

  38.   /* USER CODE BEGIN Init */

  39.   /* USER CODE END Init */

  40.   /* Configure the system clock */
  41.   SystemClock_Config();

  42.   /* USER CODE BEGIN SysInit */

  43.   /* USER CODE END SysInit */

  44.   /* Initialize all configured peripherals */
  45.   MX_GPIO_Init();
  46.   MX_DMA_Init();
  47.   MX_IWDG_Init();
  48.   MX_SPI2_Init();
  49.   MX_TIM2_Init();
  50.   MX_USART1_UART_Init();
  51. //  MX_TIM3_Init();

  52.   /* USER CODE BEGIN 2 */
  53.   HAL_TIM_Base_Start_IT(&htim2);
  54.   ads1255_init();
  55.   
  56.   Uart_Event_Init(); //初始化串口事件

  57.   
  58.   __HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE);        //使能串口1空闲中断
  59.    UartEventType DataReceive;
  60. //  HAL_TIM_Base_Start_IT(&htim3);
  61.   /* USER CODE END 2 */

  62.   /* Infinite loop */
  63.   /* USER CODE BEGIN WHILE */
  64.   while (1)
  65.   {
  66.   /* USER CODE END WHILE */

  67.   /* USER CODE BEGIN 3 */
  68.     HAL_IWDG_Refresh(&hiwdg);
  69.     if(Uart_Event_Get(&DataReceive)==true)
  70.     {
  71.       sendcommand_flag=rx_buffer1[4];
  72.     }
  73.    
  74.     if(sendcommand_flag==0x01)
  75.     {
  76.       HAL_UART_Transmit_DMA(&huart1,FidDataFunction(),12);
  77.     }
  78.     else
  79.     {
  80.       
  81.     }
  82.     HAL_UART_Receive_DMA(&huart1,rx_buffer1,BUFFER_SIZE);
  83.   }
  84.    
  85. //      printf("\r\n****** FID Controller System ******\r\n ");
  86. //      printf("Set SYSCLK to %dHz\r\n", HAL_RCC_GetSysClockFreq());
  87. //      printf("Set HCLK   to %dHz\r\n", HAL_RCC_GetHCLKFreq());
  88. //      printf("Set PCLK1  to %dHz\r\n", HAL_RCC_GetPCLK1Freq());
  89. //      printf("Set PCLK2  to %dHz\r\n", HAL_RCC_GetPCLK2Freq());
  90. //      printf("signal value:%0.4lf\r\n",sign);
  91.   }
  92.   /* USER CODE END 3 */


  93. /** System Clock Configuration
  94. */
  95. void SystemClock_Config(void)
  96. {

  97.   RCC_OscInitTypeDef RCC_OscInitStruct;
  98.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  99.     /**Initializes the CPU, AHB and APB busses clocks
  100.     */
  101.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  102.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  103.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  104.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  105.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  106.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  107.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  108.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  109.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  110.   {
  111.     _Error_Handler(__FILE__, __LINE__);
  112.   }

  113.     /**Initializes the CPU, AHB and APB busses clocks
  114.     */
  115.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  116.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  117.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  118.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  119.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  120.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  121.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  122.   {
  123.     _Error_Handler(__FILE__, __LINE__);
  124.   }

  125.     /**Configure the Systick interrupt time
  126.     */
  127.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  128.     /**Configure the Systick
  129.     */
  130.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  131.   /* SysTick_IRQn interrupt configuration */
  132.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  133. }

  134. /* USER CODE BEGIN 4 */

  135. /* USER CODE END 4 */

  136. /**
  137.   * @brief  This function is executed in case of error occurrence.
  138.   * @param  None
  139.   * @retval None
  140.   */
  141. void _Error_Handler(char * file, int line)
  142. {
  143.   /* USER CODE BEGIN Error_Handler_Debug */
  144.   /* User can add his own implementation to report the HAL error return state */
  145.   while(1)
  146.   {
  147.   }
  148.   /* USER CODE END Error_Handler_Debug */
  149. }

  150. #ifdef USE_FULL_ASSERT

  151. /**
  152.    * @brief Reports the name of the source file and the source line number
  153.    * where the assert_param error has occurred.
  154. ……………………

  155. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
FIDGC.rar (8.92 MB, 下载次数: 175)
回复

使用道具 举报

ID:400514 发表于 2018-9-21 12:00 | 显示全部楼层
最近在调试这颗IC,参考一下。。。。
回复

使用道具 举报

ID:700196 发表于 2020-3-24 09:42 | 显示全部楼层
最近在调试,参考一下。
回复

使用道具 举报

ID:385881 发表于 2020-4-3 08:56 | 显示全部楼层
参考一下。
回复

使用道具 举报

ID:229733 发表于 2020-5-12 18:05 | 显示全部楼层
厉害了我的哥,参考一下
回复

使用道具 举报

ID:434714 发表于 2020-6-5 10:53 | 显示全部楼层
赞,借鉴一下,我最近在看AD4111,楼主有推荐么
回复

使用道具 举报

ID:472280 发表于 2020-7-31 16:09 | 显示全部楼层
谢谢,参考一下
回复

使用道具 举报

ID:607298 发表于 2021-11-24 10:00 | 显示全部楼层
有人知道这边初始化时延时为什么都要几秒吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表