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

Making two Function Work in main

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








Making two Function Work in main
PostPosted: Wed Nov 07, 2007 3:43 am     Reply with quote

Hi please help me i had two functions one for Time and second for Adc value
i can't make the two function work together
Here is my code :

Code:
#include<16f876a.h>
#use delay (clock=20000000)
#include "lcd.c"
#fuses HS,nowdt
int sek=0,min=0,hour=12,i;
#define  HourB PIN_A3
#define  MinB  PIN_A1
#define  SetB  PIN_A2
   int1 AmPm;
//--------------------------//
void clock()
{
   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");
      }
      if( !input(HourB) ){
hour++;
delay_ms(50); //debounce
      } 
            if( !input(MinB) ){
               min++;
                  delay_ms(50); //debounce
      } 
                    if( !input(SetB) ){
                    AmPm = !AmPm;
                      delay_ms(50); //debounce
      } 
   }
}
void adcvalue()
{
   // Setup A/D
   setup_adc_ports(RA0_ANALOG);
   setup_adc(ADC_CLOCK_DIV_32);
   set_adc_channel(0);           
   delay_us(100);               

   while(true)
   {
   delay_ms(500);
      i = read_adc();
      printf(lcd_putc, "\n temp %lu ",(i*5/1023)- 5);
   }
}
void main(void)
{
 lcd_init();
 while(true)
 {
  clock();
   adcvalue();
}
}
Ttelmah
Guest







PostPosted: Wed Nov 07, 2007 4:04 am     Reply with quote

Think about what your functions are doing.
Will either ever _exit_?.
How can the second one be called in this case?.
Your loop in the 'main' is the only permanent loop that should exist. Each function must do it's task, either once, or a number of times if required, and then exit, to allow the other to execute.

Best Wishes
Guest








PostPosted: Wed Nov 07, 2007 4:12 am     Reply with quote

thanks a bunch
i removed the while loop from two functions and keep the one in main

Thanks again Very Happy
Guest








PostPosted: Wed Nov 07, 2007 4:18 am     Reply with quote

i had little issue with Am/Pm thing

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

i want it every 12 hours to toggle but it toggles after 12:59:59 AM
-> 1:00:00 PM

Thanks
Ttelmah
Guest







PostPosted: Wed Nov 07, 2007 5:20 am     Reply with quote

if (hour>12 && min==0 && sec==0)

Best Wishes
Guest








PostPosted: Wed Nov 07, 2007 5:36 am     Reply with quote

doesn't work
the hours start counting 13 14 15
when i set hours
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Nov 07, 2007 6:32 am     Reply with quote

if (hour > 11)
AmPm != AmPm;
if (hour > 12)
hour = 1;
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Nov 07, 2007 6:56 am     Reply with quote

Code:
f (hour > 11)
AmPm != AmPm;
if (hour > 12)
hour = 1;
This will toggle the AM/PM variable at every pass in the loop when the hour == 12.

There are several ways to display times. Can you elaborate a bit more on how you want the time to be shown?

For example I'm used to see the time on my watch as:
0:00:00 to 11:59:59, at the overflow of 11:59:59 the AM/PM sign toggles.

From your code it looks like you want the time to be shown as:
1:00:00 to 12:59:59
But at what which time do you want to toggle the AM/PM change ???
Guest








PostPosted: Wed Nov 07, 2007 7:02 am     Reply with quote

sorry doesn't work
Code:
if (hour > 11)

note that clock will return after 12 to 1
this code make the am/pm toggling (12:00 to 12:59)
Guest








PostPosted: Wed Nov 07, 2007 7:09 am     Reply with quote

i want it to toggle every 12 hours
when it's 12:00:00
Ttelmah
Guest







PostPosted: Wed Nov 07, 2007 7:39 am     Reply with quote

The best way is to put the hour test _inside_ the minute test. So:
Code:

      if(min>59)
      {
         min=0;
         ++hour;
         if (hour>12) {
            hour=1;
            AmPm^=1;
      }


Best Wishes
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Nov 07, 2007 8:02 am     Reply with quote

This still toggles am/pm at the overflow of 12:59:59 to 01:00:00.
If you want AM/PM to toggle at the change from 11:59:59 to 12:00:00 change the code to:
Code:
if (sek>59)
{
   sek=0;
   ++min;

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

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








PostPosted: Wed Nov 07, 2007 9:06 am     Reply with quote

it keep goes 12 13 14 15
doesn't work
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