Sajjadkhan
Full Member level 5
- Joined
- Sep 25, 2010
- Messages
- 307
- Helped
- 17
- Reputation
- 34
- Reaction score
- 16
- Trophy points
- 1,298
- Location
- Rawalpindi,Pakistan
- Activity points
- 4,199
this is my first attempt for I2C and i am experimenting on DS1307 with pic 16f876A. i have written a code just to read seconds on an lcd by displaying the complete byte. i am writing 0 value to the seconds register before while loop so that the oscillator starts. then in while loop i am reading the seconds register(reg0) value. but always i am receiving value of 255 and its not changing, i have also written a value to the control register but not getting a square wave on pin 7. can u guys help me for getting started?
connections are fine:
DS1307 pin 5 (SDA) ---> pic pin 15 ( 10k pullup )
DS1307 pin 6 (SCL) ---> pic pin 14 ( 10k pullup )
DS1307 pin 7 ---> ( 10k pullup ) to scope.
crystal 32.768k on pin 1 and 2.
i have checked the clock and its fine. but the data on the SDA pin is also a pulse, instead of 8bit pulses. so that means something is wrong in software side.
connections are fine:
DS1307 pin 5 (SDA) ---> pic pin 15 ( 10k pullup )
DS1307 pin 6 (SCL) ---> pic pin 14 ( 10k pullup )
DS1307 pin 7 ---> ( 10k pullup ) to scope.
crystal 32.768k on pin 1 and 2.
i have checked the clock and its fine. but the data on the SDA pin is also a pulse, instead of 8bit pulses. so that means something is wrong in software side.
Code:
sbit LCD_RS at RB0_bit;
sbit LCD_EN at RB1_bit;
sbit LCD_D4 at RB2_bit;
sbit LCD_D5 at RB3_bit;
sbit LCD_D6 at RB4_bit;
sbit LCD_D7 at RB5_bit;
sbit LCD_RS_Direction at TRISB0_bit;
sbit LCD_EN_Direction at TRISB1_bit;
sbit LCD_D4_Direction at TRISB2_bit;
sbit LCD_D5_Direction at TRISB3_bit;
sbit LCD_D6_Direction at TRISB4_bit;
sbit LCD_D7_Direction at TRISB5_bit;
sbit Soft_I2C_Scl at RC3_bit;
sbit Soft_I2C_Sda at RC4_bit;
sbit Soft_I2C_Scl_Direction at TRISC3_bit;
sbit Soft_I2C_Sda_Direction at TRISC4_bit;
int sec,sec1,sec2,sec3,temp;
void main()
{
TRISA = 0;
TRISB = 0;
TRISC = 0;
Lcd_Init();
Soft_I2C_Init();
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1, 1," : :");
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(0x07); // writing to control reg
Soft_I2C_Write(0x90);
Soft_I2C_Stop();
Soft_I2C_Start();
Soft_I2C_Write(0xD0);
Soft_I2C_Write(0x00); // writing to seconds reg
Soft_I2C_Write(0);
Soft_I2C_Stop();
while ( 1 )
{
Soft_I2C_Start();
Soft_I2C_Write(0xD1);
Soft_I2C_Write(0);
Soft_I2C_Start();
soft_I2C_Write(0xD1);
sec = Soft_I2C_Read(0); // reading from seconds reg
Soft_I2C_Stop();
temp = sec;
sec1 = temp / 100;
temp = temp % 100;
sec2 = temp / 10;
sec3 = temp % 10;
Lcd_Chr(1, 7, sec1 + 48);
Lcd_Chr(1, 8, sec2 + 48);
Lcd_Chr(1, 9, sec3 + 48);
delay_ms(500);
}
}