All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Embedded Systems and Microcontrollers

Notices

Embedded Systems and Microcontrollers Discussion forum for projects and working with embedded systems and microcontrollers (FPGAs, PICs, AVRs). Get help with hardware issues and embedded programming.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 09-08-2012, 07:52 AM
roshani roshani is offline
New Member
 
Join Date: Sep 2012
Posts: 3
Default set date and time in PIC16f877A using push buttons

I am trying to set time(hour, minute, second) and date(year, month, day) using pic16f877A, 16*2 LCD display and push buttons. i have used timer0 for RTC. How do i define variables and set the date and time with push buttons? please help me with code in c language.
Reply With Quote
  #2  
Old 09-08-2012, 09:05 AM
t06afre's Avatar
t06afre t06afre is offline
Senior Member
 
Join Date: May 2009
Posts: 5,939
Default

Schoolwork Anyway I think you have to tell us more about your project before we can help. Have you made some schematic you can show us. And what is your coding so far
Reply With Quote
  #3  
Old 09-08-2012, 02:33 PM
roshani roshani is offline
New Member
 
Join Date: Sep 2012
Posts: 3
Default

i am working on data logger project. analog inputs are made to the channels(AN0-AN5). portB pins (RB4-RB7) have been used to generate interrupt on change in their input state (RBIE=1). portD(D0-D7) is used for providing data to LCD, RC4 and RC5 connected to control pins.
The ports are initialized as:
TRISA=0xff;
TRISC=0x00;
TRISB=0b11111111;
TRISD=0x00;
i have attached 3 views of my circuit layout.
i have handled the ADC conversions. To record the data, first i have to set the date and time once.
Attached Images
File Type: png DL1.PNG (24.4 KB, 33 views)
File Type: png DL2.PNG (24.6 KB, 25 views)
File Type: png DL3.PNG (51.5 KB, 23 views)
Reply With Quote
  #4  
Old 09-08-2012, 03:05 PM
absf absf is online now
Senior Member
 
Join Date: Dec 2010
Location: Sarawak (GMT+8)
Posts: 971
Default

If you look an ordinary digital clock, there should be some buttons like MODE, UP, DOWN,"<-" and "->" for setting the clock.

Of course, you may also need to give the Date and time some default values in your program like:

(Date) 01:01:2012
(Time) 12:00:00

But once you switched on your mcu, the time and date would never be the current time and date, cause you are not using an external RTC with battery back up. Even if you did use an RTC, it would still need to be set up once at least.

As for how you can set up the Date and Time, I am sure there are lots of programs written in PIC C that you can refer to. But first you'll need to put some push button switches on your PIC.

Allen
Reply With Quote
The Following User Says Thank You to absf For This Useful Post:
roshani (09-08-2012)
  #5  
Old 09-08-2012, 03:23 PM
t06afre's Avatar
t06afre t06afre is offline
Senior Member
 
Join Date: May 2009
Posts: 5,939
Default

The analog part with the opamps of your project will not work in any way. It has to be redesign. And I can not grasp what you want it to do. Your project has several parts. Why not start with the more easy parts. Like writing stuff to the LCD.
Are you planing to use the internal oscillator. In a real world project that would not be wise. A common method then using a PIC as clock. Is to use a clock crystal connected to one of the 16 bit timers. Most often timer2 I think. As it is now your project has way to many serious issues
Reply With Quote
  #6  
Old 09-08-2012, 03:39 PM
Eric007's Avatar
Eric007 Eric007 is offline
Senior Member
 
Join Date: Aug 2011
Location: Paris, France!
Posts: 974
Default

!

I am actually doing somrthing very smilar at the moment and only have tomorrow to finish up everything or else I am dead!!

The only difference is I am Not displaying date and using seven segments and all the setting of the time, of the alarm, displaying of the alarm/current time MUST be done (or execute instantaneously)...

I am suding the concept of cooperative multitasking which is an alternative of RTOS!

Gudluck!
__________________
Designing the algorithm comes first then writing the code!!!
The best solution is the simplest one!
A good but *late* solution is a wrong one!!!
Reply With Quote
The Following User Says Thank You to Eric007 For This Useful Post:
roshani (09-08-2012)
  #7  
