PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Các ngôn ngữ lập trình khác (CCS C, HT PIC,...) (http://www.picvietnam.com/forum/forumdisplay.php?f=12)
-   -   giao tiếp i2c với ds1307, giúp mình giải thích đoạn code này với! (http://www.picvietnam.com/forum/showthread.php?t=30164)

loveboom3012 12-08-2012 06:06 PM

giao tiếp i2c với ds1307, giúp mình giải thích đoạn code này với!
 
Code:

void update_ds1307(void)
{
        int8 data=0; //luu tru du lieu tam thoi de gui vao ds1307
        i2c_start();
        i2c_write(0xd0);
        i2c_start(0x00);// ghi du lieu bat dau tu vi tri 00
        data=sec1+(sec2<<4);
        data=data&0b01111111;

        i2c_write(data);

        data=min1+(min2<<4);
        i2c_write(data);

        hour=hour1+(hour2<<4);
        i2c_write(data);
       
        data=day;
        i2c_write(data);
       
        data=date1+(date2<<4);
        i2c_write(data);

        data=month1+(month2<<4);
        i2c_write(data);

        data=year1+(year2<<4);
        i2c_write(data);
       
        data=0x00;
        i2c_write(data);
       
        i2c_stop();       
}

phần data=sec1+(sec2<<4);
data=data&0b01111111;

i2c_write(data);
có nghĩa là sao?

toancdt 12-08-2012 08:17 PM

sec2<<4; // sec2 dịch trái 4 bit
ví dụ: sec2=0b00001111 , sec2<<4 => sec2=0b11110000

data&0b01111111;//nhân từng bit giữa data và 0b01111111
ví dụ: a=data&0b00000111, data=0b01011010

0b01011010
x
0b00000111
a = 0b00000010

loveboom3012 13-08-2012 05:13 PM

mình tham khảo code như thế này, viết bằng ccs , báo lỗi, mấy bạn giúp mình với, đang làm đồ án cái lịch vạn niên
Code:

#include <16f877a.h>
#include <def_877a.h>
#fuses nowdt,noprotect,nolvp,hs,xt
#use delay(clock=4000000)
#use i2c(master,slow,sda=pin_c4,scl=pin_c3)
// cac dinh nghia
#define status        rd4;
#define mode        rd5;
#define increase rd6;
#define decrease rd7;
#define led1        ra0;
#define led2        ra1;
#define led3        ra2;
#define led4        ra3;
#define led5        ra4;
#define led6        ra5;
#define led7        re0;
#define led8        re1;
//khai bao cac bien
int8 sec,min,hour,dow,month,year;
const unsigned char maled[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
int8        sec1,sec2,min1,min2,date1,hour1,hour2,dow;
int8        day,date2,month1,month2,year1,year2,year3,year4;
void hienthi1(void);
void hienthi2(void);
void update_time(void);
void set_time(void);
void set_min(void);
void set_hour(void);
void set_day(void);
void set_date(void);
void set_month(void);
void set_year(void);
void main()
{
        set_tris_A(0x00);
        set_tris_b(0x01);
        set_tris_d(0xff);
        set_tris_e(0x00);
        porta=porte=0xff;
        while(1)
        {
                update_time();
                if(status==0)
                        {hienthi2();}
                if(mode==0)
                        {set_time();}
        }       
}       
void update_time(void)
{
        i2c_start();
        i2c_write(0xd0);        //gui dia chi cua slave
        i2c_write(0x00);        //thiet lap lai con tro
        i2c_Stop();
        i2c_start();
        i2c_write(0xd1);
        sec=i2c_read(0);
        min=i2c_read(1);
        hour=i2c_read(2);
        dow=i2c_read(3);
        date=i2c_read(4);
        month=i2c_read(5);
        year=i2c_read(6);
        i2c_stop();
        /////thuc hien chuyen doi
        sec1=(sec&0x0f);
        sec2=(sec&0x70)>>4;
        min1=(min&0x0f);
        min2=(min&0x70)>>4;
        hour1=(sec&0x0f);
        hour2=(sec&0x30)>>4;
        day=(dow&0x07);
        date1=(date&0x0f);
        date2=(date&0x30)>>4;
        month1=(sec&0x0f);
        month2=(sec&0x10)>>4;
        yeardv=(year&0x0f);
        yearch=(year&0xf0)>>4;
}
//-------------Hien thi sec-min-hour--------------
void hienthi1()
{
        portb=maled[sec1);        led6=0;        delay_us(100);        led6=1;
        portb=maled(sec2);        led5=0;        delay_us(100);        led5=1;
        portb=maled[min1);        led4=0;        delay_us(100);        led4=1;
        portb=maled(min2);        led3=0;        delay_us(100);        led3=1;
        portb=maled[hour1);led2=0;        delay_us(100);        led2=1;
        portb=maled(hour2);led1=0;        delay_us(100);        led1=1;
        portb=maled[day);        led8=0;        delay_us(100);        led8=1;
}
//------------Hien thi day-date-month-year
void hienthi2()
{
        int16 i=0;
        while(status==0)
                {
                }
        while(i<600)
                {
                        portb=maled(date1);led1=0;        delay_us(100);        led1=1;
                        portb=maled(date2);led2=0;        delay_us(100);        led1=1;
                        portb=maled(month1);led3=0;        delay_us(100);        led3=1;
                        portb=maled(month2);led4=0;        delay_us(100);        led4=1;
                        portb=maled(year1);led8=0;        delay_us(100);        led8=1;
                        portb=maled(year2);led7=0;        delay_us(100);        led7=1;
                        portb=maled(0x01);        led6=0;delay_us(100);        led6=1;
                        portb=maled(0x02);        led5=0;delay_us(100);        led5=1;
                        i++;
                }               
}
void update_ds1307(void)
{
        int8 data=0; //luu tru du lieu tam thoi de gui vao ds1307
        i2c_start();
        i2c_write(0xd0);
        i2c_start(0x00);// ghi du lieu bat dau tu vi tri 00
        data=sec1+(sec2<<4);
        data=data&0b01111111;
        i2c_write(data);

        data=min1+(min2<<4);
        i2c_write(data);

        hour=hour1+(hour2<<4);
        i2c_write(data);
       
        data=day;
        i2c_write(data);
       
        data=date1+(date2<<4);
        i2c_write(data);

        data=month1+(month2<<4);
        i2c_write(data);

        data=year1+(year2<<4);
        i2c_write(data);
       
        data=0x00;
        i2c_write(data);
       
        i2c_stop();       
}
void set_time(void)
{
        set_year(void);
        set_month(void);
        set_date(void);
        set_day(void);
        set_hour(void);
        set_min(void);
        set_sec(void);       
        while(mode==0)
        {
        }
        update_ds1307();
}
void set_year(void)
{
        while(mode==0)
                {
                }
        do
        {
                if(increase==0)
                {
                        while(increase==0)
                                {
                                }
                        year1++;
                        if((year2==9) & (year1==10))
                                {
                                        year1=year2=0;
                                }
                        if(year1==10)
                                {
                                        year2++;year1=0;
                                }
                }
                if(decrease==0)
                {
                        while(decrease==0)
                                {
                                }
                        year1--;
                        if((year2==0) & (year1==255))
                                {year1=year2=9;}
                        if(year1==255)
                                {year1=9,year2--;};
                       
                }
                portb=0x02; led5=0; delay_us(200);        led5=1;
                portb=0x00; led6=0; delay_us(200);        led6=1;
                portb=maled[year2]; led7=0; delay_us(200);        led7=1;
                portb=maled[year1]; led8=0; delay_us(200);        led8=1;
               
        }
        while(mode==1);       
}
void set_month(void)
{
        while(mode==0){}
        do
                {
                                if(increase==0)
                {
                        while(increase==0)
                                {
                                }
                        month1++;
                        if((month2==1) & (month1==3))
                                {
                                        month1=1;month2=0;
                                }
                        if(month1==10)
                                {
                                        month2++;month1=0;
                                }
                }
                if(decrease==0)
                {
                        while(decrease==0)
                                {
                                }
                        month1--;
                        if((month2==0) & (month1==0))
                                {month1=2;month2=1;}
                        if(month1=0)
                                {month1=9,month2--;};
                       
                }
                portb=maled[month2]; led3=0; delay_us(200);        led3=1;
                portb=maled[month1]; led4=0; delay_us(200);        led4=1;
                }
                while(mode==1);
}
void set_date(void)
{
        while(mode==0)
        {               
        }
        do
                {
                        if(increase==0)
                        {
                                while(increase==0){}
                                date1++;
                                if((date2==3)& (date1==2))
                                        {
                                                date2=0;date1=1
                                        }
                                if(date1==10)
                                        {date2++;date1=0;}
                        }
                        if(decrease==0)
                        {
                                while(decrease==0){}
                                date1--;
                                if((date2==0)&(date1==0))
                                {
                                        date2=3;;date1=1;
                                }
                                if(date1==255);
                                {date2--,date1=9;}
                        }
                        portb=maled[date2];        led1=0;delay_us(200);led1=1;
                        portb=maled[date1];        led2=0;delay_us(200);led2=1;
                }
                while(mode==1);
}
void set_day(void)
{
        while(mode==0){}
        do
                {
                        if(increase==0)
                        {
                                while(increase==0){}
                                day++;
                                if(day==8)
                                        {day=1;}
                        }
                        if(decrease==0)
                                {
                                        while(decrease==0){}
                                        day--;
                                        if(day==255)
                                        {day=7;}
                                }
                        portb=maled[day];        led8=0;delay_us(200);led8=1;
                }
        while(mode==1);
}
void set_hour(void)
{
        while(mode==0){}
        do
        {
                if(increase==0)
                {
                        while(increase==0){}
                        hour1++;
                        if((hour2==2)&(hour1==4))
                                {hour2=0;hour1=0;}
                        if(hour1==1)
                                {hour2++;hour1=0}
                }
                if(decrease==0)
                {
                        while(decrease==0)
                                {}
                        hour1--;
                        if((hour2==0)&(hour1==255))
                                {hour2=2;hour1=3;}
                        if(hour1==255)
                                {hour2--;hour1=9;}
                }
                portb=maled[hour2];led1=0;delay_us(200);led1=1;
                portb=maled[hour1];led2=0;delay_us(200);led2=1;
        }
        while(mode==1);
}
void set_min(void)
{
        while(mode==0){}
        do
        {
                if(increase==0)
                {
                        while(increase==0){}
                        min1++;
                        if((min2==5)&(min1==10))
                                {min2=0;min1=0;}
                        if(min1==10)
                                {min2++;min1=0}
                }
                if(decrease==0)
                {
                        while(decrease==0)
                                {}
                        min1--;
                        if((min2==0)&(min1==255))
                                {min2=5;min1=9;}
                        if(min1==255)
                                {min2--;min1=9;}
                }
                portb=maled[min2];led3=0;delay_us(200);led3=1;
                portb=maled[min1];led3=0;delay_us(200);led4=1;
        }
        while(mode==1);
}


semipower 13-08-2012 07:14 PM

Bạn cần nêu vắn tắt về nội dung chương trình, đặc biệt là nói rõ về thông báo lỗi. Như vậy mọi người mới giúp được.

dientu90 08-01-2013 01:00 PM

2 Attachment(s)
Tặng các bạn file thư viện ds1307

dientu90 09-01-2013 10:49 AM

////////////////////////////////////////////////////////////////////////////////
/// DS1307.C ///
/// Driver for Real Time Clock ///
/// ///
/// ds1307_init() - Enable oscillator without clearing the seconds register -///
/// used when PIC loses power and DS1307 run from 3V BAT ///
/// - Disable squarewave output ///
/// ///
/// ds1307_set_date_time(day,mth,year,dow,hour,min,sec ) Set the date/time ///
/// ///
/// ds1307_get_date(day,mth,year,dow) Get the date ///
/// ///
/// ds1307_get_time(hr,min,sec) Get the time ///
/// ///
////////////////////////////////////////////////////////////////////////////////

#define RTC_SDA PIN_D5
#define RTC_SCL PIN_D4

#use i2c(master, sda=RTC_SDA, scl=RTC_SCL)

BYTE bin2bcd(BYTE binary_value);
BYTE bcd2bin(BYTE bcd_value);

void ds1307_init(void)
{
BYTE seconds = 0;

i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_start();
i2c_write(0xD1); // RD from RTC
seconds = bcd2bin(i2c_read(0)); // Read current "seconds" in DS1307
i2c_stop();
seconds &= 0x7F;

delay_us(3);

i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x00); // REG 0
i2c_write(bin2bcd(seconds)); // Start oscillator with current "seconds value
i2c_start();
i2c_write(0xD0); // WR to RTC
i2c_write(0x07); // Control Register
i2c_write(0x80); // Disable squarewave output pin
i2c_stop();

}

void ds1307_set_date_time(BYTE day, BYTE mth, BYTE year, BYTE dow, BYTE hr, BYTE min, BYTE sec)
{
sec &= 0x7F;
hr &= 0x3F;

i2c_start();
i2c_write(0xD0); // I2C write address
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_write(bin2bcd(sec)); // REG 0
i2c_write(bin2bcd(min)); // REG 1
i2c_write(bin2bcd(hr)); // REG 2
i2c_write(bin2bcd(dow)); // REG 3
i2c_write(bin2bcd(day)); // REG 4
i2c_write(bin2bcd(mth)); // REG 5
i2c_write(bin2bcd(year)); // REG 6
i2c_write(0x80); // REG 7 - Disable squarewave output pin
i2c_stop();
}

void ds1307_get_date(BYTE &day, BYTE &mth, BYTE &year, BYTE &dow)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x03); // Start at REG 3 - Day of week
i2c_start();
i2c_write(0xD1);
dow = bcd2bin(i2c_read() & 0x7f); // REG 3
day = bcd2bin(i2c_read() & 0x3f); // REG 4
mth = bcd2bin(i2c_read() & 0x1f); // REG 5
year = bcd2bin(i2c_read(0)); // REG 6
i2c_stop();
}

void ds1307_get_time(BYTE &hr, BYTE &min, BYTE &sec)
{
i2c_start();
i2c_write(0xD0);
i2c_write(0x00); // Start at REG 0 - Seconds
i2c_start();
i2c_write(0xD1);
sec = bcd2bin(i2c_read() & 0x7f);
min = bcd2bin(i2c_read() & 0x7f);
hr = bcd2bin(i2c_read(0) & 0x3f);
i2c_stop();

}

BYTE bin2bcd(BYTE binary_value)
{
BYTE temp;
BYTE retval;

temp = binary_value;
retval = 0;

while(1)
{
// Get the tens digit by doing multiple subtraction
// of 10 from the binary value.
if(temp >= 10)
{
temp -= 10;
retval += 0x10;
}
else // Get the ones digit by adding the remainder.
{
retval += temp;
break;
}
}

return(retval);
}


// Input range - 00 to 99.
BYTE bcd2bin(BYTE bcd_value)
{
BYTE temp;

temp = bcd_value;
// Shifting upper digit right by 1 is same as multiplying by 8.
temp >>= 1;
// Isolate the bits for the upper digit.
temp &= 0x78;

// Now return: (Tens * 8) + (Tens * 2) + Ones

return(temp + (temp >> 2) + (bcd_value & 0x0f));
}


Múi giờ GMT. Hiện tại là 08:11 PM.

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam