次代码为stm32f系列单片机采集ads1255这款24位ad的源代码,通讯采用spi方式,24位ad的精度是可20ms采集一次,效率很高
单片机源程序:
- #include "main.h"
- #include "stm32f1xx_hal.h"
- #include "dma.h"
- #include "iwdg.h"
- #include "spi.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* USER CODE BEGIN Includes */
- #include "common.h"
- #include "ads1255.h"
- #include "Protocol.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* Buffer used for reception */
- #define BUFFER_SIZE 64 //宏定义缓冲区大小
- /* Private variables ---------------------------------------------------------*/
- uint16_t rx_len1 = 0;//定义一个接收长度变量
- uint8_t recv_end_flag1 = 0;//定义一个接收结束标变量
- uint8_t rx_buffer1[64];//定义一个接收缓冲数组,128个元素
- uint8_t sendcommand_flag=0x03;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* Private function prototypes -----------------------------------------------*/
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* 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_IWDG_Init();
- MX_SPI2_Init();
- MX_TIM2_Init();
- MX_USART1_UART_Init();
- // MX_TIM3_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim2);
- ads1255_init();
-
- Uart_Event_Init(); //初始化串口事件
-
-
- __HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE); //使能串口1空闲中断
- UartEventType DataReceive;
- // HAL_TIM_Base_Start_IT(&htim3);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- HAL_IWDG_Refresh(&hiwdg);
- if(Uart_Event_Get(&DataReceive)==true)
- {
- sendcommand_flag=rx_buffer1[4];
- }
-
- if(sendcommand_flag==0x01)
- {
- HAL_UART_Transmit_DMA(&huart1,FidDataFunction(),12);
- }
- else
- {
-
- }
- HAL_UART_Receive_DMA(&huart1,rx_buffer1,BUFFER_SIZE);
- }
-
- // printf("\r\n****** FID Controller System ******\r\n ");
- // printf("Set SYSCLK to %dHz\r\n", HAL_RCC_GetSysClockFreq());
- // printf("Set HCLK to %dHz\r\n", HAL_RCC_GetHCLKFreq());
- // printf("Set PCLK1 to %dHz\r\n", HAL_RCC_GetPCLK1Freq());
- // printf("Set PCLK2 to %dHz\r\n", HAL_RCC_GetPCLK2Freq());
- // printf("signal value:%0.4lf\r\n",sign);
- }
- /* USER CODE END 3 */
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.LSIState = RCC_LSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**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_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- _Error_Handler(__FILE__, __LINE__);
- }
- /**Configure the Systick interrupt time
- */
- HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
- /**Configure the Systick
- */
- HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
- /* SysTick_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @param None
- * @retval None
- */
- void _Error_Handler(char * file, int line)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- while(1)
- {
- }
- /* 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.
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
FIDGC.rar
(8.92 MB, 下载次数: 178)
|