Old 09-08-2012, 03:50 PM
takao21203's Avatar
takao21203 takao21203 is offline
Senior Member
 
Join Date: Apr 2012
Location: Ireland
Posts: 2,352
Blog Entries: 16
Send a message via Skype™ to takao21203
Default

Having character LCD code available (in working condition) is the foundation which is required for your project.

I have some code for that on my website:
http://pic.hitechworld.org/data/

hitachi LCD, and gc_1824 (which is a newer revision of the same code).

This code can be made working for 16F and 18F PIC relatively easily.
If you don't need the serial interface, simply remove it.

The LCD interface is not optimized for minimum possible timing, but it works.

If you expect someone to write the complete program for you, it must be someone with a lot of time available, and very generous towards the general public.

Learning to use C language is an active process which requires time, resources, experimentation, and considerable efforts. If you want to delegate the work, expect to have to pay for it if you want professional code.

On public forums you may be able to obtain some useful bits and pieces of source code, if you ask the right questions.
Reply With Quote
The Following User Says Thank You to takao21203 For This Useful Post:
roshani (09-08-2012)
  #8  
Old 09-08-2012, 05:05 PM
roshani roshani is offline
New Member
 
Join Date: Sep 2012
Posts: 3
Default

Quote:
Originally Posted by t06afre View Post
The analog part with the opamps of your project will not work in any way. It has to be redesign. And I can not grasp what you want it to do. Your project has several parts. Why not start with the more easy parts. Like writing stuff to the LCD.
Are you planing to use the internal oscillator. In a real world project that would not be wise. A common method then using a PIC as clock. Is to use a clock crystal connected to one of the 16 bit timers. Most often timer2 I think. As it is now your project has way to many serious issues
actually, i am using an external 20MHz oscillator crystal in the hardware. Don't get confused with the comparators, they are just there to generate square wave. I would be thankful if i could get some idea regarding how the selection for date and time is to be made with four push buttons for setting them and how i can use pull up or pull down buttons to make increaments and decrements in their values.
Reply With Quote
  #9  
Old 09-08-2012, 06:22 PM
BMorse's Avatar
BMorse BMorse is offline
Senior Member
 
Join Date: Sep 2009
Location: Vermontville, Michigan (GMT -5)
Posts: 2,658
Blog Entries: 15
Default

You have to have atleast 3 switches to use to set the time/Date, 1 switch will be the Select/Enter switch(Middle_Switch), the other 2 will be for incrementing the value (Right_Switch) and decrementing the value (Left_Switch), all switches are pulled High with internal pullups on PORTB, and we look for it to go LOW when a switch is pressed...

for example: (this code is actually used in a PIC16F887 @ 20Mhz, written in Hi-Tech C, and used with a DS1302 Real time clock and Calendar)

