新手上路
- 积分
- 22
- 金钱
- 22
- 注册时间
- 2019-3-6
- 在线时间
- 11 小时
|
前几天做max7219两片级联,经N多百度无果,纠结两天以及发帖无助的情况下,索性不级联了,
不就是多3个io口嘛,咱给的起,
分享下IO口模拟spi驱动多片max7219进行数显。
我用的max7219数码管是这种:

程序部分如下:
1.端口定义
disp 定义 PA3->CLK PA5->CS PA7->DIN
disp_1 定义 PA0->CLK PA2->CS PA4->DIN
- void Disp_GPIO(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 |GPIO_Pin_5 |GPIO_Pin_7 |GPIO_Pin_0 |GPIO_Pin_2 |GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_SetBits(GPIOA, GPIO_Pin_5 |GPIO_Pin_3 |GPIO_Pin_7 |GPIO_Pin_0 |GPIO_Pin_2 |GPIO_Pin_4);
- }
复制代码 2. CS拉低,clk拉低后输入低值,clk拉高移入寄存器,CS拉高锁存显示。
(之前纠结的级联,第16.5个clk脉冲后数据从DOUT输出,但是验证了N次,都是乱的,有时候第一片不显示,有时候第二片不显示,可能我理解的16.5个脉冲有问题,地址+数据刚好16个clk脉冲,发第二个地址数据前加一个clk脉冲一样不行,所以放弃了。。。)
- void Write_Max7219_byte(unsigned char DATA)
- {
- unsigned char i,j;
- for(i=8;i>=1;i--)
- {
- GPIO_ResetBits(GPIOA, GPIO_Pin_3);
- j=DATA&0x80;
- if(j == 0x80)
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_7);
- }
- else{
- GPIO_ResetBits(GPIOA, GPIO_Pin_7);
- }
- DATA=DATA<<1;
- GPIO_SetBits(GPIOA, GPIO_Pin_3);
- delay_us(10);
- GPIO_ResetBits(GPIOA, GPIO_Pin_3);
- }
- }
- void Write_Max7219_byte_1(unsigned char DATA)
- {
- unsigned char i,j;
- for(i=8;i>=1;i--)
- {
- GPIO_ResetBits(GPIOA, GPIO_Pin_0);
- delay_us(10);
- j=DATA&0x80;
- if(j == 0x80)
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_4);
- }
- else{
- GPIO_ResetBits(GPIOA, GPIO_Pin_4);
- }
- DATA=DATA<<1;
- delay_us(10);
- GPIO_SetBits(GPIOA, GPIO_Pin_0);
- delay_us(10);
- GPIO_ResetBits(GPIOA, GPIO_Pin_0);
- }
- }
- void Write_Max7219(u8 address,u8 dat)
- {
- GPIO_ResetBits(GPIOA, GPIO_Pin_5);
- delay_us(10);
- Write_Max7219_byte(address);
- Write_Max7219_byte(dat);
- delay_us(10);
- GPIO_SetBits(GPIOA, GPIO_Pin_5);
- delay_us(10);
- }
- void Write_Max7219_1(u8 address1,u8 dat1)
- {
- GPIO_ResetBits(GPIOA, GPIO_Pin_2);
- delay_us(10);
- Write_Max7219_byte_1(address1);
- Write_Max7219_byte_1(dat1);
- delay_us(10);
- GPIO_SetBits(GPIOA, GPIO_Pin_2);
- delay_us(10);
-
- }
复制代码 3.写个显示程序封装起来- void disp(int v3,int v4)
- {
- int d1,d2,d3,d4,d5,d6,d7,d8;
- d1 = v3/1000;
- d2 = v3/100%10;
- d3 = v3/10%10;
- d4 = v3 %10;
- d5 = v4/1000;
- d6 = v4/100%10;
- d7 = v4/10%10;
- d8 = v4 %10;
- Write_Max7219_1(0x01, d8);
- Write_Max7219_1(0x02, d7);
- Write_Max7219_1(0x03, d6);
- Write_Max7219_1(0x04, d5);
- Write_Max7219_1(0x05, d4);
- Write_Max7219_1(0x06, d3);
- Write_Max7219_1(0x07, d2);
- Write_Max7219_1(0x08, d1);
- }
- void disp_1(int v1,int v2)
- {
- int d1,d2,d3,d4,d5,d6,d7,d8;
- d1 = v1/1000;
- d2 = v1/100%10;
- d3 = v1/10%10;
- d4 = v1 %10;
- d5 = v2/1000;
- d6 = v2/100%10;
- d7 = v2/10%10;
- d8 = v2 %10;
- Write_Max7219(0x01, d8);
- Write_Max7219(0x02, d7);
- Write_Max7219(0x03, d6);
- Write_Max7219(0x04, d5);
- Write_Max7219(0x05, d4);
- Write_Max7219(0x06, d3);
- Write_Max7219(0x07, d2);
- Write_Max7219(0x08, d1);
- }
复制代码 4.使用BCD码初始化一下- void Init_MAX7219(void)
- {
- int i,j;
- Write_Max7219(0x09, 0xff); //译码BCD
- Write_Max7219(0x0a, 0x03); //亮度
- Write_Max7219(0x0b, 0x07); //扫描界限
- Write_Max7219(0x0c, 0x01); //普通模式1 掉电模式0
- Write_Max7219(0x0f, 0x00); //正常显示0
- delay_ms(10);
- Write_Max7219_1(0x09, 0xff);
- Write_Max7219_1(0x0a, 0x03);
- Write_Max7219_1(0x0b, 0x07);
- Write_Max7219_1(0x0c, 0x01);
- Write_Max7219_1(0x0f, 0x00);
- delay_ms(10);
- for(j=0;j<10;j++){
- for(i=1;i<9;i++){
- Write_Max7219(i,j);
- Write_Max7219_1(i,j);
- }
- delay_ms(300);
- }
- }
复制代码 5.直接调用disp(int,int)或者disp_1(int,int)就能进行显示了,如果想要更多片,继续扩展io,方法一模一样,要多少加多少,虽然很笨但很好用! |
|