#include<reg51.h> #include<stdio.h> sbit check=P2^3; sbit SDA = P2^0; sbit SCL = P2^1; //declare function void delay(void); void I2C_Start(void); void I2C_Stop(void); bit I2C_Write(unsigned char dat); unsigned char I2C_Read(bit ack); //RTC DS1307 //void rtc_write(unsigned char add , unsigned char dataa); void rtc_read(char); void main() { while(1) { char a,b,c,d,e,f; a=0x00; b=0x01; c=0x02; d=0x04; e=0x05; f=0x06; rtc_read(a); rtc_read(b); rtc_read(c); rtc_read(d); rtc_read(e); rtc_read(f); } } void delay(void) { unsigned char i; for (i = 0; i < 20; i++) { ; } } void I2C_Start(void) { SDA=1; // i2c start bit sequence delay(); SCL=1; delay(); SDA=0; delay(); SCL=0; delay(); P3=0x00; } void I2C_Stop(void) { SDA=0; // i2c stop bit sequence delay(); SCL=1; delay(); SDA=1; delay(); P3=0x01; } bit I2C_Write(unsigned char dat) { unsigned char i; bit outbit; for (i = 1; i <= 8; i++) { outbit=dat&0x80; SDA=outbit; dat = dat << 1; SCL=1; SCL=0; } SDA=1; // set SDA to receive Acknowledge SCL=0; outbit = SDA; // check busy or not SCL=0; return(outbit); // if outbit=1 ,A=1: error // if outbit=0 ,A=0: ok P3=0x03; } unsigned char I2C_Read(bit ack) { unsigned char i, dat; bit inbit; dat = 0; for(i=1;i<=8;i++) { SCL=1; inbit = SDA; dat = dat << 1; dat = dat | inbit; SCL=0; } if(ack) { SDA=0; // set SDA = 0 (ACK) } else { SDA=1; // set Non ACK } SCL=1; SCL=0; SDA=1; // Set SDA = 1 for next read delay(); return(dat); P3=0x04; } void rtc_read(char add) { bit ch1,ch2,ch3; unsigned char dataa ; I2C_Start(); ch1=I2C_Write(0xd0); // check=ch1; ch2=I2C_Write(add); // check=ch2; I2C_Start(); // restart ch3=I2C_Write(0xd1); // check=ch3; dataa = I2C_Read(0); I2C_Stop(); dataa = (dataa & 0x0f) + (dataa>>4)*10; // P1=dataa; } I have written this code for read value from rtc.I am not getting any syntax error but its not working.Someone pls help me to correct code and if I have made any logical error to write this code.
"But it is not working"???
Don't you manage to give a better description of what happens/doesn't happen, and what you have done to try to figure out what can be wrong?
By the way - notice the very wide layout of this thread. Don't mark your message text as source code. The difference between message text and source code is that source code must maintain original line breaks...
void delay(void) { unsigned char i; for (i = 0; i < 20; i++) { ; } }
Oh no, not again!
You didn't seriously expect that nonsense to actually produce any delay, did you?
ANOTHER FOOL!
MY DISASEMBLY HAS CODE FOR DELAY LIKE THAT. SWITCH OFF OPTIMIZATION AND THE GOOD CODE APPEARS.
Is your keyboard broken, or do you specifically like to scream like a small child? I kind of suspect the keyboard is in working order...
NO CODE IN LOOP. MY BACKSIDE!
MY BACKSIDE is that what is sitting on the shift key?
Erik
YOU ARE MISSING THE POINT. C CODED DELAYS MAY VARY BASED ON THE COMPILER VERSION. Or even the optimization level (As you noted).