帮我设计一个stm32f103标准库程序使其可以接收来自python串口通信的字符,并根据接受的字符控制两个级联MAX7219led点阵显示上箭头下箭头左箭头右箭头四种不同的表情,在未接收到时显示问号的表情,我需要完整的代码
时间: 2023-06-19 22:03:04 浏览: 88
基于STM32F103 处理器的串口发送数据程序
好的,下面是我为您编写的代码:
```c
#include "stm32f10x.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX7219_REG_NOOP 0x00
#define MAX7219_REG_DIGIT0 0x01
#define MAX7219_REG_DIGIT1 0x02
#define MAX7219_REG_DIGIT2 0x03
#define MAX7219_REG_DIGIT3 0x04
#define MAX7219_REG_DIGIT4 0x05
#define MAX7219_REG_DIGIT5 0x06
#define MAX7219_REG_DIGIT6 0x07
#define MAX7219_REG_DIGIT7 0x08
#define MAX7219_REG_DECODEMODE 0x09
#define MAX7219_REG_INTENSITY 0x0A
#define MAX7219_REG_SCANLIMIT 0x0B
#define MAX7219_REG_SHUTDOWN 0x0C
#define MAX7219_REG_DISPLAYTEST 0x0F
#define LED_NOOP 0x00
#define LED_CLEAR_ALL 0x01
#define LED_UP 0x02
#define LED_DOWN 0x03
#define LED_LEFT 0x04
#define LED_RIGHT 0x05
#define LED_QUESTION 0x06
void USART1_Init(void);
void USART_SendString(USART_TypeDef* USARTx, char* s);
void MAX7219_Send(uint8_t address, uint8_t data);
void MAX7219_Init(void);
void LED_Show(uint8_t led_type);
int main(void)
{
USART1_Init();
MAX7219_Init();
char c;
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)
{
c = USART_ReceiveData(USART1);
switch (c)
{
case 'U':
case 'u':
LED_Show(LED_UP);
break;
case 'D':
case 'd':
LED_Show(LED_DOWN);
break;
case 'L':
case 'l':
LED_Show(LED_LEFT);
break;
case 'R':
case 'r':
LED_Show(LED_RIGHT);
break;
default:
LED_Show(LED_QUESTION);
break;
}
}
}
}
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART_SendString(USART_TypeDef* USARTx, char* s)
{
while (*s)
{
while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USART_SendData(USARTx, *s++);
}
}
void MAX7219_Send(uint8_t address, uint8_t data)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
for (int i = 0; i < 8; i++)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_13, (BitAction)((address >> (7 - i)) & 0x01));
GPIO_WriteBit(GPIOB, GPIO_Pin_15, (BitAction)((data >> (7 - i)) & 0x01));
GPIO_SetBits(GPIOB, GPIO_Pin_14);
GPIO_ResetBits(GPIOB, GPIO_Pin_14);
}
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
void MAX7219_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
MAX7219_Send(MAX7219_REG_SCANLIMIT, 0x07);
MAX7219_Send(MAX7219_REG_DECODEMODE, 0x00);
MAX7219_Send(MAX7219_REG_DISPLAYTEST, 0x00);
MAX7219_Send(MAX7219_REG_SHUTDOWN, 0x01);
MAX7219_Send(MAX7219_REG_INTENSITY, 0x0F);
MAX7219_Send(MAX7219_REG_CLEAR_ALL, 0x00);
}
void LED_Show(uint8_t led_type)
{
switch (led_type)
{
case LED_CLEAR_ALL:
MAX7219_Send(MAX7219_REG_DIGIT0, 0);
MAX7219_Send(MAX7219_REG_DIGIT1, 0);
MAX7219_Send(MAX7219_REG_DIGIT2, 0);
MAX7219_Send(MAX7219_REG_DIGIT3, 0);
MAX7219_Send(MAX7219_REG_DIGIT4, 0);
MAX7219_Send(MAX7219_REG_DIGIT5, 0);
MAX7219_Send(MAX7219_REG_DIGIT6, 0);
MAX7219_Send(MAX7219_REG_DIGIT7, 0);
break;
case LED_UP:
MAX7219_Send(MAX7219_REG_DIGIT0, 0x18);
MAX7219_Send(MAX7219_REG_DIGIT1, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT2, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT3, 0xFF);
MAX7219_Send(MAX7219_REG_DIGIT4, 0xFF);
MAX7219_Send(MAX7219_REG_DIGIT5, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT6, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT7, 0x18);
break;
case LED_DOWN:
MAX7219_Send(MAX7219_REG_DIGIT0, 0x18);
MAX7219_Send(MAX7219_REG_DIGIT1, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT2, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT3, 0xFF);
MAX7219_Send(MAX7219_REG_DIGIT4, 0xFF);
MAX7219_Send(MAX7219_REG_DIGIT5, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT6, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT7, 0x18);
break;
case LED_LEFT:
MAX7219_Send(MAX7219_REG_DIGIT0, 0x18);
MAX7219_Send(MAX7219_REG_DIGIT1, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT2, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT3, 0xFF);
MAX7219_Send(MAX7219_REG_DIGIT4, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT5, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT6, 0x18);
MAX7219_Send(MAX7219_REG_DIGIT7, 0x00);
break;
case LED_RIGHT:
MAX7219_Send(MAX7219_REG_DIGIT0, 0x00);
MAX7219_Send(MAX7219_REG_DIGIT1, 0x18);
MAX7219_Send(MAX7219_REG_DIGIT2, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT3, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT4, 0xFF);
MAX7219_Send(MAX7219_REG_DIGIT5, 0x7E);
MAX7219_Send(MAX7219_REG_DIGIT6, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT7, 0x18);
break;
case LED_QUESTION:
MAX7219_Send(MAX7219_REG_DIGIT0, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT1, 0x42);
MAX7219_Send(MAX7219_REG_DIGIT2, 0x40);
MAX7219_Send(MAX7219_REG_DIGIT3, 0x3C);
MAX7219_Send(MAX7219_REG_DIGIT4, 0x04);
MAX7219_Send(MAX7219_REG_DIGIT5, 0x08);
MAX7219_Send(MAX7219_REG_DIGIT6, 0x10);
MAX7219_Send(MAX7219_REG_DIGIT7, 0x1F);
break;
default:
break;
}
}
```
这份代码实现了串口接收并根据接收到的字符控制MAX7219点阵LED显示不同的图案。其中,USART1_Init()函数初始化了USART1串口通信,MAX7219_Init()函数初始化了MAX7219点阵LED显示模块,LED_Show()函数根据输入的参数控制LED显示不同的图案。
在这份代码中,我们假设您将MAX7219点阵LED模块的VCC、GND、DIN、CS、CLK引脚分别连接到了STM32F103的5V、GND、PA7、PB12、PB13上。另外,我们也假设您将串口通信的TX、RX引脚分别连接到了STM32F103的PA9、PA10上。
希望这份代码对您有所帮助!
阅读全文