Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PIC18F4550 RTC interfacing

Status
Not open for further replies.

georgiod9

Newbie level 4
Newbie level 4
Joined
Dec 21, 2015
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
83
Hello,

I am interfacing the RTC DS1307 by simply writing and reading to that IC and displaying what i read on an LCD. When i run the simulation on Proteus, the RTC does not get written on nor does it read the correct time, it just read 00:00:00 PM. Now i am guessing this is a problem with the I2C protocol because when i plug the I2C Debugger i get thousands of errors: "Spurious SCL transition detected" and some random time is displayed.
I hope the problem is from some kind of bit that I should enable on some register so that the MCU is informed that I am using an I2C protocol. Below is the code and attached is the Proteus file.
Thank you.


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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
 
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
 
 
 
 
unsigned short read_ds1307(unsigned short address)
{
  unsigned short r_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
  r_data=I2C1_Rd(0);
  I2C1_Stop();
  return(r_data);
}
 
 
void write_ds1307(unsigned short address,unsigned short w_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(w_data); // send data (data to be written)
  I2C1_Stop(); // issue I2C stop signal
}
 
 
unsigned char BCD2UpperCh(unsigned char bcd)
{
  return ((bcd >> 4) + '0');
}
 
 
unsigned char BCD2LowerCh(unsigned char bcd)
{
  return ((bcd & 0x0F) + '0');
}
 
 
int Binary2BCD(int a)
{
   int t1, t2;
   t1 = a%10;
   t1 = t1 & 0x0F;
   a = a/10;
   t2 = a%10;
   t2 = 0x0F & t2;
   t2 = t2 << 4;
   t2 = 0xF0 & t2;
   t1 = t1 | t2;
   return t1;
}
 
 
int BCD2Binary(int a)
{
   int r,t;
   t = a & 0x0F;
   r = t;
   a = 0xF0 & a;
   t = a >> 4;
   t = 0x0F & t;
   r = t*10 + r;
   return r;
}
 
 
 
int second;
int minute ;
int hour ;
int hr;
int ap;
 
char time[] = "00:00:00 PM";
short set;
 
void main()
{
    LCD_INIT();
    INTCON = 0x88;
    ADCON1.PCFG3 = 1; //as digital
    ADCON1.PCFG2 = 1; //as digital
    ADCON1.PCFG1 = 1; //as digital
    ADCON1.PCFG0 = 1; //as digital
    //INTCON2.RBPU = 1; //I tried enabling the internal pullups but it ddnt work
    TRISB = 0x0F;
   /*TRISB.RB4 = 1;
    TRISB.RB5 = 1;
    TRISB.RB6 = 1;
    TRISB.RB7 = 1;*/
    PORTB = 0;
    i2c1_init(100000);
 
    LCD_CMD(_lcd_CLEAR);
    LCD_CMD(_LCD_CURSOR_OFF);
    lcd_out(1,1,"TEST");
    set = 1;               //this value acts as the increment on setting time
    
   //DO THE WRITING ON RTC
    hour = BCD2Binary(hour);
    hour = hour + set;     //here is the increment
    hour = Binary2BCD(hour);
    if((hour & 0x1F) >= 0x13)
    {
      hour = hour & 0b11100001;
      hour = hour ^ 0x20;
    }
    else if((hour & 0x1F) <= 0x00)
    {
      hour = hour | 0b00010010;
      hour = hour ^ 0x20;
    }
    write_ds1307(2, hour); //write hour
    
    minute = BCD2Binary(minute);
    minute = minute + set; //also here is the increment
    if(minute >= 60)
       minute = 0;
    if(minute < 0)
       minute = 59;
    minute = Binary2BCD(minute);
    write_ds1307(1, minute); //write minute
    write_ds1307(2,0x00); // Write second
    
    //END WRITING ON RTC
 
    while(1)
    {
      //DO THE READING FROM RTC
      second = read_ds1307(0);
      minute = read_ds1307(1);
      hour = read_ds1307(2);
      hr = hour & 0b00011111;
      ap = hour & 0b00100000;
 
 
      time[0] = BCD2UpperCh(hr);
      time[1] = BCD2LowerCh(hr);
      time[3] = BCD2UpperCh(minute);
      time[4] = BCD2LowerCh(minute);
      time[6] = BCD2UpperCh(second);
      time[7] = BCD2LowerCh(second);
      
      LCD_OUT(2,1,time);
 
        
        
    }
 
}

 

Attachments

  • SIMULATION_DS1307.zip
    18.4 KB · Views: 66
Last edited by a moderator:

Hi,

This is a very frequently discussed topic.
With many good informations, software, libraries explanations and so on.

Please do a forum search, I'm sure the solution is already here. Everything. Setting up correctely I2C interface, communicate with RTC, write to display..

In case you don't have success, then show us your I2C functions.

Klaus
 

You should set the bit 7 of the address 0 and write it to ds1307 in order to start clock count.
 

"Spurious SCL transition detected"

Hi,
I am not sure, but I think the above warning occur when you connect any other (Non I2C) peripherals to SCL line, other than pull ups.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top