本帖最后由 比神乐 于 2021-1-27 17:29 编辑
今天捣鼓了一下MAX7219
MAX7219是数码管驱动芯片,模块是我自己做的。
硬件接线:
DIN:PD0
LOAD:PD2
CLK:PD4
代码:
- #include <stdio.h>
- #include "at32f4xx.h"
- #include "at32_board.h"
- /** @addtogroup AT32F407_StdPeriph_Examples
- * @{
- */
- /** @addtogroup GPIO_LEDToggle
- * @{
- */
- #define uchar unsigned char
- #define uint unsigned int
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- #define CLK_0 CLK_GPIO->BRE = CLK_PIN
- #define CLK_1 CLK_GPIO->BSRE = CLK_PIN
- #define LOAD_0 LOAD_GPIO->BRE = LOAD_PIN
- #define LOAD_1 LOAD_GPIO->BSRE = LOAD_PIN
- #define DIN_0 DIN_GPIO->BRE = DIN_PIN
- #define DIN_1 DIN_GPIO->BSRE = DIN_PIN
- #define LED4_0 LED4_GPIO->BRE = LED4_PIN
- #define LED4_1 LED4_GPIO->BSRE = LED4_PIN
- #define LED2_0 LED2_GPIO->BRE = LED2_PIN
- #define LED2_1 LED2_GPIO->BSRE = LED2_PIN
- #define LED3_0 LED3_GPIO->BRE = LED3_PIN
- #define LED3_1 LED3_GPIO->BSRE = LED3_PIN
- #define NoOp 0x00 //空操作寄存器
- #define Digit0 0x01 // 数码管1寄存器
- #define Digit1 0x02 // 数码管2寄存器
- #define Digit2 0x03 // 数码管3寄存器
- #define Digit3 0x04 // 数码管4寄存器
- #define Digit4 0x05 // 数码管5寄存器
- #define Digit5 0x06 // 数码管6寄存器
- #define Digit6 0x07 // 数码管7寄存器
- #define Digit7 0x08 // 数码管8寄存器
- #define DecodeMode 0x09 // 译码模式寄存器
- #define Intensity 0x0a // 亮度寄存器
- #define ScanLimit 0x0b // 扫描位数寄存器
- #define ShutDown 0x0c // 低功耗模式寄存器
- #define DisplayTest 0x0f // 显示测试寄存器
- #define ShutdownMode 0x00 // 低功耗方式
- #define NormalOperation 0x01 // 正常操作方式
- #define ScanDigit 0x07 // 扫描位数设置,显示8位数码管
- #define DecodeDigit 0xff // 译码设置,8位均为BCD码
- #define IntensityGrade 0x0a // 亮度级别设置
- #define TestMode 0x01 // 显示测试模式
- #define TextEnd 0x00 // 显示测试结束,恢复正常工作模式
- /*****************************************************************************
- * Function implementation - global ('extern') and local ('static')
- ******************************************************************************/
- uchar DisBuffer[8]={0,0,0,0,0,0,0,0}; // 显示缓存区
- //******************延时t毫秒**************************************
- void delay(uint t)
- {
- uint i;
- while(t--)
- {
- /* 对于12M时钟,约延时1ms */
- for (i=0;i<3625;i++)
- {}
- }
- }
- //*************向MAX7219写入字节(8位)********************
- void SendChar (uchar ch)
- {
- uchar i,temp;
- delay(1);
- for (i=0;i<8;i++)
- {
- temp=ch&0x80;
- ch=ch<<1;
- if(temp)
- {
- DIN_1;
- delay(1);
- CLK_0;
- delay(1);
- CLK_1;
- delay(1);
- }
- else
- {
- DIN_0;
- delay(1);
- CLK_0;
- delay(1);
- CLK_1;
- delay(1);
- }
- }
- }
- //**************向MAX7219写入字(16位)*****************************
- void WriteWord (uchar addr,uchar num)
- {
- LOAD_0;
- delay(1);
- SendChar(addr);
- delay(1);
- SendChar(num);
- delay(1);
- LOAD_1; // 锁存进相应寄存器
- }
- //*********************** MAX7219初始化 ******************
- void InitDisplay (void)
- {
- WriteWord (ScanLimit,ScanDigit); // 设置扫描界限
- WriteWord (DecodeMode,DecodeDigit); // 设置译码模式
- WriteWord (Intensity,IntensityGrade); // 设置亮度
- WriteWord (ShutDown,NormalOperation); // 设置为正常工作模式
- }
- void AT32_MAX7219_Init(void)
- {
- GPIO_InitType GPIO_InitStructure;
- /*Enable the LED Clock*/
- RCC_APB2PeriphClockCmd(CLK_GPIO_RCC_CLK|LOAD_GPIO_RCC_CLK|DIN_GPIO_RCC_CLK, ENABLE);
- /*Configure the LED pin as ouput push-pull*/
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pins = CLK_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;
- GPIO_Init(CLK_GPIO, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pins = LOAD_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;
- GPIO_Init(LOAD_GPIO, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pins = DIN_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;
- GPIO_Init(DIN_GPIO, &GPIO_InitStructure);
- }
- void AT32_LED_Init(void)
- {
- GPIO_InitType GPIO_InitStructure;
- /*Enable the LED Clock*/
- RCC_APB2PeriphClockCmd(LED2_GPIO_RCC_CLK|LED3_GPIO_RCC_CLK|LED4_GPIO_RCC_CLK, ENABLE);
- /*Configure the LED pin as ouput push-pull*/
- GPIO_StructInit(&GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pins = LED2_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;
- GPIO_Init(LED2_GPIO, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pins = LED3_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;
- GPIO_Init(LED3_GPIO, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pins = LED4_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT_PP;
- GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_2MHz;
- GPIO_Init(LED4_GPIO, &GPIO_InitStructure);
- }
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Main Function.
- * @param None
- * @retval None
- */
- int main(void)
- {
- //AT32_Board_Init();
- Delay_init();
- AT32_LED_Init();
- AT32_MAX7219_Init();
- InitDisplay (); // MAX7219初始化
- WriteWord(DisplayTest,TestMode); // 开始显示测试,点亮所有LED
- delay(1500); // 延时约1.5s
- WriteWord (DisplayTest,TextEnd); // 退出显示测试模式
- WriteWord (Digit0,0);
- WriteWord (Digit1,1);
- WriteWord (Digit2,2);
- WriteWord (Digit3,3);
- for(;;)
- {
- // AT32_LEDn_Toggle(LED2);
- // Delay_ms(1000);
- LED2_0;
- LED3_0;
- LED4_0;
- Delay_ms(1000);
- LED2_1;
- LED3_1;
- LED4_1;
- Delay_ms(1000);
- }
- }
效果图:
MAX7219模块原理图
|