PIC Vietnam

Go Back   PIC Vietnam > Microchip PIC > Các ngôn ngữ lập trình khác (CCS C, HT PIC,...)

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Trả lời
 
Ðiều Chỉnh Xếp Bài
Old 12-08-2012, 06:06 PM   #1
loveboom3012
Đệ tử 1 túi
 
Tham gia ngày: Jun 2011
Bài gửi: 18
:
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?
loveboom3012 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 12-08-2012, 08:17 PM   #2
toancdt
Đệ tử 2 túi
 
toancdt's Avatar
 
Tham gia ngày: Apr 2011
Bài gửi: 31
:
Send a message via Yahoo to toancdt
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
__________________
[email]toancdt09@gmail.com
toancdt vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 13-08-2012, 05:13 PM   #3
loveboom3012
Đệ tử 1 túi
 
Tham gia ngày: Jun 2011
Bài gửi: 18
:
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);
}
loveboom3012 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 13-08-2012, 07:14 PM   #4
semipower
Đệ tử 5 túi
 
semipower's Avatar
 
Tham gia ngày: Jul 2010
Nơi Cư Ngụ: Hà Nội, VIE
Bài gửi: 114
:
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.
__________________
Vì sao nên đọc tài liệu tiếng Anh?
http://www.picvietnam.com/forum/showthread.php?t=6819
Nguồn xung các loại: www.semitech.vn
semipower vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 08-01-2013, 01:00 PM   #5
dientu90
Đệ tử 1 túi
 
Tham gia ngày: Mar 2012
Bài gửi: 16
:
Tặng các bạn file thư viện ds1307
File Kèm Theo
File Type: rar ds1307.rar (72 Bytes, 98 lần tải)

thay đổi nội dung bởi: dientu90, 09-01-2013 lúc 10:46 AM.
dientu90 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Old 09-01-2013, 10:49 AM   #6
dientu90
Đệ tử 1 túi
 
Tham gia ngày: Mar 2012
Bài gửi: 16
:
////////////////////////////////////////////////////////////////////////////////
/// 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));
}
dientu90 vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
Trả lời


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


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


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam