#include "usart.h" #include <stdio.h> #include <stdlib.h> #include <string.h> int fputc(int c, FILE *f) { ?????? /* Place your implementation of fputc here */ ?????? /* e.g. write a character to the USART */ ?????? usart1_1_byte(c); ?????? return c; } void uart1_init(uint32_t bound) { ?????? GPIO_InitTypeDef GPIO_InitStructure; ?????? USART_InitTypeDef USART_InitStructure; ?????? NVIC_InitTypeDef NVIC_InitStructe; ?????? ?????? RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); ?????? RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); ?????? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; ?????? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; ?????? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; ?????? GPIO_Init(GPIOA, &GPIO_InitStructure); ?????? GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; ?????? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; ?????? GPIO_Init(GPIOA, &GPIO_InitStructure); ?????? USART_InitStructure.USART_BaudRate = bound; ?????? 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; ?????? NVIC_InitStructe.NVIC_IRQChannel = USART1_IRQn; ?????? NVIC_InitStructe.NVIC_IRQChannelCmd = ENABLE; ?????? NVIC_InitStructe.NVIC_IRQChannelPreemptionPriority = 1; ?????? NVIC_InitStructe.NVIC_IRQChannelSubPriority = 0; ?????? NVIC_Init(&NVIC_InitStructe); ?????? ?????? USART_Init(USART1, &USART_InitStructure); ?????? USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接受中断 ?????? USART_Cmd(USART1, ENABLE); } void usart1_1_byte(int ch) { ?????? while (!USART_GetFlagStatus(USART1, USART_FLAG_TC)); ?????? USART_SendData(USART1, (uint8_t)ch); ?????? while (!USART_GetFlagStatus(USART1, USART_FLAG_TC)); } void USART1_IRQHandler(void)??????????????? ???? //串口1中断服务程序 { ?????? u8 st,sbuf; ?????? st=USART_GetITStatus(USART1, USART_IT_RXNE); ?????? if(st==SET)? // ?????? { ????????????? sbuf=USART1->DR; ????????????? usart1_1_byte(sbuf); ?????? } } |