MAX7219芯片简单驱动

MAX7219.h

#ifndef _MAX7219_H_
#define _MAX7219_H_
void Init_MAX7219(unsigned char Light_Coding,unsigned char SEG_Coding_Mode,unsigned char SEG_DIG_Num);  //使用前调用我设置MAX7219
void Write_SEG(unsigned char Data,unsigned char DIG);
#endif

MAX7219.c

/*本驱动文件依赖reg51.h*/
#include

sbit DIN = P1^0;       //根据实际DIN引脚连接修改
sbit LOAD = P1^1;      //根据实际LOAD引脚连接修改
sbit CLK = P1^2;       //根据实际CLK引脚连接修改

void Delay_10_us(unsigned char Us)   //延时函数(内部函数不可引用)
{
    unsigned char a;
    for(Us=1;Us>0;Us--)
        for(a=2;a>0;a--);
}

void Write_MAX7219(unsigned char Addr,unsigned char Data)//地址和数据写入(内部函数不可引用)
{
    /*地址和数据均为两位16进制数,如0xFF*/
    unsigned char Count;    //计数变量
    LOAD = 0;   //LOAD引脚拉低电平开始写入数据
    for(Count = 0;Count < 8;Count++)        //写地址
    {
        CLK = 0;
        Addr <<= 1;
        DIN = CY;       //通过移位方式将一位数据移至单片机状态字寄存器的CY位,然后送出
        CLK = 1;
        Delay_10_us(1);
        CLK = 0;
    }
    for(Count = 0;Count < 8;Count++)        //写数据
    {
        CLK = 0;
        Data <<= 1;
        DIN = CY;       //通过移位方式将一位数据移至单片机状态字寄存器的CY位,然后送出
        CLK = 1;
        Delay_10_us(1);
        CLK = 0;
    }
    LOAD = 1;   //LOAD引脚拉高电平锁定数据
}

void Init_MAX7219(unsigned char Light_Coding,unsigned char SEG_Coding_Mode,unsigned char SEG_DIG_Num)   //使用前调用我设置MAX7219
{
    /*亮度(0x00~0x0F),编码模式(0x00,0x01,0x0F,0xFF),数码扫描管个数(0x00~0x07)*/
    Write_MAX7219(0x09,SEG_Coding_Mode);
    Write_MAX7219(0x0A,Light_Coding);
    Write_MAX7219(0x0B,SEG_DIG_Num);
    Write_MAX7219(0x0C,0x01);       //记得设置MAX7219的关闭模式为1
}

void Write_SEG(unsigned char Data,unsigned char DIG)
{
    /*使用此函数编码模式应设置为0xFF*/
    /*Data:要送入数码管的数字或字符,应为字符型,DIG:对应的数码管接的DIG脚编号,为0~7,整型*/
    /*在Data参数输入'*'将会什么都不显示*/
    unsigned char DIG_Buff[8] = {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};

    switch(Data)
    {
        case '0' : Write_MAX7219(DIG_Buff[DIG],0x00);break;
        case '1' : Write_MAX7219(DIG_Buff[DIG],0x01);break;
        case '2' : Write_MAX7219(DIG_Buff[DIG],0x02);break;
        case '3' : Write_MAX7219(DIG_Buff[DIG],0x03);break;
        case '4' : Write_MAX7219(DIG_Buff[DIG],0x04);break;
        case '5' : Write_MAX7219(DIG_Buff[DIG],0x05);break;
        case '6' : Write_MAX7219(DIG_Buff[DIG],0x06);break;
        case '7' : Write_MAX7219(DIG_Buff[DIG],0x07);break;
        case '8' : Write_MAX7219(DIG_Buff[DIG],0x08);break;
        case '9' : Write_MAX7219(DIG_Buff[DIG],0x09);break;
        case '-' : Write_MAX7219(DIG_Buff[DIG],0x0A);break;
        case 'E' : Write_MAX7219(DIG_Buff[DIG],0x0B);break;
        case 'H' : Write_MAX7219(DIG_Buff[DIG],0x0C);break;
        case 'L' : Write_MAX7219(DIG_Buff[DIG],0x0D);break;
        case 'P' : Write_MAX7219(DIG_Buff[DIG],0x0E);break;
        case '*' : Write_MAX7219(DIG_Buff[DIG],0x0F);break;
        default : break;
    }
}

最后是测试程序

(驱动测试)显示PZ-122345.c

#include
#include"MAX7219.h"

void main()
{
    unsigned char Data[8] = {'P','2','-','1','2','3','4','5'};
    unsigned char Count;
    Init_MAX7219(0x0F,0xFF,0x07);
    for(Count = 0;Count<8;Count++)
    {
        Write_SEG(Data[Count],Count);
    }
    while(1);
}

测试模拟电路
MAX7219芯片简单驱动_第1张图片

你可能感兴趣的:(单片机C语言)