[PIC] Problem in Glcd clock with ds1307 rtc

Status
Not open for further replies.

adarsh33

Newbie level 2
Joined
Jan 18, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
183
Hello Everyone,
Kindly anyone could help me, I am making a Glcd based clock with ds1307 rtc using pic 18f4520,I am using HS oscillator of 20Mhz. My Glcd(12864a) is working fine and I have also checked rtc(also using 32khz crystal for drving the rtc ) through oscilloscope. I am not able to find why my clock is not working and It only displays one digit ascii character and keeps blinking always. I am attaching my source code. I am using MikroC pro for Pic compiler and pic kit 3 programmer in .

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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Glcd module connections
char GLCD_DataPort at PORTD;
 
sbit GLCD_CS1 at LATB3_bit;
sbit GLCD_CS2 at LATB4_bit;
sbit GLCD_RS  at LATB0_bit;
sbit GLCD_RW  at LATB1_bit;
sbit GLCD_EN  at LATB2_bit;
sbit GLCD_RST at LATB5_bit;
sbit GLCD_DB0 at LATD0_bit;
sbit GLCD_DB1 at LATD1_bit;
sbit GLCD_DB2 at LATD2_bit;
sbit GLCD_DB3 at LATD3_bit;
sbit GLCD_DB4 at LATD4_bit;
sbit GLCD_DB5 at LATD5_bit;
sbit GLCD_DB6 at LATD6_bit;
sbit GLCD_DB7 at LATD7_bit;
 
sbit GLCD_CS1_Direction at TRISB3_bit;
sbit GLCD_CS2_Direction at TRISB4_bit;
sbit GLCD_RS_Direction  at TRISB0_bit;
sbit GLCD_RW_Direction  at TRISB1_bit;
sbit GLCD_EN_Direction  at TRISB2_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
 
sbit GLCD_DB0_Direction at TRISD0_bit;
sbit GLCD_DB1_Direction at TRISD1_bit;
sbit GLCD_DB2_Direction  at TRISD2_bit;
sbit GLCD_DB3_Direction  at TRISD3_bit;
sbit GLCD_DB4_Direction  at TRISD4_bit;
sbit GLCD_DB5_Direction at TRISD5_bit;
sbit GLCD_DB6_Direction  at TRISD6_bit;
sbit GLCD_DB7_Direction at TRISD7_bit;
// End Glcd module connections
 
 
 
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) + '1');
}
 
 
unsigned char BCD2LowerCh(unsigned char bcd)
{
  return ((bcd & 0x0F) + '1');
}
 
 
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 day;
int dday;
int month;
int year;
int ap;
 
unsigned short set_count = 0;
short set;
 
char *time = "00:00:00 PM";
char *date = "00-00-00";
 
void main()
{
 
 
I2C1_Init(100000); //DS1307 I2C is running at 100KHz
 
    ADRESL = 0;
    ADRESH = 0;
   CMCON = 0x07;   // To turn off comparators
   ADCON1 = 0xFF;  // To turn off analog to digital converters
 
 
   TRISA = 0x07;
   PORTA = 0x00;
 
   Glcd_Init();                        // Initialize GLCD
   Glcd_Fill(0xFF);
   Delay_ms(100);
 
   Glcd_Set_Font(Font_Glcd_Character8x7, 8 , 7, 32);
   Glcd_Write_Text( "Time:" , 80, 1, 2);
   Glcd_Write_Text( "Date:" , 65, 5, 2);
 
 
   do
   {
     set = 0;
     if(PORTA.F0 == 0)
     {
         Delay_ms(100);
         if(PORTA.F0 == 0)
         {
             set_count++;
             if(set_count >= 7)
             {
                set_count = 0;
             }
         }
     }
     if(set_count)
     {
        if(PORTA.F2 == 0)
        {
          Delay_ms(100);
          if(PORTA.F2 == 0)
              set = 1;
        }
 
        if(PORTA.F0 == 0)
        {
          Delay_ms(100);
          if(PORTA.F2 == 0)
              set = -1;
        }
        if(set_count && set)
        {
          switch(set_count)
          {
            case 1:
                    hour = BCD2Binary(hour);
                    hour = hour + set;
                    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
                    break;
            case 2:
                     minute = BCD2Binary(minute);
                     minute = minute + set;
                     if(minute >= 60)
                        minute = 0;
                     if(minute < 0)
                        minute = 59;
                     minute = Binary2BCD(minute);
                     write_ds1307(1, minute); //write min
                     break;
            case 3:
                    if(abs(set))
                      write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
                    break;
            case 4:
                     day = BCD2Binary(day);
                     day = day + set;
                     day = Binary2BCD(day);
                     if(day >= 0x32)
                        day = 1;
                     if(day <= 0)
                        day = 0x31;
                     write_ds1307(4, day); // write date 17
                     break;
            case 5:
                    month = BCD2Binary(month);
                    month = month + set;
                    month = Binary2BCD(month);
                    if(month > 0x12)
                      month = 1;
                    if(month <= 0)
                      month = 0x12;
                    write_ds1307(5,month); // write month 6 June
                    break;
            case 6:
                    year = BCD2Binary(year);
                    year = year + set;
                    year = Binary2BCD(year);
                    if(year <= -1)
                       year = 0x99;
                    if(year >= 0x50)
                       year = 0;
                    write_ds1307(6, year); // write year
                    break;
          }
        }
     }
 
      second = read_ds1307(0);
      minute = read_ds1307(1);
      hour = read_ds1307(2);
       hr = hour & 0b00011111;
       ap = hour & 0b00100000;
      dday = read_ds1307(3);
      day = read_ds1307(4);
      month = read_ds1307(5);
      year = read_ds1307(6);
 
 
      time[0] = BCD2UpperCh(hr);
      time[1] = BCD2LowerCh(hr);
      time[3] = BCD2UpperCh(minute);
      time[4] = BCD2LowerCh(minute);
      time[6] = BCD2UpperCh(second);
      time[7] = BCD2LowerCh(second);
 
      date[0] = BCD2UpperCh(day);
      date[1] = BCD2LowerCh(day);
      date[3] = BCD2UpperCh(month);
      date[4] = BCD2LowerCh(month);
      date[6] = BCD2UpperCh(year);
      date[7] = BCD2LowerCh(year);
 
      if(ap)
      {
         time[9] = 'P';
         time[10] = 'M';
      }
      else
      {
         time[9] = 'A';
         time[10] = 'M';
      }
 
      Glcd_Write_Char( time, 80, 2, 2);
      Glcd_Write_Char( date, 70, 6, 2);
 
      Delay_ms(100);
 
 
   }while(1);
}

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…