Hi All,
I am trying to interface RTC(DS1307 WITH I2C ) with pic24fj128ga010, somewhere i m doing mistake but i m not getting where please can any one find wt i m doing wrong and help me please..
here is my code
Code C - [expand ] 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <libpic30.h>
#include<p24fj128ga010.h>
#define FCY 16000000
_CONFIG1( JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
_CONFIG2( FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_HS & FNOSC_PRIPLL)
#define SECOND 0x00
#define MINUTE 0x01
#define HOUR 0X02
#define DAY 0x03
#define DATE 0x04
#define MONTH 0X05
#define YEAR 0X06
unsigned char TIME[ 6 ] = " : : " ;
unsigned char sec, min, hour, day, date, month, year;
unsigned char date_day[ 9 ] = "00/00/0000" ;
//****************I2C DRIVERS***************************
void __attribute__( ( interrupt, no_auto_psv) ) _MI2C1Interrupt( void )
{
//do nothing
IFS1bits.MI2C1IF = 0 ;
}
void I2c_init( void )
{
//TRISGbits.TRISG2 = 1;
//TRISGbits.TRISG3 = 1;
//I2C1STAT= 0x0030;
I2C1STAT= 0x0000 ;
//I2C1CON = 0x1240;
I2C1CON = 0x8000 ;
//I2C1ADD = 0x0D;
I2C1MSK = 0x0000 ;
I2C1BRG = 79 ; //BUAD RATE 100KHz
//IEC1bits.MI2C1IE = 1;
//I2C1CONbits.I2CEN = 1;
}
void start( void ) //start of i2c
{
I2C1CONbits.SEN = 1 ;
while ( I2C1CONbits.SEN ) ;
}
void stop( void ) //stop i2c
{
I2C1CONbits.PEN = 1 ;
while ( I2C1CONbits.PEN ) ;
}
void repeate_start( void ) //restart i2c
{
I2C1CONbits.RSEN = 1 ;
while ( I2C1CONbits.RSEN ) ;
}
void data_byte_write( unsigned char write_dat) //writing from master to slave
{
I2C1TRN = write_dat; //
while ( I2C1STATbits.TBF ) ; // Write Information bit waiting for tbf = 1;
}
unsigned char data_byte_read( unsigned char ack)
{
unsigned char recval;
I2C1CONbits.RSEN = 1 ;
while ( I2C1CONbits.RSEN ) ;
//I2C1RCV = I2C1TRN;
recval = I2C1RCV;
__delay32( 40 ) ;
while ( I2C1STATbits.RBF ) ;
while ( ! IFS1bits.MI2C1IF ) ;
if ( ack == 0 )
{
I2C1CONbits.ACKDT = 0 ;
}
else if ( ack == 1 )
{
I2C1CONbits.ACKDT = 1 ;
}
I2C1CONbits.ACKEN = 1 ;
while ( I2C1CONbits.ACKEN ) ;
return ( recval) ;
}
unsigned char getdata( unsigned char address) //reading the data from slave
{
unsigned char val;
I2C1CONbits.I2CEN = 1 ;
start( ) ;
data_byte_write( 0xD0 ) ; //slave address
while ( ! IFS1bits.MI2C1IF ) ;
while ( I2C1STATbits.ACKSTAT ) ;
data_byte_write( address) ;
while ( ! IFS1bits.MI2C1IF ) ;
while ( I2C1STATbits.ACKSTAT ) ;
repeate_start( ) ;
data_byte_write( 0xD0 + 1 ) ;
while ( ! IFS1bits.MI2C1IF ) ;
while ( I2C1STATbits.ACKSTAT ) ;
val = data_byte_read( 1 ) ;
stop( ) ;
I2C1CONbits.I2CEN = 0 ;
return ( val) ;
}
//**********************END OF I2C DRIVERS**************
int main ( void )
{
while ( ! OSCCONbits.LOCK ) ;
// CLOCK FREQUENCY
I2c_init( ) ;
while ( 1 )
{
// __delay_ms(100);
sec = getdata( SECOND) ;
min = getdata( MINUTE) ;
hour = getdata( HOUR) ;
day = getdata( DAY) ;
date = getdata( DATE) ;
month = getdata( MONTH) ;
year = getdata( YEAR) ;
}
return 0 ;
}
Thanks.
Last edited by a moderator: Apr 11, 2014