標(biāo)題:
這個(gè)單片機(jī)程序?yàn)槭裁磾?shù)據(jù)都不能存入at24c02中?
[打印本頁]
作者:
無意之罪
時(shí)間:
2021-3-27 17:13
標(biāo)題:
這個(gè)單片機(jī)程序?yàn)槭裁磾?shù)據(jù)都不能存入at24c02中?
請(qǐng)問一下這個(gè)程序里為什么數(shù)據(jù)都不能存入at24c02中
#include <STC15F2K60S2.h>
#include "ds1302.h"
#include "iic.h"
typedef unsigned char u8;
typedef unsigned int u16;
sbit COL1 = P4^4;
sbit COL2 = P4^2;
sbit COL3 = P3^5;
sbit COL4 = P3^4;
bit flat,add,m=0,del,non=0;
u8 timer=0,key_val=0,n=0,mom=0;
u16 f=0,fre=0,cyl=0,vol=0,vh=20,vl=10;
u8 num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
u8 write_time[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
u8 read_time[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
u8 time[7]={0x55,0x59,0x23,0x01,0x02,0x03,0x12};
u8 seg[8];
void delay(u8 x)
{
while(x--);
}
void select(u8 deat,u8 date)
{
switch(deat)
{
case 4:COL3=0;P2=(P2&0x1f)|0x80;break;
case 5:COL3=0;P2=(P2&0x1f)|0xa0;break;
case 6:COL3=0;P2=(P2&0x1f)|0xc0;break;
case 7:COL3=0;P2=(P2&0x1f)|0xe0;break;
}
P0 = date;
}
void close()
{
select(4,0xff);
select(5,0x00);
select(6,0x00);
select(7,0xff);
}
void ds1302_init()
{
u8 i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<7;i++)
{
Write_Ds1302_Byte(write_time[i],time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
void timer_init()
{
TMOD = 0x15;
AUXR |= 0x00;
TH0 = 0xff;
TL0 = 0xff;
TH1 = (65535-50000)/256;
TL1 = (65535-50000)%256;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
EA = 1;
}
void timer0_() interrupt 1
{
TH0 = 0xff;
TL0 = 0xff;
f++;
}
void timer1_() interrupt 3
{
TH1 = (65535-50000)/256;
TL1 = (65535-50000)%256;
timer++;
if(!(timer%20)) flat=!flat;
}
void time_proc()
{
u8 i;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++)
{
time[i]=Read_Ds1302_Byte(read_time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
seg[0] = num[time[2]/16];
seg[1] = num[time[2]%16];
seg[2] = seg[5] = 0xbf;
seg[3] = num[time[1]/16];
seg[4] = num[time[1]%16];
seg[6] = num[time[0]/16];
seg[7] = num[time[0]%16];
}
void time_deal()
{
u8 i;
for(i=0;i<3;i++)
time[i]=Read_Ds1302_Byte(read_time[i]);
seg[0] = num[time[2]/16];
seg[1] = num[time[2]%16];
seg[2] = seg[5] = 0xbf;
seg[3] = num[time[1]/16];
seg[4] = num[time[1]%16];
seg[6] = num[time[0]/16];
seg[7] = num[time[0]%16];
if(flat)
{
n%=3;
seg[7-n*3]=0xff;
seg[7-n*3-1]=0xff;
}
if(add)
{
add=0;
n%=3;
time[n]++;
if((time[n]&0x0f)>9) time[n]+=6;
if((time[n]>0x59)&&(n<2)) time[n]=0x00;
else if((time[n]>0x23)&&(n==2)) time[n]=0x00;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++)
{
Write_Ds1302_Byte(write_time[i],time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
if(del)
{
del=0;
n%=3;
if(((time[n]&0x0f)==0)&&(time[n]!=0)) time[n]= time[n]-0x10+0x09;
else if((time[n]==0)&&(n<2)) time[n]=0x59;
else if((time[n]==0)&&(n==2)) time[n]=0x23;
else time[n]--;
Write_Ds1302_Byte(0x8e,0x00);
for(i=0;i<3;i++)
{
Write_Ds1302_Byte(write_time[i],time[i]);
}
Write_Ds1302_Byte(0x8e,0x80);
}
}
void vol_proc()
{
vol=read_date(0x90,0x03);
vol=vol/5.1;
seg[0]=seg[2]=0xbf;
seg[1]=num[1];
seg[3]=0xff;
seg[4]=num[vol%100/10];
seg[5]=num[vol%10];
seg[6]=num[0];
seg[7]=num[0];
}
void vol_deal()
{
vh=at24c02read(0x09);
vl=at24c02read(0x0b);
seg[0]=num[vh/10];
seg[1]=num[vh%10];
seg[2]=num[0];
seg[3]=num[0];
seg[4]=num[vl/10];
seg[5]=num[vl%10];
seg[6]=num[0];
seg[7]=num[0];
if(flat)
{
n%=2;
seg[n*4]=0xff;
seg[n*4+1]=0xff;
seg[n*4+2]=0xff;
seg[n*4+3]=0xff;
}
if(add)
{
add=0;
if(!n)
vh+=5;
else
vl+=5;
if(vh>=50) vh=50;
if(vl>=50) vl=50;
}
if(del)
{
del=0;
if(!n)
vh-=5;
else
vl-=5;
if(vh==0) vh=0;
if(vl==0) vl=0;
}
write_date(0x09,vh);
write_date(0x0b,vl);
}
void fre_proc()
{
TR0=1;
TR1=1;
if(timer>=20)
{
timer%=20;
fre=f;
f=0;
}
if(fre>=20000) fre=20000;
if(fre<=500) fre=500;
seg[0]=seg[2]=0xbf;
seg[1]=num[2];
seg[3]=num[fre/10000];
seg[4]=num[fre%10000/1000];
seg[5]=num[fre%1000/100];
seg[6]=num[fre%100/10];
seg[7]=num[fre%10];
}
void cyl_proc()
{
float cy;
TR0=1;TR1=1;
if(timer>20)
{
timer%=20;
fre = f;
f=0;
}
if(fre>=20000) fre=20000;
if(fre<=500) fre=500;
cy=1.0/fre;
cyl=cy*1000*1000;
seg[0]=seg[2]=0xbf;
seg[1]=num[2];
seg[3]=num[cyl/10000];
seg[4]=num[cyl%10000/1000];
seg[5]=num[cyl%1000/100];
seg[6]=num[cyl%100/10];
seg[7]=num[cyl%10];
}
void timer_proc()
{
u8 timed[3];
time[0]=at24c02read(0x03);
time[1]=at24c02read(0x05);
time[2]=at24c02read(0x07);
seg[0]=num[timed[2]/16];
seg[1]=num[timed[2]%16];
seg[2]=seg[5]=0xbf;
seg[3]=num[timed[1]/16];
seg[4]=num[timed[1]%16];
seg[6]=num[timed[0]/16];
seg[7]=num[timed[0]%16];
}
void lei_proc()
{
u8 lei;
lei = at24c02read(0x01);
seg[0]=seg[1]=seg[2]=seg[3]=seg[4]=seg[5]=0xff;
seg[6]=num[0];
seg[7]=num[lei];
}
void key_proc()
{
u8 key;
P3=0x0f;
COL1=0;COL2=1;
COL3=1;COL4=1;
key = P3&0x0f;
if(key!=0x0f)
key |= 0x70;
else
{
COL1=1;COL2=0;
key = P3&0x0f;
if(key!=0x0f)
key |= 0xb0;
else
{
COL2=1;COL3=0;
key = P3&0x0f;
if(key!=0x0f)
key |= 0xd0;
else
{
COL3=1;COL4=0;
key = P3&0x0f;
if(key!=0x0f)
key |= 0xe0;
else
COL4=1;
}
}
}
switch(key)
{
case 0x77:key_val=4;break;
case 0x7b:key_val=5;break;
case 0x7d:key_val=6;break;
case 0x7e:key_val=7;break;
case 0xbb:key_val=9;break;
case 0xbd:key_val=10;break;
case 0xbe:key_val=11;break;
default:key_val=0;
}
}
void seg_proc()
{
select(6,0x01);select(7,seg[0]);delay(1000);
select(6,0x02);select(7,seg[1]);delay(1000);
select(6,0x04);select(7,seg[2]);delay(1000);
select(6,0x08);select(7,seg[3]);delay(1000);
select(6,0x10);select(7,seg[4]);delay(1000);
select(6,0x20);select(7,seg[5]);delay(1000);
select(6,0x40);select(7,seg[6]);delay(1000);
select(6,0x80);select(7,seg[7]);delay(1000);
close();
}
void mom_proc()
{
if((vol>vh)||(vol<vl))
{
if(vol>vh)
{write_date(0x01,1);delay(5000);}
else if(vol<vl)
{write_date(0x01,0);delay(5000);}
write_date(0x03,time[0]);delay(5000);
write_date(0x05,time[1]);delay(5000);
write_date(0x07,time[2]);delay(5000);
}
key_proc();
if(key_val==11)
{
add=1;
while(key_val==11)
{
key_proc();
seg_proc();
}
}
if(key_val==10)
{
del=1;
while(key_val==10)
{
key_proc();
seg_proc();
}
}
if(key_val==9)
{
mom=3;
m=0;
while(key_val==9)
{
key_proc();
seg_proc();
}
}
if(key_val==7)
{
mom=0;
non=0;
n=2;
while(key_val==7)
{
key_proc();
seg_proc();
}
}
if(key_val==6)
{
mom=1;
non=0;
n=1;
while(key_val==6)
{
key_proc();
seg_proc();
}
}
if(key_val==5)
{
mom=2;
m=0;
while(key_val==5)
{
key_proc();
seg_proc();
}
}
if(key_val==4)
{
m=!m;
non=1;
n++;
while(key_val==4)
{
key_proc();
seg_proc();
}
}
}
void main()
{
timer_init();
ds1302_init();
close();
vh=at24c02read(0x09);
vl=at24c02read(0x0b);
while(1)
{
mom_proc();
if((!mom)&&(non))
{
time_deal();
seg_proc();
}
else if((!mom)&&(!non))
{
time_proc();
seg_proc();
}
else if((mom==1)&&(non))
{
vol_deal();
seg_proc();
}
else if((mom==1)&&(!non))
{
vol_proc();
seg_proc();
}
else if((mom==2)&&(m))
{
cyl_proc();
seg_proc();
}
else if((mom==2)&&(!m))
{
fre_proc();
seg_proc();
}
else if((mom==3)&&(m))
{
timer_proc();
seg_proc();
}
else if((mom==3)&&(!m))
{
lei_proc();
seg_proc();
}
}
}
#include "iic.h"
#include "reg52.h"
#include "intrins.h"
#define DELAY_TIME 5
#define SlaveAddrW 0xA0
#define SlaveAddrR 0xA1
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
void IIC_SendAck(bit ackbit)
{
SCL = 0;
SDA = ackbit;
IIC_Delay(DELAY_TIME);
SCL = 1;
IIC_Delay(DELAY_TIME);
SCL = 0;
SDA = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
unsigned char IIC_RecByte(void)
{
unsigned char i, da;
for(i=0; i<8; i++)
{
SCL = 1;
IIC_Delay(DELAY_TIME);
da <<= 1;
if(SDA) da |= 1;
SCL = 0;
IIC_Delay(DELAY_TIME);
}
return da;
}
void write_date(unsigned char deet,unsigned char date)
{
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(deet);
IIC_WaitAck();
IIC_SendByte(date);
IIC_SendAck(1);
IIC_Stop();
}
unsigned char read_date(unsigned char deat,unsigned char deet)
{
unsigned char date;
IIC_Start();
IIC_SendByte(deat);
IIC_WaitAck();
IIC_SendByte(deet);
IIC_WaitAck();
IIC_Stop();
IIC_Start();
IIC_SendByte(deat+1);
IIC_WaitAck();
date = IIC_RecByte();
// IIC_SendAck(1);
IIC_Stop();
return date;
}
unsigned char at24c02read(unsigned char deet)
{
unsigned char dat;
IIC_Start();
IIC_SendByte(0xa0);
IIC_WaitAck();
IIC_SendByte(deet);
IIC_WaitAck();
IIC_Start();
IIC_SendByte(0xa1);
IIC_WaitAck();
dat=IIC_RecByte();
// IIC_SendAck(0);
IIC_Stop();
return dat;
}
#include "ds1302.h"
#include <reg52.h>
#include <intrins.h>
sbit SCK=P1^7;
sbit SDA=P2^3;
sbit RST = P1^3;
void Write_Ds1302(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)
{
SCK=0;
SDA=temp&0x01;
temp>>=1;
SCK=1;
}
}
void Write_Ds1302_Byte( unsigned char address,unsigned char dat )
{
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
Write_Ds1302(dat);
RST=0;
}
unsigned char Read_Ds1302_Byte ( unsigned char address )
{
unsigned char i,temp=0x00;
RST=0; _nop_();
SCK=0; _nop_();
RST=1; _nop_();
Write_Ds1302(address);
for (i=0;i<8;i++)
{
SCK=0;
temp>>=1;
if(SDA)
temp|=0x80;
SCK=1;
}
RST=0; _nop_();
SCK=0; _nop_();
SCK=1; _nop_();
SDA=0; _nop_();
SDA=1; _nop_();
return (temp);
}
復(fù)制代碼
作者:
man1234567
時(shí)間:
2021-3-29 17:36
如果有人讀并且試你這個(gè)700多行程序并且告訴你答案,那么大概率是有現(xiàn)成的和你一樣的硬件也同程度并同樣學(xué)習(xí)到這里并且同樣用一個(gè)母板......
作者:
無意之罪
時(shí)間:
2021-4-1 19:49
已經(jīng)解決了
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1