接收函数
uint8_t Reception_buff[RX_SIZE] attribute((aligned(4)));
uint8_t USART_flag=0;
void USART1_IRQHandler(void)
{
if(USART1 ->ISR & USART_FLAG_IDLE){
Reception_buff[RX_SIZE-1] = USART1->RDR & 0x1ff; //读取一次数据清除标志
USART_flag = 0xff;
DMA1_Channel3->CCR &= (uint16_t)(~DMA_CCR_EN);
DMA1_Channel3->CNDTR = RX_SIZE;
DMA1_Channel3->CCR |= DMA_CCR_EN;
}
USART1->ICR = (uint32_t)USART_FLAG_IDLE;
}
发送函数
void Scan_data(uint8_t *p,uint8_t num)
{
GPIOB->BRR |= GPIO_Pin_1;
while(num--){
while((USART1->ISR & 0x40)==0);
USART1->TDR = *p++;
}
while((USART1->ISR & 0x40)==0);
delay_ms(2);
GPIOB->BSRR |= GPIO_Pin_1;
}
串口配置
void USART1_Init(uint32_t baud)
{
char usart_isr;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStruct;
USART1_DMA_Init();
USART_DeInit(USART1);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = baud;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0x0;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
usart_isr = USART1->ISR ;
GPIOB->BSRR |= GPIO_Pin_1;
}
int fputc(int c, FILE stream)
{
/ 将Printf内容发往串口 */
GPIOB->BRR |= GPIO_Pin_1;
USART1->TDR = (uint8_t)c;
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}
delay_ms(1);
GPIOB->BSRR |= GPIO_Pin_1;
return (c);
}
extern uint8_t Reception_buff[RX_SIZE] attribute((aligned(4)));
static void USART1_DMA_Init(void)
{
NVIC_InitTypeDef NVIC_InitStruct;
DMA_InitTypeDef DMA_InitMode;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
DMA_DeInit(DMA1_Channel3);
DMA_InitMode.DMA_BufferSize = RX_SIZE;
DMA_InitMode.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitMode.DMA_M2M = DMA_M2M_Disable;
DMA_InitMode.DMA_MemoryBaseAddr = (uint32_t)Reception_buff;
DMA_InitMode.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitMode.DMA_Mode = DMA_Mode_Circular;
DMA_InitMode.DMA_PeripheralBaseAddr = (uint32_t)&USART1->RDR;
DMA_InitMode.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitMode.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitMode.DMA_Priority = DMA_Priority_High;
DMA_InitMode.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_Init(DMA1_Channel3,&DMA_InitMode);
DMA_Cmd(DMA1_Channel3,ENABLE);
}
芯片是stm32f030f4,程序运行到发送函数的GPIOB->BRR |= GPIO_Pin_1;然后会发送00。但我没给从机发数据,他自动回传的00,这是为什么。