帮我设计一个stm32f103标准库程序使其可以接收来自python串口通信的字符,并根据接受的字符控制两个级联MAX7219led点阵显示上箭头下箭头左箭头右箭头四种不同的表情,在未接收到时显示问号的表情
时间: 2023-06-16 12:07:57 浏览: 148
好的,我们可以利用STM32F103的USART模块来进行串口通信,然后通过MAX7219的SPI接口来控制LED点阵显示。下面是一个简单的示例程序:
```c
#include "stm32f10x.h"
#include <stdio.h>
#define USART USART1
#define MAX7219_CS GPIO_Pin_4
#define MAX7219_CLK GPIO_Pin_5
#define MAX7219_DIN GPIO_Pin_7
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1, ENABLE);
// USART1_TX
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);
// USART1_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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 MAX7219_Send(uint8_t address, uint8_t data)
{
GPIO_ResetBits(GPIOA, MAX7219_CS);
for (int i = 7; i >= 0; i--)
{
GPIO_ResetBits(GPIOA, MAX7219_CLK);
if (i == 7)
SPI_I2S_SendData(SPI1, address);
else if (i == 6)
SPI_I2S_SendData(SPI1, data);
else
SPI_I2S_SendData(SPI1, 0);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET)
;
GPIO_SetBits(GPIOA, MAX7219_CLK);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)
;
SPI_I2S_ReceiveData(SPI1);
}
GPIO_SetBits(GPIOA, MAX7219_CS);
}
void MAX7219_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = MAX7219_CS | MAX7219_CLK | MAX7219_DIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
MAX7219_Send(0x0C, 0x01); // Shutdown register: Normal operation(关断寄存器设置正常操作)
MAX7219_Send(0x0F, 0x00); // Display test: off(显示测试关闭)
MAX7219_Send(0x09, 0xFF); // Decode mode: BCD for digit 0-7(解码模式设置为BCD码)
MAX7219_Send(0x0B, 0x07); // Scan limit: 8 digits(扫描限制设置为8位)
MAX7219_Send(0x0A, 0x0F); // Intensity: 15/32 duty cycle(亮度设置为15/32)
}
void MAX7219_Display(uint8_t data[])
{
for (int i = 1; i <= 8; i++)
MAX7219_Send(i, data[i - 1]);
}
int main(void)
{
USART_Config();
MAX7219_Init();
char c;
uint8_t data[8] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
while (1)
{
if (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET)
{
c = USART_ReceiveData(USART1);
if (c == 'u')
data[0] = 0x18, data[1] = 0x3C, data[2] = 0x7E, data[3] = 0xFF, data[4] = 0x00, data[5] = 0x00, data[6] = 0x00, data[7] = 0x00;
else if (c == 'd')
data[0] = 0x00, data[1] = 0x00, data[2] = 0x00, data[3] = 0xFF, data[4] = 0x7E, data[5] = 0x3C, data[6] = 0x18, data[7] = 0x00;
else if (c == 'l')
data[0] = 0x00, data[1] = 0x00, data[2] = 0x00, data[3] = 0x3C, data[4] = 0x7E, data[5] = 0xFF, data[6] = 0x7E, data[7] = 0x3C;
else if (c == 'r')
data[0] = 0x3C, data[1] = 0x7E, data[2] = 0xFF, data[3] = 0x7E, data[4] = 0x3C, data[5] = 0x00, data[6] = 0x00, data[7] = 0x00;
else
data[0] = 0x3C, data[1] = 0x42, data[2] = 0x95, data[3] = 0xA1, data[4] = 0xA1, data[5] = 0x95, data[6] = 0x42, data[7] = 0x3C;
MAX7219_Display(data);
}
}
}
```
在主函数中,我们不断地从串口接收字符,并根据接收到的字符设置LED点阵的显示。如果没有接收到字符,则默认显示问号的表情。MAX7219_Send和MAX7219_Display函数则是用来控制MAX7219芯片的SPI通信和LED点阵的显示。注意,这里只是一个简单的示例程序,实际应用中还需要根据具体需求进行修改。
阅读全文