Ðăng Nhập

View Full Version : Thắc mắc về DS1307,xin tiền bối giúp đỡ!


toanfet
20-07-2010, 12:01 PM
Em đang làm 1 ví dụ nhỏ với DS1307 là dùng pic 16f877a đọc giá trị của thanh ghi giây (thanh ghi 0x00),dựa vào giá trị đó sẽ làm cho led sáng.Nếu Second=(0,15),led 1 sáng,=(16,30),led2 sáng,=(31,45),led3 sáng, =(46,60)led 4 sáng.Nhưng không hiểu sao PIC lai không thể chuyển trạng thái được.Ví dụ như ở khoảng thời gian từ giây thứ 1->15, led1 sáng nhưng đến giây thứ 16, led 2 không sáng mà led 1 vẫn sáng.Đây là code và mạch của em, mong các tiền bối chỉ giáo!
////////////////////////////////////////////////////////////////////////////////////////////
#include "D:\Electronic and Telecomunication\sendpicvietnam.h"
#use i2c(MASTER,FAST,SDA=PIN_C4,SCL=PIN_C3)
#include <C:\Program Files\PICC\Devices\ds1307.c>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#byte PORTB=0x06
int8 mode,seconds;
//#int_ext
//void ngat_RB0()
//{
//seconds=read_DS1307(0);
//}
int8 time()
{
int8 s,second;
second=read_DS1307(0);
s=(second/16)*10 +second%16;
if(s<15) return 1;
if((s>=15)&&(s<30)) return 2;
if((s>=30)&&(s<45)) return 3;

if(s>=45) return 4;
}



void main()
{
//enable_interrupts(int_ext);
//ext_int_edge(0,H_TO_L);
//enable_interrupts(global);
set_tris_b(0x00);
init_DS1307();
while(1){mode=time();
if(mode==1){
output_high(pin_b1);output_low(pin_b2);output_low( pin_b3);}
if(mode==2){
output_high(pin_b2);output_low(pin_b1);output_low( pin_b3);}
if(mode==3){
output_high(pin_b3);output_low(pin_b2);output_low( pin_b1);}
if(mode==4){output_high(pin_b3);output_high(pin_b1 );output_high(pin_b2);}

}}
////////////////////////////////////////////////////////////////////////
Còn đây là code ds1307
/////////////////////////////////////////////////////////////////////////
#define DS1307_SDA PIN_C4
#define DS1307_SCL PIN_C3

void write_DS1307(byte address, BYTE data)
{
short int status;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_write(data);
i2c_stop();
i2c_start();
status=i2c_write(0xd0);
while(status==1)
{
i2c_start();
status=i2c_write(0xd0);
}
}
BYTE read_DS1307(byte address)
{
BYTE data;
i2c_start();
i2c_write(0xd0);
i2c_write(address);
i2c_start();
i2c_write(0xd1);
data=i2c_read(0);
i2c_stop();
return(data);
}

void init_DS1307()
{
int8 temp1,temp2,temp3;
output_float(DS1307_SCL);
output_float(DS1307_SDA);
temp1= read_DS1307(0);//giay
temp2= read_DS1307(1);//phut
temp3= read_DS1307(2);//gio
temp1 &= 0x7f;
temp2 &= 0x7f;
temp3 &= 0x3f;
delay_us(5);
write_DS1307(0,temp1);
write_DS1307(1,temp2);
write_DS1307(2,temp3);
}