我用MSP430怎么就读不出DS1302中的内容呢,看了一天了,真的不知道哪里有错啊。感觉时序什么的各方面都没问题啊,硬件本人确定了 没有问题啊程序如下
#include"msp430x14x.h"
#define uint unsigned int
#define uchar unsigned char
#define SCLK_HIGH P3OUT |=BIT3
#define SCLK_LOW P3OUT &=~BIT3
#define RST_HIGH P3OUT |=BIT1
#define RST_LOW P3OUT &=BIT1
#define IO_HIGH P3OUT |=BIT2
#define IO_LOW P3OUT &=~BIT2
#define IO_OUT P3DIR |=BIT2
#define IO_IN P3DIR &=~BIT2
#define IO_DATA P3IN&BIT2
uchar shi=0,fen=0,miao=0,wei=0;
uchar Code[]={0x3f,0x06 , //对数码管进行编码,
0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x40};
void Delayus(uint us)
{
while(us--)
{
for(uint i=0;i<8;i++);
}
}
//void Reset_DS1302(void)
//{
// DS_RST_OUT; //RST对应的IO设置为输出状态
// DS_SCL_OUT; //SCLK对应的IO设置为输出状态
// DS_SCL_L; //SCLK=0
// DS_RST_L; //RST=0
// delay(10);
// DS_SCL_H; //SCLK=1
//}
void DelayMs(uint ms)
{
while(ms--){
for(uint i=0;i<800;i++);
}
}
void InitPort(void)
{
P3SEL=0x00;//P3口所有引脚设置为一般的IO口
P3DIR=0x0E;//P3.1 P3.2 P3.3设置为输出方向
P2SEL=0x00;//P2口所有引脚设置为一般的IO口
P4SEL=0x00;//P4口所有引脚设置为一般的IO口
P2DIR=0xFF;//P2口所有引脚设置为输出方向
P4DIR=0xFF;//P4口所有引脚设置为输出方向
P2OUT=0x00;//P2口先输出低电平
P4OUT=0xFF;//P4口先输出低电平
}
void InitClock(void)
{
BCSCTL1=RSEL2+RSEL1+RSEL0;//XT2开启 LFXT1工作在低频模式 ACLK不分频 最高的标称频率
DCOCTL=DCO2+DCO1+DCO0;//DCO为最高频率
do{
IFG1&=~OFIFG;//清除振荡器失效标志
for(uint i=255;i>0;i--);
}while(IFG1&OFIFG);//判断XT2是否起振
BCSCTL2=SELM1+SELS;//MCLK SMCLK时钟源为TX2CLK不分频
}
void WriteByte(uchar Command,uchar WriteData)
{
uchar j,k=1;
RST_LOW;
SCLK_LOW;
Delayus(100);
RST_HIGH;
for(j=0;j<=7;j++)
{
SCLK_LOW;
if(Command&k) IO_HIGH;
else IO_LOW;
Delayus(100);
SCLK_HIGH;
k<<=1;
SCLK_LOW;
}
k=1;
for(j=0;j<=7;j++)
{
SCLK_LOW;
if(WriteData&k) IO_HIGH;
else IO_LOW;
Delayus(100);
SCLK_HIGH;
k<<=1;
SCLK_LOW;
}
RST_LOW;
}
uchar ReadByte(uchar Command)
{
uchar i,k=1,data;
RST_LOW;
SCLK_LOW;
RST_HIGH;
for(i=0;i<8;i++)
{
SCLK_LOW;
if(Command&k) IO_HIGH;
else IO_LOW;
Delayus(10);
SCLK_HIGH;
k<<=1;
if(i!=7)
SCLK_LOW;
}
k=0;
SCLK_HIGH;
IO_IN;
for(i=0;i<8;i++)
{
k>>=1;
SCLK_HIGH;
Delayus(2);
SCLK_LOW;
Delayus(10);
if(IO_DATA) k|=0x80;
}
// IO_OUT;
RST_LOW;
data=k/16;
k=k%16;
k=k+data*10;
return(k);
}
void display(uchar shi,uchar fen,uchar miao)
{
uchar shishi,geshi,shifen,gefen,shimiao,gemiao;
shishi=shi/10;
geshi=shi%10;
shifen=fen/10;
gefen=fen%10;
shimiao=miao/10;
gemiao=miao%10;
P4OUT=wei;
P2OUT=Code[shishi];
DelayMs(2);
P2OUT=0X00;
wei++;
P4OUT=wei;
P2OUT=Code[geshi];
DelayMs(2);
P2OUT=0X00;
wei++;
P4OUT=wei;
P2OUT=Code[16];
DelayMs(2);
P2OUT=0X00;
wei++;
P4OUT=wei;
P2OUT=Code[shifen];
DelayMs(2);
P2OUT=0X00;
wei++;
P4OUT=wei;
P2OUT=Code[gefen];
DelayMs(2);
P2OUT=0X00;
wei++;
P4OUT=wei;
P2OUT=Code[16];
DelayMs(2);
P2OUT=0X00;
wei++;
P4OUT=wei;
P2OUT=Code[shimiao];
DelayMs(2);
P2OUT=0X00;
wei++;
P4OUT=wei;
P2OUT=Code[gemiao];
DelayMs(2);
P2OUT=0X00;
if(wei==7)
wei=0;
}
void main()
{
WDTCTL=WDTPW+WDTHOLD;//关闭看门狗
InitClock();
InitPort();
SCLK_LOW; //SCLK=0
RST_LOW; //RST=0
Delayus(10);
SCLK_HIGH; //SCLK=1
WriteByte(0x8e,0x00);
WriteByte(0x80,0x50);
WriteByte(0x82,0x40);
WriteByte(0x84,0x20);
WriteByte(0x8e,0x80);
while(1)
{
miao=ReadByte(0x81);
fen=ReadByte(0x83);
shi=ReadByte(0x85);
display(shi,fen,miao);
}
}