⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 max_7219.h

📁 51 单片机驱动max7219 多位数码管显示程序
💻 H
字号:
#include <reg51.h>

/*macro define*/
#define HIGH     1
#define LOW      0
#define TRUE     1
#define FALSE    0
#define ZERO     0 
#define MSB      0x80
#define LSB      0x01

/*mode define*/
#define NO_OP         0x00 
#define DECODE_MODE   0x09 
#define INTENSITY     0x0A 
#define SCAN_LIMIT    0x0B 
#define SHUTDOWN      0x0C 
#define DISPLAY_TEST  0x0F

/*pin define*/
sbit DIN=P1^2; //MAX7219    Serial-Data Input:   rising edge  pin 1
sbit LOAD=P1^1; //MAX7219    Load-Data Input:    rising edge  pin 12
sbit CLK=P1^0; //MAX7219   Serial-Clock Input:  maximum 10MHz  pin 13
unsigned char code seg_code[] = {0x7e, 0x30, 0x6d, 0x79, 0x33, 0x5b, 0x5f,0x70,0x7F, 0x7b,0x77,0x1f,0x4e,0x3d,0x4f,0x47,0x00,0x80};	// 0--f,消隐,点


/*funtion define*/
void Init_Max7219(void);//initialize max7219
void Write_Max7219_byte(unsigned char temp);//send max7219 a byte
void Write_Max7219(unsigned char address,unsigned char dat);// send max7219 command and data(0至F)
void Write_Max7219_0(unsigned char address,unsigned char dat,unsigned char point);//address为数码管号 dat为要显示数据  point(0为有点)
//void Show_Max7219_0(unsigned int left,unsigned int right);    // 右左分别显示四位整数 0至9999(小数舍掉)满10000后模10000再输出
void Byte_Max7219_0(unsigned char byte_data);                 // 左边输出0到255,满255后模256再输出
//void Regprint_Max7219_0(unsigned char byte_data);             // 十进制转换为二进制输出,满255后模256再输出
void Show_Max7219_R(float byte_data);                         // 右边直接输出四位实数
/************************************************************************************
       code of funtions
************************************************************************************/

void Init_Max7219(void)      
{ 
 Write_Max7219(SHUTDOWN, 0x01);//Normal Operation
 Write_Max7219(DISPLAY_TEST, 0x00);//Normal Operation
 Write_Max7219(DECODE_MODE, 0x00);//Code B decode for digits 7-0
 Write_Max7219(SCAN_LIMIT, 0x07);//Display digits 7-0
 Write_Max7219(INTENSITY, 0x01);//Duty Cycle 9/32
}

void Write_Max7219_byte(unsigned char temp)
{
 unsigned char i;
 for (i=0;i<8;i++)     
  { 
     //P4=0xF0;//CLK输出
     CLK=LOW;
     DIN=(bit)(temp&MSB);      
     temp<<=1;
     //P4=0xF8;//CLK输出
     CLK=HIGH;
  }
}

void Write_Max7219(unsigned char address,unsigned char dat)
{ 
  LOAD=LOW;
  Write_Max7219_byte((address+3)%8+1); 
  Write_Max7219_byte(dat);
  LOAD=HIGH;                 
}

void Write_Max7219_0(unsigned char address,unsigned char dat,unsigned char point)
{  
   //Write_Max7219(address,seg_code[16]);
   if(point)
	Write_Max7219(address,seg_code[dat]);
	else
    Write_Max7219(address,seg_code[dat]|0x80);
}



void Show_Max7219_R(float byte_data)
{
  long i;
  unsigned char j;
 i=(long)(byte_data*1000+0.5f);
  if(i/10000)    //十位不为零
 {
   Write_Max7219_0(1,(i/10000)%10,1);
   Write_Max7219_0(2,(i/1000)%10,0);
   Write_Max7219_0(3,(i/100)%10,1);
   Write_Max7219_0(4,(i/10)%10,1);
   for(j=5;j<9;j++)
   Write_Max7219(j,0x00);
 }
  else if(i/1000)      //个位不为零
 {
   Write_Max7219_0(1,(i/1000)%10,0);
   Write_Max7219_0(2,(i/100)%10,1);
   Write_Max7219_0(3,(i/10)%10,1);
   Write_Max7219_0(4,i%10,1);
   for(j=5;j<9;j++)
   Write_Max7219(j,0x00);
 }
   else if(i/100)  //十分位不为零
 {
   Write_Max7219_0(1,0,0);
   Write_Max7219_0(2,(i/100)%10,1);
   Write_Max7219_0(3,(i/10)%10,1);
   Write_Max7219_0(4,i%10,1);
   for(j=5;j<9;j++)
   Write_Max7219(j,0x00);
 }
    else if(i/10) //百分位不为零
 {
   Write_Max7219_0(1,0,0);
   Write_Max7219_0(2,0,1);
   Write_Max7219_0(3,(i/10)%10,1);
   Write_Max7219_0(4,i%10,1);
   for(j=5;j<9;j++)
   Write_Max7219(j,0x00);
 }
     else if(i%10) //千分位不为零
 {
   Write_Max7219_0(1,0,0);
   Write_Max7219_0(2,0,1);
   Write_Max7219_0(3,0,1);
   Write_Max7219_0(4,i%10,1);
   for(j=5;j<9;j++)
   Write_Max7219(j,0x00);
 }
}



/*
void Byte_Max7219_0(unsigned char byte_data)
{
    unsigned char i,digit;

    for(i=8;i>0;i--)
    {
        digit=byte_data%10;
        Write_Max7219_0(i,digit);
        byte_data/=10;
    }
}

void Show_Max7219_0(unsigned int left,unsigned int right)
{
   unsigned char  i,digit_l,digit_r;	   
   for(i=8;i>4;i--)
   {
       digit_l=left%10;
       Write_Max7219_0(i-4,digit_l);
       left/=10;
    	 
       digit_r=right%10;
       Write_Max7219_0(i,digit_r);
       right/=10;
    }
}


void Regprint_Max7219_0(unsigned char byte_data)
{
    unsigned char i,reg_bit,test_bit=0x01;

    for(i=8;i>0;i--)
    {
        reg_bit=byte_data&test_bit;
        Write_Max7219_0(i,reg_bit);
        byte_data=byte_data>>1;
    }
}
*/



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -