CODE:
unsigned char read_I2C(bit ACK_Bit)
{
unsigned char Data=0;
int i;
SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
nop();
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
nop();nop();
//ACK
SCL = 1;
nop();nop();
SCL = 0;
return Data;
}
đây là hàm lấy dữ liệu từ DS1307, mình ko hiểu tại sao lại cho SDA = 1; ( phần in đậm trong code ). Bạn nào biết giúp mình với ????????
code:
#include "RTC/rtc.h"
xdata unsigned char RTC_ARR[28]; // second,minute,.....,year,sum
unsigned char BCD2HEX(unsigned int bcd)
{
unsigned char temp;
tempbcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
return temp;
}
void delay()
{
int i,j;
for(i =0;i<100;i++)
{
for(j=0;j<100;j++)
{
}
}
}
void main()
{
P1 = 0x00;
P3 = 0xFF;
RTC_ARR[0] = 0x40;
RTC_ARR[1] = 0x19;
RTC_ARR[2] = 0x10;
RTC_ARR[3] = 0x03;
RTC_ARR[4] = 0x03;
RTC_ARR[5] = 0x02;
RTC_ARR[6] = 0x10;
RTC_ARR[7] = 0x14;
RTC_ARR[8] = 0x40;
RTC_ARR[9] = 0x19;
RTC_ARR[10] = 0x10;
RTC_ARR[11] = 0x03;
RTC_ARR[12] = 0x03;
RTC_ARR[13] = 0x02;
RTC_ARR[14] = 0x10;
RTC_ARR[15] = 0x14;
RTC_ARR[16] = 0x40;
RTC_ARR[17] = 0x19;
RTC_ARR[18] = 0x10;
RTC_ARR[19] = 0x03;
RTC_ARR[20] = 0x03;
RTC_ARR[21] = 0x02;
RTC_ARR[22] = 0x10;
RTC_ARR[23] = 0x14;
RTC_ARR[24] = 0x40;
RTC_ARR[25] = 0x19;
RTC_ARR[26] = 0x10;
RTC_ARR[27] = 0x03;
write_RTC(&RTC_ARR[0]); // Set RTC
while(1)
{
read_RTC(&RTC_ARR[0]);
P3=RTC_ARR[0];//BCD2HEX(RTC_ARR[0]);
delay();
//DelayMs(1000); // delay about 1 second
}
}
đây là chương trình DS1307 với 8051. mình không hiểu xdata unsigned char là kiểu dữ liệu như thế nào ?
thanks mọi người trước nha ! Cố gắng giúp mình với
unsigned char read_I2C(bit ACK_Bit)
{
unsigned char Data=0;
int i;
SDA = 1;
for (i=0;i<8;i++)
{
SCL = 1;
Data<<= 1;
Data = (Data | SDA);
SCL = 0;
nop();
}
if (ACK_Bit == 1)
SDA = 0; // Send ACK
else
SDA = 1; // Send NO ACK
nop();nop();
//ACK
SCL = 1;
nop();nop();
SCL = 0;
return Data;
}
đây là hàm lấy dữ liệu từ DS1307, mình ko hiểu tại sao lại cho SDA = 1; ( phần in đậm trong code ). Bạn nào biết giúp mình với ????????
code:
#include "RTC/rtc.h"
xdata unsigned char RTC_ARR[28]; // second,minute,.....,year,sum
unsigned char BCD2HEX(unsigned int bcd)
{
unsigned char temp;
tempbcd>>8)*100)|((bcd>>4)*10)|(bcd&0x0f);
return temp;
}
void delay()
{
int i,j;
for(i =0;i<100;i++)
{
for(j=0;j<100;j++)
{
}
}
}
void main()
{
P1 = 0x00;
P3 = 0xFF;
RTC_ARR[0] = 0x40;
RTC_ARR[1] = 0x19;
RTC_ARR[2] = 0x10;
RTC_ARR[3] = 0x03;
RTC_ARR[4] = 0x03;
RTC_ARR[5] = 0x02;
RTC_ARR[6] = 0x10;
RTC_ARR[7] = 0x14;
RTC_ARR[8] = 0x40;
RTC_ARR[9] = 0x19;
RTC_ARR[10] = 0x10;
RTC_ARR[11] = 0x03;
RTC_ARR[12] = 0x03;
RTC_ARR[13] = 0x02;
RTC_ARR[14] = 0x10;
RTC_ARR[15] = 0x14;
RTC_ARR[16] = 0x40;
RTC_ARR[17] = 0x19;
RTC_ARR[18] = 0x10;
RTC_ARR[19] = 0x03;
RTC_ARR[20] = 0x03;
RTC_ARR[21] = 0x02;
RTC_ARR[22] = 0x10;
RTC_ARR[23] = 0x14;
RTC_ARR[24] = 0x40;
RTC_ARR[25] = 0x19;
RTC_ARR[26] = 0x10;
RTC_ARR[27] = 0x03;
write_RTC(&RTC_ARR[0]); // Set RTC
while(1)
{
read_RTC(&RTC_ARR[0]);
P3=RTC_ARR[0];//BCD2HEX(RTC_ARR[0]);
delay();
//DelayMs(1000); // delay about 1 second
}
}
đây là chương trình DS1307 với 8051. mình không hiểu xdata unsigned char là kiểu dữ liệu như thế nào ?
thanks mọi người trước nha ! Cố gắng giúp mình với
Comment