打算用max7219驱动点阵显示时钟,但是在max7219驱动中出现问题:
问题描述:第一片和第二片显示正常,第三篇出现残影,仔细观察第三片的残影是第一片的数据。
检查过硬件和软件,硬件应该没有问题,就三根数据线,芯片为stc15w408as的IO口直连接动三片max7219级联,max7219驱动部分程序如下,请各位前辈不腻赐教!
单片机源程序如下:
#define _max7219_C
#include "max7219.h"
#include "main.h"
#include "DS3231.h"
#include "DS18B20.h"
#include <intrins.h>
/************************************************************************************************/uchar code disp1[10][8]={
{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C},//0
{0x10,0x18,0x14,0x10,0x10,0x10,0x10,0x10},//1
{0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E},//2
{0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0},//3
{0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x8},//4
{0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C,0x0},//5
{0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C,0x0},//6
{0x3E,0x22,0x4,0x8,0x8,0x8,0x8,0x8},//7
{0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E},//8
{0x3E,0x22,0x22,0x3E,0x2,0x2,0x2,0x3E},//9
};
/*n毫秒的延时程序*/
void Delay_xms(uint x)
{
uint i,j;
for(i=0;i<x;i++)
for(j=0;j<112;j++);
}
//--------------------------------------------
//向MAX7219写入字节
void Write_Max7219_byte(uchar DATA)
{
uchar i;
Max7219_pinCS=0; //CS=0有效,CS=1锁存
for(i=8;i>=1;i--)
{
Max7219_pinCLK=0;
Max7219_pinDIN=DATA&0x80;//&10000000, 编译器对位操作的理解:非0即为1
DATA=DATA<<1;
Max7219_pinCLK=1; //上升沿把数据送出去
}
}
//-------------------------------------------
/*向M第一片MAX7219写入数据*/
void Write_Max7219_1(uchar add1,uchar dat1)
{
Max7219_pinCS=0;
Write_Max7219_byte(add1); //写入地址,即数码管编号
Write_Max7219_byte(dat1); //写入数据,即数码管显示数字
Max7219_pinCS=1;
}
/*第二片MAX7219的写入数据*/
void Write_Max7219_2(uchar add2,uchar dat2)
{
Max7219_pinCS=0;
Write_Max7219_byte(add2);
Write_Max7219_byte(dat2);
Max7219_pinCLK=1;
Write_Max7219_byte(0x00); //片1写入空
Write_Max7219_byte(0x00);
Max7219_pinCS=1;
}
/*第三片MAX7219的写入数据*/
void Write_Max7219_3(uchar add3,uchar dat3)
{
Max7219_pinCS=0;
Write_Max7219_byte(add3);
Write_Max7219_byte(dat3);
Max7219_pinCLK=1;
Write_Max7219_byte(0x00);//片1写入空
Write_Max7219_byte(0x00);
// _nop_();_nop_();
Write_Max7219_byte(0x00); //片2写入空
Write_Max7219_byte(0x00);
// Max7219_pinCLK=1;
Max7219_pinCS=1;
}
/*初始化芯片*/
void Init_MAX7219(void)
{
Write_Max7219_1(0x09, 0x00); //译码方式:BCD码
Write_Max7219_1(0x0a, 0x00); //亮度
Write_Max7219_1(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219_1(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219_1(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
Write_Max7219_2(0x09, 0x00); //译码方式:BCD码
Write_Max7219_2(0x0a, 0x00); //亮度
Write_Max7219_2(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219_2(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219_2(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
Write_Max7219_3(0x09, 0x00); //译码方式:BCD码
Write_Max7219_3(0x0a, 0x00); //亮度
Write_Max7219_3(0x0b, 0x07); //扫描界限;8个数码管显示
Write_Max7219_3(0x0c, 0x01); //掉电模式:0,普通模式:1
Write_Max7219_3(0x0f, 0x00); //显示测试:1;测试结束,正常显示:0
}
/**********************显示程序**************************/
void dis(void)
{
unsigned char i,k,l,m;
DS3231_Read_time();//读时间
l=RTC_Data[0]/10;//秒
k=RTC_Data[0]%10;//秒
m=RTC_Data[1]%10;//分
for(i=1;i<9;i++)//i是数字
{
Write_Max7219_1(i,disp1[k][i-1]);
Write_Max7219_2(i,disp1[l][i-1]);
Write_Max7219_3(i,disp1[m][i-1]);
}
/**********************主程序**************************/
void main ()
{
Delay_xms(1000);
Delay_xms(1000);
while(1)
{
dis(); //显示函数
}
}
|