这个文章仅做思路查看
具体解决看这里
- 使用CUBE+KEIL编程
查看文章如下
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2021 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 "adc.h"
#include "dma.h"
#include "iwdg.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/*
说明:
cube生成debug模式不可以使用JINK(5PIN),会导致FREEMODBUS读写异常。
独立看门狗 4s一次。超过4s没更新 即复位
nvic优先级设置
中断 抢占优先级 子优先级
系统时钟 0 0
TIM4(FREEMODBUS时钟)0 1[不可改变,已测试]
串口1 0 2[不可改变,已测试]
TIM1捕获(PWM) 2 2
TIM3(编码器) 3 1
外部中断1和2 3 2
TIM1更新中断(没用) 3 3
GPIO
TIM2_CH2 PA1
MOTOR_TURN PA2
ADC1_IN5 PA5
TIM3_CH1 PA6
TIM3_CH2 PA7
USART1_TX PA9
USART1_RX PA10
Photoelectric_Sensor1 PB0
LED PB1
Photoelectric_Sensor2 PC4
RCC_OSC_IN PH0
RCC_OSC_OUT PH1
USART1:用于MODBUS传输
TIM1:用于计数脉冲个数。 https://www.jianshu.com/p/eae5386aa8ee
TIM2:输出PWM https://www.jianshu.com/p/eae5386aa8ee
TIM3:正交解码模式。
tim4:用于连接porttimer.c文件的函数,启用FREEMODBUS
*/
/* FREEMODBUS Includes */
#include "mb.h"
#include "port.h"
#include "demo.h"
/* other Includes */
#include "stdio.h"
#include "delay.h"
#include "motor.h"
#include "encoder.h"
#include "led.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define ADC1_BUFFER_SIZE 32*4 //ADC1用了4通道, 存32组, 方便做平均
#define ADC3_BUFFER_SIZE 32*8 //ADC3用了8通道, 存32组, 方便做平均
//32字节对齐(地址+大小)
//adc1_data指定到 AXI SRAM 的0x24000000
//adc3_data指定到 SRAM4 的0x38000000
//ALIGN_32BYTES (uint16_t adc1_data[ADC1_BUFFER_SIZE]) __attribute__((section(".ARM.__at_0x24000000")));
//ALIGN_32BYTES (uint16_t adc3_data[ADC3_BUFFER_SIZE]) __attribute__((section(".ARM.__at_0x38000000")));
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
u8 once_flag = 1;//仅运行一次标志位。只有当其为0,才运行其他代码
uint8_t zero_error = HAL_BUSY;//零位传感器错误码
float ane = -90;
__IO uint16_t ADC_ConvertedValue[20];
/* 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 */
unsigned int i = 0, ADC_Value = 0;
/* USER CODE END 1 */
/* Enable I-Cache---------------------------------------------------------*/
SCB_EnableICache();
/* Enable D-Cache---------------------------------------------------------*/
SCB_EnableDCache();
/* 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 */
//delay_init(216); //延时初始化
//TIM1必需在TIM2前面初始化!!!
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
MX_TIM2_Init();
MX_TIM4_Init();
MX_TIM3_Init();
MX_IWDG_Init();
/* USER CODE BEGIN 2 */
eMBInit( MB_RTU, 0x01, 1, 9600, MB_PAR_NONE);//初始化modbus,走modbusRTU,从站地址为0x01,端口为1。
eMBEnable( );//使能modbus
use_led_init();
HAL_ADC_Start_DMA(&hadc1, (uint32_t*)&ADC_ConvertedValue[0], 20);
// {
// Error_Handler();
// }
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_IWDG_Refresh(&hiwdg);//看门狗喂狗[debug设置断点时请注释看门狗]
modbus_handler();
(void)eMBPoll();//启动modbus
for(i = 0; i < 20; ++i)
{
ADC_Value += ADC_ConvertedValue[i];
}
ADC_Value = ADC_Value / 20;
if(!once_flag)
{
motor_Absolute_angle_out(ane);
}
else if(once_flag)
{
if(Zero_Detection(0X2FFFFF) == HAL_OK)
{
HAL_Delay(500);
once_flag = 0;
pusle_it = 0;//初始化电机脉冲计数值
motor_out_ok = HAL_OK;
}
else if(zero_error == HAL_TIMEOUT)
{
use_led_setmode();//如果归零传感器异常超时,led灯两闪两灭
while(1);//错误
}
}
}
/* 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 LSE Drive Capability
*/
HAL_PWR_EnableBkUpAccess();
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSIState = RCC_LSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 432;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Activate the Over-Drive mode
*/
if (HAL_PWREx_EnableOverDrive() != 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_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_7) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
//void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc)
//{
// /* Invalidate Data Cache to get the updated content of the SRAM on the first half of the ADC converted data buffer: 32 bytes */
// SCB_InvalidateDCache_by_Addr((uint32_t *) &aADCxConvertedData[0], ADC_CONVERTED_DATA_BUFFER_SIZE);
//}
//
///**
// * @brief Conversion DMA half-transfer callback in non-blocking mode
// * @param hadc: ADC handle
// * @retval None
// */
//void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
//{
// /* Invalidate Data Cache to get the updated content of the SRAM on the second half of the ADC converted data buffer: 32 bytes */
// SCB_InvalidateDCache_by_Addr((uint32_t *) &aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE/2], ADC_CONVERTED_DATA_BUFFER_SIZE);
//}
/* 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 */
- 配置之后debug下数组没有更新。都是0
3.注释读取数组代码后debug有数据
for(i = 0; i < 20; ++i)
{
ADC_Value += ADC_ConvertedValue[i];
}
ADC_Value = ADC_Value / 20;
- 怀疑是编译器优化问题。调整之后无效
- __IO uint16_t ADC_ConvertedValue[20];
加上__IO,防止编译器优化无效。
#define __IO volatile /*!< Defines ‘read / write’ permissions */ - 百度过其他adc+dma设置方法,均无效果。
- 机缘巧合之下搜索RTC的时候看到这篇文章
- 按照办法进行编程。无效,进不去debug。
查看代码
ALIGN_32BYTES (static uint16_t aADCxConvertedData[ADC_CONVERTED_DATA_BUFFER_SIZE]) __attribute__((section(".ARM.__at_0x24000000")));
查看这篇文章
得知F7的地址不是这个
- 又无了。。。。知道看到这篇帖子
https://bbs.21ic.com/icview-2380632-1-1.html
- 有思路了,使用懒办法
改工程程序开始地址从flash开始