基于stm32f103c8t6的max7912驱动8×8点阵屏的测试代码
时间: 2024-03-10 16:49:21 浏览: 111
stm32f103c8t6_max30102_max30102代码_stm32f103c8t6例程_
5星 · 资源好评率100%
以下是基于stm32f103c8t6的max7912驱动8×8点阵屏的测试代码,供您参考:
```
#include "stm32f10x.h"
// 定义MAX7219各个寄存器地址
#define MAX7219_REG_NOOP 0x00
#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
// 数码管编码表
const uint8_t DIGIT_CODE[] = {
0x7E, 0x30, 0x6D, 0x79, 0x33, 0x5B, 0x5F, 0x70,
0x7F, 0x7B, 0x77, 0x1F, 0x0D, 0x3D, 0x4F, 0x47
};
// MAX7219发送一个字节
void MAX7219_SendByte(uint8_t reg, uint8_t data) {
GPIO_ResetBits(GPIOB, GPIO_Pin_12); // 使能
SPI_I2S_SendData(SPI2, reg);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
SPI_I2S_SendData(SPI2, data);
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
GPIO_SetBits(GPIOB, GPIO_Pin_12); // 禁止
}
// 初始化MAX7219
void MAX7219_Init() {
MAX7219_SendByte(MAX7219_REG_SCANLIMIT, 0x07); // 扫描位数,0-7都扫描
MAX7219_SendByte(MAX7219_REG_DECODEMODE, 0x00); // BCD编码模式
MAX7219_SendByte(MAX7219_REG_DISPLAYTEST, 0x00); // 关闭测试模式
MAX7219_SendByte(MAX7219_REG_INTENSITY, 0x05); // 亮度调节
MAX7219_SendByte(MAX7219_REG_SHUTDOWN, 0x01); // 开启正常工作模式
}
// 显示一个数字
void MAX7219_DisplayDigit(uint8_t digit, uint8_t value) {
MAX7219_SendByte(digit + 1, DIGIT_CODE[value]); // 发送数码管编码
}
int main() {
// 初始化SPI2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
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_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, &SPI_InitStructure);
SPI_Cmd(SPI2, ENABLE);
// 初始化MAX7219
MAX7219_Init();
// 显示数字0-9
for (uint8_t i = 0; i < 10; i++) {
MAX7219_DisplayDigit(7, i);
for (volatile uint32_t j = 0; j < 1000000; j++) {}
}
while (1) {}
}
```
这段代码中,使用了STM32的SPI2接口来与MAX7219通信,将数字编码发送到MAX7219,显示在8×8点阵屏上。您可以根据具体硬件的接口定义进行修改。
阅读全文