https://static.assets-stash.eet-china.com/album/old-resources/2009/3/11/eb122c0a-d697-4791-a526-6c2fce735b4e.zip" target=_blank>unsigned char code dat3[] = {1,3,1,4,5,2,0,8};
/*--------------------------------------------------------------*/
//MAX7221写入8位数据(内部函数)
void Write8_MAX7221(unsigned char dat)
{
unsigned char i;
for(i = 8; i > 0; i--)
{
MAX7221_CLK = 0;
MAX7221_DIN = (bit)(dat & 0x80);
MAX7221_CLK = 1;
dat <<= 1;
}
}
/*--------------------------------------------------------------*/
//延时1000000+1us 函数定义
void delay1000ms(void)
{
unsigned char i,j,k;
for(i=167;i>0;i--)
for(j=171;j>0;j--)
for(k=16;k>0;k--);
}
/*--------------------------------------------------------------*/
//MAX7221在指定寄存器地址写入数据
void Write_MAX7221(unsigned char add, unsigned char dat)
{
MAX7221_CS = 0;
Write8_MAX7221(add); //写入寄存器地址
Write8_MAX7221(dat); //写入设置参数
MAX7221_CS = 1;
}
/*--------------------------------------------------------------*/
//MAX7221初始化
void Initial_MAX7221(void)
{
Write_MAX7221(Decode_Mode, Decode_All); //BCD译码方式,8位译码
Write_MAX7221(Intensity, 0x0f); //16级亮度调节,最亮
Write_MAX7221(Scan_Limit, 0x07); //扫描位数,8位扫描
Write_MAX7221(Show_Power, Show_On); //显示开关,开显示
Write_MAX7221(Show_Test, Test_Off); //显示测试,关
}
/*--------------------------------------------------------------*/
//定位显示
//传递参数
//pos: 位置,从低位到高位依次为1-8
//dat: 数字,数字0-9
//dot: 小数点,0不带,1加小数点
//图形模式不用管扫描位数就可以消除多余的0 ^_^
void DisplayP_MAX7221(unsigned char pos, unsigned char dat, bit dot)
{
Write_MAX7221(Decode_Mode, Decode_None);//不译码
Write_MAX7221(Scan_Limit, 0x07); //扫描位数,8位扫描
if(dot) dat += 128; //加小数点
Write_MAX7221(pos, seg[dat]);
}
/*--------------------------------------------------------------*/
//定位显示
//传递参数
//pos: 位置,从低位到高位依次为1-8
//dat: 数字,数字0-99
//图形模式不用管扫描位数就可以消除多余的0 ^_^
void DisplayPD_MAX7221(unsigned char pos, unsigned char dat)
{
unsigned char x,y;
x = dat/10;
y = dat%10;
Write_MAX7221(Decode_Mode, Decode_None);//不译码
Write_MAX7221(Scan_Limit, 0x07); //扫描位数,8位扫描
Write_MAX7221(pos, seg[x]);
Write_MAX7221(pos-1,seg[y]);
}
/*--------------------------------------------------------------*/
//MAX7221显示,字符串方式
/*void DisplayS_MAX7221(unsigned char *dat)
{
unsigned char i, k="0", *p = dat; //变量初始化很重要!!!
while(*p) {p++; k++;} //求出字符串长度
Write_MAX7221(Scan_Limit, k-1); //扫描位数k-1
for(i = 0; i < k ; i++)
{ //先写最后一位
Write_MAX7221(i+1, *(dat+k-1-i)); //由于48 = 32 + 16 分别为D5、D4
//而MAX7221BCD方式显示时只与低四位有关
} //与高四位无关,故*dat可不必减去48
}
*/
/*--------------------------------------------------------------*/
//MAX7221显示,字符串方式,或数组方式,需要手动算出大小
void DisplayAS_MAX7221(unsigned char *dat,unsigned char n)
{
unsigned char i;
Write_MAX7221(Scan_Limit, n-1); //扫描位数n
for(i = 0; i < n ; i++)
{ //先写最后一位
Write_MAX7221(i+1, *(dat+n-1-i)); //由于48 = 32 + 16 分别为D5、D4
//而MAX7221BCD方式显示时只与低四位有关
} //与高四位无关,故*dat可不必减去48
}
/*--------------------------------------------------------------*/
//MAX7221显示,数组方式,或字符串方式,8位固定显示
void DisplayAS8_MAX7221(unsigned char *dat)
{
unsigned char i;
Write_MAX7221(Scan_Limit, 7); //扫描位数8
for(i = 0; i < 8; i++)
{
Write_MAX7221(i+1, *(dat+7-i));
}
}
/*--------------------------------------------------------------*/
//MAX7221显示,自己定义图形,数组方式
/*void DisplayG_MAX7221(unsigned char *dat)
{
unsigned char i;
Write_MAX7221(Decode_Mode, Decode_None);//不译码
Write_MAX7221(Scan_Limit, 7); //扫描位数8
for(i = 0; i < 8; i++)
{
Write_MAX7221(i+1, *(dat+7-i));
}
}
/*--------------------------------------------------------------*/
//MAX7221显示,数组方式,图形模式不用管扫描位数就可以消除多余的0 ^_^
void DisplayG_MAX7221(unsigned char *dat)
{
unsigned char i;
Write_MAX7221(Decode_Mode, Decode_None);//不译码
Write_MAX7221(Scan_Limit, 7); //扫描位数8
for(i = 0; i < 8; i++)
{
Write_MAX7221(i+1, seg[dat]);
}
}
/*--------------------------------------------------------------*/
//MAX7221显示,数字方式16位,最大为65535
void DisplayD_MAX7221(unsigned int dat)
{
unsigned char i="0"; //变量初始化很重要!!!
unsigned char k[5];
for(i = 0; i < 5; i++) k=0;
i = 0;
while(dat / 10)
{
k[4-i] = dat%10 + 48; //转化为字符
dat /= 10;
i++;
}
k[4-i] = dat+48;
DisplayAS_MAX7221(k,5);
}
/*--------------------------------------------------------------*/
//显示测试
void Display_Test(void)
{
Write_MAX7221(Show_Test, Test_On); //显示测试,开
delay1000ms();
Write_MAX7221(Show_Test, Test_Off); //显示测试,关
}
/*--------------------------------------------------------------*/
//清数据寄存器
void ClearRam_MAX7221(unsigned char n)
{
unsigned char i;
for(i = n; i > 0; i--)
Write_MAX7221(i, 0);
}
/*--------------------------------------------------------------*/
#endif
//演示程序
/*--------------------------------------------------------------*/
//File: MAX7221基本应用
//Date: 09-3-3
//Time: 14:43
/*--------------------------------------------------------------*/
//包含头文件
#include <REG52.H>
#include "MAX7221.H"
/*--------------------------------------------------------------*/
//演示主函数
void main(void)
{
unsigned char t[8],i,j;
Initial_MAX7221();
Display_Test();
DisplayD_MAX7221(123); //数字,最大65535
delay1000ms();
DisplayAS8_MAX7221(dat1); //数字数组
delay1000ms();
DisplayAS8_MAX7221(dat2); //字符串数组
delay1000ms();
DisplayG_MAX7221(dat3);
delay1000ms();
ClearRam_MAX7221(8);
delay1000ms();
for(i = 8; i > 0; i--)
{
for(j = 0; j <= i; j++)
{
DisplayP_MAX7221(i,j,0);
delay1000ms();
}
}
for(i = 8; i > 0; i--)
{
for(j = 1; j <=i ; j++)
{
DisplayP_MAX7221(j,i,0);
delay1000ms();
}
}
for(i = 8; i > 0; i--)
{
for(j = 1; j <=i ; j++)
{
ClearRam_MAX7221(i);
DisplayP_MAX7221(j,i,0);
delay1000ms();
}
}
Write_MAX7221(Decode_Mode, Decode_All);//译码
while(t[0]<15)
{
for(i = 0; i < 8; i++) t++;
DisplayAS8_MAX7221(t);
delay1000ms();
}
DisplayAS8_MAX7221("89272965"); //字符串、数组,固定8个
delay1000ms();
DisplayAS_MAX7221("89272965",4);//字符串、数组,手动个数
delay1000ms();
ClearRam_MAX7221(8);
delay1000ms();
loop:
for(i = 0; i <= 60; i++)
{
DisplayPD_MAX7221(5,i);
delay1000ms();
}
for(i = 59; i > 0; i--)
{
DisplayPD_MAX7221(5,i);
delay1000ms();
}
goto loop;
}
/*--------------------------------------------------------------*/
文章评论(0条评论)
登录后参与讨论