View previous topic :: View next topic |
Author |
Message |
olivier
Joined: 08 Apr 2010 Posts: 11
|
I need help in coding |
Posted: Fri May 27, 2011 8:04 am |
|
|
Hi everyone,
I am new in C programming and I need help in find out what wrong.
I have global variable executed_time and its updated after the function call, then printf. For some reason it keep printing zero. I try to find where I init it to zero but don't know where it come from.
here is my code
Code: |
char Seconds = 0,Interrupt_Flag = 0, interruption_time = 0;
float executed_time = 0.00000;
int32 Inst_Cycle_Per_Second = 0;
void my_time_ms (char i,j,k);
void LCD_Pwr_Initialze(void);
void Power_On_Chk (void);
void RTC_Initialize (void);
void Final_Delay_Time (void);
void Blink_LED (char number_flash, int flash_duty_cycle);
void main()
{
RTC_Initialize();
while (TRUE)
{
Seconds = 0; // clear all global variables before next test
Interrupt_Flag = 0;
interruption_time = 0;
executed_time = 0.00000;
Inst_Cycle_Per_Second = 0;
i = 1;
while (input(START_SW) == 1)
{
if (i == 1)
{
lcd_gotoxy (7,2);
printf (lcd_putc, "READY !"); //do nothing
i = 0;
}
}
clear_interrupt(int_timer1);
set_timer1(0);
clear_interrupt(INT_EXT);
enable_interrupts(GLOBAL);
do
{
;
} while ((input (STOP_SW) == 1) && (interrupt_flag == 0));
executed_time = Final_Delay_Time();
printf (lcd_putc,"%7.5f S",executed_time);
while (input(RESET_SW) == 1)
{
;//do nothing and wait for reset switch pressed to start new test
}
printf (lcd_putc, "\f"); //clear lcd screen
} //end of main while loop
}//end of main
Final_Delay_Time ()
{
float delay_time = 0.00000;
executed_time = INTERNAL_FREQUENCY - Inst_Cycle_Per_Second - get_timer1();
executed_time = executed_time * (1/INTERNAL_FREQUENCY);
delay_time = Seconds + executed_time;
return (delay_time);
} |
|
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Fri May 27, 2011 8:23 am |
|
|
Well, one obvious thing that I see is your line:
Code: | executed_time = executed_time * (1/INTERNAL_FREQUENCY); |
if "executed_time" starts as 0, then anything times 0 is still 0 --- perhaps you meant to ADD the increment, not multiply ?
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
olivier
Joined: 08 Apr 2010 Posts: 11
|
|
Posted: Fri May 27, 2011 9:28 am |
|
|
gpsmikey wrote: | Well, one obvious thing that I see is your line:
Code: | executed_time = executed_time * (1/INTERNAL_FREQUENCY); |
if "executed_time" starts as 0, then anything times 0 is still 0 --- perhaps you meant to ADD the increment, not multiply ?
mikey |
Mikey,
thank you for replying back.
I have executed_time = 0; as the begining then
"executed_time = INTERNAL_FREQUENCY - Inst_Cycle_Per_Second - get_timer1();
"
this line should give it a different number. then "executed_time = executed_time * (1/INTERNAL_FREQUENCY); "
should not be zero. |
|
|
andrewg
Joined: 17 Aug 2005 Posts: 316 Location: Perth, Western Australia
|
|
Posted: Fri May 27, 2011 10:06 pm |
|
|
I don't know what INTERNAL_FREQUENCY is (please include all your code), but if it's an integer (and I suspect it is), then Code: | (1/(anything greater than 1)) | will be calculated by the compiler as zero due to integer math and rounding down. Then you have: Code: | executed_time = executed_time * 0; | which is, of course, zero! Perhaps Code: | executed_time = executed_time / INTERNAL_FREQUENCY; | will work better? _________________ Andrew |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Jun 02, 2011 3:56 am |
|
|
I biggest error I can see is that your function Final_Delay_Time () definition does not include a return type!
Not sure how the compiler would handle that.
float Final_Delay_Time () ????????? |
|
|
gpsmikey
Joined: 16 Nov 2010 Posts: 588 Location: Kirkland, WA
|
|
Posted: Thu Jun 02, 2011 4:04 am |
|
|
Hmmm - I missed that one - I would think the compiler *should* at least warn you about a mismatch between the prototype and the actual function.
mikey _________________ mikey
-- you can't have too many gadgets or too much disk space !
old engineering saying: 1+1 = 3 for sufficiently large values of 1 or small values of 3 |
|
|
olivier
Joined: 08 Apr 2010 Posts: 11
|
|
Posted: Fri Jun 03, 2011 10:59 am |
|
|
gpsmikey wrote: | Hmmm - I missed that one - I would think the compiler *should* at least warn you about a mismatch between the prototype and the actual function.
mikey |
the compiler told me that I can not use void so I remove void
Code: |
void Final_Delay_Time (void)
{...;}
|
to Final_Delay_Time ()
and after that the compiler does not say a thing.
I will change that to float Final_Delay_Time() then.
if I am not return anything do I still need to define float or integer for function?
thank you for help.
as far as my execute_time always zero concern, I define INTERNAL FREQUENCY on top of my code as
Code: |
#define XTAL_FREQUENCY 8000000
#define INTERNAL_FREQUENCY (1/XTAL_FREQUENCY)
|
when I debug my code with whole bunches of printf to RS232, I found that INTERNAL_FREQUENCY somehow change. the result is random each time the code execute at the main function. I then do this
Code: |
#define XTAL_FREQUENCY 800000
float INTERNAL_FREQUENCY = 0;
void main ()
{
INTERNAL_FREQUENCY = 1/XTAL_FREQUENCY;
...
...
..
}
|
that fix my problem. I still new and as of this point, I just add a band aid on top of my problem. I will revisit the problem again to find out why later on. |
|
|
|