分别生成一个stm32f407zgt6与openmv串口通信的代码
时间: 2023-11-27 09:05:43 浏览: 91
stm32与openmv串口通信.rar
5星 · 资源好评率100%
STM32F407ZGT6与OpenMV串口通信的代码如下:
STM32F407ZGT6串口通信代码:
```c
#include "stm32f4xx.h"
#include <stdio.h>
#include <string.h>
#define UART_TX GPIO_Pin_9
#define UART_RX GPIO_Pin_10
void USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = UART_TX | UART_RX;
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_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
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(USART2, &USART_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void USART2_SendChar(uint8_t ch)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
;
USART_SendData(USART2, ch);
}
void USART2_SendString(char *str)
{
while (*str != '\0')
{
USART2_SendChar(*str);
str++;
}
}
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART2);
USART2_SendChar(data);
}
}
int main(void)
{
USART2_Init();
while (1)
{
// 代码逻辑
}
}
```
OpenMV串口通信代码:
```python
import sensor, image, time, pyb
uart = pyb.UART(3, 115200, timeout_char=1000)
while(True):
# 代码逻辑
uart.write("Hello World!\n")
pyb.delay(1000)
```
其中,STM32F407ZGT6串口通信代码使用USART2进行通信,波特率为115200。OpenMV串口通信代码使用UART3进行通信,波特率也为115200。每次循环都会向对方发送一条字符串,并延时1秒。
阅读全文