Code:
/*********************************************************************/
void set_clock()
{	unsigned char i;   
	unsigned char temp;      //temp is used to hold a value specifying which digit we are changing
	temp=1;                      //start with HOUR
	i=1;
	lcd_clear();                 //clear display
	lcd_goto(0x40);           //go to second row
	lcd_puts(mSet);          //display user message
	//must convert values to decimal for displaying since they are in Binary Coded Decimal format used in the RTC
	rtc_hour=myBCD2DEC(rtc_hour);
	rtc_min=myBCD2DEC(rtc_min);
	rtc_month=myBCD2DEC(rtc_month);
	rtc_date=myBCD2DEC(rtc_date);
	rtc_year=myBCD2DEC(rtc_year);
	rtc_day=myBCD2DEC(rtc_day);
	update_time();             //display time/date on LCD

	while(!Middle_Switch){};		//wait for button to be released
	
	while(i){
	if(!Middle_Switch){
		++temp;
		DelayMs(200);
		while(!Middle_Switch){};		//wait for button to be released
	};	
	switch(temp) {
		case 1:	//Hour
			if (!Right_Switch){
				++rtc_hour;
				if(rtc_hour>23){rtc_hour=0;};
				DelayMs(200);           //delay to De-Bounce switch
			}
			else if (!Left_Switch){
				if(rtc_hour==0){
					rtc_hour=23;}
				else{
					--rtc_hour;
				};
				DelayMs(200);
			};
			break;
		case 2:	//Minutes
			if (!Right_Switch){
				++rtc_min;
				if(rtc_min>59){rtc_min=0;};
				DelayMs(200);
			}
			else if (!Left_Switch){
				if(rtc_min==0){
					rtc_min=59;}
				else{
					--rtc_min;
				};
				DelayMs(200);
			};
			break;
		case 3:	//Month
			if (!Right_Switch){
				++rtc_month;
				if(rtc_month>12){rtc_month=1;};
				DelayMs(200);
			}
			else if (!Left_Switch){
				if(rtc_month==1){
					rtc_month=12;}
				else{
					--rtc_month;
				};
				DelayMs(200);
			};
			break;
		case 4:	//Date
			if (!Right_Switch){
				++rtc_date;
				if(rtc_date>31){rtc_date=1;};
				DelayMs(200);
			}
			else if (!Left_Switch){
				if(rtc_date==1){
					rtc_date=31;}
				else{
					--rtc_date;
				};
				DelayMs(200);
			};
			break;
		case 5:	//year
			if (!Right_Switch){
				++rtc_year;
				if(rtc_year>99){rtc_year=0;};
				DelayMs(200);
			}
			else if (!Left_Switch){
				if(rtc_year==0){
					rtc_year=99;}
				else{
					--rtc_year;
				};
				DelayMs(200);
			};
			break;
		case 6:	//day of week (sunday = 1)
			if (!Right_Switch){
				++rtc_day;
				if(rtc_day>7){rtc_day=1;};
				DelayMs(200);
			}
			else if (!Left_Switch){
				if(rtc_day==1){
					rtc_day=7;}
				else{
					--rtc_day;
				};
				DelayMs(200);
			};
			break;
		case 7:
			//convert settings back to BCD
			rtc_hour=myDEC2BCD(rtc_hour);
			rtc_min=myDEC2BCD(rtc_min);
			rtc_sec=0;
			rtc_month=myDEC2BCD(rtc_month);
			rtc_date=myDEC2BCD(rtc_date);
			rtc_year=myDEC2BCD(rtc_year);
			rtc_day=myDEC2BCD(rtc_day);
			//save all settings back to registers
			write_clk_regs();
			i=0;		
			break;
		};//end switch
		update_time();
	};//end while
}//end set clock
/*********************************************************************/
//Function used to display the date/time on 2x16 LCD//
void update_time()
{	lcd_goto(0x00);
	sprintf(LCD_Buffer,"%2d:%2d %2d|%2d|%2d %d",rtc_hour,rtc_min,rtc_month,rtc_date,rtc_year,rtc_day);
	lcd_puts(LCD_Buffer);
}
/*********************************************************************/
Select All
__________________
There is no spoon -Neo-
Disclaimer: The example programs,circuits, projects and information I provide or post on this web site are for educational purposes only. By copying anything from this site posted by me, you agree to the "as is" nature of the programs, circuits, information and to the statements listed in this disclaimer.No warranty or liability is expressed or implied. Working with AC /DC voltages can be dangerous and even deadly. Proceed at your own risk!

Last edited by BMorse; 09-08-2012 at 06:35 PM.
Reply With Quote
The Following 3 Users Say Thank You to BMorse For This Useful Post:
absf (09-08-2012), Eric007 (09-08-2012), roshani (09-09-2012)
  #10  
Old 09-09-2012, 11:32 AM
MMcLaren's Avatar
MMcLaren MMcLaren is offline
Senior Member
 
Join Date: Feb 2010
Location: Michigan, USA
Posts: 639
Default

Would anyone mind if I supplement the fine example code excerpt from BMorse? I don't mean to suggest one example is "better" than the other... just different.

In my single chip Charlie Clock program (2007) I decided to use a single set of routines for the <up> arrow, <down> arrow, and <right> arrow keys while in [set] mode. Basically, the <up> and <down> arrow keys are used to increment or decrement the current display group (month, day, year, hour, minutes, or seconds) of the current display (clock, timer, or calendar) and the <right> arrow key is used to move to the next display group. The routines include relatively simple upper/lower field limit logic which prevents users from entering invalid dates such as 02/30/12 or 04/31/12.

Good luck on your project.

Cheerful regards, Mike

