找回密码
 注册会员
更新自动建库工具PCB Footprint Expert 2024.04 Pro / Library Expert 破解版

帮忙改改程序

[复制链接]
admin 发表于 2012-9-3 01:57:47 | 显示全部楼层 |阅读模式

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有账号?注册会员

×
我想把时钟芯片的秒,分读出来,用LED显示,下面的程序运行了之后秒,分的值一直是0FFH,不知道程序哪里错了,请高手帮帮忙,告诉我哪里要改一下,万分感激。(刚开始学单片机,好多都不懂)。


#include<reg51.h>
#define uchar unsigned char

unsigned char code dispcode[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x00,0xFF};

uchar sec,min,hr,day,month,week,year,number;
void ds1302_init(); /*申明DS1302初始化函数*/

sbit ds1302_clk=P1^0;
sbit ds1302_dat=P1^1;
sbit ds1302_rst=P1^2;
sbit LED1=P1^3;
sbit LED2=P1^4;
sbit LED3=P1^5;
sbit LED4=P1^6;

sbit ACC_0=ACC^0;
sbit ACC_1=ACC^1;
sbit ACC_2=ACC^2;
sbit ACC_3=ACC^3;
sbit ACC_4=ACC^4;
sbit ACC_5=ACC^5;
sbit ACC_6=ACC^6;
sbit ACC_7=ACC^7;
/*****************************函数说明**********************************/
void Read(void); /*写DS1302操作*/
void Write(void); /*读DS1302操作*/
uchar ReadRam_B(uchar address); /*从Ram中读出一个字节*/
uchar ReadRTC_B(uchar address); /*从DS1302中读出一个字节*/
uchar WriteRam_B(uchar address,uchar number); /*向Ram中写入一个字节*/
uchar WriteRTC_B(uchar address,uchar number); /*向DS1302中写入一个字节*/
void ReadRam(uchar data*buffer,uchar size); /*从Ram中读出多字节 */
void ReadRTC(uchar data*buffer,uchar size); /*从DS1302中读出多字节*/
void WriteRam(uchar data*buffer,uchar size); /*向Ram中写入多字节*/
void WriteRTC(uchar data*buffer,uchar size); /*向DS1302中写入多字节 */
uchar ReadSec(void); /*从DS1302中读出秒字节*/
uchar ReadMin(void); /*从DS1302中读出分字节*/
uchar ReadHr(void); /*从DS1302中读出时字节*/
uchar ReadDay(void); /*从DS1302中读出日字节*/
uchar ReadMonth(void); /*从DS1302中读出月字节*/
uchar ReadWeek(void); /*从DS1302中读出星期字节*/
uchar ReadYear(void); /*从DS1302中读出年字节*/
void WriteSec(uchar number); /*向DS1302中写入秒字节*/
void WriteMin(uchar number); /*向DS1302中写入分字节*/
void WriteHr(uchar number); /*向DS1302中写入时字节*/
void WriteDay(uchar number); /*向DS1302中写入日字节*/
void WriteMonth(uchar number); /*向DS1302中写入月字节*/
void WriteWeek(uchar number); /*向DS1302中写入星期字节*/
void WriteYear(uchar number); /*向DS1302中写入年字节*/
void WriteControl(uchar number); /*向慢速充电控制寄存器写入控制字节*/
void EnableWP(void); /*禁止写保护*/
void DisableWP(void); /*允许写保护*/




sfr WDTRST = 0xA6; /*看门狗寄存器*/

uchar address;

void Write(void) /*写DS1302操作*/
{
ACC=address;
ACC<<=1;
ACC|=0x80;
ds1302_dat=ACC_0; /*写DS1302*/
ds1302_clk=1; /*时钟上升沿,发送数据有效*/
ds1302_clk=0; /*清零时钟总线*/
ds1302_dat=ACC_1;
ds1302_clk=1;
ds1302_clk=0;
ds1302_dat=ACC_2;
ds1302_clk=1;
ds1302_clk=0;
ds1302_dat=ACC_3;
ds1302_clk=1;
ds1302_clk=0;
ds1302_dat=ACC_4;
ds1302_clk=1;
ds1302_clk=0;
ds1302_dat=ACC_5;
ds1302_clk=1;
ds1302_clk=0;
ds1302_dat=ACC_6;
ds1302_clk=1;
ds1302_clk=0;
ds1302_dat=ACC_7;
ds1302_clk=1;
ds1302_clk=0;
}

