【BL606P-DVK开发板】驱动MAX7219点阵模组
[复制链接]
本帖最后由 乘简 于 2023-1-17 11:04 编辑
我买了一款MAX7219芯片的点阵模块,8个8x8连在一起的模块,只需要3个GPIO口即可驱动,可以显示两个16x16点阵的汉字
连线图:
GPIO2连入DIN,GPIO3连入CS,GPIO4连入CLK,VCC接5V,GND接GND
main.c的代码:
#include <aos/aos.h>
#include <stdio.h>
#include <sys_clk.h>
#include <drv/pin.h>
#include "app_main.h"
#include "max7219.h"
int main(int argc, char *argv[])
{
board_yoc_init();
max7219_Init();
printf("\r\napp start core clock %d........\r\n", soc_get_cur_cpu_freq());
Display();
return 0;
}
max7219.h
#ifndef _MAX7219_H_
#define _MAX7219_H_
#include <aos/aos.h>
#include <drv/pin.h>
#include <aos/hal/gpio.h>
#define MODS_7219 8//点阵模块数量
void max7219_Init();
void Display();
#endif
max7219.c
#include "max7219.h"
gpio_dev_t gpio_din = { GPIO_PIN_2, OUTPUT_PUSH_PULL, (void*)0 };
gpio_dev_t gpio_cs= { GPIO_PIN_3, OUTPUT_PUSH_PULL, (void*)0 };
gpio_dev_t gpio_clk = { GPIO_PIN_4, OUTPUT_PUSH_PULL, (void*)0 };
#define din_l() hal_gpio_output_low(&gpio_din)
#define din_h() hal_gpio_output_high(&gpio_din)
#define clk_l() hal_gpio_output_low(&gpio_clk)
#define clk_h() hal_gpio_output_high(&gpio_clk)
#define cs_l() hal_gpio_output_low(&gpio_cs)
#define cs_h() hal_gpio_output_high(&gpio_cs)
uint8_t FONT8[][8]={
{0x38,0x44,0x44,0x44,0x44,0x44,0x44,0x38},/*"0",0*/
{0x10,0x30,0x10,0x10,0x10,0x10,0x10,0x38},/*"1",1*/
{0x38,0x44,0x44,0x08,0x10,0x20,0x40,0x7C},/*"2",2*/
{0x38,0x44,0x04,0x18,0x04,0x04,0x44,0x38},/*"3",3*/
{0x08,0x18,0x18,0x28,0x48,0x7C,0x08,0x1C},/*"4",4*/
{0x7C,0x40,0x40,0x78,0x44,0x04,0x44,0x38},/*"5",5*/
{0x18,0x24,0x40,0x58,0x64,0x44,0x44,0x38},/*"6",6*/
{0x3C,0x04,0x08,0x08,0x10,0x10,0x10,0x10},/*"7",7*/
{0x38,0x44,0x44,0x38,0x44,0x44,0x44,0x38},/*"8",8*/
{0x38,0x44,0x44,0x4C,0x34,0x04,0x48,0x30},/*"9",9*/
};
//写1个字节
void max7219_writebyte(uint8_t dat)
{
uint8_t i;
for(i=0;i<8;i++){
clk_l();
if(dat & 0x80)din_h();else din_l();
dat<<=1;
clk_h();
}
}
//写入配置数据
void max7219_writeset(uint8_t address, uint8_t dat)
{
uint8_t i;
cs_l();
for(i=0;i<MODS_7219;i++){//有多少个max7219模块需要初始化
max7219_writebyte(address);
max7219_writebyte(dat);
}
cs_h();
}
//写入显示数据
void max7219_writedata(uint8_t address, uint8_t dat)
{
max7219_writebyte(address);
max7219_writebyte(dat);
}
void max7219_Init()
{
csi_pin_set_mux(GPIO_PIN_2, PIN_FUNC_GPIO);
csi_pin_set_mux(GPIO_PIN_3, PIN_FUNC_GPIO);
csi_pin_set_mux(GPIO_PIN_4, PIN_FUNC_GPIO);
hal_gpio_init(&gpio_din);
hal_gpio_init(&gpio_cs);
hal_gpio_init(&gpio_clk);
max7219_writeset(0x09,0x00);//解码模式,0:BCD码
max7219_writeset(0x0a,0x00);//亮度,0~15(因为USB连电脑供电,亮度不敢调太高,怕带不起)
max7219_writeset(0x0b,0x07);//扫描位数,7
max7219_writeset(0x0c,0x01);//工作模式,关机0,运行1
max7219_writeset(0x0f,0x00);//自检1,0正常
}
void Display()
{
uint8_t i,j;
for(i=0;i<8;i++)
{
cs_l();
for(j=0;j<4;j++){
max7219_writedata(i+1, FONT8[j][i]);
}
for(j=0;j<4;j++){
max7219_writedata(i+1, FONT8[j+4][i]);
}
cs_h();
}
}
效果如下:
|