AD7606
ad7606.c
#include "gpio.h"
#include "spi.h"
#include "tim.h"
#include "ad7606.h"
#include "sys_zdyz.h"
#include "usart.h"
uint16_t ad7606Buff[SAMPLING_POINTS][SAMPLING_CHANNEL] = {0};
uint8_t ad7606SamplingDoneFlag = 0;
int nums=0;
float sam_i[SAMPLING_POINTS],sam_v[SAMPLING_POINTS];
static void AD7606Reset(void)
{
/*! ___|-----|________ >= 50ns */
AD7606Rst_Low();
AD7606Rst_High();
for(int i = 20; i > 0; i--){
__NOP();//1000/168 ns = 5.85ns
}
AD7606Rst_Low();
}
void AD7606Init(void)
{
AD7606Cs_High();
AD7606Reset();
}
void AD7606Start(void)
{
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
}
void AD7606Stop(void)
{
HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
AD7606Cs_High();
}
void AD7606BusyIrqCallback(uint16_t *ad7606Val,uint8_t ad7606Chl)
{
AD7606Cs_Low();
HAL_SPI_Receive(&hspi1,(uint8_t *)ad7606Val,ad7606Chl,10);
AD7606Cs_High();
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == AD7606_BUSY_Pin){
//read AD7606
if(nums < SAMPLING_POINTS)
{
AD7606BusyIrqCallback(ad7606Buff[nums],SAMPLING_CHANNEL);
nums++;
ad7606SamplingDoneFlag = 0;
}
else{
ad7606SamplingDoneFlag = 1;
}
}
}
/
float AD7606ConvValue(uint16_t bin)
{
int _val;
float adcValue;
_val = bin&0x8000 ? (-((~bin+1)&0x7fff)) : bin;
adcValue = 10.0*_val/32768.0;
return adcValue;
}
//GPIO
ad7606.h
#ifndef __AD_7606_H_
#define __AD_7606_H_
#ifdef __cplusplus
extern "C" {
#endif
/*! -------------------------------------------------------------------------- */
/*! Include headers */
#include <stdint.h>
/*! -------------------------------------------------------------------------- */
/*! Public function declarations */
#define AD7606Cs_High() HAL_GPIO_WritePin(AD7606_CS_GPIO_Port, AD7606_CS_Pin, GPIO_PIN_SET)
#define AD7606Cs_Low() HAL_GPIO_WritePin(AD7606_CS_GPIO_Port, AD7606_CS_Pin, GPIO_PIN_RESET)
#define AD7606Rst_High() HAL_GPIO_WritePin(AD7606_RST_GPIO_Port, AD7606_RST_Pin, GPIO_PIN_SET)
#define AD7606Rst_Low() HAL_GPIO_WritePin(AD7606_RST_GPIO_Port, AD7606_RST_Pin, GPIO_PIN_RESET)
#define SAMPLING_POINTS 10
#define SAMPLING_CHANNEL 8
void AD7606Init(void);
void AD7606Start(void);
void AD7606Stop(void);
void AD7606BusyIrqCallback(uint16_t *da7606Val,uint8_t ad7606Chl);
float AD7606ConvValue(uint16_t bin);
#ifdef __cplusplus
}
#endif
#endif
/*! end of the file */
main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "sys_zdyz.h"
#include "usart_zdyz.h"
#include "delay_zdyz.h"
#include "ad7606.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 */
extern uint16_t ad7606Buff[SAMPLING_POINTS][SAMPLING_CHANNEL] ;
extern uint8_t ad7606SamplingDoneFlag ;
extern int nums;
/* 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 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
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_USART1_UART_Init();
MX_USART3_UART_Init();
MX_SPI1_Init();
MX_TIM3_Init();
/* USER CODE BEGIN 2 */
Uart_printf(&huart1,"BEGIN!\r\n");//串口屏实时显�??????
AD7606Init();
AD7606Start();
// Uart_printf(&huart1,"t0.txt=\"-\"\xff\xff\xff");//串口屏实时显
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
if(ad7606SamplingDoneFlag)
{
AD7606Stop();
ad7606SamplingDoneFlag = 0;
//输出每个采样点的数据
for(uint32_t idexPoints = 0; idexPoints < SAMPLING_POINTS; idexPoints++)
{
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][0]));//串口屏实时显�?????????
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][1]));//串口屏实时显�?????????
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][2]));//串口屏实时显�?????????
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][3]));//串口屏实时显�?????????
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][4]));//串口屏实时显�?????????
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][5]));//串口屏实时显�?????????
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][6]));//串口屏实时显�?????????
Uart_printf(&huart1,"%f\r\n",AD7606ConvValue(ad7606Buff[idexPoints][7]));//串口屏实时显�?????????
Uart_printf(&huart1,"\r\n");
}
HAL_Delay(1000);
AD7606Start();
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Supply configuration update enable
*/
HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLM = 4;
RCC_OscInitStruct.PLL.PLLN = 9;
RCC_OscInitStruct.PLL.PLLP = 2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOMEDIUM;
RCC_OscInitStruct.PLL.PLLFRACN = 3072;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != 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 */
__disable_irq();
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.
* @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,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
cudeide