```#include "stm32f10x.h"
#include "usart.h"
#include "delay.h"
void My_USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStrue;
//①使能GPIOA和串口1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
//②初始化GPIOA两个引脚
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOA,&GPIO_InitStrue);
//③串口初始化
//波特率
USART_InitStrue.USART_BaudRate=115200;
//硬件控制
USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStrue.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
//奇偶验证
USART_InitStrue.USART_Parity=USART_Parity_No;
//停止位
USART_InitStrue.USART_StopBits=USART_StopBits_1;
//字长
USART_InitStrue.USART_WordLength=USART_WordLength_8b;
USART_Init(USART1,&USART_InitStrue);
//串口使能
USART_Cmd(USART1,ENABLE);//③
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//开启接受中断
USART_ITConfig(USART1,USART_IT_ORE,ENABLE);//开启溢出中断
//初始化中断
NVIC_InitStrue.NVIC_IRQChannel=USART1_IRQn;
NVIC_InitStrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStrue.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStrue.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStrue);
}
u8 usart_buf[4][USART_REC_LEN];//接收缓冲,最大USART_REC_LEN个字节
static volatile u16 count=0;//接收状态标记
static volatile int p=0;
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//开启接收中断
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除标志中断位
p = (count / USART_REC_LEN) % 4;
usart_buf[p][count % USART_REC_LEN] = USART_ReceiveData(USART1);
count++;
}
}
int main(void)
{
//要使用中断,先要中断分组,一般在主函数的开头
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_USART1_Init();
delay_init();
while(1){
if(count > 0){
if(count % 256 == 0){
for(int i = 0; i < 256; i++){
USART_SendData(USART1,usart_buf[p][i]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
}
printf("\r\n\r\n");
delay_ms(100);
}
}
};
}
|