CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

simple Time display problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








simple Time display problem
PostPosted: Thu Jan 04, 2007 3:11 am     Reply with quote

Hi i built small time display but iam facing a problem with AM/PM allthing allright when hour become over 12 it's going AM but After 1 it goes PM
Here's the code

Code:
#include<16f876a.h>
#use delay (clock=20000000)
#include "lcd.c"
#fuses HS,nowdt
int sek=0,min=53,hour=10;
//--------------------------//
void main()
{
while(true){
delay_ms(1000); // Time
printf(lcd_putc,("\f"));
printf(lcd_putc, " %2u",hour);
printf(lcd_putc,(":"));
printf(lcd_putc, "%02u",min);
printf(lcd_putc,(":"));
printf(lcd_putc, "%02u",sek);
sek++;
If(sek>59){
sek=0;
++min;}
if(min>59){
min=0;
++hour;}
if(hour>12)
hour = 1;
If(hour>11)
printf(lcd_putc, " AM");
else
printf(lcd_putc, " PM");
}
}
davekelly



Joined: 04 Oct 2006
Posts: 53
Location: Berkshire, England

View user's profile Send private message

PostPosted: Thu Jan 04, 2007 3:52 am     Reply with quote

You need an extra flag to track whether it is Am or Pm, which gets toggled every 12 hours

eg

Code:

#include<16f876a.h>
#use delay (clock=20000000)
#include "lcd.c"
#fuses HS,nowdt
int sek=0,min=53,hour=10;
//--------------------------//
void main()
{
   int1 AmPm;

   while(true)
   {
      delay_ms(1000); // Time
      printf(lcd_putc,("\f"));
      printf(lcd_putc, " %2u",hour);
      printf(lcd_putc,(":"));
      printf(lcd_putc, "%02u",min);
      printf(lcd_putc,(":"));
      printf(lcd_putc, "%02u",sek);
      sek++;
      If(sek>59)
      {
         sek=0;
         ++min;
      }

      if(min>59)
      {
         min=0;
         ++hour;
      }

      if(hour>12)
      {
         hour = 1;
         AmPm = !AmPm
      }

      If(!AmPm)
      {
         printf(lcd_putc, " AM");
      }
      else
      {
         printf(lcd_putc, " PM");
      }
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group