环境和硬件及功能:
环境:ubuntu20.04
ros2版本:galatic
硬件:stm32c8t6最小系统板,ttl转usb模块,led灯
功能:实现上位机和下位机的串口通信,通过ros2控制stm32实现led灯的点亮与熄灭(以最简单的点灯操作来模拟ros2和stm32的通信,可自己改写32程序来实现更复杂的功能)。
32端:
接线:串口模块的txd接32板的A10引脚,rxd接A9引脚,二极管负极接A2引脚
main.c
#include "stm32f10x.h"
#include "Serial.h"
#include "LED.h"
uint8_t RxData;
int main(void)
{
Serial_Init();
LED_Init();
while (1)
{
if (Serial_GetRxFlag() == 1)
{
RxData = Serial_GetRxData();
if(RxData=='A')
{
LED_ON();
}
else
{
LED_OFF();
}
}
}
}
当串口收到字符A亮灯,否则灭灯。
Serial.c
#include "stm32f10x.h"
#include <stdio.h>
#include <stdarg.h>
uint8_t Serial_RxData;
uint8_t Serial_RxFlag;
void Serial_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_Init