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.

Adding timer to Clock system

Status
Not open for further replies.

Tarneem

Newbie level 6
Joined
Mar 25, 2015
Messages
11
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
264
PIC18F4550 Real time clock using MikroC_Need help PLZ~

Hi to All ,

I have to designe a smart digital clock, I am using PIC18F4550 and ds1307 RTC chip for generating a real time clock. I have the following code which compiles successfully on MikroC compiler. And I test it on proteus it is giving me a good results.

My problem is in real connection , I burn the code by Pickit2 burner , and i made the same connection wich is in proteus program , but the button dose not perform any thing ,, when I prees it nothing is shown , but in protuse the buttons work successfully right.

Now m Here is:
1- the code in C langage --> work fine no errors:thumbsup:
2- CKT connection in protuse --> no problems at all :thumbsup:
3- the real connection --> doeasnot perform right :thumbsdown:



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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// LCD module connections
sbit LCD_RS at RB3_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
 
sbit LCD_RS_Direction at TRISB3_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
 
#define   set PORTC.F0
#define   alarm_set PORTC.F4
#define   inc PORTC.F1
#define   next PORTC.F2
#define   dec PORTC.F5
#define salrm PORTC.F6
//#define   buttons
 
set_time();        //time/date/day set function
display();          //time/date/day display function
alarm();            //alarm set function
 
char day1=1;                    // Size spaces
char date1[]="00";
char mon1[]="00";
char year1[]="00";
char hr1[]="00";
char min11[]="00";
char sec1[]="00";
 
char k,x;
 unsigned short date=0,hour=0, mon=0, year=0x00, sec=0,min1=0;
 unsigned short hr;
 unsigned short ap;
 
int alarm_min, alarm_hour;
 
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
  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;
}
 
  void day(char d)       // Function for display day on LCD
{
switch(d)
{
  case 0:
 // print("DAY");
 Lcd_Out_Cp("DAY");
  break;
  case 1:
  //print("SUN");
  Lcd_Out_Cp("SUN");
  break;
  case 2:
  //print("MON");
  Lcd_Out_Cp("MON");
  break;
  case 3:
  //print("TUE");
  Lcd_Out_Cp("TUE");
  break;
  case 4:
  //print("WED");
  Lcd_Out_Cp("WED");
  break;
  case 5:
  //print("THU");
  Lcd_Out_Cp("THU");
  break;
  case 6:
  //print("FRI");
  Lcd_Out_Cp("FRI");
  break;
  case 7:
  //print("SAT");
  Lcd_Out_Cp("SAT");
  break;
 }
 }
 
 
 
