stm32f1标准库串口1DMA收发数据代码
测试效果 bsp_usart_dma.c文件代码 #include “bsp_usart_dma.h” u8 RxBuffer[DMASIZE] = {0x00}; //串口1接收后存放到此数组 u8 TxBuffer[DMASIZE] = {0x00}; //串口1发送此数组的数据 u16 RxBufferSize = 0; void usart_dma_init(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_No; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_BaudRate = 9600; USART_Init(USART1, &USART_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //空闲中断接收 USART_ITConfig(USART1, USART_IT_TC, DISABLE); USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); //DMA的TCIF中断发送 NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(USART1_TX_CHANNEL); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (u32)TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 0; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(USART1_TX_CHANNEL, &DMA_InitStructure); DMA_ITConfig(USART1_TX_CHANNEL, DMA_IT_TC, ENABLE); //DMA_Cmd(USART1_TX_CHANNEL, ENABLE);//使能通道4,一般发送的时候再使能 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(USART1_RX_CHANNEL); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (u32)RxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = DMASIZE; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(USART1_RX_CHANNEL, &DMA_InitStructure); DMA_Cmd(USART1_RX_CHANNEL, ENABLE); USART_Cmd(USART1, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE); } void uart_dma_send_enable(uint16_t size) { USART1_TX_CHANNEL->CNDTR = size; DMA_Cmd(USART1_TX_CHANNEL, ENABLE); } void decode(void) { //解析接收到的数据,然后发送 if (0 == RxBuffer[0]) { if (0X06 == RxBuffer[1]) FLASH_W(FLASH_START_ADDR, RxBuffer[2]); //此时在中断中,不能进入main函数将从机地址写入TxBuffer[0],所以写入一次 TxBuffer[0] = FLASH_R(FLASH_START_ADDR); uart_dma_send_enable(2); } else if (TxBuffer[0] == RxBuffer[0]) { if (0X03 == RxBuffer[1]) uart_dma_send_enable(10); } } void USART1_IRQHandler(void) { u32 t, temp; if (USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) { t = USART1->SR; t = USART1->DR; //清USART_IT_IDLE标志 DMA_Cmd(USART1_RX_CHANNEL, DISABLE); RxBufferSize = DMASIZE – DMA_GetCurrDataCounter(USART1_RX_CHANNEL); decode(); // uart_dma_send_enable(2); 用于测试验证 DMA_SetCurrDataCounter(USART1_RX_CHANNEL, DMASIZE); DMA_Cmd(USART1_RX_CHANNEL, ENABLE); } } void DMA1_Channel4_IRQHandler(void) { if (DMA_GetITStatus(DMA1_FLAG_TC4) == !RESET) { DMA_ClearFlag(DMA1_FLAG_GL4); DMA_Cmd(USART1_TX_CHANNEL, DISABLE); } } /////重定向c库函数printf到串口,重定向后可使用printf函数 //int fputc(int ch, FILE *f) //{ // /* 发送一个字节数据到串口 */ // USART_SendData(USART1, (uint8_t)ch); // /* 等待发送完毕 */ // while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET) // ; // return (ch); //} // ///重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数 // int fgetc(FILE *f) // { // /* 等待串口输入数据 */ // while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET) // ; // return (int)USART_ReceiveData(USART1); // } bsp_usart_dma.h文件代码 #ifndef __USARTDMA_H #define __USARTDMA_H #include “stm32f10x.h” #include “stm32f10x_conf.h” #include <stdio.h> #define DMASIZE 250 #define USART1_RX_CHANNEL DMA1_Channel5 #define USART1_TX_CHANNEL DMA1_Channel4 void usart_dma_init(void); u16 Uart_Send_Data(void *buffer, u16 size); #endif
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/27125.html