这个usart2的tx引脚模式默认只有pp一种,要改成OD模式,可以直接在源程序里面改,注意再次生成后,需要再次修改
要把配置生成适应另一种芯片的程序,要重新新建程序
注意DMA的循环模式和正常模式区分
定时器要和时钟一致
其他系列教程参考http://www.waveshare.net/study/article-629-1.html
要下载cube中文使用手册和HAL库参考https://download.csdn.net/download/fei_yang_yf/10814557
多路DMA_AD采集+定时器+串口发送+printf部分源程序:
printf:(在uart.c里面添加下列程序)
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return ch;
}
串口:(注意初始化时要开启串口字节中断)
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(&huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
uint8_t clear =clear;
if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_RXNE)!=RESET))
{
Rx_tempBuffer[rx_len++] = (uint8_t)(huart1.Instance->DR&(uint8_t)0x00FF);
__HAL_UART_ENABLE_IT(&huart1,UART_IT_IDLE); //打开帧中短
}
else if((__HAL_UART_GET_FLAG(&huart1,UART_FLAG_IDLE)!=RESET))
{
__HAL_UART_DISABLE_IT(&huart1,UART_IT_IDLE); //关了空闲中断
if(rx_len == 8)
recv_end_flag = 1;//接收帧
else
rx_len =0;
}
/* USER CODE END USART1_IRQn 1 */
}
主程序部分(喂狗+DMA):
int main(void)
{
/* USER CODE BEGIN 1 */
// uint8_t temp_buff[12] = {0,1,2,3,4,5,6,7,8,9,10,11};
/* 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_ADC1_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
MX_IWDG_Init();
/* USER CODE BEGIN 2 */
__HAL_UART_ENABLE_IT(&huart1,UART_IT_RXNE);
HAL_IWDG_Init(&hiwdg);//cube先设置好
// printf("\n\r***** start main before while *****\n\r");
HAL_ADC_Start_DMA(&hadc1,(uint32_t*)&ADC_Value,4);//DMA循环模式只需调用1次,正常模式调用1次采1次
HAL_TIM_Base_Start_IT(&htim1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
HAL_IWDG_Refresh(&hiwdg);//喂狗
//其它
}
}
定时中断:(主要在cube设置,可参考上文教程链接)
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)//0.2秒中断
{
if (htim->Instance == htim1.Instance)
{
}
}
不定长接收参考