我的具体设置如下:
//激活外围设备时钟
RCC->AHBENR=RCC_AHBENR_GPIOCEN|RCC_AHBENR_GPIOBEN|RCC_AHBENR_GPIOAEN;
RCC->APB2ENR|=RCC_APB2ENR_USART1EN;
RCC->APB1ENR|=RCC_APB1ENR_USART2EN;
//IO口模式设置
GPIOA->MODER=0x691A55A5; //A14、A13、A9、A8、A3、A2为AF,A11为输入,其余输出
GPIOB->MODER=0x55555A95; //B5、B4、B3为AF,其余为输出
GPIOC->MODER=0x51555555; //C13为输入,其余为输出
//IO口输出类型
GPIOA->OTYPER=GPIO_OTYPER_OT_9; //TX1(A9)为开漏
//IO输入模式设置
GPIOA->PUPDR=GPIO_PUPDR_PUPDR11_0|GPIO_PUPDR_PUPDR9_0|GPIO_PUPDR_PUPDR3_0
|GPIO_PUPDR_PUPDR2_0; //A11、A9、A3、A2上拉
GPIOC->PUPDR|=GPIO_PUPDR_PUPDR13_0; //C13上拉
//设置口线AF功能
GPIOA->AFR[0]=0x00001100; //A2-TX2,A3-RX2
GPIOA->AFR[1]=0x00000011; //A8-SCK1,A9-TX1
//设置口线速度
GPIOA->OSPEEDR|=GPIO_OSPEEDR_OSPEEDR8;
GPIOC->OSPEEDR=0xFFFFFFFF;
//USART1设置
USART1->CR1&=(uint32_t)~(1<<0); //禁能USART1
USART1->BRR=14318180/9600; //9600,14.31818M
USART1->GTPR=(uint32_t)((1<<11)|(1<<10)|(1<<1)); //保护时间12BIT,4分频
USART1->CR3=USART_CR3_SCEN; //设置SmartCard模式
USART1->CR2=USART_CR2_STOP_1|USART_CR2_STOP_0|USART_CR2_CLKEN;
//1.5位停止位,使能CLK时钟
USART1->CR1=USART_CR1_M|USART_CR1_PCE|USART_CR1_TE|USART_CR1_RXNEIE; //9位数据字长,使能校验
USART1->CR1|=USART_CR1_PEIE;
USART1->CR1|=USART_CR1_UE; //使能USART1
NVIC_EnableIRQ(USART1_IRQn); //27
while((USART1->ISR&USART_ISR_TC)!=USART_ISR_TC);
我的具体设置如下:
//激活外围设备时钟
RCC->AHBENR=RCC_AHBENR_GPIOCEN|RCC_AHBENR_GPIOBEN|RCC_AHBENR_GPIOAEN;
RCC->APB2ENR|=RCC_APB2ENR_USART1EN;
RCC->APB1ENR|=RCC_APB1ENR_USART2EN;
//IO口模式设置
GPIOA->MODER=0x691A55A5; //A14、A13、A9、A8、A3、A2为AF,A11为输入,其余输出
GPIOB->MODER=0x55555A95; //B5、B4、B3为AF,其余为输出
GPIOC->MODER=0x51555555; //C13为输入,其余为输出
//IO口输出类型
GPIOA->OTYPER=GPIO_OTYPER_OT_9; //TX1(A9)为开漏
//IO输入模式设置
GPIOA->PUPDR=GPIO_PUPDR_PUPDR11_0|GPIO_PUPDR_PUPDR9_0|GPIO_PUPDR_PUPDR3_0
|GPIO_PUPDR_PUPDR2_0; //A11、A9、A3、A2上拉
GPIOC->PUPDR|=GPIO_PUPDR_PUPDR13_0; //C13上拉
//设置口线AF功能
GPIOA->AFR[0]=0x00001100; //A2-TX2,A3-RX2
GPIOA->AFR[1]=0x00000011; //A8-SCK1,A9-TX1
//设置口线速度
GPIOA->OSPEEDR|=GPIO_OSPEEDR_OSPEEDR8;
GPIOC->OSPEEDR=0xFFFFFFFF;
//USART1设置
USART1->CR1&=(uint32_t)~(1<<0); //禁能USART1
USART1->BRR=14318180/9600; //9600,14.31818M
USART1->GTPR=(uint32_t)((1<<11)|(1<<10)|(1<<1)); //保护时间12BIT,4分频
USART1->CR3=USART_CR3_SCEN; //设置SmartCard模式
USART1->CR2=USART_CR2_STOP_1|USART_CR2_STOP_0|USART_CR2_CLKEN;
//1.5位停止位,使能CLK时钟
USART1->CR1=USART_CR1_M|USART_CR1_PCE|USART_CR1_TE|USART_CR1_RXNEIE; //9位数据字长,使能校验
USART1->CR1|=USART_CR1_PEIE;
USART1->CR1|=USART_CR1_UE; //使能USART1
NVIC_EnableIRQ(USART1_IRQn); //27
while((USART1->ISR&USART_ISR_TC)!=USART_ISR_TC);
举报