/var/www/restricted/ssh/stm32/www/stm32circle/ STM CircleOS forum / Bad math in varsion 1.0 code.

Username:     
Password:     
             

Forum

# 1   2009-05-01 19:00:06 Bad math in varsion 1.0 code.

rlstrand
New member
Registered: 2009-04-30
Posts: 1

Bad math in varsion 1.0 code.

There is a bug in the original code due to integer math.

The original code tries to move the hour hand to a location between the hours.

i.e.  at 01:30:30 the hour hand should point midway betweem the 1 and the 2

The original code had this:

   RTC_GetTime(&hh,&mm,&ss);
   LCD_FillRect ( 32, 32, 65, 63, 65535);
   d(29,mm,6);       
   n=hh+mm/60;
   d(20,n,30);
   //DRAW_Clear();
   d(30,ss,6);

hh,mm,ss are declared int and n is declared u32

For hh=1 mm=30 ss=30 then n = 1 + 30/60 gives n = 1 as 30/60 for integer math is 0 even if converted u32.

macro d takes n and multiplies by 30 to get the angle of the hour hand.

As written the code can only point the hour hand to multiples of 30 degrees depending only on hh.

Here is an improvement for this:

Does not update the display unless 1 second has elapsed (less flicker).
Correctly updates the hour hand.
Also adjusts the minute hand.

    static lastSS = -1;
    if (lastSS != ss)
        {
        LCD_FillRect ( 32, 32, 65, 63, 65535);
        d(29,mm*6 + ss*6/60,1);       
        d(20,hh*30 + mm*30/60,1);
        d(30,ss,6);
        lastSS = ss;
        }

As an aditional improvement some rounding can be added to the floating point to integer x.y conversions to slightly improve the look.

rlstrand

Offline

 

Board footer