View previous topic :: View next topic |
Author |
Message |
Guest
|
Making two Function Work in main |
Posted: Wed Nov 07, 2007 3:43 am |
|
|
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
|
|
Posted: Wed Nov 07, 2007 4:04 am |
|
|
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
|
|
Posted: Wed Nov 07, 2007 4:12 am |
|
|
thanks a bunch
i removed the while loop from two functions and keep the one in main
Thanks again |
|
|
Guest
|
|
Posted: Wed Nov 07, 2007 4:18 am |
|
|
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
|
|
Posted: Wed Nov 07, 2007 5:20 am |
|
|
if (hour>12 && min==0 && sec==0)
Best Wishes |
|
|
Guest
|
|
Posted: Wed Nov 07, 2007 5:36 am |
|
|
doesn't work
the hours start counting 13 14 15
when i set hours |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Wed Nov 07, 2007 6:32 am |
|
|
if (hour > 11)
AmPm != AmPm;
if (hour > 12)
hour = 1; |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Nov 07, 2007 6:56 am |
|
|
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
|
|
Posted: Wed Nov 07, 2007 7:02 am |
|
|
sorry doesn't work
note that clock will return after 12 to 1
this code make the am/pm toggling (12:00 to 12:59) |
|
|
Guest
|
|
Posted: Wed Nov 07, 2007 7:09 am |
|
|
i want it to toggle every 12 hours
when it's 12:00:00 |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 07, 2007 7:39 am |
|
|
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
|
|
Posted: Wed Nov 07, 2007 8:02 am |
|
|
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
|
|
Posted: Wed Nov 07, 2007 9:06 am |
|
|
it keep goes 12 13 14 15
doesn't work |
|
|
|