[51单片机] 51单片机驱动MAX7219

[复制链接]
3308|0
 楼主| 比神乐 发表于 2024-10-13 12:07 | 显示全部楼层 |阅读模式
代码:
  1. #include<reg51.h>
  2. #define uchar unsigned char  
  3. #define uint unsigned int
  4. /** @addtogroup 425_GPIO_led_toggle GPIO_led_toggle
  5.   * @{
  6.   */


  7. #define NoOp   0x00           //空操作寄存器
  8. #define Digit0 0x01           // 数码管1寄存器
  9. #define Digit1 0x02           // 数码管2寄存器
  10. #define Digit2 0x03           // 数码管3寄存器
  11. #define Digit3 0x04           // 数码管4寄存器
  12. #define Digit4 0x05           // 数码管5寄存器
  13. #define Digit5 0x06           // 数码管6寄存器
  14. #define Digit6 0x07           // 数码管7寄存器
  15. #define Digit7 0x08           // 数码管8寄存器

  16. #define DecodeMode 0x09       // 译码模式寄存器
  17. #define Intensity 0x0a        // 亮度寄存器
  18. #define ScanLimit 0x0b        // 扫描位数寄存器
  19. #define ShutDown 0x0c         // 低功耗模式寄存器

  20. #define DisplayTest 0x0f      // 显示测试寄存器
  21. #define ShutdownMode 0x00     // 低功耗方式
  22. #define NormalOperation 0x01  // 正常操作方式
  23. #define ScanDigit 0x07        // 扫描位数设置,显示8位数码管
  24. #define DecodeDigit 0xff      // 译码设置,8位均为BCD码
  25. #define IntensityGrade 0x0a   // 亮度级别设置
  26. #define TestMode 0x01         // 显示测试模式
  27. #define TextEnd 0x00          // 显示测试结束,恢复正常工作模式



  28. sbit CLK=P1^0;
  29. sbit LOAD=P1^1;
  30. sbit DIN=P1^2;


  31. #define _nop_()  i=i
  32. uchar i;

  33. //******************延时t毫秒**************************************
  34. void delay(uint t)
  35. {
  36.        uint i;
  37.        while(t--)
  38.        {
  39.               /* 对于12M时钟,约延时1ms */
  40.               for (i=0;i<125;i++)
  41.               {}
  42.        }
  43. }
  44. //*************向MAX7219写入字节(8位)********************//
  45. void SendChar (uchar ch)
  46. {
  47.     uchar i,temp;
  48.     _nop_();
  49.     for (i=0;i<8;i++)
  50.     {
  51.          temp=ch&0x80;
  52.          ch=ch<<1;
  53.          if(temp)
  54.          {
  55.             DIN=1;
  56.                         _nop_();
  57.             CLK=0;
  58.                         _nop_();
  59.             CLK=1;
  60.                         _nop_();
  61.          }
  62.          else
  63.          {
  64.             DIN=0;
  65.                         _nop_();
  66.             CLK=0;
  67.                         _nop_();
  68.             CLK=1;
  69.                         _nop_();
  70.          }
  71.     }
  72. }
  73. //**************向MAX7219写入字(16位)***************************** //
  74. void WriteWord (uchar addr,uchar num)
  75. {
  76.     LOAD=0;
  77.     _nop_();
  78.     SendChar(addr);
  79.     _nop_();
  80.     SendChar(num);
  81.     _nop_();
  82.     LOAD=1;                            // 锁存进相应寄存器
  83. }
  84. void InitDisplay(void)
  85. {
  86.     WriteWord (ScanLimit,ScanDigit);         // 设置扫描界限
  87.     WriteWord (DecodeMode,DecodeDigit);      // 设置译码模式
  88.     WriteWord (Intensity,IntensityGrade);    // 设置亮度
  89.     WriteWord (ShutDown,NormalOperation);    // 设置为正常工作模式

  90. }
  91. void main(void)
  92. {
  93.         InitDisplay (); // MAX7219初始化
  94.     //WriteWord(DisplayTest,TestMode);  // 开始显示测试,点亮所有LED
  95.     delay(15);                      // 延时约1.5s
  96.     WriteWord (DisplayTest,TextEnd);  // 退出显示测试模式
  97.     WriteWord (Digit0,4);
  98.     WriteWord (Digit1,1);
  99.     WriteWord (Digit2,2);
  100.     WriteWord (Digit3,3);       
  101.         while(1)
  102.         {
  103.                  InitDisplay (); // MAX7219初始化
  104. //   
  105.             delay(15);                      // 延时约1.5s
  106.             WriteWord (DisplayTest,TextEnd);  // 退出显示测试模式
  107.             WriteWord (Digit0,0);
  108.             WriteWord (Digit1,1);
  109.             WriteWord (Digit2,2);
  110.             WriteWord (Digit3,3);
  111.         }
  112. }
效果图:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

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

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部