帮我设计一个stm32f103程序使其可以接收来自python串口通信的字符,并根据接受的字符控制两个级联MAX7219led点阵显示上箭头下箭头左箭头右箭头四种不同的表情,在未接收到时显示问号的表情,我需要完整的代码
时间: 2023-06-19 22:03:36 浏览: 161
好的,这是一个简单的程序示例,可以实现你所需的功能。请注意,本程序使用的是HAL库。
```c
/* Includes */
#include "stm32f1xx_hal.h"
#include <string.h>
/* Defines */
#define MAX7219_LOAD_Pin GPIO_PIN_0
#define MAX7219_LOAD_GPIO_Port GPIOA
#define MAX7219_DIN_Pin GPIO_PIN_1
#define MAX7219_DIN_GPIO_Port GPIOA
#define MAX7219_CLK_Pin GPIO_PIN_2
#define MAX7219_CLK_GPIO_Port GPIOA
/* Function prototypes */
void SystemClock_Config(void);
void MX_GPIO_Init(void);
void MX_USART1_UART_Init(void);
void MAX7219_Init(void);
void MAX7219_WriteCmd(uint8_t reg, uint8_t data);
void MAX7219_DisplayChar(uint8_t digit, uint8_t character);
/* Global variables */
UART_HandleTypeDef huart1;
char received_char = '?';
/* Main function */
int main(void)
{
/* MCU Configuration */
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
MAX7219_Init();
/* Main loop */
while (1)
{
/* Check for received character */
if (HAL_UART_Receive(&huart1, (uint8_t *)&received_char, 1, 100) == HAL_OK)
{
/* Display corresponding character on the MAX7219 */
switch (received_char)
{
case 'u':
MAX7219_DisplayChar(0, 0x7E);
MAX7219_DisplayChar(1, 0x42);
MAX7219_DisplayChar(2, 0x42);
MAX7219_DisplayChar(3, 0x42);
MAX7219_DisplayChar(4, 0x42);
MAX7219_DisplayChar(5, 0x42);
MAX7219_DisplayChar(6, 0x42);
MAX7219_DisplayChar(7, 0x00);
break;
case 'd':
MAX7219_DisplayChar(0, 0x00);
MAX7219_DisplayChar(1, 0x42);
MAX7219_DisplayChar(2, 0x42);
MAX7219_DisplayChar(3, 0x42);
MAX7219_DisplayChar(4, 0x42);
MAX7219_DisplayChar(5, 0x42);
MAX7219_DisplayChar(6, 0x7E);
MAX7219_DisplayChar(7, 0x00);
break;
case 'l':
MAX7219_DisplayChar(0, 0x00);
MAX7219_DisplayChar(1, 0x7E);
MAX7219_DisplayChar(2, 0x02);
MAX7219_DisplayChar(3, 0x02);
MAX7219_DisplayChar(4, 0x02);
MAX7219_DisplayChar(5, 0x02);
MAX7219_DisplayChar(6, 0x02);
MAX7219_DisplayChar(7, 0x00);
break;
case 'r':
MAX7219_DisplayChar(0, 0x00);
MAX7219_DisplayChar(1, 0x02);
MAX7219_DisplayChar(2, 0x02);
MAX7219_DisplayChar(3, 0x02);
MAX7219_DisplayChar(4, 0x02);
MAX7219_DisplayChar(5, 0x02);
MAX7219_DisplayChar(6, 0x7E);
MAX7219_DisplayChar(7, 0x00);
break;
default:
MAX7219_DisplayChar(0, 0x3E);
MAX7219_DisplayChar(1, 0x41);
MAX7219_DisplayChar(2, 0x49);
MAX7219_DisplayChar(3, 0x49);
MAX7219_DisplayChar(4, 0x49);
MAX7219_DisplayChar(5, 0x41);
MAX7219_DisplayChar(6, 0x3E);
MAX7219_DisplayChar(7, 0x00);
break;
}
}
}
}
/* System Clock Configuration */
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK |
RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* GPIO Initialization */
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOA_CLK_ENABLE();
/* Configure GPIO pins */
GPIO_InitStruct.Pin = MAX7219_LOAD_Pin | MAX7219_DIN_Pin | MAX7219_CLK_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
/* USART Initialization */
void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
/* MAX7219 Initialization */
void MAX7219_Init(void)
{
/* Set decode mode */
MAX7219_WriteCmd(0x09, 0xFF);
/* Set intensity */
MAX7219_WriteCmd(0x0A, 0x03);
/* Set scan limit */
MAX7219_WriteCmd(0x0B, 0x07);
/* Enable display */
MAX7219_WriteCmd(0x0C, 0x01);
/* Clear display */
for (int i = 0; i < 8; i++)
{
MAX7219_DisplayChar(i, 0x00);
}
}
/* MAX7219 Write Command */
void MAX7219_WriteCmd(uint8_t reg, uint8_t data)
{
HAL_GPIO_WritePin(MAX7219_LOAD_GPIO_Port, MAX7219_LOAD_Pin, GPIO_PIN_RESET);
for (int i = 0; i < 8; i++)
{
if (i == 0)
HAL_GPIO_WritePin(MAX7219_DIN_GPIO_Port, MAX7219_DIN_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(MAX7219_DIN_GPIO_Port, MAX7219_DIN_Pin, (reg >> (i - 1)) & 0x01);
HAL_GPIO_WritePin(MAX7219_CLK_GPIO_Port, MAX7219_CLK_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(MAX7219_CLK_GPIO_Port, MAX7219_CLK_Pin, GPIO_PIN_RESET);
}
for (int i = 0; i < 8; i++)
{
if (i == 0)
HAL_GPIO_WritePin(MAX7219_DIN_GPIO_Port, MAX7219_DIN_Pin, GPIO_PIN_RESET);
else
HAL_GPIO_WritePin(MAX7219_DIN_GPIO_Port, MAX7219_DIN_Pin, (data >> (i - 1)) & 0x01);
HAL_GPIO_WritePin(MAX7219_CLK_GPIO_Port, MAX7219_CLK_Pin, GPIO_PIN_SET);
HAL_GPIO_WritePin(MAX7219_CLK_GPIO_Port, MAX7219_CLK_Pin, GPIO_PIN_RESET);
}
HAL_GPIO_WritePin(MAX7219_LOAD_GPIO_Port, MAX7219_LOAD_Pin, GPIO_PIN_SET);
}
/* MAX7219 Display Character */
void MAX7219_DisplayChar(uint8_t digit, uint8_t character)
{
if (digit < 0 || digit > 7)
return;
MAX7219_WriteCmd(digit + 1, character);
}
/* Error Handler */
void Error_Handler(void)
{
while (1)
{
}
}
/* System Clock Configuration */
void SystemClock_Config(void);
/* GPIO Initialization */
void MX_GPIO_Init(void);
/* USART Initialization */
void MX_USART1_UART_Init(void);
/* MAX7219 Initialization */
void MAX7219_Init(void);
/* MAX7219 Write Command */
void MAX7219_WriteCmd(uint8_t reg, uint8_t data);
/* MAX7219 Display Character */
void MAX7219_DisplayChar(uint8_t digit, uint8_t character);
/* Error Handler */
void Error_Handler(void);
```
在上面的代码中,我们使用了 MAX7219 级联的 LED 点阵模块来显示箭头图标。MAX7219 驱动模块的 DIN、CLK 和 LOAD 引脚分别连接到 STM32F103 的 PA1、PA2 和 PA0 引脚。
接收到的字符通过 USART1 的 RX 引脚从串口接收,并在 switch 语句中进行解析和处理。在每个 case 中,我们使用 MAX7219_DisplayChar 函数来显示相应的箭头图标。如果接收到的字符无法解析,则显示问号图标。
MAX7219_Init 函数用于初始化 MAX7219 驱动模块,设置其工作模式、扫描限制、亮度等参数,并清空显示。
MAX7219_WriteCmd 函数用于向 MAX7219 发送命令和数据,MAX7219_DisplayChar 函数用于在指定的数字管上显示指定的字符。
最后,Error_Handler 函数用于处理错误。
阅读全文