C51编程:簡單問題愁煞人
各位大俠﹕
我寫了一個DS1302的應用程序﹐運行時LED顯示有錯﹕數字顯示不全﹐如"8"共陰極字型
碼是0X80,但顯示結果是0X87,后3位出錯﹔其它數字類似﹐均是后3位出錯。經運行測試程
序﹐硬件上無錯。百思不得其解﹐請教個位大俠。附源程序如下﹕
/***************************************************************************/
/* DS1302.C */
/***************************************************************************/
#include <stdio.h>
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
/***************************************************************************/
/* Prototypes */
/***************************************************************************/
void clkwritebyte();
void ramwritebyte();
uchar rbyte_3w();
void reset_3w();
void wbyte_3w(uchar);
void initialize_DS1302();
void disp_clk_regs();
void burstramrd();
void burstramwr();
sbit SCLK = P1^2;
sbit IO = P1^3;
sbit RSTB = P1^4;
/* ----------------------------------------------------------------------- */
void reset_3w()
{
SCLK = 0;
RSTB = 0;
RSTB = 1;
}
/* ----------------------------------------------------------------------- */
void wait(uchar t)
{
uchar i,j;
for (i=0; i<=t; i++)
{
for (j=0; j<120; j++)
{
_nop_();
}
}
}
/* ----------------------------------------------------------------------- */
void wbyte_3w(uchar W_Byte)
{
uchar i;
for(i = 0; i < 8; ++i)
{
IO = 0;
SCLK = 0;
if(W_Byte & 0x01)
{
IO = 1; /* set port pin high to read data */
}
SCLK = 1;
W_Byte >>= 1;
}
}
/* ----------------------------------------------------------------------- */
uchar rbyte_3w()
{
uchar i;
uchar R_Byte;
uchar TmpByte;
R_Byte = 0x00;
/*IO = 1;*/
for(i=0; i<8; ++i)
{
SCLK = 1;
SCLK = 0;
TmpByte = IO;
TmpByte <<= 7;
R_Byte >>= 1;
R_Byte |= TmpByte;
}
return R_Byte;
}
/* ---- read and display clock registers ---- */
void disp_clk_regs()
{
uchar yr, dy, mn, date, hr, min, sec;
uchar a[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
reset_3w();
wbyte_3w(0xBF); /* clock burst */
sec = rbyte_3w();
min = rbyte_3w();
hr = rbyte_3w();
date = rbyte_3w();
mn = rbyte_3w();
dy = rbyte_3w();
yr = rbyte_3w();
reset_3w();
P0 = 0xff;
P2 = 0x01;
P0 = a[sec & 0x0f];
P0 = P0 & 0x7f;
wait(1);
P0 = 0xff;
P2 <<= 1;
P0 = a[sec >>= 4];
wait(1);
P0 = 0xff;
P2 <<= 1;
P0 = a[min & 0x0f];
P0 = P0 & 0x7f;
wait(1);
P0 = 0xff;
P2 <<= 1;
P0 = a[min >>= 4];
wait(1);
P0 = 0xff;
P2 <<= 1;
P0 = a[hr & 0x0f];
P0 = P0 & 0x7f;
wait(1);
P0 = 0xff;
P2 <<= 1;
P0 = a[hr >>= 4];
wait(1);
}
/* ----------------------------------------- */
void initialize_DS1302()
{
uchar yr, dy, mn, date, hr, min, sec;
yr = 0x02;
mn = 0x01;
date = 0x17;
dy = 0x01;
hr = 0x11;
min = 0x55;
sec = 0x00;
reset_3w();
wbyte_3w(0x8e); /* control register */
wbyte_3w(0); /* disable write protect */
reset_3w();
wbyte_3w(0x90); /* trickle charger register */
wbyte_3w(0xab); /* enable, 2 diodes, 8K resistor */
reset_3w();
wbyte_3w(0xbe); /* clock burst write (eight registers) */
wbyte_3w(sec);
wbyte_3w(min);
wbyte_3w(hr);
wbyte_3w(date);
wbyte_3w(mn);
wbyte_3w(dy);
wbyte_3w(yr);
wbyte_3w(0); /* must write control register in
burst mode */
reset_3w();
}
/* ----------------------------------------------------- */
main (void)
{
initialize_DS1302();
while(1)
{
disp_clk_regs();
}
}
发表时间:2002年1月25日8:56:00