OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 2394|回复: 0

max7219“级联”新方法 之 模拟spi

[复制链接]

3

主题

8

帖子

0

精华

新手上路

积分
22
金钱
22
注册时间
2019-3-6
在线时间
11 小时
发表于 2019-9-18 09:59:58 | 显示全部楼层 |阅读模式
前几天做max7219两片级联,经N多百度无果,纠结两天以及发帖无助的情况下,索性不级联了,
不就是多3个io口嘛,咱给的起,
分享下IO口模拟spi驱动多片max7219进行数显。

我用的max7219数码管是这种:

程序部分如下:
1.端口定义
disp    定义  PA3->CLK PA5->CS PA7->DIN
disp_1 定义 PA0->CLK PA2->CS PA4->DIN
  1. void Disp_GPIO(void)               
  2. {
  3.   GPIO_InitTypeDef GPIO_InitStructure;
  4.   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
  5.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 |GPIO_Pin_5 |GPIO_Pin_7 |GPIO_Pin_0 |GPIO_Pin_2 |GPIO_Pin_4;       
  6.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      
  7.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  8.   GPIO_Init(GPIOA, &GPIO_InitStructure);
  9.   GPIO_SetBits(GPIOA, GPIO_Pin_5 |GPIO_Pin_3 |GPIO_Pin_7 |GPIO_Pin_0 |GPIO_Pin_2 |GPIO_Pin_4);         
  10. }
复制代码
2. CS拉低,clk拉低后输入低值,clk拉高移入寄存器,CS拉高锁存显示。
(之前纠结的级联,第16.5个clk脉冲后数据从DOUT输出,但是验证了N次,都是乱的,有时候第一片不显示,有时候第二片不显示,可能我理解的16.5个脉冲有问题,地址+数据刚好16个clk脉冲,发第二个地址数据前加一个clk脉冲一样不行,所以放弃了。。。)
  1. void Write_Max7219_byte(unsigned char DATA)         
  2.         {
  3.                                 unsigned char i,j;                   
  4.                                         for(i=8;i>=1;i--)
  5.                                                 {                 
  6.                                                         GPIO_ResetBits(GPIOA, GPIO_Pin_3);
  7.                                                         j=DATA&0x80;
  8.                                                         if(j == 0x80)
  9.                                                         {
  10.                                                          GPIO_SetBits(GPIOA, GPIO_Pin_7);
  11.                                                                 }
  12.                                                         else{
  13.                                                                 GPIO_ResetBits(GPIOA, GPIO_Pin_7);
  14.                                                                          }                                                       
  15.                                                         DATA=DATA<<1;
  16.                                                         GPIO_SetBits(GPIOA, GPIO_Pin_3);
  17.                                                         delay_us(10);
  18.                                                         GPIO_ResetBits(GPIOA, GPIO_Pin_3);
  19.                                                  }                                 
  20.         }
  21.         void Write_Max7219_byte_1(unsigned char DATA)         
  22.         {
  23.                                 unsigned char i,j;                   
  24.                                         for(i=8;i>=1;i--)
  25.                                                 {                 
  26.                                                         GPIO_ResetBits(GPIOA, GPIO_Pin_0);
  27.                                                         delay_us(10);
  28.                                                         j=DATA&0x80;
  29.                                                         if(j == 0x80)
  30.                                                         {
  31.                                                          GPIO_SetBits(GPIOA, GPIO_Pin_4);
  32.                                                                 }
  33.                                                         else{
  34.                                                                 GPIO_ResetBits(GPIOA, GPIO_Pin_4);
  35.                                                                          }                                                       
  36.                                                         DATA=DATA<<1;
  37.                                                         delay_us(10);
  38.                                                         GPIO_SetBits(GPIOA, GPIO_Pin_0);
  39.                                                         delay_us(10);
  40.                                                         GPIO_ResetBits(GPIOA, GPIO_Pin_0);
  41.                                                  }                                 
  42.         }

  43. void Write_Max7219(u8 address,u8 dat)
  44. {
  45.             GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  46.                   delay_us(10);
  47.             Write_Max7219_byte(address);
  48.             Write_Max7219_byte(dat);
  49.             delay_us(10);       
  50.             GPIO_SetBits(GPIOA, GPIO_Pin_5);
  51.                   delay_us(10);
  52. }

  53. void Write_Max7219_1(u8 address1,u8 dat1)
  54. {
  55.             GPIO_ResetBits(GPIOA, GPIO_Pin_2);
  56.                   delay_us(10);
  57.             Write_Max7219_byte_1(address1);
  58.             Write_Max7219_byte_1(dat1);
  59.             delay_us(10);       
  60.             GPIO_SetBits(GPIOA, GPIO_Pin_2);
  61.                   delay_us(10);
  62.                          
  63. }
