完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
直接上代码
#define SENDBUFF_SIZE 128 #define RECEBUFF_SIZE 128 #define USART1_DR_Base 0x40013804 uint8_t SendBuff[SENDBUFF_SIZE]; uint8_t RecvBuff[SENDBUFF_SIZE]; void DMAUart1Tx_Init() { DMA_InitTypeDef DMA_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = USART1_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)SendBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = SENDBUFF_SIZE; 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_Medium; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel4, &DMA_InitStructure); DMA_Cmd (DMA1_Channel4,ENABLE); // DMA_ITConfig(DMA1_Channel4,DMA_IT_TC,ENABLE); } void DMAUsar1Rx_Init() { DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RecvBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = RECEBUFF_SIZE; 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(DMA1_Channel5,&DMA_InitStructure); DMA_Cmd(DMA1_Channel5,ENABLE); USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); } void USART1_Init(u32 My_BaudRate) { GPIO_InitTypeDef GPIO_InitStructure ; USART_InitTypeDef USART_InitStructure ; NVIC_InitTypeDef NVIC_InitStructure ; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO , ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 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); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE ; NVIC_Init(&NVIC_InitStructure); USART_DeInit(USART1); USART_InitStructure.USART_BaudRate = My_BaudRate; 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_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx ; USART_Init(USART1, &USART_InitStructure); USART_ITConfig(USART1, USART_IT_IDLE, ENABLE); DMAUart1Tx_Init(); DMAUsar1Rx_Init(); USART_Cmd(USART1 , ENABLE); } void USART_SendData(USART_TypeDef* USARTx, uint16_t Data) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data)); /* Transmit Data */ USARTx->DR = (Data & (uint16_t)0x01FF); } void Usart_SendString(USART_TypeDef *USARTx, uint8_t *str, uint16_t len) { unsigned short count = 0; for(; count < len; count++) { USART_SendData(USARTx, *str++); while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET); } } void MYDMA_Enable(DMA_Channel_TypeDef*DMA_CHx, uint16_t len) { DMA_Cmd(DMA_CHx, DISABLE ); DMA_SetCurrDataCounter(DMA_CHx, len); DMA_Cmd(DMA_CHx, ENABLE); } uint16_t uart_read( uint8_t* buf, uint16_t read_len) { uint16_t Usart1_Rec_Cnt = 0; if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET) { USART_ReceiveData(USART1); Usart1_Rec_Cnt = RECEBUFF_SIZE - DMA_GetCurrDataCounter(DMA1_Channel5); Usart_SendString(USART1, RecvBuff, Usart1_Rec_Cnt); memcpy(buf, RecvBuff, Usart1_Rec_Cnt); USART_ClearITPendingBit(USART1,USART_IT_IDLE); MYDMA_Enable(DMA1_Channel5, RECEBUFF_SIZE); } return Usart1_Rec_Cnt; } void USART1_IRQHandler(void) { uart_read(RX1_Receive, RX1_Receive_LEN); } uint16_t uart_puts(const uint8_t *src, uint16_t len) { const uint8_t *pstr_tmp = src; uint16_t len_temp = len; MYDMA_Enable(DMA1_Channel4, len_temp); for(int i = 0;i < len_temp; i++) { SendBuff = *pstr_tmp++; } USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); } int main(void) { SystemInit(); USART1_Init(9600); for(;;) { } } |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1685 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1588 浏览 1 评论
1013 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
706 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1631 浏览 2 评论
1893浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
679浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
538浏览 3评论
558浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
526浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-5 00:12 , Processed in 0.726789 second(s), Total 75, Slave 58 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号