12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- #include "usart.h"
- //重定向c库函数printf到串口,重定向后可使用printf函数
- int fputc(int ch, FILE *f)
- {
- /* 发送一个字节数据到串口 */
- USART_SendData(USART_232, (uint8_t) ch);
-
- /* 等待发送完毕 */
- while (USART_GetFlagStatus(USART_232, USART_FLAG_TXE) == RESET);
- return (ch);
- }
- //重定向c库函数scanf到串口,重写向后可使用scanf、getchar等函数
- int fgetc(FILE *f)
- {
- /* 等待串口输入数据 */
- while (USART_GetFlagStatus(USART_232, USART_FLAG_RXNE) == RESET);
- return (int)USART_ReceiveData(USART_232);
- }
- //USART3的初始化
- void USART3_config()
- {
- USART_InitTypeDef USART_InitStruct;
- GPIO_InitTypeDef GPIO_InitStruct;
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
-
- GPIO_PinAFConfig(USART3_RX_GPIO_PORT, USART3_RX_SOURCE, USART3_RX_AF);
- GPIO_PinAFConfig(USART3_TX_GPIO_PORT, USART3_TX_SOURCE, USART3_TX_AF);
-
- GPIO_InitStruct.GPIO_Pin = USART3_TX_PIN ;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStruct);
-
- GPIO_InitStruct.GPIO_Pin = USART3_RX_PIN;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_Init(GPIOD, &GPIO_InitStruct);
- USART_InitStruct.USART_BaudRate = USART_BAUDRATE;
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;
- USART_InitStruct.USART_StopBits = USART_StopBits_1;
- USART_InitStruct.USART_Parity = USART_Parity_No;
- USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_Init(USART3, &USART_InitStruct);
- USART_Cmd(USART3, ENABLE);
-
- USART_ClearFlag(USART3, USART_FLAG_TC);
- USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
- USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);
-
- NVIC_InitTypeDef NVIC_InitStruct;
- NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStruct);
- }
- // USART3的中断函数
- void USART3_IRQHandler(){
- call_back();
- }
|