复制代码
3.写个显示程序封装起来
  1. void disp(int v3,int v4)
  2. {  
  3.    int d1,d2,d3,d4,d5,d6,d7,d8;
  4.          d1 = v3/1000;
  5.          d2 = v3/100%10;
  6.          d3 = v3/10%10;
  7.          d4 = v3 %10;
  8.          d5 = v4/1000;
  9.          d6 = v4/100%10;
  10.          d7 = v4/10%10;
  11.          d8 = v4 %10;

  12.   Write_Max7219_1(0x01, d8);
  13.         Write_Max7219_1(0x02, d7);
  14.         Write_Max7219_1(0x03, d6);
  15.         Write_Max7219_1(0x04, d5);
  16.         Write_Max7219_1(0x05, d4);
  17.         Write_Max7219_1(0x06, d3);
  18.         Write_Max7219_1(0x07, d2);
  19.         Write_Max7219_1(0x08, d1);

  20. }

  21. void disp_1(int v1,int v2)
  22. {  
  23.    int d1,d2,d3,d4,d5,d6,d7,d8;
  24.          d1 = v1/1000;
  25.          d2 = v1/100%10;
  26.          d3 = v1/10%10;
  27.          d4 = v1 %10;
  28.          d5 = v2/1000;
  29.          d6 = v2/100%10;
  30.          d7 = v2/10%10;
  31.          d8 = v2 %10;

  32.   Write_Max7219(0x01, d8);
  33.         Write_Max7219(0x02, d7);
  34.         Write_Max7219(0x03, d6);
  35.         Write_Max7219(0x04, d5);
  36.         Write_Max7219(0x05, d4);
  37.         Write_Max7219(0x06, d3);
  38.         Write_Max7219(0x07, d2);
  39.         Write_Max7219(0x08, d1);

  40. }
复制代码
4.使用BCD码初始化一下
  1. void Init_MAX7219(void)
  2. {
  3.         int i,j;
  4. Write_Max7219(0x09, 0xff);       //译码BCD
  5. Write_Max7219(0x0a, 0x03);       //亮度
  6. Write_Max7219(0x0b, 0x07);       //扫描界限
  7. Write_Max7219(0x0c, 0x01);       //普通模式1 掉电模式0
  8. Write_Max7219(0x0f, 0x00);       //正常显示0
  9. delay_ms(10);
  10. Write_Max7219_1(0x09, 0xff);
  11. Write_Max7219_1(0x0a, 0x03);  
  12. Write_Max7219_1(0x0b, 0x07);
  13. Write_Max7219_1(0x0c, 0x01);
  14. Write_Max7219_1(0x0f, 0x00);
  15.         delay_ms(10);
  16.   for(j=0;j<10;j++){
  17.         for(i=1;i<9;i++){
  18.    Write_Max7219(i,j);
  19.                 Write_Max7219_1(i,j);
  20.   }
  21.         delay_ms(300);
  22. }
  23. }
复制代码
5.直接调用disp(int,int)或者disp_1(int,int)就能进行显示了,如果想要更多片,继续扩展io,方法一模一样,要多少加多少,虽然很笨但很好用!
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-28 22:38

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表