请帮我设计一个stm32f103程序使其可以接收来自python串口通信的字符,并根据接受的字符控制两个级联MAX7219led点阵显示笑脸和爱心两种不同的表情
时间: 2023-06-17 11:06:55 浏览: 166
好的,首先需要在stm32f103上配置串口接收功能和使用MAX7219控制led点阵的功能。以下是一个简单的示例程序供参考:
```c
#include "stm32f10x.h"
#include "stdio.h"
#define MAX7219_CS_PIN GPIO_Pin_4
#define MAX7219_CS_PORT GPIOA
#define USART_RX_PIN GPIO_Pin_3
#define USART_RX_PORT GPIOA
#define USART_TX_PIN GPIO_Pin_2
#define USART_TX_PORT GPIOA
#define BAUD_RATE 9600
void USART1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = USART_RX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART_RX_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = USART_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART_TX_PORT, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = BAUD_RATE;
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_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = MAX7219_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(MAX7219_CS_PORT, &GPIO_InitStructure);
GPIO_SetBits(MAX7219_CS_PORT, MAX7219_CS_PIN);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_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_Write(0x09, 0x00);
MAX7219_Write(0x0a, 0x03);
MAX7219_Write(0x0b, 0x07);
MAX7219_Write(0x0c, 0x01);
MAX7219_Write(0x0f, 0x00);
}
void MAX7219_Write(unsigned char reg, unsigned char data)
{
GPIO_ResetBits(MAX7219_CS_PORT, MAX7219_CS_PIN);
SPI_I2S_SendData(SPI1, reg);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, data);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
GPIO_SetBits(MAX7219_CS_PORT, MAX7219_CS_PIN);
}
void MAX7219_Display(unsigned char row, unsigned char col, unsigned char data)
{
if (row > 7 || col > 7)
return;
unsigned char reg = 0x01 + row;
unsigned char mask = 0x80 >> col;
unsigned char val = data & mask ? 0x01 : 0x00;
MAX7219_Write(reg, (MAX7219_Read(reg) & ~mask) | (val << col));
}
unsigned char MAX7219_Read(unsigned char reg)
{
unsigned char data = 0;
GPIO_ResetBits(MAX7219_CS_PORT, MAX7219_CS_PIN);
SPI_I2S_SendData(SPI1, reg | 0x80);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI1, 0x00);
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);
data = SPI_I2S_ReceiveData(SPI1);
GPIO_SetBits(MAX7219_CS_PORT, MAX7219_CS_PIN);
return data;
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
unsigned char c = USART_ReceiveData(USART1);
switch (c)
{
case '1':
// 显示笑脸
MAX7219_Display(0, 1, 0x18);
MAX7219_Display(0, 2, 0x24);
MAX7219_Display(0, 3, 0x42);
MAX7219_Display(0, 4, 0x42);
MAX7219_Display(0, 5, 0x42);
MAX7219_Display(0, 6, 0x42);
MAX7219_Display(0, 7, 0x3c);
break;
case '2':
// 显示爱心
MAX7219_Display(0, 0, 0x18);
MAX7219_Display(0, 1, 0x3c);
MAX7219_Display(0, 2, 0x7e);
MAX7219_Display(0, 3, 0xff);
MAX7219_Display(0, 4, 0xff);
MAX7219_Display(0, 5, 0x7e);
MAX7219_Display(0, 6, 0x3c);
MAX7219_Display(0, 7, 0x18);
break;
}
}
}
int main(void)
{
USART1_Init();
MAX7219_Init();
while (1)
{
}
}
```
在上面的代码中,我们使用了stm32f103的串口1和SPI1模块。串口1用于接收来自python程序发送的字符,SPI1用于控制两个级联的MAX7219芯片,从而控制两个led点阵。
在main函数中,我们初始化了串口1和MAX7219芯片,然后进入一个无限循环等待串口1接收到字符。当接收到字符后,我们根据字符的值选择显示笑脸或爱心。具体的显示方式在USART1_IRQHandler中实现,这是一个由stm32f103内部硬件自动调用的中断服务程序。
需要注意的是,MAX7219_Display函数用于控制具体的led点阵显示方式,其中第一个参数row表示行数,第二个参数col表示列数,第三个参数data表示具体的显示数据。使用该函数前需要先初始化MAX7219芯片。
此外,还需要在python程序中使用pyserial模块打开串口并发送字符。以下是一个简单的python程序供参考:
```python
import serial
ser = serial.Serial("/dev/ttyUSB0", 9600, timeout=1)
while True:
cmd = input("请输入指令(1.笑脸 2.爱心): ")
if cmd == "1" or cmd == "2":
ser.write(cmd.encode())
else:
print("无效指令")
```
在该python程序中,我们通过pyserial模块打开串口/dev/ttyUSB0,并设置波特率为9600。然后进入一个循环等待用户输入指令。如果用户输入了有效指令,则将指令发送到stm32f103的串口1中。需要注意的是,由于串口数据是以字节形式传输的,因此需要将指令先编码为字节再发送到串口中。
综上所述,该程序可以实现通过串口控制两个级联MAX7219led点阵显示笑脸和爱心两种不同的表情。
阅读全文