void main()
{
   I2C1_Init(100000); //DS1307 I2C is running at 100KHz
 
   ADCON1 = 0x0F ;
   CMCON  = 0x07  ;
   TRISD0_bit=0;
   TRISD1_bit=0;
   PORTD=0;      // LED Off initially
 
   Lcd_Init();                        // Initialize LCD
   Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
   Lcd_Out(1,2,"Smart Digital");
   Lcd_Out(2,6,"Clock");
   Delay_ms(1000);
   Lcd_Cmd(0x01);
 
   while(1)
   {
     if(set==0)    //check alarm set button press
 {
  k=1;
 
 set_time();   // call time set function
 
 write_ds1307(2, hour);
 write_ds1307(1, min1);
 write_ds1307(3, day1);
 write_ds1307(4, date);
 write_ds1307(5,mon);
 write_ds1307(6,year );
 write_ds1307(0,sec);
 
   }
 if(alarm_set==0)    //check alarm set button press
 {
  alarm();    //call alarm set function
  while(alarm_set==0);
  k=5;
 }
 
        hour= read_ds1307(2);   //Read Hour
        //hr = hour & 0b00011111;
 
        if((hour& 0x1F) >= 0x13)
                    {
                     hour = hour & 0b11100001;
                      hour = hour ^ 0x20;
                    }
 
if((hour& 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00000001;
                      //hour = hour ^ 0x20;
 
                    }
 
        min1= read_ds1307(1);   //Read minutes
        sec= read_ds1307(0);   //Read seconds
        day1= read_ds1307(3);   //Read The day of the week (sunday to saturday)
        date= read_ds1307(4);   //Read The day of the week (1st to 31st)
 
        mon= read_ds1307(5);   //Read month
        year= read_ds1307(6);   //Read year
 
                    Lcd_Cmd(0x0c);
  if(hour==alarm_hour && min1==alarm_min)    // check alarm time with real time
  {
  k=7;
  if((sec>88) ||(salrm==0) ){PORTD=0;k=0;
  alarm_min=0;
  }
  PORTD=1;    //alarm output set
  Delay_ms(350); //1 Second Delay
  PORTD= 0; //LED OFF
  }
 else
  {
   PORTD=0;    //alarm output reset
  }
   display();    //display time/date/day
   Delay_ms(100);
 }
 }
 
  set_time()
{
 
  Lcd_Cmd(0x0e);
 while(k<7)
 {
  while(k==3) //set date
  {
   x=year%4;
   if(inc==0)
   {
    date = BCD2Binary(date);
    date++;while(inc==0);
 
                   //  day = day + set;
                     date = Binary2BCD(date);
                     if(date >= 0x32)
                        date = 1;
                     //if(day <= 0)
                       // day = 0x31;
     // check for 30 day month
   if(date==50){date=1;} // check for 31 day month
   display();}
if(dec==0)
   {
    date = BCD2Binary(date);
    date--;while(dec==0);
                     date = Binary2BCD(date);
 
                     if(date <= 0)
                        date = 0x31;
   display();}
 
   if(next==0){k=4;while(next==0);} //check for next digit
   Lcd_Cmd(0x81);
  }
  while(k==2) //set month
  {
   if(inc==0)
   {
    mon++;while(inc==0);
    if(mon==10){mon=mon+6;}
    if(mon==19){mon=1;}   //check for end of year
    display();
    }
if(dec==0)
   {
     mon = BCD2Binary(mon);
    mon--;while(dec==0);
mon = Binary2BCD(mon);
                   // if(mon > 0x12)
                    //  mon = 1;
                    if(mon <= 0)
                      mon = 0x12;
 
    display();
    }
 
 
    if(next==0){k=3;while(next==0);}
    Lcd_Cmd(0x84);
   }
   while(k==1)  //set year
  {
   if(inc==0)
   {
   year++;while(inc==0);
   if(year==10){year=year+6;}
   if(year==26){year=year+6;}
   if(year==41){year=0;}
   display();
   }
 
if(dec==0)
   {
   year = BCD2Binary(year);
   year--;while(dec==0);
 
                    year = Binary2BCD(year);
                    if(year <= -1)
                       year = 0x99;
                    if(year >= 0x50)
                       year = 0;
   display();
   }
 
 
 
   if(next==0){k=2;while(next==0);}
   Lcd_Cmd(0x89);
  }
  while(k==4)    //set day
  {
   if(inc==0)
   {
   day1++;while(inc==0);
   if(day1==8){day1=1;}
   display();
   }
    if(dec==0)
   {
   day1--;while(dec==0);
   if(day1<1){day1=7;}
   display();
   }
   if(next==0){
   k=5;while(next==0);
   }
   Lcd_Cmd(0x8f);
  }
  while(k==5)   //set hour
  {
   if(inc==0)
   {
   hour = BCD2Binary(hour);
   hour++;while(inc==0);
 
 
    hour = Binary2BCD(hour);
   if((hour& 0x1F) >= 0x13)
                    {
                     hour = hour & 0b11100001;
                      hour = hour ^ 0x20;
                    }
 
   display();
   }
 
if(dec==0)
   {
   hour = BCD2Binary(hour);
   hour--;while(dec==0);
 
 
    hour = Binary2BCD(hour);
 
                    if((hour& 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;
 
                    }
 
   display();
   }
 
   if(next==0){
   k=6;while(next==0);
  }
   Lcd_Cmd(0xc1);
   }
  while(k==6)    //set min
  {
   if(inc==0)
   {
   min1 = BCD2Binary(min1);
   min1++;while(inc==0);
 
 
                     if(min1 >= 60)
                        min1 = 0;
             //        if(min1 < 0)
               //         min1 = 59;
                     min1 = Binary2BCD(min1);
 
   display();}
if(dec==0)
   {
   min1 = BCD2Binary(min1);
   min1--;while(dec==0);
 
 
         //            if(min1 >= 60)
           //             min1 = 0;
                     if(min1 < 0)
                        min1 = 59;
                     min1 = Binary2BCD(min1);
 
   display();}
 
   if(next==0){k=10;while(next==0);}
   Lcd_Cmd(0xc4);
  }
 }
 
  }
   display()
   {
 
      Lcd_Cmd(0x80);
    date1[0]=BCD2UpperCh(date);
    date1[1]=BCD2LowerCh(date);
   Lcd_Out_Cp(date1);
 
  Lcd_Cmd(0x82);
  Lcd_Out_Cp("/");
  //print("/");
  Lcd_Cmd(0x83);
   mon1[0]=BCD2UpperCh(mon);
    mon1[1]=BCD2LowerCh(mon);
   Lcd_Out_Cp(mon1);
  //lcdnum(mon);
  Lcd_Cmd(0x85);
  Lcd_Out_Cp("/20");
  //print("/20");
  Lcd_Cmd(0x88);
  year1[0]=BCD2UpperCh(year);
    year1[1]=BCD2LowerCh(year);
   Lcd_Out_Cp(year1);
  //lcdnum(year);
  Lcd_Cmd(0x8a);
  Lcd_Out_Cp("  ");
  //print("  ");
  Lcd_Cmd(0x8c);
  day(day1);
  Lcd_Cmd(0xc0);
  hr = hour & 0b00011111;
   //hr = hour;
  ap = hour & 0b00100000;
  hr1[0]=BCD2UpperCh(hr);
    hr1[1]=BCD2LowerCh(hr);
   Lcd_Out_Cp(hr1);
   if(ap)
      {
        Lcd_Cmd(0xc8);
     Lcd_Out_Cp("PM");
      }
      else
      {
        Lcd_Cmd(0xc8);
     Lcd_Out_Cp("AM");
      }
  //lcdnum(hour);
  Lcd_Cmd(0xc2);
  Lcd_Out_Cp(":");
 // print(":");
  Lcd_Cmd(0xc3);
  min11[0]=BCD2UpperCh(min1);
    min11[1]=BCD2LowerCh(min1);
   Lcd_Out_Cp(min11);
 //lcdnum(min);
  Lcd_Cmd(0xc5);
  Lcd_Out_Cp(":");
 // print(":");
  Lcd_Cmd(0xc6);
  sec1[0]=BCD2UpperCh(sec);
    sec1[1]=BCD2LowerCh(sec);
   Lcd_Out_Cp(sec1);
  //lcdnum(sec);
  if(k==7)
  {
   if(sec%2==0)
    {
    Lcd_Cmd(0xca);
    // Lcd_Out_Cp("alarm");
  //  print("ALARM");
    }
else
   {
    //Lcd_Out_Cp("   ");
//print("       ");
   }
  }
  else if(k==5)
  {
    Lcd_Cmd(0xca);
    Lcd_Out_Cp("   ");;
  }
  else
  {
  //Lcd_Cmd(0xca);
  //Lcd_Out_Cp("alarm");
  }
}
 
 
 
alarm() // set alarm
 {
   unsigned int k=1;
  alarm_hour=0, alarm_min=0;
  hour=alarm_hour;
  min1=alarm_min;
  Lcd_Cmd(0x0e);
  Lcd_Cmd(0x01);
  Lcd_Out_Cp("Alarm set :");
  Delay_ms(500);
  display();
  while(k<3) //set hour for alarm
  {
   while(k==1)
   {
 
   if(inc==0)
   {
   alarm_hour = BCD2Binary(alarm_hour);
   alarm_hour++;while(inc==0);
 
    alarm_hour = Binary2BCD(alarm_hour);
    hour=alarm_hour;
   if((alarm_hour& 0x1F) >= 0x13)
                    {
                     alarm_hour = alarm_hour & 0b11100001;
                      alarm_hour = alarm_hour ^ 0x20;
                    hour=alarm_hour;
                    }
 
   display();
   }
 
    if(dec==0)
   {
   alarm_hour = BCD2Binary(alarm_hour);
   alarm_hour--;while(dec==0);
 
 
    alarm_hour = Binary2BCD(alarm_hour);
 hour=alarm_hour;
 
                    if((alarm_hour& 0x1F) <= 0x00)
                    {
                      alarm_hour = alarm_hour | 0b00010010;
                      alarm_hour = alarm_hour ^ 0x20;
                                hour=alarm_hour;
                    }
 
   display();
   }
 
   if(next==0){
   k=2;while(next==0);
  }
   Lcd_Cmd(0xc1);
   }
     //set min for alarm
   while(k==2)
   {
    if(inc==0)
   {
   alarm_min = BCD2Binary(alarm_min);
   alarm_min++;while(inc==0);
 
 
                     if(alarm_min >= 60)
                        alarm_min = 0;
             //        if(min1 < 0)
               //         min1 = 59;
                     alarm_min = Binary2BCD(alarm_min);
                     min1=alarm_min;
 
   display();}
if(dec==0)
   {
   alarm_min = BCD2Binary(alarm_min);
   alarm_min--;while(dec==0);
 
 
         //            if(min1 >= 60)
           //             min1 = 0;
                     if(alarm_min < 0)
                        alarm_min = 59;
                     alarm_min = Binary2BCD(alarm_min);
                                       min1=alarm_min;
   display();}
    if(next==0){k=3;while(next==0);}
    Lcd_Cmd(0xc4);
  }
 }
  }




2- proteus connection CKT diagram :

**broken link removed**http://www.up-00.com/



3- Real Connection Picture :

**broken link removed**http://www.up-00.com/



I have tried to change a lot of things and I have tried to research on how to solve this problem but am still facing the same problem. So can someone help me with this...
Please , I m really in trouble ..
Thank u ~
 

Re: PIC18F4550 Real time clock using MikroC_Need help PLZ~

Check your connection from buttons to the left side of MCU- on the picture it looks like you have first button connected to OSC2 pin. Maybe that picture is not clean enough ....

When system starts and without clicking buttons 1,2,4 and when click any other is it works ( ofcourse, thay has to be enabled in the code to be active at startup)
 

Re: PIC18F4550 Real time clock using MikroC_Need help PLZ~

There is a mikroC DS1307 RTC Code at libstock.com. Search for RTC2 Click.
 

Re: PIC18F4550 Real time clock using MikroC_Need help PLZ~

When simulating electronic components it is very important to have good models of components (good libraries). I thing the code is OK since it works on Proteus and your circuit uses only HIGH/LOW level - models are standard and probably OK.

[edit] I was thinking if button1 is connected to OCS2 input of PIC18F4550 (red wire), it shouldn't - but now when I rotate picture I can see that that connection is probably OK. Sorry.

You probably having a problem on your breadboard.

Sometimes those push buttons having problem to make connection on breadboard ... short legs or deep breadboard.

Next, power supply. As far as I know those components you are using can have max +5.5V as power supply.

You have power supply of 8x1.5V batteries. Do you have them in series of eight (8x1.5V = 12V) or parallel of four (4x1.5=6V two times)?

In case that you have 2x6V separated as two separate power supplies: - it is enough to have working LCD ( have tolerance to +12V) and to power PIC mcu since you have 10k resistor to get voltage lower but I would prefer having 5V without that resistor. In further, does DS1307 working on +5V - you don't have resistor there to lower voltage from 6V to 5V?
 
Last edited:

Re: PIC18F4550 Real time clock using MikroC_Need help PLZ~

When system starts and without clicking buttons 1,2,4 and when click any other is it works ( ofcourse, thay has to be enabled in the code to be active at startup)

How i can enabled them ?

You probably having a problem on your breadboard.

I have change the breadboard , but i got same result ><

Sometimes those push buttons having problem to make connection on breadboard ... short legs or deep breadboard.

I made a project just to click the button and make the LED on and of ..
with the same real connection and same code instruction and in same breadboard.. and it WORK ..
so I came to know that the problem is not in my breadboard >>:!:

Next, power supply. As far as I know those components you are using can have max +5.5V as power supply.

You have power supply of 8x1.5V batteries. Do you have them in series of eight (8x1.5V = 12V) or parallel of four (4x1.5=6V two times)?

In case that you have 2x6V separated as two separate power supplies: - it is enough to have working LCD ( have tolerance to +12V) and to power PIC mcu since you have 10k resistor to get voltage lower but I would prefer having 5V without that resistor. In further, does DS1307 working on +5V - you don't have resistor there to lower voltage from 6V to 5V?

I used 2 separate batteries , one for the pic which is 5.3 V and the other for LCD which is 4.5 v

and I made " common Ground " to avoid drop of currents "

What ur suggestion to do ?
 

Re: PIC18F4550 Real time clock using MikroC_Need help PLZ~

The problem with breadboards is that they add a lot of capacitance between the rows of connectors. If you can, use a PCB, but if you must use a breadboard keep everything as close as you can and keep the wires short.
Also you must make sure that you have all of the bypass capacitors on all of your Vdd lines to the nearest Vss connection.
I would strip out nearly everything from your code and make sure that you have the processor running correctly to simply flash a LED. Once you *know* you have the oscillator working etc. and at the correct frequency (I can't see any of the config settings in your code and I can't see where you set up the oscillator in your code either so you are possibly using the default EC setting which will not work with your circuit) then you can start adding back in one part a t a time to make sure that it is working correctly. For example make sure you can write to the LCD, then that you can read and write to the RTC etc..
I cannot see where you are debouncing your buttons. Physical buttons can bounce for anything up to 10mSec (some longer) and in that time you will get multiple transitions. When you are using each button transition to (for example) increment or decrement a value, then you can get tens of apparent pushes for each time you push or release the button.
Susan
 

Re: PIC18F4550 Real time clock using MikroC_Need help PLZ~

Aussie Susan

I want to thank u so much , I were not used bypass capacitor , and really i was know anything about it ,,

I search and read about it .. and i apply it then directly it works ..

I m really thankful to u ..

" about config setting , i m using MikroC program .. so this config i set it through a window , and I set it according to the my circuit "
 

Re: PIC18F4550 Real time clock using MikroC_Need help PLZ~

" about config setting , i m using MikroC program .. so this config i set it through a window , and I set it according to the my circuit "
That may be so, but when you are asking for assistance, especially when the issue mighty involve timing, power on setup and the like, and you don't show any code that sets these things up (i.e. you are relying on the config settings) then you really need to show us that as well. The config settings are as important (if not more so) than parts of your code.
However, I'm pleased that you have it working. And I hope that you will learn that simulators can be very misleading and that working with real components in the real world is the only way to learn.
In that vain, have you debounced the buttons as I mentioned before? If not then you will still have problem ahead.
Susan
 

Hi to All ,

I have to designe a smart digital clock, I am using PIC18F4550 and ds1307 RTC chip for generating a real time clock. I have the following code which compiles successfully on MikroC compiler. And I test it on proteus it is giving me a good results.

I posted the code previously , but i m going to post it again because I did some modification on it ,
This is the clock system .. Having a feature of alarm ,,
I tried a loot to add to it a function of " timer or countdown "
I searchd and i wrote many codes , but i dont git it .. I really need to get help .. ~

PHP:
// LCD module connections
sbit LCD_RS at RB3_bit;
sbit LCD_EN at RB2_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;

sbit LCD_RS_Direction at TRISB3_bit;
sbit LCD_EN_Direction at TRISB2_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;

#define   set PORTD.F4
#define   alarm_set PORTD.F0
#define   inc PORTD.F1
#define   next PORTD.F2
#define   dec PORTD.F5
#define salrm PORTD.F3
//#define   alarm_beep LATB.F0
// End LCD module connections
  set_time();        //time/date/day set function
 display();          //time/date/day display function
alarm();            //alarm set function
char ack;
char day1=1;
char date1[]="00";
char mon1[]="00";
char year1[]="00";
char hr1[]="00";
char min11[]="00";
char sec1[]="00";
//char date2[];
char k,x;
 unsigned short date=1,hour=1, mon=1, year=0x14, sec=0,min1=0;
 unsigned short hr;
unsigned short ap;
int alarm_min=2, alarm_hour;

unsigned short set_count = 0;

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;
}

  void day(char d)                         // Function for display day on LCD
{
switch(d)
{
  case 0:
 // print("DAY");
 Lcd_Out_Cp("DAY");
  break;
  case 1:
  //print("SUN");
  Lcd_Out_Cp("SUN");
  break;
  case 2:
  //print("MON");
  Lcd_Out_Cp("MON");
  break;
  case 3:
  //print("TUE");
  Lcd_Out_Cp("TUE");
  break;
  case 4:
  //print("WED");
  Lcd_Out_Cp("WED");
  break;
  case 5:
  //print("THU");
  Lcd_Out_Cp("THU");
  break;
  case 6:
  //print("FRI");
  Lcd_Out_Cp("FRI");
  break;
  case 7:
  //print("SAT");
  Lcd_Out_Cp("SAT");
  break;
 }
 }



void main()
{
   I2C1_Init(100000); //DS1307 I2C is running at 100KHz

    ADCON1 = 0x0F; // page 268, disable analaog
        CMCON = 0x07;
        INTCON2 = 0x80; // disable pull up in port b

        // clears internal latches
        // LATB = 0x03; // enable internal pull ups
        LATA = 0x00;
        LATC = 0x00;
        LATD = 0x00;
        LATE = 0x00;
TRISC0_bit=0;

         //TRISA0_bit = 1;
           //              TRISA1_bit = 1;
             //             TRISA2_bit = 1;
   //TRISA = 0x07;
   //PORTA = 0x00;

   Lcd_Init();                        // Initialize LCD
   Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  //Lcd_Cmd(0x80);
 Lcd_Out(1,2,"Digital Desk  ");
// Lcd_Cmd(0xc0);
 Lcd_Out(2,5,"Clock");
 Delay_ms(1000);
    Lcd_Cmd(0x01);
   //Lcd_Cmd(_LCD_CLEAR);               // Clear display

  // Lcd_out(1,1,"real time");

  // Lcd_out(2,1,"Date:");
//////hor///
// write_ds1307(2, 0x60);

write_ds1307(0,0x80);
 write_ds1307(1, 0);
   write_ds1307(2, 0X41);
 write_ds1307(3, 0X01);
 write_ds1307(4, 0X01);
 write_ds1307(5,0X01);
write_ds1307(6, 0X00);

   /*I2C1_Start();          // issue start signal
        I2C1_Wr(0XD0);       // address DS1307
        I2C1_Wr(0);            // start from word at address (REG0)
        I2C1_Wr(0x80);         // write $80 to REG0. (pause counter + 0 sec)
        I2C1_Wr(0);            // write 0 to minutes word to (REG1)
        I2C1_Wr(0x17);         // write 17 to hours word (24-hours mode)(REG2)
        I2C1_Wr(0x02);         // write 2 - Monday (REG3)
        I2C1_Wr(0x04);         // write 4 to date word (REG4)
        I2C1_Wr(0x05);         // write 5 (May) to month word (REG5)
        I2C1_Wr(0x01);         // write 01 to year word (REG6)
        I2C1_Stop();           // issue stop signal*/
       I2C1_Start();          // issue start signal
        I2C1_Wr(0XD0);       // address DS1307
        I2C1_Wr(0);            // start from word at address 0
        I2C1_Wr(0);            // write 0 to REG0 (enable counting + 0 sec)
        I2C1_Stop();           // issue stop signal
   while(1)
   {
     if(set==0)    //check alarm set button press
 {

 //display();    //display time/date/day
  // Delay_ms(1000);
        /*Lcd_Cmd(0x80);
 Lcd_Out_Cp("bbbb ");
 Lcd_Cmd(0xc0);
 Lcd_Out_Cp("ccccc ");
 Delay_ms(1000);*/
         //Delay_ms(100);
        // if(PORTA.F0 == 0)
         //{

         //Delay_ms(100);
         //if(PORTA.F0 == 0)


/*write_ds1307(2, 0);
 write_ds1307(1, 0);
 write_ds1307(3, 0);
 write_ds1307(4, 0);
 write_ds1307(5,0);
write_ds1307(6, 0);
write_ds1307(0,0x80)*/;




  k=1;
 //set=0;

 set_time();   // call time set function
 // hour=hour |0x40;
 write_ds1307(2, hour);
 write_ds1307(1, min1);
 write_ds1307(3, day1);
 write_ds1307(4, date);
 write_ds1307(5,mon);
 write_ds1307(6,year );
 write_ds1307(0,sec);

   //}
   }
 if(alarm_set==0)    //check alarm set button press
 {
  alarm();    //call alarm set function
  while(alarm_set==0);
  k=5;
 }

        hour= read_ds1307(2);   //Read Hour
        //hour= hour & 0b00011111;

       /*if((hour& 0x1F) >= 0x13)
                    {
                     hour = hour & 0b11100001;
                      hour = hour ^ 0x20;
                    }

if((hour& 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00000001;
                      //hour = hour ^ 0x20;

                    }*/

        //ap = hour & 0b00100000;


         min1= read_ds1307(1);   //Read minutes
        sec= read_ds1307(0);   //Read seconds
        day1= read_ds1307(3);   //Read The day of the week (sunday to saturday)
        date= read_ds1307(4);   //Read The day of the week (1st to 31st)

        mon= read_ds1307(5);   //Read month
        year= read_ds1307(6);   //Read year

                    Lcd_Cmd(0x0c);
  if((hour & 0b00111111)==alarm_hour && min1==alarm_min)    // check alarm time with real time
  {
  k=7;
  if((sec>88) ||(salrm==0) ){PORTD=0;k=0;
  alarm_min=0;
  }    //k=0; remove alarm

  /*if(alarm_set==0)
  {
         k=0;
     PORTC=0;    //alarm output reset
  }*/
     //Lcd_Cmd(0xca);
    // Lcd_Out_Cp("ding");
  PORTC=1;    //alarm output set
  Delay_ms(350); //1 Second Delay
  PORTC= 0; //LED OFF



  }
 else
  {
   PORTC=0;    //alarm output reset
  }

   display();    //display time/date/day
   Delay_ms(100);
 }
 }


  set_time()
{

  Lcd_Cmd(0x0e);
 while(k<7)
 {
  while(k==3) //set date
  {
   x=year%4;
   if(inc==0)
   {
    date = BCD2Binary(date);
    date++;while(inc==0);

                   //  day = day + set;
                     date = Binary2BCD(date);
                     if(date >= 0x32)
                        date = 1;
                     //if(day <= 0)
                       // day = 0x31;
     // check for 30 day month
   if(date==50){date=1;} // check for 31 day month
   display();}
if(dec==0)
   {
    date = BCD2Binary(date);
    date--;while(dec==0);


                     date = Binary2BCD(date);

                     if(date <= 0)
                        date = 0x31;
   display();}

   if(next==0){k=4;while(next==0);} //check for next digit
   Lcd_Cmd(0x81);
  }
  while(k==2) //set month
  {
   if(inc==0)
   {
    mon++;while(inc==0);
    if(mon==10){mon=mon+6;}
    if(mon==19){mon=1;}   //check for end of year
    display();
    }
if(dec==0)
   {
     mon = BCD2Binary(mon);
    mon--;while(dec==0);
mon = Binary2BCD(mon);
                   // if(mon > 0x12)
                    //  mon = 1;
                    if(mon <= 0)
                      mon = 0x12;

//    if(mon==10){mon=mon+6;}
  //  if(mon==19){mon=1;}   //check for end of year
    //if(mon<=0){mon=18;}

    display();
    }


    if(next==0){k=3;while(next==0);}
    Lcd_Cmd(0x84);
   }
   while(k==1)  //set year
  {
   if(inc==0)
   {
   year++;while(inc==0);
   if(year==10){year=year+6;}
   if(year==26){year=year+6;}
   if(year==41){year=0;}
   display();
   }

if(dec==0)
   {
   year = BCD2Binary(year);
   year--;while(dec==0);


                    year = Binary2BCD(year);
                    if(year <= -1)
                       year = 0x99;
                    if(year >= 0x50)
                       year = 0;
   display();
   }



   if(next==0){k=2;while(next==0);}
   Lcd_Cmd(0x89);
  }
  while(k==4)    //set day
  {
   if(inc==0)
   {
   day1++;while(inc==0);
   if(day1==8){day1=1;}
   display();
   }
    if(dec==0)
   {
   day1--;while(dec==0);
   if(day1<1){day1=7;}
   display();
   }
   if(next==0){
   k=5;while(next==0);
   }
   Lcd_Cmd(0x8f);
  }
  while(k==5)   //set hour
  {
   if(inc==0)
   {
   hour = BCD2Binary(hour);
   hour++;while(inc==0);
   /*if(hour==10){
   hour=hour+6;
   }*/

    hour = Binary2BCD(hour);
   if((hour& 0x1F) >= 0x13)
                    {
                     hour = hour & 0b11100001;
                      hour = hour ^ 0x20;
                    }
                    /*else if((hour& 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;
                    }*/

                    /*else if((BCD2Binary(hour) & 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;
                    }*/
  /*if(hour==26){
   hour=hour+6;
   }

   if(hour==36)
   {
   hour=0;
   }*/
   display();
   }

   /////////////dec////////
if(dec==0)
   {
   hour = BCD2Binary(hour);
   hour--;while(dec==0);
   /*if(hour==10){
   hour=hour+6;
   }*/

    hour = Binary2BCD(hour);
 //  if((hour& 0x1F) >= 0x13)
   //                 {
     //                hour = hour & 0b11100001;
       //               hour = hour ^ 0x20;
                    //}
                    if((hour& 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;

                    }

                    /*else if((BCD2Binary(hour) & 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;
                    }*/
  /*if(hour==26){
   hour=hour+6;
   }

   if(hour==36)
   {
   hour=0;
   }*/
   display();
   }


   ///////////////////dec//////////////////
   if(next==0){
   k=6;while(next==0);
  }
   Lcd_Cmd(0xc1);
   }
  while(k==6)    //set min
  {
   if(inc==0)
   {
   min1 = BCD2Binary(min1);
   min1++;while(inc==0);


                     if(min1 >= 60)
                        min1 = 0;
             //        if(min1 < 0)
               //         min1 = 59;
                     min1 = Binary2BCD(min1);

   display();}
if(dec==0)
   {
   min1 = BCD2Binary(min1);
   min1--;while(dec==0);


         //            if(min1 >= 60)
           //             min1 = 0;
                     if(min1 < 0)
                        min1 = 59;
                     min1 = Binary2BCD(min1);

   display();}

   if(next==0){k=10;while(next==0);}
   Lcd_Cmd(0xc4);
  }
 }

  }
   display()
   {

  // Lcd_Out(1,1,txt1);

      Lcd_Cmd(0x80);
    date1[0]=BCD2UpperCh(date);
    date1[1]=BCD2LowerCh(date);
   Lcd_Out_Cp(date1);
   //Lcd_Out_Cp(date2);
  //lcdnum(date);
  Lcd_Cmd(0x82);
  Lcd_Out_Cp("/");
  //print("-");
  Lcd_Cmd(0x83);
   mon1[0]=BCD2UpperCh(mon);
    mon1[1]=BCD2LowerCh(mon);
   Lcd_Out_Cp(mon1);
  //lcdnum(mon);
  Lcd_Cmd(0x85);
  Lcd_Out_Cp("/20");
  //print("-20");
  Lcd_Cmd(0x88);
  year1[0]=BCD2UpperCh(year);
    year1[1]=BCD2LowerCh(year);
   Lcd_Out_Cp(year1);
  //lcdnum(year);
  Lcd_Cmd(0x8a);
  Lcd_Out_Cp("  ");
  //print("  ");
  Lcd_Cmd(0x8c);
  day(day1);
  Lcd_Cmd(0xc0);
  hr = hour & 0b00011111;
   //hr = hour;
  ap = hour & 0b00100000;
  hr1[0]=BCD2UpperCh(hr);
    hr1[1]=BCD2LowerCh(hr);
   Lcd_Out_Cp(hr1);
   if(ap)
      {
        Lcd_Cmd(0xc8);
     Lcd_Out_Cp("PM");
        // time[9] = 'P';
         //time[10] = 'M';
      }
      else
      {
        Lcd_Cmd(0xc8);
     Lcd_Out_Cp("AM");
      }
  //lcdnum(hour);
  Lcd_Cmd(0xc2);
  Lcd_Out_Cp(":");
 // print(":");
  Lcd_Cmd(0xc3);
  min11[0]=BCD2UpperCh(min1);
    min11[1]=BCD2LowerCh(min1);
   Lcd_Out_Cp(min11);
 //lcdnum(min);
  Lcd_Cmd(0xc5);
  Lcd_Out_Cp(":");
 // print(":");
  Lcd_Cmd(0xc6);
  sec1[0]=BCD2UpperCh(sec);
    sec1[1]=BCD2LowerCh(sec);
   Lcd_Out_Cp(sec1);
  //lcdnum(sec);
  if(k==7)
  {
   if(sec%2==0)
    {
    //Lcd_Cmd(0xca);
     //Lcd_Out_Cp("  ");
  //  print("ALARM");
    }
else
   {
    //Lcd_Out_Cp("   ");
//print("       ");
   }
  }
  else if(k==5)
  {
    //Lcd_Cmd(0xca);
    //Lcd_Out_Cp("   ");;
  }
  else
  {
  //Lcd_Cmd(0xca);
  //Lcd_Out_Cp("alarm");
  }
}



alarm() // set alarm
 {
   unsigned int k=1;
  alarm_hour=1, alarm_min=0;
  hour=alarm_hour;
  min1=alarm_min;
  Lcd_Cmd(0x0e);
  Lcd_Cmd(0x01);
  Lcd_Out_Cp("Alarm set ");
  Delay_ms(200);
  display();
  while(k<3) //set hour for alarm
  {
   while(k==1)
   {

   if(inc==0)
   {
   alarm_hour = BCD2Binary(alarm_hour);
   alarm_hour++;while(inc==0);
   /*if(hour==10){
   hour=hour+6;
   }*/

    alarm_hour = Binary2BCD(alarm_hour);
    hour=alarm_hour;
   if((alarm_hour& 0x1F) >= 0x13)
                    {
                     alarm_hour = alarm_hour & 0b11100001;
                      alarm_hour = alarm_hour ^ 0x20;
                    hour=alarm_hour;
                    }
                    /*else if((hour& 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;
                    }*/

                    /*else if((BCD2Binary(hour) & 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;
                    }*/
  /*if(hour==26){
   hour=hour+6;
   }

   if(hour==36)
   {
   hour=0;
   }*/
   display();
  /*
   if(inc==0)
    {alarm_hour++;while(inc==0);
    if(alarm_hour==10){alarm_hour=alarm_hour+6;}
    if(alarm_hour==26){alarm_hour=alarm_hour+6;}
    if(alarm_hour==36){alarm_hour=0;}
hour=alarm_hour;
    display();
   */
   //display();
   }

    if(dec==0)
   {
   alarm_hour = BCD2Binary(alarm_hour);
   alarm_hour--;while(dec==0);

   /*if(hour==10){
   hour=hour+6;
   }*/

    alarm_hour = Binary2BCD(alarm_hour);
 hour=alarm_hour;
 //  if((hour& 0x1F) >= 0x13)
   //                 {
     //                hour = hour & 0b11100001;
       //               hour = hour ^ 0x20;
                    //}
                    if((alarm_hour& 0x1F) <= 0x00)
                    {
                      alarm_hour = alarm_hour | 0b00010010;
                      alarm_hour = alarm_hour ^ 0x20;
                                hour=alarm_hour;
                    }

                    /*else if((BCD2Binary(hour) & 0x1F) <= 0x00)
                    {
                      hour = hour | 0b00010010;
                      hour = hour ^ 0x20;
                    }*/
  /*if(hour==26){
   hour=hour+6;
   }

   if(hour==36)
   {
   hour=0;
   }*/
   display();
   }
   ///////////////////dec//////////////////
   if(next==0){
   k=2;while(next==0);
  }
   Lcd_Cmd(0xc1);
   }
     //set min for alarm
   while(k==2)
   {
    if(inc==0)
   {
   alarm_min = BCD2Binary(alarm_min);
   alarm_min++;while(inc==0);


                     if(alarm_min >= 60)
                        alarm_min = 0;
             //        if(min1 < 0)
               //         min1 = 59;
                     alarm_min = Binary2BCD(alarm_min);
                     min1=alarm_min;

   display();}
if(dec==0)
   {
   alarm_min = BCD2Binary(alarm_min);
   alarm_min--;while(dec==0);


         //            if(min1 >= 60)
           //             min1 = 0;
                     if(alarm_min < 0)
                        alarm_min = 59;
                     alarm_min = Binary2BCD(alarm_min);
                                       min1=alarm_min;
   display();}
    if(next==0){k=3;while(next==0);}
    Lcd_Cmd(0xc4);
  }
 }
  }

I m using buttons to set the time :
6 buttons :
1- Set button " To make the cursor appears "
2&3- UP and Down " to change the values "
4- Next " To move whiten the variable "

5- Set Alarm " to enter the user's values "
6- close " to close the LED after it become on when the alarm is on "

Now what I decide :
Having the seventh button that is responsible for the " Countdown "
when it clicked : This is going to be shown in the LCD


Countdown
00 : 00 : 00


" the user going to use the same Up & down & next buttons which already been exist, to enter his valuse ~
For example : 00 : 05 : 00
so when next will be clicked , directly on the LCD will start decreasing :

Countdown
00 : 04 : 59


when it become all zeros
Countdown
00 : 00 :00


It will convert the displaying to the real time and make the LED flashing as it there in the code for Alarm , and it can be close the flashing by using also the same sixth button ..




Help pllllllz
 

Pleas Help ..

I really need that ,, :-( :-( :-( :-( :-(
 

Quite frankly, your code is too difficult to read and analyze in detail for anyone to try and help you out, and I think part of why you are having a difficult time is because your code is written as such. Simplify your program. With an RTC, I don't think you should need so many lines of code for analyzing the hours, minutes, seconds, etc. to display (because all you have to do is read the RTC).

Mikroc also has functions for dealing with BCD if you need those: https://www.mikroe.com/download/eng/documents/compilers/mikroc/pro/pic/help/conversions_library.htm

You also did not say what specifically the problem with your code is (i.e. What part is running correctly on your hardware and which part isn't?) Also, I would suggest using unsigned variables to make sure nothing is interpreted as a signed value (you don't need signed values on a clock).
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top