Code:
#define setmode swflags.0       // set sw on RB0
#define rtarrow swflags.1       // rt arrow on RB1
#define uparrow swflags.2       // up arrow on RB2
#define dnarrow swflags.3       // dn arrow on RB3
#define uplatch swlatch.2       // up arrow switch state latch bit
#define dnlatch swlatch.3       // dn arrow switch state latch bit
Select All
Code:
    if(setmode & mode < 3)      // if <set> switch and display "on"
    { loadbuffer();             // copy source array to set[] buffer
      group = 0;                // set starting group (hours/months)
      setlimits();              // set lower/upper field limits
      while(setmode)            // while <set> switch "on"
      { if(rtarrow)             // if <rt arrow> press
        { rtarrow = 0;          // clear the "new press" flag and
          if(group == 2)        // bump display group, 0..2
            group = 0;          //
          else                  //
            group++;            //
          setlimits();          // set lower/upper field limits
        }                       //
        while(uplatch)          // while <up arrow> held pressed
        { uparrow = 0;          // increment display group value
          if(set[group] == hilimit)
            set[group] = lolimit;
          else                  //
            set[group]++;       //
          delay_ms(220);        // delay approx 1/4 sec
          if(uplatch)           // if <up arrow> still pressed
            beepctr = 16;       // send repeat 'click'
        }
        while(dnlatch)          // while <dn arrow> held pressed
        { dnarrow = 0;          // decrement display group value
          if(set[group] == lolimit)
            set[group] = hilimit;
          else                  //
            set[group]--;       //
          delay_ms(220);        // delay approx 1/4 sec
          if(dnlatch)           // if still pressed
            beepctr = 16;       // send repeat 'click'
        }
      }
      intcon.GIE = 0;           // suspend interrupts
      savebuffer();             // copy set[] buffer to source array
      if(mode == 0)             // if 'clock' was 'source' then
      { rtcl = 25;              // reset RTC counters, including
        rtch = 0;               // set switch debounce time...
      }
      intcon.GIE = 1;           // restart interrupts
    }
Select All
Code:
void setlimits()
{ if(mode == 2)                 // if calendar mode
  { lolimit = 1;                //
    hilimit = days_in[month-1]; // limits 1..days_in_month
    if(group == 0)              // if 'month' group
      hilimit = 12;             // limits 1..12
    if(group == 1)              // if 'date' group and
    { if(month == 2)            // if 'month' is february and
        if(!(year & 3))         // if a leap year then
          hilimit = 29;         // limits 1..29
      if(date > hilimit)        // if 'date' field > hilimit
        date = hilimit;         // set it to hilimit
    }
    if(group == 2)              // if 'year' group
    { lolimit = 0;              //
      hilimit = 99;             // limits 0..99
    }
  }
  else                          // clock or timer mode
  { lolimit = 0;                //
    hilimit = 59;               // limits 00..59
    if(group == 0)              // if 'hour' group
      hilimit = 23;             // limits 00..23
  }
}
Select All
Attached Images
File Type: jpg K8LH Charlie Clock.JPG (83.2 KB, 27 views)
File Type: png Charlie Clock 1 (small).PNG (185.6 KB, 20 views)

Last edited by MMcLaren; 09-09-2012 at 08:18 PM.
Reply With Quote
The Following 2 Users Say Thank You to MMcLaren For This Useful Post:
absf (09-09-2012), roshani (09-09-2012)
Reply   Post New Thread

Tags
, , , , ,


Related Site Pages
Section Title
Worksheet Time-delay electromechanical relays
Worksheet Microcontroller principles
Worksheet CMOS logic gates
Textbook Switch types : Switches
Worksheet Class B BJT amplifiers
Worksheet Performance-based assessments for AC circuit competencies
Textbook Hysteresis : Thyristors
Video Lecture Electronic Components - Basic Components and Technical Notation
Textbook Finite State Machines : Sequential Circuits Counters
Textbook Class B audio amplifier : Analog Integrated Circuits


Similar Threads
Thread Thread Starter Forum Replies Last Post
Date and Time Display using LCD and RTC innovationlabs Embedded Systems and Microcontrollers 13 11-01-2010 02:03 AM

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:31 PM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin
Copyright ©2000 - 2014, vBulletin Solutions, Inc.