[活动]

EFM8BB1测评——MAX7219

[复制链接]
590|0
手机看帖
扫描二维码
随时随地手机跟帖
Cjy_JDxy|  楼主 | 2019-11-30 22:48 | 显示全部楼层 |阅读模式
今天搞了一下MAX7219。
代码:
#include <SI_EFM8SB2_Register_Enums.h>                // SFR declarations
#include "InitDevice.h"


SI_SBIT(LED, SFR_P1, 4);   // LED
SI_SBIT(S1, SFR_P0, 2);                                   // BTN0
SI_SBIT(CLK, SFR_P0, 5);   // CLK
SI_SBIT(LOAD, SFR_P0, 4);   // LOAD
SI_SBIT(DIN, SFR_P0, 3);   // DIN


#define uchar unsigned char
#define uint  unsigned int

#define SYSCLK      24500000/8         // Clock speed in Hz (default)

#define SW_PRESSED           0
#define SW_NOT_PRESSED       1

#define LED_ON               0
#define LED_OFF              1

#define CLK_0           0
#define CLK_1           1
#define LOAD_0           0
#define LOAD_1           1
#define DIN_0           0
#define DIN_1           1

#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};    // 显示缓存区
unsigned int j;

//******************延时t毫秒**************************************
void delay(uint t)
{
       uint i;
       while(t--)
       {
              /* 对于12M时钟,约延时1ms */
              for (i=0;i<125;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=DIN_1;
            //for(j=0;j<30;j++);
            CLK=CLK_0;
            //for(j=0;j<30;j++);
            CLK=CLK_1;
            //for(j=0;j<30;j++);
         }
         else
         {
            DIN=DIN_0;
            //for(j=0;j<30;j++);
            CLK=CLK_0;
            //for(j=0;j<30;j++);
            CLK=CLK_1;
            //for(j=0;j<30;j++);
         }
    }
}
//**************向MAX7219写入字(16位)*****************************
void WriteWord (uchar addr,uchar num)
{
    LOAD=LOAD_0;
    //delay(1);
    SendChar(addr);
    //delay(1);
    SendChar(num);
    //delay(1);
    LOAD=LOAD_1;                            // 锁存进相应寄存器
}
//*********************** MAX7219初始化 ******************
void InitDisplay (void)
{
    WriteWord (ScanLimit,ScanDigit);         // 设置扫描界限
    WriteWord (DecodeMode,DecodeDigit);      // 设置译码模式
    WriteWord (Intensity,IntensityGrade);    // 设置亮度
    WriteWord (ShutDown,NormalOperation);    // 设置为正常工作模式

}



//-----------------------------------------------------------------------------
// SiLabs_Startup() Routine
// ----------------------------------------------------------------------------
// This function is called immediately after reset, before the initialization
// code is run in SILABS_STARTUP.A51 (which runs before main() ). This is a
// useful place to disable the watchdog timer, which is enable by default
// and may trigger before main() in some instances.
//-----------------------------------------------------------------------------
void SiLabs_Startup (void)
{
  // Disable the watchdog here
}

//-----------------------------------------------------------------------------
// main() Routine
// ----------------------------------------------------------------------------
// Note: the software watchdog timer is not disabled by default in this
// example, so a long-running program will reset periodically unless
// the timer is disabled or your program periodically writes to it.
//
// Review the "Watchdog Timer" section under the part family's datasheet
// for details. To find the datasheet, select your part in the
// Simplicity Launcher and click on "Data Sheet".
//-----------------------------------------------------------------------------
int main (void)
{
        uint i;
        uchar k=0;
        enter_DefaultMode_from_RESET();
        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);
        while (1)
        {
                LED = LED_OFF;
                DIN=DIN_1;
                for(i=0;i<60000;i++);
                LED = LED_ON;
                DIN=DIN_0;
                for(i=0;i<60000;i++);
                InitDisplay (); // MAX7219初始化
                WriteWord (Digit0,0);
                WriteWord (Digit1,1);
                WriteWord (Digit2,2);
                WriteWord (Digit3,k);
                k++;
                if(k>=10)
                        k=0;
        }                             // Spin forever
}

效果图:
7.jpg

9.jpg 照片拍的不太好。


使用特权

评论回复

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:绿水本无忧因风皱面,青山原不老为雪白头。

553

主题

3520

帖子

19

粉丝