串口
嵌入式中的串口一般指UART口,知道他就是用来通信的就OK。一般会使用三种方式进行数据的通信处理。分别是:轮询、中断、DMA。 以USART1为例,查询手册可知:USART1: 发送:PA9 接收:PA10 轮询: 使用串口助手测试,将接收到的串口数据发送给串口助手。
int main( void )
{
unsigned char test =0;
GPIO_InitTypeDef gpio_Init;
USART_InitTypeDef usart_Init;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
gpio_Init.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_Init.GPIO_Pin = GPIO_Pin_9;
gpio_Init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_Init);
gpio_Init.GPIO_Mode = GPIO_Mode_IPU;
gpio_Init.GPIO_Pin = GPIO_Pin_10;
gpio_Init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_Init);
usart_Init.USART_BaudRate = 115200;
usart_Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usart_Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usart_Init.USART_Parity = USART_Parity_No;
usart_Init.USART_StopBits = USART_StopBits_1;
usart_Init.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&usart_Init);
USART_Cmd(USART1,ENABLE);
while(1)
{
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE))
{
test = USART_ReceiveData(USART1);
USART_SendData(USART1,test);
}
}
return 0;
}
中断: 简单串口助手测试:接收到数据存在recvBuff中,并向串口助手发送数据0x20,发送完成cnt值加一。
unsigned char recvBuff[200]={0};
unsigned char sendTest =0x20;
int len =0,cnt=0;
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
recvBuff[len++] = USART_ReceiveData(USART1);
USART_SendData(USART1,sendTest);
}
if(USART_GetITStatus(USART1, USART_IT_TC) != RESET)
{
USART_ClearITPendingBit(USART1,USART_IT_TC);
cnt++;
}
}
int main( void )
{
unsigned char test =0;
GPIO_InitTypeDef gpio_Init;
USART_InitTypeDef usart_Init;
NVIC_InitTypeDef nvic_Init;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
gpio_Init.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_Init.GPIO_Pin = GPIO_Pin_9;
gpio_Init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_Init);
gpio_Init.GPIO_Mode = GPIO_Mode_IPU;
gpio_Init.GPIO_Pin = GPIO_Pin_10;
gpio_Init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_Init);
usart_Init.USART_BaudRate = 115200;
usart_Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
usart_Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usart_Init.USART_Parity = USART_Parity_No;
usart_Init.USART_StopBits = USART_StopBits_1;
usart_Init.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&usart_Init);
NVIC_PriorityGroupConfig(2);
nvic_Init.NVIC_IRQChannel =USART1_IRQn;
nvic_Init.NVIC_IRQChannelCmd =ENABLE;
nvic_Init.NVIC_IRQChannelPreemptionPriority =2;
nvic_Init.NVIC_IRQChannelSubPriority =2;
NVIC_Init(&nvic_Init);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_TC,ENABLE);
USART_Cmd(USART1,ENABLE);
while(1)
{
}
return 0;
}
DMA: 串口简单测试:一直发送dmaSend中的数据给串口助手,串口助手发出的数据,USART1接收后存在dmaRecv中,注意大小。
int main( void )
{
unsigned char test =0;
unsigned char dmaSend[10]={0x1,0x2,0x3,0x4,0x5,0x6,0x7,0x8,0x9,0xA};
unsigned char dmaRecv[100]={0};
GPIO_InitTypeDef gpio_Init;
USART_InitTypeDef usart_Init;
DMA_InitTypeDef dma_Init;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
gpio_Init.GPIO_Mode = GPIO_Mode_AF_PP;
gpio_Init.GPIO_Pin = GPIO_Pin_9;
gpio_Init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_Init);
gpio_Init.GPIO_Mode = GPIO_Mode_IPU;
gpio_Init.GPIO_Pin = GPIO_Pin_10;
gpio_Init.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&gpio_Init);
usart_Init.USART_BaudRate = 115200;
usart_Init.USART_HardwareFlowControl =USART_HardwareFlowControl_None;
usart_Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
usart_Init.USART_Parity = USART_Parity_No;
usart_Init.USART_StopBits = USART_StopBits_1;
usart_Init.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1,&usart_Init);
dma_Init.DMA_BufferSize =10;
dma_Init.DMA_DIR =DMA_DIR_PeripheralDST;
dma_Init.DMA_M2M =DMA_M2M_Disable;
dma_Init.DMA_MemoryBaseAddr =(unsigned int)dmaSend;
dma_Init.DMA_MemoryDataSize =DMA_MemoryDataSize_Byte;
dma_Init.DMA_MemoryInc =DMA_MemoryInc_Enable;
dma_Init.DMA_Mode =DMA_Mode_Circular;
dma_Init.DMA_PeripheralBaseAddr =(unsigned int)&USART1->DR;
dma_Init.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte;
dma_Init.DMA_PeripheralInc =DMA_PeripheralInc_Disable;
dma_Init.DMA_Priority =DMA_Priority_Medium;
DMA_Init(DMA1_Channel4,&dma_Init);
DMA_Cmd(DMA1_Channel4,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Tx,ENABLE);
dma_Init.DMA_BufferSize =sizeof(dmaRecv)/sizeof(dmaRecv[0]);
dma_Init.DMA_DIR =DMA_DIR_PeripheralSRC;
dma_Init.DMA_M2M =DMA_M2M_Disable;
dma_Init.DMA_MemoryBaseAddr =(unsigned int)dmaRecv;
dma_Init.DMA_MemoryDataSize =DMA_MemoryDataSize_Byte;
dma_Init.DMA_MemoryInc =DMA_MemoryInc_Enable;
dma_Init.DMA_Mode =DMA_Mode_Circular;
dma_Init.DMA_PeripheralBaseAddr =(unsigned int)&USART1->DR;
dma_Init.DMA_PeripheralDataSize =DMA_PeripheralDataSize_Byte;
dma_Init.DMA_PeripheralInc =DMA_PeripheralInc_Disable;
dma_Init.DMA_Priority =DMA_Priority_Medium;
DMA_Init(DMA1_Channel5,&dma_Init);
DMA_Cmd(DMA1_Channel5,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
USART_Cmd(USART1,ENABLE);
while(1)
{
}
return 0;
}
|