最后一波了,这次准备做USART+DMA数据传输的实验。
因为USART,也就是串口是传输常用的协议,后续准备做串口屏幕的话,这个一定要用到,所以需要完全掌握。但是串口传输一般一次只能一帧,后续传输就会浪费CPU资源,所以DMA方式是最好的方式。这就是我自己的理解,代码如下:
USART初始化代码
void USART_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; USART_GPIO_CLK_ENA(USART_GPIO_CLK,ENABLE); USART_CLK_ENA(USART_CLK,ENABLE); GPIO_InitStructure.GPIO_Pin = USART_TX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(USART_TX_Port,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = USART_RX; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_Init(USART_RX_Port,&GPIO_InitStructure); GPIO_PinAFConfig(USART_TX_Port , GPIO_PinSource2 , GPIO_AF_USART2); GPIO_PinAFConfig(USART_RX_Port , GPIO_PinSource3 , GPIO_AF_USART2); USART_InitStructure.USART_BaudRate = USART_Baudrate; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(Use_USART,&USART_InitStructure); NVIC_Configration(); USART_ITConfig(Use_USART,USART_IT_RXNE,ENABLE); USART_Cmd(Use_USART,ENABLE); }
其中的NVIC配置函数:
static void NVIC_Configration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitStructure.NVIC_IRQChannel = USART_IRQ; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_Init(&NVIC_InitStructure); }
发送代码:
void USART_SendByte(USART_TypeDef * pUSARTx , uint8_t ch) { USART_SendData(pUSARTx , ch); while(USART_GetFlagStatus(pUSARTx , USART_FLAG_TXE) == RESET); } void USART_SenString(USART_TypeDef * pUSARTx , char * str) { unsigned int k = 0; for(k = 0 ; *(str+k)!='\0' ; k++) { USART_SendByte(pUSARTx , *(str+k)); } while(USART_GetFlagStatus(pUSARTx , USART_FLAG_TC) == RESET); }
配置完USART之后就是配置DMA。
DMA初始化代码:
void USART_DMA_Config(void) { DMA_InitTypeDef DMA_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1,ENABLE); DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_PeripheralBaseAddr = USART2_BASE + 0x04; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)SendBuff; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; 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_FIFOMode = DMA_FIFOMode_Disable; /* FIFOģʽ/Ö±½Óģʽ */ DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; /* FIFO´óС */ DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; /* µ¥´Î´«Êä */ DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream6 , &DMA_InitStructure); DMA_Cmd(DMA1_Stream6 , ENABLE); }
在main函数中启动DMA:
int main() { LED_GPIO_Config(); LED_OFF; USART_Config(); USART_DMA_Config(); //USART_SenString(Use_USART , "helloworld\n"); USART_DMACmd(Use_USART , USART_DMAReq_Tx , ENABLE); while(1); }