unsignedshort read_ds1307(unsignedshort address){unsignedshort read_data;
I2C1_Start();
I2C1_Wr(0xD0);//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(address);
I2C1_Repeated_Start();
I2C1_Wr(0xD1);//0x68 followed by 1 --> 0xD1
read_data=I2C1_Rd(0);
I2C1_Stop();return(read_data);}void write_ds1307(unsignedshort address,unsignedshort write_data){
I2C1_Start();// issue I2C start signal//address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
I2C1_Wr(0xD0);// send byte via I2C (device address + W)
I2C1_Wr(address);// send byte (address of DS1307 location)
I2C1_Wr(write_data);// send data (data to be written)
I2C1_Stop();// issue I2C stop signal}unsignedchar MSB(unsignedchar x)//Display Most Significant Bit of BCD number{return((x >>4)+'0');}unsignedchar LSB(unsignedchar x)//Display Least Significant Bit of BCD number{return((x &0x0F)+'0');}void main(){
I2C1_Init(100000);//DS1307 I2C is running at 100KHz
PORTA =0;
ADCON1 =0x06;// Configure all ports with analog function as digital
TRISA =0b000111;
TRISB =0;
TRISC =0b00011000;
PORTC =0;
PORTB =0;
I2C1_Start();
I2C1_wr(0x0D);
I2C1_wr(0);
I2C1_wr(0x80);
I2C1_wr(0x51);
I2C1_wr(0x71);
I2C1_Stop();
write_ds1307(7,0x50);// SQWE output 1Hz
write_ds1307(2,0x40);// <======== time set for 12 hour mode
oldstate =1;do{. . . .
. . . . }
second = read_ds1307(0);
minute = read_ds1307(1);
hour = read_ds1307(2);
hr = hour &0b00011111;
At least this is an information we did not know before.
We can´t see this as long as you don´t show us.
--> Please show us exactely the 8 bit that you read from the clock hours register. Without changing anything.
(We need to see ALL related bits. The "13" doesn´t tell much, because we don´t see the setup the clock is running currently)
Best is to read ALL related registers from the clock and show - without change. Best in binary or HEX.