em chào tất cả mọi người.em có đoạn code về đồng hồ thời gian thực em đã lấy ở trên mạng về chỉnh sửa lại rồi.nó có hiển thị nhưng bị sai ạ.em không thể tìm ra lỗi sai ở đâu.kiến thức của em có hạn.em không thể nghĩ thêm được nữa.vậy em mong các anh cố gắng bỏ chút thời gian xem giúp em với ạ.em cảm ơn mọi người ạ.
đây là code của em ạ.em có gửi thêm file đính kèm.các anh giúp em với
#include<16f877a.h>
#use delay(clock=12m)
#fuses NOPROTECT,HS,NOWDT,NOLVP,NODEBUG,NOPUT
#byte PORTB =0x06
#byte TRISB =0x86
#bit SCL =0x06.0
#bit trisb0 =0x86.0
#bit SDA =0x06.1
#bit trisb1 =0x86.1
#byte PORTD =0x08
#byte TRISD =0x88
unsigned char hour, minute, second,c;
unsigned char ma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
unsigned char kt=0,a=0,i,k,x=0;
void delay(){
unsigned char i;
for (i = 0; i < 4; i++){
}
}
//-----------------------------------------
void SCL_high(){
SCL=1;
delay();
}
//------------------------------------------
void SCL_low(){
SCL=0;
delay();
}
//-------------------------------------------
void I2C_Start(){///ham chuan bi chuyen du lieu cua ds1307///
SDA = 1;//do no dc keo len nguon duong 5v
SCL = 1;//-----------------------------//
SDA = 0;//sau do sda dc keo xuong thap 0v
delay();//tao tre 1 time
SCL = 0;//scl cung dc keo xuong 0v
SDA = 1;//sau do dua sda len 1(bat dau thuc hien truyen du lieu)
}
//--------------------------------------------
void I2C_Stop(){
SDA = 0;
SCL_high();
SDA = 1;
}
//----------------------------------------------
boolean I2C_Write(unsigned char dat){//qua trinh ghi 1 byte//
unsigned char i;
boolean outbit;
for (i = 1; i <= 8; i++){
outbit=dat&0x80;//lay bit cao nhat
SDA = outbit;
dat = dat << 1;
SCL_high();//khi moi 1 bit du lieu dc truyen di thi xung scl dc dua len 1 va xuong 0
SCL_low();
}
SDA = 1; //
SCL_high();//sau ghi du 1 byte no se cho ve gia tri cho la sda va scl=1;
outbit = SDA;
SCL_low();
return(outbit);
}
//----------------------------------------------------
unsigned char I2C_Read(boolean ack){
unsigned char i, dat;
boolean inbit;
dat = 0;
for(i=1;i<=8;i++) {
SCL_high();
inbit = SDA;
dat = dat << 1;
dat = dat | inbit;
SCL_low();
}
if (ack) SDA = 0;
else SDA = 1;
SCL_high();
SCL = 0;
SDA = 1;
delay();
return(dat);
}
//-------------------------------------------------------------
void rtc_write(unsigned char add, unsigned char dat){
I2C_Start();
I2C_Write(0xd0);//gui lenh ghi.
I2C_Write(add);
I2C_Write(((dat/10)<<4)|(dat%10));
I2C_Stop();
}
unsigned char rtc_read(unsigned char add){
unsigned char dat;
I2C_Start();
I2C_Write(0xd0);
I2C_Write(add);
I2C_Start();
I2C_Write(0xd1);
dat = I2C_Read(0);
I2C_Stop();
dat = (dat & 0x0f) + (dat>>4)*10;
return (dat);
}
void writeds(){
rtc_write(0x00, second);
rtc_write(0x01, minute);
rtc_write(0x02, hour);
}
void readds()
{
if(kt==0)
{
if(a==1)
{writeds();a=0;
}
second = rtc_read(0x00);
minute = rtc_read(0x01);
hour = rtc_read(0x02);
}
}
void hienthi(unsigned char x)
{
unsigned int i,temp;
for(i=0;i<8;i++)
{
temp=x;
temp=temp&0x80;
if(temp==0x80)
output_high(pin_d2);
else
output_low(pin_d2);
x*=2;
output_low(pin_d0);
output_high(pin_d0);
}
}
void quetled()
{
if(kt==0)
{
hienthi(ma[second%10]);
hienthi(ma[second/10]);
hienthi(ma[minute%10]);
hienthi(ma[minute/10]);
hienthi(ma[hour%10]);
hienthi(ma[hour/10]);
output_low(pin_d1);
output_high(pin_d1);
}
if(kt==1)
{
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
hienthi(ma[hour%10]);
hienthi(ma[hour/10]);
output_low(pin_d1);
output_high(pin_d1);
}
if(kt==2)
{
hienthi(0xff);
hienthi(0xff);
hienthi(ma[minute%10]);
hienthi(ma[minute/10]);
hienthi(0xff);
hienthi(0xff);
output_low(pin_d1);
output_high(pin_d1);
}
if(kt==3)
{
hienthi(ma[second%10]);
hienthi(ma[second/10]);
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
output_low(pin_d1);
output_high(pin_d1);
}
}
void ktphim()
{
if(input(pin_d5)==0)
{
while(input(pin_d5)==0){}
kt++;
if(kt>3)kt=0;
}
switch(kt)
{
case 0:break;
case 1:
{a=1;
if(input(pin_d6)==0)
{ while(input(pin_d6)==0){}
hour++;
if(hour>23)hour=0;
}
if(input(pin_d7)==0)
{ while(input(pin_d7)==0){}
hour--;
if(hour==-1)
hour=23;
}
break;
}
case 2:
{a=1;
if(input(pin_d6)==0)
{ while(input(pin_d6)==0){}
minute++;
if(minute>59)
minute=0;
}
if(input(pin_d7)==0)
{ while(input(pin_d7)==0){}
minute--;
if(minute==-1)
minute=59;
}
break;
}
case 3:
{a=1;
if(input(pin_d6)==0)
{ while(input(pin_d6)==0){}
second++;
if(second>59)
second++;
}
if(input(pin_d7)==0)
{ while(input(pin_d7)==0){}
second=0;
}
break;
}
}
}
void main()
{
Trisb = 0x00;
Trisd = 0x00;
I2C_Start();
I2C_Write(0xD0);
I2C_Write(0x07);
I2C_Write(0x10);
I2C_Stop();
while(1)
{
ktphim();
readds();
quetled();
}
}
đây là code của em ạ.em có gửi thêm file đính kèm.các anh giúp em với
#include<16f877a.h>
#use delay(clock=12m)
#fuses NOPROTECT,HS,NOWDT,NOLVP,NODEBUG,NOPUT
#byte PORTB =0x06
#byte TRISB =0x86
#bit SCL =0x06.0
#bit trisb0 =0x86.0
#bit SDA =0x06.1
#bit trisb1 =0x86.1
#byte PORTD =0x08
#byte TRISD =0x88
unsigned char hour, minute, second,c;
unsigned char ma[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x9 0};
unsigned char kt=0,a=0,i,k,x=0;
void delay(){
unsigned char i;
for (i = 0; i < 4; i++){
}
}
//-----------------------------------------
void SCL_high(){
SCL=1;
delay();
}
//------------------------------------------
void SCL_low(){
SCL=0;
delay();
}
//-------------------------------------------
void I2C_Start(){///ham chuan bi chuyen du lieu cua ds1307///
SDA = 1;//do no dc keo len nguon duong 5v
SCL = 1;//-----------------------------//
SDA = 0;//sau do sda dc keo xuong thap 0v
delay();//tao tre 1 time
SCL = 0;//scl cung dc keo xuong 0v
SDA = 1;//sau do dua sda len 1(bat dau thuc hien truyen du lieu)
}
//--------------------------------------------
void I2C_Stop(){
SDA = 0;
SCL_high();
SDA = 1;
}
//----------------------------------------------
boolean I2C_Write(unsigned char dat){//qua trinh ghi 1 byte//
unsigned char i;
boolean outbit;
for (i = 1; i <= 8; i++){
outbit=dat&0x80;//lay bit cao nhat
SDA = outbit;
dat = dat << 1;
SCL_high();//khi moi 1 bit du lieu dc truyen di thi xung scl dc dua len 1 va xuong 0
SCL_low();
}
SDA = 1; //
SCL_high();//sau ghi du 1 byte no se cho ve gia tri cho la sda va scl=1;
outbit = SDA;
SCL_low();
return(outbit);
}
//----------------------------------------------------
unsigned char I2C_Read(boolean ack){
unsigned char i, dat;
boolean inbit;
dat = 0;
for(i=1;i<=8;i++) {
SCL_high();
inbit = SDA;
dat = dat << 1;
dat = dat | inbit;
SCL_low();
}
if (ack) SDA = 0;
else SDA = 1;
SCL_high();
SCL = 0;
SDA = 1;
delay();
return(dat);
}
//-------------------------------------------------------------
void rtc_write(unsigned char add, unsigned char dat){
I2C_Start();
I2C_Write(0xd0);//gui lenh ghi.
I2C_Write(add);
I2C_Write(((dat/10)<<4)|(dat%10));
I2C_Stop();
}
unsigned char rtc_read(unsigned char add){
unsigned char dat;
I2C_Start();
I2C_Write(0xd0);
I2C_Write(add);
I2C_Start();
I2C_Write(0xd1);
dat = I2C_Read(0);
I2C_Stop();
dat = (dat & 0x0f) + (dat>>4)*10;
return (dat);
}
void writeds(){
rtc_write(0x00, second);
rtc_write(0x01, minute);
rtc_write(0x02, hour);
}
void readds()
{
if(kt==0)
{
if(a==1)
{writeds();a=0;
}
second = rtc_read(0x00);
minute = rtc_read(0x01);
hour = rtc_read(0x02);
}
}
void hienthi(unsigned char x)
{
unsigned int i,temp;
for(i=0;i<8;i++)
{
temp=x;
temp=temp&0x80;
if(temp==0x80)
output_high(pin_d2);
else
output_low(pin_d2);
x*=2;
output_low(pin_d0);
output_high(pin_d0);
}
}
void quetled()
{
if(kt==0)
{
hienthi(ma[second%10]);
hienthi(ma[second/10]);
hienthi(ma[minute%10]);
hienthi(ma[minute/10]);
hienthi(ma[hour%10]);
hienthi(ma[hour/10]);
output_low(pin_d1);
output_high(pin_d1);
}
if(kt==1)
{
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
hienthi(ma[hour%10]);
hienthi(ma[hour/10]);
output_low(pin_d1);
output_high(pin_d1);
}
if(kt==2)
{
hienthi(0xff);
hienthi(0xff);
hienthi(ma[minute%10]);
hienthi(ma[minute/10]);
hienthi(0xff);
hienthi(0xff);
output_low(pin_d1);
output_high(pin_d1);
}
if(kt==3)
{
hienthi(ma[second%10]);
hienthi(ma[second/10]);
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
hienthi(0xff);
output_low(pin_d1);
output_high(pin_d1);
}
}
void ktphim()
{
if(input(pin_d5)==0)
{
while(input(pin_d5)==0){}
kt++;
if(kt>3)kt=0;
}
switch(kt)
{
case 0:break;
case 1:
{a=1;
if(input(pin_d6)==0)
{ while(input(pin_d6)==0){}
hour++;
if(hour>23)hour=0;
}
if(input(pin_d7)==0)
{ while(input(pin_d7)==0){}
hour--;
if(hour==-1)
hour=23;
}
break;
}
case 2:
{a=1;
if(input(pin_d6)==0)
{ while(input(pin_d6)==0){}
minute++;
if(minute>59)
minute=0;
}
if(input(pin_d7)==0)
{ while(input(pin_d7)==0){}
minute--;
if(minute==-1)
minute=59;
}
break;
}
case 3:
{a=1;
if(input(pin_d6)==0)
{ while(input(pin_d6)==0){}
second++;
if(second>59)
second++;
}
if(input(pin_d7)==0)
{ while(input(pin_d7)==0){}
second=0;
}
break;
}
}
}
void main()
{
Trisb = 0x00;
Trisd = 0x00;
I2C_Start();
I2C_Write(0xD0);
I2C_Write(0x07);
I2C_Write(0x10);
I2C_Stop();
while(1)
{
ktphim();
readds();
quetled();
}
}