帮我写一个stm32f103mini的程序使其可以通过接收来自于python的字符消息,并根据接收到的消息来控制两个max7219级联的led点阵显示不同的表情,并给出我python端的程序,不要用max7219库和STM32CubeMX
时间: 2024-05-11 08:18:26 浏览: 125
stm32f103c8t6 写一个串口DMA程序
好的,下面是一个简单的示例程序,可以实现你的需求。在这个程序中,我们使用USART1来接收从Python发送过来的字符消息,并使用GPIO口以及SPI来控制两个级联的MAX7219 LED点阵。在此之前,需要你先在STM32CubeMX中将USART1和SPI的引脚进行配置,然后将生成的代码导入到Keil MDK中进行编译和下载。
```c
#include "stm32f10x.h"
#define USART1_DR_Base 0x40013804
#define MAX7219_CS_H() GPIOA->BSRR = GPIO_BSRR_BS4
#define MAX7219_CS_L() GPIOA->BSRR = GPIO_BSRR_BR4
void USART1_Init(void);
void MAX7219_Init(void);
void MAX7219_SendByte(uint8_t addr, uint8_t data);
void MAX7219_SendChar(uint8_t addr, uint8_t ch);
void MAX7219_SendString(uint8_t addr, char *str);
void delay_ms(uint16_t ms);
int main(void)
{
char buffer[32];
uint8_t smiley_face[8] = {0x3C, 0x42, 0x95, 0xA1, 0xA1, 0x95, 0x42, 0x3C};
uint8_t sad_face[8] = {0x3C, 0x42, 0xA5, 0x91, 0x91, 0xA5, 0x42, 0x3C};
USART1_Init();
MAX7219_Init();
while(1)
{
if(USART1->SR & USART_SR_RXNE)
{
char ch = USART1->DR;
if(ch == '1')
{
MAX7219_SendChar(1, 1);
MAX7219_SendChar(2, 1);
MAX7219_SendChar(3, 2);
MAX7219_SendChar(4, 4);
MAX7219_SendChar(5, 8);
MAX7219_SendChar(6, 16);
MAX7219_SendChar(7, 32);
MAX7219_SendChar(8, 64);
}
else if(ch == '2')
{
MAX7219_SendChar(1, 64);
MAX7219_SendChar(2, 32);
MAX7219_SendChar(3, 16);
MAX7219_SendChar(4, 8);
MAX7219_SendChar(5, 4);
MAX7219_SendChar(6, 2);
MAX7219_SendChar(7, 1);
MAX7219_SendChar(8, 1);
}
else if(ch == '3')
{
MAX7219_SendChar(1, 8);
MAX7219_SendChar(2, 20);
MAX7219_SendChar(3, 34);
MAX7219_SendChar(4, 65);
MAX7219_SendChar(5, 65);
MAX7219_SendChar(6, 34);
MAX7219_SendChar(7, 20);
MAX7219_SendChar(8, 8);
}
else if(ch == '4')
{
MAX7219_SendChar(1, 255);
MAX7219_SendChar(2, 129);
MAX7219_SendChar(3, 129);
MAX7219_SendChar(4, 129);
MAX7219_SendChar(5, 129);
MAX7219_SendChar(6, 129);
MAX7219_SendChar(7, 129);
MAX7219_SendChar(8, 255);
}
else if(ch == '5')
{
MAX7219_SendChar(1, 0);
MAX7219_SendChar(2, 0);
MAX7219_SendChar(3, 0);
MAX7219_SendChar(4, 0);
MAX7219_SendChar(5, 0);
MAX7219_SendChar(6, 0);
MAX7219_SendChar(7, 0);
MAX7219_SendChar(8, 0);
}
else if(ch == '6')
{
MAX7219_SendChar(1, 0);
MAX7219_SendChar(2, 0);
MAX7219_SendChar(3, 0);
MAX7219_SendChar(4, 0);
MAX7219_SendChar(5, 0);
MAX7219_SendChar(6, 0);
MAX7219_SendChar(7, 0);
MAX7219_SendChar(8, 0);
}
else if(ch == '7')
{
MAX7219_SendString(1, "HELLO!");
}
else if(ch == '8')
{
MAX7219_SendChar(1, 0);
MAX7219_SendChar(2, 0);
MAX7219_SendChar(3, 0);
MAX7219_SendChar(4, 0);
MAX7219_SendChar(5, 0);
MAX7219_SendChar(6, 0);
MAX7219_SendChar(7, 0);
MAX7219_SendChar(8, 0);
}
else if(ch == '9')
{
MAX7219_SendChar(1, 0);
MAX7219_SendChar(2, 0);
MAX7219_SendChar(3, 0);
MAX7219_SendChar(4, 0);
MAX7219_SendChar(5, 0);
MAX7219_SendChar(6, 0);
MAX7219_SendChar(7, 0);
MAX7219_SendChar(8, 0);
}
else
{
sprintf(buffer, "Unknown command: %c", ch);
MAX7219_SendString(1, buffer);
}
}
}
}
void USART1_Init(void)
{
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
GPIOA->CRH &= ~(GPIO_CRH_CNF9 | GPIO_CRH_MODE9 | GPIO_CRH_CNF10 | GPIO_CRH_MODE10);
GPIOA->CRH |= GPIO_CRH_CNF9_1 | GPIO_CRH_MODE9_0 | GPIO_CRH_CNF10_0;
USART1->BRR = 0x271; // 72MHz / 115200 = 625
USART1->CR1 = USART_CR1_RE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_UE;
NVIC_EnableIRQ(USART1_IRQn);
}
void MAX7219_Init(void)
{
GPIOA->CRH &= ~(GPIO_CRH_CNF7 | GPIO_CRH_MODE7 | GPIO_CRH_CNF5 | GPIO_CRH_MODE5 | GPIO_CRH_CNF6 | GPIO_CRH_MODE6 | GPIO_CRH_CNF4 | GPIO_CRH_MODE4);
GPIOA->CRH |= GPIO_CRH_MODE7_0 | GPIO_CRH_MODE5_0 | GPIO_CRH_MODE6_0 | GPIO_CRH_MODE4_0;
MAX7219_CS_H();
MAX7219_SendByte(0x09, 0x00);
MAX7219_SendByte(0x0A, 0x03);
MAX7219_SendByte(0x0B, 0x07);
MAX7219_SendByte(0x0C, 0x01);
MAX7219_SendByte(0x0F, 0x00);
MAX7219_SendByte(0x0F, 0x01);
}
void MAX7219_SendByte(uint8_t addr, uint8_t data)
{
MAX7219_CS_L();
SPI1->DR = addr;
while(!(SPI1->SR & SPI_SR_TXE));
SPI1->DR = data;
while(!(SPI1->SR & SPI_SR_TXE));
MAX7219_CS_H();
}
void MAX7219_SendChar(uint8_t addr, uint8_t ch)
{
uint8_t i;
if(addr > 8) return;
MAX7219_SendByte(addr, ch);
for(i = 1; i < 8; i++)
{
MAX7219_SendByte(addr + i, 0);
}
}
void MAX7219_SendString(uint8_t addr, char *str)
{
uint8_t i = 0;
while(str[i] != '\0')
{
MAX7219_SendChar(addr + i, str[i]);
i++;
}
}
void delay_ms(uint16_t ms)
{
for(uint16_t i = 0; i < ms; i++)
{
for(uint16_t j = 0; j < 1000; j++);
}
}
void USART1_IRQHandler(void)
{
if(USART1->SR & USART_SR_RXNE)
{
char ch = USART1->DR;
USART1->DR = ch;
while(!(USART1->SR & USART_SR_TXE));
}
}
```
下面是python端的示例代码,发送数字1到9可以控制不同的表情,发送其他字符会提示“Unknown command”:
```python
import serial
ser = serial.Serial('COM7', 115200, timeout=0.5)
while True:
cmd = input("Enter command: ")
if cmd == 'exit':
break
ser.write(cmd.encode('utf-8'))
print(ser.readline().decode('utf-8'))
ser.close()
```
注意:在使用此程序时,请确保你的LED点阵与MAX7219控制器的连接正确,并注意正确配置USART1和SPI的引脚。
阅读全文