我的ds1302走时比正常的快了十倍, 不知道什么原因? 请大侠们帮忙看一下单片机程序: |
#include <STC12C5630AD.h>
#include <string.h>
typedef unsigned char uint8;
#define LCM2402_DB0_DB7 P2
sbit LCM2402_RS = P1 ^ 5;
sbit LCM2402_RW = P1 ^ 6;
sbit LCM2402_E = P1 ^ 7;
sbit LCM2402_Busy = P2 ^ 7;
sbit DQ = P1 ^ 3;
sbit clock_clk = P1 ^ 0;
sbit clock_dat = P1 ^ 1;
sbit clock_Rst = P1 ^ 2;
data unsigned int ii;
data unsigned char mode = 0;
data unsigned char TIME_WW;
#define CMD_clear 0x01
#define CMD_back 0x02
#define CMD_dec1 0x04
#define CMD_add1 0x06
#define CMD_dis_gb1 0x0f
#define CMD_dis_gb2 0x0e
#define CMD_dis_gb3 0x0c
#define CMD_OFF_dis 0x08
#define CMD_set82 0x38
#define CMD_set81 0x30
#define CMD_set42 0x28
#define CMD_set41 0x20
#define lin_1 0x80
#define lin_2 0xc0
void MCU_MS (unsigned int a){
unsigned int i;
while( --a != 0){
for(i = 0; i < 600; i++);
}
}
void PWM_init (void){
CMOD=0x02;
CL=0x00;
CH=0x00;
CCAPM0=0x42;
CCAP0L=0x00;
CCAP0H=0x00;
CR=1;
}
void PWM0_set (unsigned char a){
CCAP0L= a;
CCAP0H= a;
}
void LCM2402_TestBusy(void){
LCM2402_DB0_DB7 = 0xff;
LCM2402_RS = 0;
LCM2402_RW = 1;
LCM2402_E = 1;
while(LCM2402_Busy);
LCM2402_E = 0;
}
void LCM2402_WriteCMD(uint8 LCM2402_command) {
LCM2402_TestBusy();
LCM2402_RS = 0;
LCM2402_RW = 0;
LCM2402_DB0_DB7 = (LCM2402_command/16)*16;
LCM2402_E = 1;
LCM2402_E = 0;
LCM2402_DB0_DB7 = (LCM2402_command%16)*16;
LCM2402_E = 1;
LCM2402_E = 0;
}
void LCM2402_WriteData(uint8 LCM2402_data){
LCM2402_TestBusy();
LCM2402_RS = 1;
LCM2402_RW = 0;
LCM2402_DB0_DB7 = (LCM2402_data/16)*16;
LCM2402_E = 1;
LCM2402_E = 0;
LCM2402_DB0_DB7 = (LCM2402_data%16)*16;
LCM2402_E = 1;
LCM2402_E = 0;
}
void print(uint8 a,uint8 *str){
LCM2402_WriteCMD(a | 0x80);
while(*str != '\0'){
LCM2402_WriteData(*str++);
}
*str = 0;
}
void print2(uint8 a,uint8 t){
LCM2402_WriteCMD(a | 0x80);
LCM2402_WriteData(t);
}
uint8 code Xword[]={
0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00,
0x00,0x00,0x00,0x00,0xff,0x00,0x00,0x00,
0x00,0x00,0x00,0x0e,0x00,0xff,0x00,0x00,
0x00,0x00,0xff,0x00,0x0e,0x00,0xff,0x00,
0x00,0x00,0xff,0xf5,0xfb,0xf1,0xff,0x00,
0x00,0xfe,0x08,0xfe,0x0a,0x0a,0xff,0x00,
0x00,0x04,0x00,0xff,0x00,0x0a,0x11,0x00,
0x00,0x1f,0x11,0x1f,0x11,0x11,0x1f,0x00,
};
void CgramWrite(void) {
uint8 i;
LCM2402_WriteCMD(0x06);
LCM2402_WriteCMD(0x40);
for(i=0;i<64;i++) {
LCM2402_WriteData(Xword[i]);
}
}
void LCM2402_Init(void){
LCM2402_WriteCMD(CMD_set42); /
sbit a0 = ACC ^ 0;
sbit a1 = ACC ^ 1;
sbit a2 = ACC ^ 2;
sbit a3 = ACC ^ 3;
sbit a4 = ACC ^ 4;
sbit a5 = ACC ^ 5;
sbit a6 = ACC ^ 6;
sbit a7 = ACC ^ 7;
void clock_out(unsigned char dd){
ACC=dd;
clock_dat=a0; clock_clk=1; clock_clk=0;
clock_dat=a1; clock_clk=1; clock_clk=0;
clock_dat=a2; clock_clk=1; clock_clk=0;
clock_dat=a3; clock_clk=1; clock_clk=0;
clock_dat=a4; clock_clk=1; clock_clk=0;
clock_dat=a5; clock_clk=1; clock_clk=0;
clock_dat=a6; clock_clk=1; clock_clk=0;
clock_dat=a7; clock_clk=1; clock_clk=0;
}
unsigned char clock_in(void){
clock_dat=1;
a0=clock_dat;
clock_clk=1; clock_clk=0; a1=clock_dat;
clock_clk=1; clock_clk=0; a2=clock_dat;
clock_clk=1; clock_clk=0; a3=clock_dat;
clock_clk=1; clock_clk=0; a4=clock_dat;
clock_clk=1; clock_clk=0; a5=clock_dat;
clock_clk=1; clock_clk=0; a6=clock_dat;
clock_clk=1; clock_clk=0; a7=clock_dat;
return(ACC);
}
void write_clock(unsigned char ord, unsigned char dd){
clock_clk=0;
clock_Rst=0;
clock_Rst=1;
clock_out(ord);
clock_out(dd);
clock_Rst=0;
clock_clk=1;
}
unsigned char read_clock(unsigned char ord){
unsigned char dd=0;
clock_clk=0;
clock_Rst=0;
clock_Rst=1;
clock_out(ord);
dd=clock_in();
clock_Rst=0;
clock_clk=1;
return(dd);
}
void Init_1302(void){
if(read_clock(0xc1) != 0xaa){
write_clock(0x8e,0x00);
write_clock(0x8c,0x21);
write_clock(0x8a,0x07);
write_clock(0x88,0x09);
write_clock(0x86,0x015);
write_clock(0x84,0x00);
write_clock(0x82,0x00);
write_clock(0x80,0x00);
write_clock(0x90,0xa5);
write_clock(0xc0,0xaa);
write_clock(0x8e,0x80);
}
}
void Delay(int num){
while(num--) ;
}
void Init_DS18B20(void){
unsigned char x=0;
DQ = 1;
Delay(35);
DQ = 0;
Delay(480);
DQ = 1;
Delay(70);
x=DQ;
Delay(90);
}
unsigned char ReadOneChar(void){
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--){
DQ = 0;
dat>>=1;
DQ = 1;
if(DQ)
dat|=0x80;
Delay(30);
}
return(dat);
}
void WriteOneChar(unsigned char dat){
unsigned char i=0;
for (i=8; i>0; i--){
DQ = 0;
DQ = dat&0x01;
Delay(30);
DQ = 1;
dat>>=1;
}
}
unsigned int ReadTemperature(void){
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0x44);
Init_DS18B20();
WriteOneChar(0xCC);
WriteOneChar(0xBE);
a=ReadOneChar();
b=ReadOneChar();
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5;
return(t);
}
void RealTime_Display(void){
unsigned int i=0,o=0,o2=0;
unsigned char a=0,b=0,c=0;
i=ReadTemperature();
a=i/100;
print2(0x40,a+0x30);
b=i/10-a*10;
print2(0x41,b+0x30);
print(0x42,".");
c=i-a*100-b*10;
print2(0x43,c+0x30);
print2(0x44,0x00);
print(0x00,"20");
print2(0x02,(read_clock(0x8d)/16)+0x30);
print2(0x03,(read_clock(0x8d)%16)+0x30);
print(0x04,"/");
print2(0x05,(read_clock(0x89)/16)+0x30);
print2(0x06,(read_clock(0x89)%16)+0x30);
print(0x07,"/");
print2(0x08,(read_clock(0x87)/16)+0x30);
print2(0x09,(read_clock(0x87)%16)+0x30);
print2(0x0b,(read_clock(0x85)/16)+0x30);
print2(0x0c,(read_clock(0x85)%16)+0x30);
print(0x0d,":");
print2(0x0e,(read_clock(0x83)/16)+0x30);
print2(0x0f,(read_clock(0x83)%16)+0x30);
print(0x10,":");
print2(0x11,(read_clock(0x81)/16)+0x30);
print2(0x12,(read_clock(0x81)%16)+0x30);
TIME_WW=read_clock(0x8b)%16;
print(0x46,"[");
switch (TIME_WW){
case 1:print(0x47,"Mon");break;
case 2:print(0x47,"Tues");break;
case 3:print(0x47,"Wed");break;
case 4:print(0x47,"Thur");break;
case 5:print(0x47,"Fri");break;
case 6:print(0x47,"Sat");break;
case 7:print(0x47,"Sun");break;
default: print(0x47,"Error");break;
}
if(TIME_WW==2||TIME_WW==4)
print(0x4b,"]");
else print(0x4a,"]");
print(0x54,"Lamp brightness:");
o=(ii/100*10+ii/10%10)*4;
print2(0x64,o/100+0x30);
print2(0x65,o/10%10+0x30);
print2(0x66,o%10+0x30);
print(0x67,"%");
print(0x14,"-------b:");
print(0x20,"%-------");
print(0x4E,"MODE:");
print2(0x53,mode+0x30);
}
void main (void){
LCM2402_Init();
Init_1302();
PWM_init();
while(1){
RealTime_Display();
}
}