STM32F407 openmv巡线传值 进行巡线代码
时间: 2023-07-09 15:06:43 浏览: 197
OPENMV巡线代码
5星 · 资源好评率100%
以下是一个简单的STM32F407与OpenMV进行巡线传值的示例代码:
```c
#include "stm32f4xx.h"
#include "usart.h"
#define UART_BUFFER_SIZE 256
uint8_t uart_buffer[UART_BUFFER_SIZE];
volatile uint16_t uart_head = 0;
volatile uint16_t uart_tail = 0;
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)
{
uart_buffer[uart_head] = USART_ReceiveData(USART3);
uart_head = (uart_head + 1) % UART_BUFFER_SIZE;
}
}
void uart_send_byte(uint8_t byte)
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, byte);
}
void uart_send_string(const char* str)
{
while(*str)
{
uart_send_byte(*str++);
}
}
int main(void)
{
USART_Config();
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
NVIC_EnableIRQ(USART3_IRQn);
uart_send_string("Ready to receive data...\r\n");
while(1)
{
if(uart_head != uart_tail)
{
uint8_t command = uart_buffer[uart_tail];
uart_tail = (uart_tail + 1) % UART_BUFFER_SIZE;
if(command == 'L')
{
uint16_t left = ADC_GetConversionValue(ADC1);
uart_send_string("Left: ");
uart_send_string(itoa(left));
uart_send_string("\r\n");
}
else if(command == 'C')
{
uint16_t center = ADC_GetConversionValue(ADC2);
uart_send_string("Center: ");
uart_send_string(itoa(center));
uart_send_string("\r\n");
}
else if(command == 'R')
{
uint16_t right = ADC_GetConversionValue(ADC3);
uart_send_string("Right: ");
uart_send_string(itoa(right));
uart_send_string("\r\n");
}
}
}
}
```
在这个示例代码中,我们使用了STM32F407的USART3模块与OpenMV进行通讯。当OpenMV发送'L'、'C'或'R'这三个指令中的任意一个时,STM32F407会对应地读取PC0~PC3四个引脚的ADC值,并将其通过USART3发送回OpenMV。
阅读全文