这些怎么改都不对,要么时钟不走,要么时钟走就是显示的数字不对,我真的要疯掉了,发狂中
读写时序与BCD转16进制,10进制转BCD?
现在不知道是时序的问题还是BCD转码的问题?:Q :Q :Q
还有就是读写时序:手册是读为下降沿,写为上升沿;为什么我按手册时序写的就不能运转;看网上的代码读写都是下降沿,读写都改为下降沿
,可以运转了,为什么?难道手册有问题?还是怎么?
#include <reg52.h>
#include "intrins.h"
typedef unsigned char uChar;
typedef unsigned int uInt;
//typedef unsigned long uInt32;
#define SEG_dat_IO P0
#define LED P1
uChar SEG_su[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f, //0,1,2,3,4,5,6,7,8,9,
0x6f,0x77,0x7c,0x58,0x5e,0x79,0x71,0x76,0x40}; // A,b,c,d,E,F,H,-
#define Waddress_Seconds 0x80
#define Raddress_Seconds 0x81
#define Waddress_Minutes 0x82
#define Raddress_Minutes 0x83
#define Waddress_Hour 0x85
#define Raddress_Hour 0x84
//#define Waddress_Date 0x86
//#define Raddress_Date 0x87
//
//#define Waddress_Month 0x88
//#define Raddress_Month 0x89
//
//#define Waddress_Day 0x8A
//#define Raddress_Day 0x8B
//
//#define Waddress_Year 0x8C
//#define Raddress_Year 0x8D
#define Waddress_WP 0x8E
sbit ds1302_CE = P1^0;
sbit ds1302_IO = P1^1;
sbit ds1302_SCK = P1^2;
sbit SEG_WEL = P2^7;
sbit SEG_DUL = P2^6;
sbit LED_EN = P2^5;
void delay_ms(uChar time);
void mcu_Init();
void why_Init();
//void display_LED(uInt32 dis_date);
void dis(uChar ma,uChar mb,uChar mc);
void Write_byte(uChar Add,uChar Data);
uChar Read_byte(uChar Adds);
void ds1302_Write(uChar WRaddress);
uChar ds1302_Read();
uChar m1,s1,f1,m2,s2,f2;
void main()
{
uChar miao,shi,fend,miao1,fend1,shi1;
mcu_Init();
why_Init();
while(1)
{
miao = Read_byte(Raddress_Seconds);
miao1 = ((miao >> 4)&0x0f)*10 + (miao & 0x0f);
fend = Read_byte(Raddress_Minutes);
fend1 = ((fend >> 4)&0x0f)*10 + (fend & 0x0f);
shi = Read_byte(Raddress_Hour);
shi1 = ((shi >> 4)&0x0f)*10 + (shi & 0x0f);
dis(shi1,fend1,miao1);
delay_ms(1);
}
}
void delay_ms(uChar time)
{
uChar i;
uInt j;
for(i = time;i > 0;i--)
for(j = 200;j > 0;j--)
_nop_();
}
void mcu_Init()
{
LED_EN = 1;
LED = 0xff;
LED_EN = 0;
SEG_dat_IO = 0;
SEG_DUL = 0;
SEG_DUL = 1;
SEG_dat_IO = 0xff;
SEG_WEL = 0;
SEG_WEL = 1;
}
void why_Init()
{
ds1302_CE = 0;
ds1302_SCK = 0;
ds1302_IO = 1;
Write_byte(Waddress_WP,0);
Write_byte(Waddress_Seconds,20);
Write_byte(Waddress_Minutes,12);
Write_byte(Waddress_Hour,10);
Write_byte(Waddress_WP,80);
// BCD <----(HEX /10 )*16 + (HEX % 10) HEX--->BCD
// 10(D)--->16(H) D = (D%16 = m; m%16 = n;n%16 = k;) H = knm(H)
}
void dis(uChar ma,uChar mb,uChar mc)
{
static uChar num1 = 1;
m1 = ma/10;
m2 = ma%10;
s1 = mb/10;
s2 = mb%10;
f1 = mc/10;
f2 = mc%10;
switch(num1)
{
case 1:{SEG_DUL = 1;SEG_dat_IO = SEG_su[m1];SEG_DUL = 0;SEG_dat_IO = 0xff;
SEG_WEL = 1;SEG_dat_IO = 0xfe;SEG_WEL = 0;num1++;}break;
case 2:{SEG_DUL = 1;SEG_dat_IO = SEG_su[m2];SEG_DUL = 0;SEG_dat_IO = 0xff;
SEG_WEL = 1;SEG_dat_IO = 0xfd;SEG_WEL = 0;num1++;}break;
case 3:{SEG_DUL = 1;SEG_dat_IO = SEG_su[s1];SEG_DUL = 0;SEG_dat_IO = 0xff;
SEG_WEL = 1;SEG_dat_IO = 0xfb;SEG_WEL = 0;num1++;}break;
case 4:{SEG_DUL = 1;SEG_dat_IO = SEG_su[s2];SEG_DUL = 0;SEG_dat_IO = 0xff;
SEG_WEL = 1;SEG_dat_IO = 0xf7;SEG_WEL = 0;num1++;}break;
case 5:{SEG_DUL = 1;SEG_dat_IO = SEG_su[f1];SEG_DUL = 0;SEG_dat_IO = 0xff;
SEG_WEL = 1;SEG_dat_IO = 0xef;SEG_WEL = 0;num1++;}break;
case 6:{SEG_DUL = 1;SEG_dat_IO = SEG_su[f2];SEG_DUL = 0;SEG_dat_IO = 0xff;
SEG_WEL = 1;SEG_dat_IO = 0xdf;SEG_WEL = 0;num1 = 1;}break;
default:SEG_dat_IO = 0xff;break;
}
}
void ds1302_Write(uChar WRaddress)
{
uChar a;
for(a = 0;a < 8;a++)
{
// ds1302_SCK = 0;
// _nop_();_nop_();
ds1302_IO = WRaddress & 0x01;
_nop_();_nop_();
ds1302_SCK = 1;
_nop_();_nop_();
ds1302_SCK = 0;
_nop_();_nop_();
WRaddress >>= 1;
}
}
uChar ds1302_Read()
{
uChar m,tm = 0;
for(m = 0;m < 8;m++)
{
ds1302_SCK = 1;
_nop_();_nop_();
tm >>= 1;
ds1302_SCK = 0;
_nop_();_nop_();
if(ds1302_IO)
tm = ds1302_IO | 0x80;
}
// H = tm / 16;
// L = tm % 16;
// S = H * 10 + L;
return tm;
}
void Write_byte(uChar Add,uChar Data)
{
ds1302_CE = 0;
_nop_();_nop_();
ds1302_SCK = 0;
_nop_();_nop_();
ds1302_CE = 1;
_nop_();_nop_();
ds1302_Write(Add);
ds1302_Write((Data/10)<<4)+((Data%10)&0x0f));
ds1302_SCK = 1;
_nop_();_nop_();
ds1302_CE = 0;
_nop_();_nop_();
ds1302_IO = 1;
_nop_();_nop_();
}
//((Data/10)<<4)+((Data%10)&0x0f)
uChar Read_byte(uChar Adds)
{
uChar tab;
ds1302_CE = 0;
_nop_();_nop_();
ds1302_SCK = 0;
_nop_();_nop_();
ds1302_CE = 1;
_nop_();_nop_();
ds1302_Write(Adds);
tab = ds1302_Read();
ds1302_SCK = 1;
_nop_();_nop_();
ds1302_CE = 0;
_nop_();_nop_();
ds1302_IO = 1;
_nop_();_nop_();
return tab;
} |