[活动专区] 【AT-START-F407测评】+ MAX7219

[复制链接]
 楼主| 比神乐 发表于 2021-1-27 16:40 | 显示全部楼层 |阅读模式
本帖最后由 比神乐 于 2021-1-27 17:29 编辑

今天捣鼓了一下MAX7219
MAX7219是数码管驱动芯片,模块是我自己做的。
硬件接线:
DIN:PD0
LOAD:PD2
CLK:PD4
代码:
  1. #include <stdio.h>
  2. #include "at32f4xx.h"
  3. #include "at32_board.h"

  4. /** @addtogroup AT32F407_StdPeriph_Examples
  5.   * @{
  6.   */

  7. /** @addtogroup GPIO_LEDToggle
  8.   * @{
  9.   */
  10. #define uchar unsigned char
  11. #define uint unsigned int
  12. /* Private typedef -----------------------------------------------------------*/
  13. /* Private define ------------------------------------------------------------*/
  14. /* Private macro -------------------------------------------------------------*/
  15. /* Private variables ---------------------------------------------------------*/
  16. /* Private function prototypes -----------------------------------------------*/
  17. /* Private functions ---------------------------------------------------------*/







  18. #define CLK_0           CLK_GPIO->BRE = CLK_PIN
  19. #define CLK_1           CLK_GPIO->BSRE = CLK_PIN
  20. #define LOAD_0          LOAD_GPIO->BRE = LOAD_PIN
  21. #define LOAD_1          LOAD_GPIO->BSRE = LOAD_PIN
  22. #define DIN_0           DIN_GPIO->BRE = DIN_PIN
  23. #define DIN_1           DIN_GPIO->BSRE = DIN_PIN

  24. #define LED4_0          LED4_GPIO->BRE = LED4_PIN
  25. #define LED4_1          LED4_GPIO->BSRE = LED4_PIN
  26. #define LED2_0          LED2_GPIO->BRE = LED2_PIN
  27. #define LED2_1          LED2_GPIO->BSRE = LED2_PIN
  28. #define LED3_0          LED3_GPIO->BRE = LED3_PIN
  29. #define LED3_1          LED3_GPIO->BSRE = LED3_PIN

  30. #define NoOp   0x00           //空操作寄存器
  31. #define Digit0 0x01           // 数码管1寄存器
  32. #define Digit1 0x02           // 数码管2寄存器
  33. #define Digit2 0x03           // 数码管3寄存器
  34. #define Digit3 0x04           // 数码管4寄存器
  35. #define Digit4 0x05           // 数码管5寄存器
  36. #define Digit5 0x06           // 数码管6寄存器
  37. #define Digit6 0x07           // 数码管7寄存器
  38. #define Digit7 0x08           // 数码管8寄存器

  39. #define DecodeMode 0x09       // 译码模式寄存器
  40. #define Intensity 0x0a        // 亮度寄存器
  41. #define ScanLimit 0x0b        // 扫描位数寄存器
  42. #define ShutDown 0x0c         // 低功耗模式寄存器

  43. #define DisplayTest 0x0f      // 显示测试寄存器
  44. #define ShutdownMode 0x00     // 低功耗方式
  45. #define NormalOperation 0x01  // 正常操作方式
  46. #define ScanDigit 0x07        // 扫描位数设置,显示8位数码管
  47. #define DecodeDigit 0xff      // 译码设置,8位均为BCD码
  48. #define IntensityGrade 0x0a   // 亮度级别设置
  49. #define TestMode 0x01         // 显示测试模式
  50. #define TextEnd 0x00          // 显示测试结束,恢复正常工作模式

  51. /*****************************************************************************
  52. * Function implementation - global ('extern') and local ('static')
  53. ******************************************************************************/
  54. uchar DisBuffer[8]={0,0,0,0,0,0,0,0};    // 显示缓存区
  55. //******************延时t毫秒**************************************
  56. void delay(uint t)
  57. {
  58.        uint i;
  59.        while(t--)
  60.        {
  61.               /* 对于12M时钟,约延时1ms */
  62.               for (i=0;i<3625;i++)
  63.               {}
  64.        }
  65. }
  66. //*************向MAX7219写入字节(8位)********************
  67. void SendChar (uchar ch)
  68. {
  69.     uchar i,temp;
  70.     delay(1);
  71.     for (i=0;i<8;i++)
  72.     {
  73.          temp=ch&0x80;
  74.          ch=ch<<1;
  75.          if(temp)
  76.          {
  77.             DIN_1;
  78.                                                 delay(1);
  79.             CLK_0;
  80.                                                 delay(1);
  81.             CLK_1;
  82.                                                 delay(1);
  83.          }
  84.          else
  85.          {
  86.             DIN_0;
  87.                                                 delay(1);
  88.             CLK_0;
  89.                                                 delay(1);
  90.             CLK_1;
  91.                                                 delay(1);
  92.          }
  93.     }
  94. }
  95. //**************向MAX7219写入字(16位)*****************************
  96. void WriteWord (uchar addr,uchar num)
  97. {
  98.     LOAD_0;
  99.     delay(1);
  100.     SendChar(addr);
  101.     delay(1);
  102.     SendChar(num);
  103.     delay(1);
  104.     LOAD_1;                            // 锁存进相应寄存器
  105. }
  106. //*********************** MAX7219初始化 ******************
  107. void InitDisplay (void)
  108. {
  109.     WriteWord (ScanLimit,ScanDigit);         // 设置扫描界限
  110.     WriteWord (DecodeMode,DecodeDigit);      // 设置译码模式
  111.     WriteWord (Intensity,IntensityGrade);    // 设置亮度
  112.     WriteWord (ShutDown,NormalOperation);    // 设置为正常工作模式

  113. }


  114. void AT32_MAX7219_Init(void)
  115. {
  116.   GPIO_InitType GPIO_InitStructure;
  117.   /*Enable the LED Clock*/

  118.   RCC_APB2PeriphClockCmd(CLK_GPIO_RCC_CLK|LOAD_GPIO_RCC_CLK|DIN_GPIO_RCC_CLK, ENABLE);


  119.   /*Configure the LED pin as ouput push-pull*/
  120.   GPIO_StructInit(&GPIO_InitStructure);
  121.   GPIO_InitStructure.GPIO_Pins = CLK_PIN;                                 

  122.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;

  123.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;        
  124.   GPIO_Init(CLK_GPIO, &GPIO_InitStructure);
  125.         
  126.         GPIO_InitStructure.GPIO_Pins = LOAD_PIN;                                 

  127.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;

  128.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;        
  129.   GPIO_Init(LOAD_GPIO, &GPIO_InitStructure);
  130.         
  131.         GPIO_InitStructure.GPIO_Pins = DIN_PIN;                                 

  132.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;

  133.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;        
  134.   GPIO_Init(DIN_GPIO, &GPIO_InitStructure);
  135. }
  136. void AT32_LED_Init(void)
  137. {
  138.   GPIO_InitType GPIO_InitStructure;
  139.   /*Enable the LED Clock*/

  140.   RCC_APB2PeriphClockCmd(LED2_GPIO_RCC_CLK|LED3_GPIO_RCC_CLK|LED4_GPIO_RCC_CLK, ENABLE);


  141.   /*Configure the LED pin as ouput push-pull*/
  142.   GPIO_StructInit(&GPIO_InitStructure);
  143.   GPIO_InitStructure.GPIO_Pins = LED2_PIN;                                 

  144.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;

  145.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;        
  146.   GPIO_Init(LED2_GPIO, &GPIO_InitStructure);
  147.         
  148.         GPIO_InitStructure.GPIO_Pins = LED3_PIN;                                 

  149.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;

  150.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;        
  151.   GPIO_Init(LED3_GPIO, &GPIO_InitStructure);
  152.         
  153.         GPIO_InitStructure.GPIO_Pins = LED4_PIN;                                 

  154.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;

  155.   GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;        
  156.   GPIO_Init(LED4_GPIO, &GPIO_InitStructure);
  157. }
  158. /**
  159.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Main Function.
  160.   * @param  None
  161.   * @retval None
  162.   */
  163. int main(void)
  164. {
  165.   //AT32_Board_Init();
  166.         Delay_init();
  167.         AT32_LED_Init();
  168.         AT32_MAX7219_Init();
  169.         InitDisplay (); // MAX7219初始化
  170.         WriteWord(DisplayTest,TestMode);  // 开始显示测试,点亮所有LED
  171.         delay(1500);                      // 延时约1.5s
  172.         WriteWord (DisplayTest,TextEnd);  // 退出显示测试模式
  173.         WriteWord (Digit0,0);
  174.         WriteWord (Digit1,1);
  175.         WriteWord (Digit2,2);
  176.         WriteWord (Digit3,3);
  177.   for(;;)
  178.   {
  179. //    AT32_LEDn_Toggle(LED2);
  180. //    Delay_ms(1000);
  181.     LED2_0;
  182.                 LED3_0;
  183.                 LED4_0;
  184.                 Delay_ms(1000);
  185.                 LED2_1;
  186.                 LED3_1;
  187.                 LED4_1;
  188.                 Delay_ms(1000);
  189.   }
  190. }
效果图:
1.jpg
MAX7219模块原理图
4.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3541

帖子

7

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