程序:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit din1=P1^0;
sbit load1=P1^1;
sbit clk1=P1^2;
sbit din2=P1^3;
sbit load2=P1^4;
sbit clk2=P1^5;
sbit as=P3^4; //DS12C887 地址、数据选择端口
sbit cs=P3^5;
sbit rw=P3^6; //DS12C887数据写端口
sbit rd=P3^7; //DS12C887数据允许端口
const uchar dofly[]={0x7e,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,
0x7f,0x7b,0x01,0x4f,0x37,0x0e,0x67}; //0123456789-EHLP
char shi,fen,miao,shi1,shi2,fen1,fen2,miao1,miao2;
void write_ds(uchar address,uchar dsdata) //写ds12c887
{
cs=0;
rd=1;
rw=1;
as=1;
P0=address;
as=0;
rw=0;
P0=dsdata;
rw=1;
as=1;
cs=1;
}
uchar read_ds(uchar address) //读取ds12c887
{
uchar rdata;
cs=0;
rw=1;
rd=1;
as=1;
P0=address;
as=0;
rd=0;
P0=0xff; //防止数据没有变化,这样可以线与
rdata=P0;
rd=1;
as=1;
cs=1;
return (rdata);
}
void dsinit() //初始化ds12c887
{
write_ds(0x0a,0x20); //开启内部震荡
write_ds(0x0b,0x06); //DM=1/二进制格式,24小时工作制,关闭闹铃中断;
}
void settime()
{
write_ds(0x0b,0x86); //set=1,芯片ds12c887处于禁止更新(不再加减)状态,禁止更新才可以设置
write_ds(0x00,19); //设置秒
write_ds(0x02,43);//分
write_ds(0x04,11);//时
write_ds(0x0b,0x06); //set=0,芯片更新
}
void gettime()
{
uchar temp;
temp=read_ds(0x0a);
if(temp&0x80==0x00)
{
shi=read_ds(0x04); //读取时
fen=read_ds(0x02);//读取分
miao=read_ds(0x00);//读取秒
shi1=shi/10;//小时的十位
shi2=shi%10;//小时的个位
fen1=fen/10;//分钟的十位
fen2=fen%10;//分钟的个位
miao1=miao/10;//秒钟的十位
miao2=miao%10;//秒钟的个位
}
}
void send_byte1(uchar dataout) //字节写入函数(第一片max7219)
{
uchar temp,i;
for(i=0;i<8;i++)
{
clk1=0;
temp=dataout&0x80;
if(temp==0x80)
din1=1;
else
din1=0;
dataout<<=1;
clk1=1;
}
clk1=0;
}
void write_data1(uchar address,uchar dataout)
{
load1=1;
load1=0;
send_byte1(address);
send_byte1(dataout);
load1=1;
}
void send_byte2(uchar dataout) //字节写入函数
{
uchar temp,i;
for(i=0;i<8;i++)
{
clk2=0;
temp=dataout&0x80;
if(temp==0x80)
din2=1;
else
din2=0;
dataout<<=1;
clk2=1;
}
clk2=0;
}
void write_data2(uchar address,uchar dataout)
{
load2=1;
load2=0;
send_byte2(address);
send_byte2(dataout);
load2=1;
}
void led_display(uchar max,uchar address,uchar num)
{
if(max==1)
write_data1(address,dofly[num]);
else if(max==2)
write_data2(address,dofly[num]);
} //显示函数
void max7219init(void)
{
write_data1(0x0f,0x00); //显示测试关
write_data1(0x0c,0x01); //显示开
write_data1(0x0b,0x07); //扫描界限0-8位
write_data1(0x0a,0x0c); //设置亮度25/32
write_data1(0x09,0x00); //译码模式关
write_data2(0x0f,0x00); //显示测试关
write_data2(0x0c,0x01); //显示开
write_data2(0x0b,0x07); //扫描界限0-8位
write_data2(0x0a,0x0c); //设置亮度25/32
write_data2(0x09,0x00); //译码模式关
}
void main()
{
max7219init();
dsinit();
settime();
while(1)
{
gettime();
led_display(1,0x01,2);
led_display(1,0x02,2);
led_display(1,0x03,10);
led_display(1,0x04,1);
led_display(1,0x05,1);
led_display(1,0x06,10);
led_display(1,0x07,5);
led_display(1,0x08,5); //制定的数字,第一块max7219显示正常
led_display(2,0x01,shi1);
led_display(2,0x02,shi2);
led_display(2,0x03,10);
led_display(2,0x04,fen1);
led_display(2,0x05,fen1);
led_display(2,0x06,10);
led_display(2,0x07,miao1);
led_display(2,0x08,miao1); // 第二块max7219显示的时间一直为00-00-00,ds12c887读不到设定时间,这是问题关键
}
}
请教:在工厂加工了一(敏_感_词0137)历板子,元器件都焊好了,调试程序时,其余都正常,就是数码管max7219显示的时间一直为00-00-00,ds12c887读不到设定时间,不知道是程序问题还是硬件问题,在此求助各位,谢谢,不胜感激!
一周热门 更多>