stm32f407与openmv串口通信代码标准库函数版本
时间: 2023-10-09 10:05:34 浏览: 77
以下是使用标准库函数的stm32f407与openmv串口通信代码示例:
```c
#include "stm32f4xx.h"
#include <stdio.h>
#define USARTx USART2
#define USARTx_CLK RCC_APB1Periph_USART2
#define USARTx_TX_PIN GPIO_Pin_2
#define USARTx_TX_GPIO_PORT GPIOA
#define USARTx_TX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define USARTx_TX_SOURCE GPIO_PinSource2
#define USARTx_TX_AF GPIO_AF_USART2
#define USARTx_RX_PIN GPIO_Pin_3
#define USARTx_RX_GPIO_PORT GPIOA
#define USARTx_RX_GPIO_CLK RCC_AHB1Periph_GPIOA
#define USARTx_RX_SOURCE GPIO_PinSource3
#define USARTx_RX_AF GPIO_AF_USART2
#define USARTx_IRQn USART2_IRQn
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIO clock */
RCC_AHB1PeriphClockCmd(USARTx_TX_GPIO_CLK | USARTx_RX_GPIO_CLK, ENABLE);
/* Enable USART clock */
RCC_APB1PeriphClockCmd(USARTx_CLK, ENABLE);
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(USARTx_TX_GPIO_PORT, USARTx_TX_SOURCE, USARTx_TX_AF);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(USARTx_RX_GPIO_PORT, USARTx_RX_SOURCE, USARTx_RX_AF);
/* Configure USART Tx and Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = USARTx_TX_PIN;
GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USARTx_RX_PIN;
GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStructure);
/* USART configuration */
USART_InitStructure.USART_BaudRate = 115200;
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(USARTx, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USARTx, ENABLE);
/* Configure USART interrupt */
USART_ITConfig(USARTx, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USARTx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
int fputc(int ch, FILE *f)
{
/* Send a character to the USARTx */
USART_SendData(USARTx, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);
return ch;
}
void USARTx_IRQHandler(void)
{
if (USART_GetITStatus(USARTx, USART_IT_RXNE) != RESET)
{
char rx_data = USART_ReceiveData(USARTx);
printf("Received: %c\n", rx_data);
USART_ClearITPendingBit(USARTx, USART_IT_RXNE);
}
}
int main(void)
{
USART_Config();
while (1)
{
/* Send data to OpenMV */
printf("Hello OpenMV!\n");
delay_ms(1000);
}
}
```
在此示例中,我们使用USART2与OpenMV进行通信。我们使用标准库函数进行USART2的初始化,并在中断处理程序中接收OpenMV发送的数据并打印到终端。在主函数中,我们使用printf函数向OpenMV发送数据。
阅读全文