void Read(void) /*读DS1302读操作*/
{
ACC_0=ds1302_dat; /*读取DS1302数据*/
ds1302_clk=1; /*时钟上升沿,发送数据有效*/
ds1302_clk=0; /*清零时钟总线*/
ACC_1=ds1302_dat;
ds1302_clk=1;
ds1302_clk=0;
ACC_2=ds1302_dat;
ds1302_clk=1;
ds1302_clk=0;
ACC_3=ds1302_dat;
ds1302_clk=1;
ds1302_clk=0;
ACC_4=ds1302_dat;
ds1302_clk=1;
ds1302_clk=0;
ACC_5=ds1302_dat;
ds1302_clk=1;
ds1302_clk=0;
ACC_6=ds1302_dat;
ds1302_clk=1;
ds1302_clk=0;
ACC_7=ds1302_dat;
ds1302_clk=1;
ds1302_clk=0;
}

uchar ReadRam_B(uchar address) /*从RAM中读出一个字节*/
{
ds1302_rst=0; /*复位引脚为低电平,所有数据传送终止*/
ds1302_clk=0; /*清零时钟总线*/
ds1302_rst=1; /*复位引脚为高电平,逻辑控制有效*/
ACC=address;
Write(); /*写DS1302操作*/
ds1302_dat=1;
Read(); /*读DS1302操作*/
return ACC;
}


uchar ReadRTC_B(uchar address) /*读出DS1302一字节*/
{
ds1302_rst=0; /*复位引脚为低电平,所有数据传送终止*/
ds1302_clk=0; /*清零时钟总线*/
ds1302_rst=1; /*复位引脚为高电平,逻辑控制有效*/
ACC=address;
Write(); /*写DS1302操作*/
ds1302_dat=1;
Read(); /*读DS1302操作*/
return ACC;
}


uchar WriteRam_B(uchar address,uchar number) /*向RAM中写入一个字节*/
{
ds1302_clk=0;
ds1302_rst=0;
ds1302_rst=1;
ACC=address;
Write(); /*对DS1302写命令地址和地址*/
ACC=number;
Write(); /*对DS1302写命令字和数据*/
ds1302_rst=0;
ds1302_clk=1;
}

uchar WriteRTC_B(uchar address,uchar number) /*向DS1302中写入一字节*/
{
ds1302_clk=0;
ds1302_rst=0;
ds1302_rst=1;
Write(); /*对DS1302写命令字和地址*/
ACC=number;
Write(); /*对DS1302写命令字和数据*/
ds1302_rst=0;
ds1302_clk=1;
}

void ReadRam(uchar data*buffer,uchar size) /*从RAM中读出多字节*/
{
register uchar a,b;
ds1302_clk=0;
ds1302_rst=0;
ds1302_rst=1;
ACC=0xff; /*命令字为FFH*/
Write(); /*对DS1302写命令字和地址*/
ds1302_dat=1;
for(a=0;a<size;a++)
{
ACC_0=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_1=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_2=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_3=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_4=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_5=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_6=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_7=ds1302_dat;

b=ACC;
*(buffer+a)=b;
ds1302_clk=1;
ds1302_clk=0;
}
ds1302_rst=0;
ds1302_clk=1;
}

void ReadRTC(uchar data*buffer,uchar size) /*从DS1302中读出多字节*/
{
register uchar a,b;
ds1302_clk=0;
ds1302_rst=0;
ds1302_rst=1;
ACC=0xbf; /*命令字为BFH*/
Write(); /*对DS1302写命令字和地址*/
ds1302_dat=1;
for(a=0;a<size;a++)
{
ACC_0=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_1=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_2=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_3=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_4=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_5=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_6=ds1302_dat;
ds1302_clk=1;ds1302_clk=0;ACC_7=ds1302_dat;

b=ACC;
*(buffer+a)=b;
ds1302_clk=1;
ds1302_clk=0;
}
ds1302_rst=0;
ds1302_clk=1;
}




void WriteRam(uchar data*buffer,uchar size) /*向RAM中写入多字节*/
{
register uchar a;
ds1302_clk=0;
ds1302_rst=0;
ds1302_rst=1;
ACC=0xfe; /*命令字为FEH*/
Write(); /*对DS1302写命令字和地址*/
for(a=0;a<size;a++)
{
ACC=*(buffer+a);
Write(); /*对DS1302写数据*/
}
ds1302_rst=0;
ds1302_clk=1;
}

void WriteRTC(uchar data*buffer,uchar size) /*向DS1302中写入多字节 */
{/*注意:连续写DS1302只能是8字节一起写*/
register uchar a;
ds1302_clk=0;
ds1302_rst=0;
ds1302_rst=1;
ACC=0xbe; /*命令字为BEH*/
Write(); /*对DS1380写命令字和地址*/
for(a=0;a<size;a++)
{
ACC=*(buffer+a);
Write(); /*对DS1302写数据*/
}
ds1302_rst=0;
ds1302_clk=1;
}


uchar ReadSec(void) /*从DS1302中读出秒字节*/
{
return ReadRTC_B(0x81);
}

uchar ReadMin(void) /*从DS1302中读出分字节*/
{
return ReadRTC_B(0x83);
}

uchar ReadHr(void) /*从DS1302中读出时字节*/
{
return ReadRTC_B(0x85);
}

uchar ReadDay(void) /*从DS1302中读出日字节*/
{
return ReadRTC_B(0x87);
}

uchar ReadMonth(void) /*从DS1302中读出月字节*/
{
return ReadRTC_B(0x89);
}


uchar ReadWeek(void) /*从DS1302中读出星期字节*/
{
return ReadRTC_B(0x8B);
}

uchar ReadYear(void) /*从DS1302中读出年字节*/
{
return ReadRTC_B(0x8D);
}


void WriteSec(uchar number) /*向DS1302中写入秒字节*/
{
WriteRTC_B(0x80,number);
}

void WriteMin(uchar number) /*向DS1302中写入分字节*/
{
WriteRTC_B(0x82,number);
}

void WriteHr(uchar number) /*向DS1302中写入时字节*/
{
WriteRTC_B(0x84,number);
}

void WriteDay(uchar number) /*向DS1302中写入日字节*/
{
WriteRTC_B(0x86,number);
}

void WriteMonth(uchar number) /*向DS1302中写入月字节*/
{
WriteRTC_B(0x88,number);
}

void WriteWeek(uchar number) /*向DS1302中写入星期字节*/
{
WriteRTC_B(0x8A,number);
}

void WriteYear(uchar number) /*向DS1302中写入年字节*/
{
WriteRTC_B(0x8C,number);
}


void WriteControl(uchar number) /*向慢速充电控制寄存器写入控制字节*/
{
WriteRTC_B(0x90,number);
}

void DisableWP(void) /*禁止写保护*/
{
WriteRTC_B(0x8E,0x80);
}

void EnableWP(void) /*允许写保护*/
{
WriteRTC_B(0x8E,0);
}


void delay(unsigned char m) /*延时程序500us*/
{
unsigned char i,j;
for(i=m;i!=0;i--);
for(j=250;j!=0;j--);

}

void led() /*数码管显示及数据处理程序*/
{
LED1=0;
LED2=1;
LED3=1;
LED4=1;
P0=dispcode[sec%10]; /*秒个位*/
delay(1);

LED2=0;
LED1=1;
LED3=1;
LED4=1;
 楼主| admin 发表于 2012-9-3 01:57:50 | 显示全部楼层
P0=dispcode[(sec/10)%10]; /*秒十位 */
delay(1);

LED3=0;
LED1=1;
LED2=1;
LED4=1;
P0=dispcode[min%10]; /*分个位 */
delay(1);

LED4=0;
LED1=1;
LED2=1;
LED3=1;
P0=dispcode[(min/10)%10]; /*分十位 */
delay(1);
}


/*DS1302初始化函数*/
void ds1302_init()
{
ds1302_clk=0;
ds1302_rst=0;
ds1302_rst=1; /*使能DS1302*/
EnableWP(); /*允许写DS1302*/

ds1302_rst=0; /*复位*/
}





void main() /*主程序*/
{

uchar data buffer1[32]=
{32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,
52,53,54,55,56,57,58,59,60,61,62,63};
uchar data buffer2[8]={45,45,15,13,3,4,01,13};
WriteSec(0);
P0=0x00;
P1=0x07;
delay(100);
delay(100);
ds1302_init();
ReadRTC(buffer2,8);
DisableWP(); /*禁止写保护*/
WriteControl(0xaa); /*向慢速充电控制寄存器写入控制字节*/
WriteRam(buffer1,31); /*写DS1302RAM*/
WriteRTC(buffer2,8); /*写DS1302时钟*/
EnableWP(); /*允许写保护*/
ReadRam(buffer1,31); /*读DS1302RAM*/
ReadRTC(buffer2,8); /*读DS1302时钟*/
sec=ReadSec(); /*读秒*/
min=ReadMin(); /*读分*/
hr=ReadHr(); /*读时*/
day=ReadDay(); /*读日*/
month=ReadMonth(); /*读月*/
week=ReadWeek(); /*读星期*/
year=ReadYear(); /*读年*/
ACC=0;
WDTRST=0x1E;
WDTRST=0xE1; /*初始化看门狗*/
while (1)
{ led();
WDTRST=0x1E;
WDTRST=0xE1; /*喂狗指令*/
}
}
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-23 20:42 , Processed in 0.064643 second(s), 12 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表