issue with if statement with integers

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

issue with if statement with integers

2,252 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by perez1028 on Thu Dec 20 21:42:13 MST 2012
I'm making a binary clock using a LPC1343 I used for a class a while back. The code is based off the blinky example from NXP (uses same source and header files).

i have a integer variable sec made to be a counter each timer cycle. It's initially set to 0. The Debugging as soon as it goes through the timer if statement it jumps to if(sec == 2 || 3 || 6 || 7), but this should not happen  sec is = 0 not 2 or 3 or 6 or 7... I don't understand why its doing this.


#ifdef __USE_CMSIS
#include "LPC13xx.h"
#include "clkconfig.h"
#include "gpio.h"
#include "config.h"

#include "timer32.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

int main(void) {

  init_timer32(0, TIME_INTERVAL);

  enable_timer32(0);

  GPIOInit();
  /*SECONDS : 0-9*/
  GPIOSetDir( 1, 9, 1 );
  GPIOSetDir( 1, 10, 1 );
  GPIOSetDir( 1, 11, 1 );
  GPIOSetDir( 2, 3, 1 );
  /*SECONDS : 0x-5x*/
  GPIOSetDir( 2, 4, 1 );
  GPIOSetDir( 2, 5, 1 );
  GPIOSetDir( 2, 6, 1 );

  int sec = 0 ;
  int sec2 = 0 ;

  GPIOSetValue( 1, 9, LED_OFF );
  GPIOSetValue( 1, 10, LED_OFF );
  GPIOSetValue( 1, 11, LED_OFF );
  GPIOSetValue( 2, 3, LED_OFF );
  GPIOSetValue( 2, 4, LED_OFF );
  GPIOSetValue( 2, 5, LED_OFF );
  GPIOSetValue( 2, 6, LED_OFF );

while(1)
{

/*
 * ***********************************
 * Seconds (LED1 - LED7)
 * ***********************************
*/
if ( (timer32_0_counter%LED_TOGGLE_TICKS) < (LED_TOGGLE_TICKS/2) )
{
  GPIOSetValue( 1, 9, LED_OFF );
}
else
{
if(sec == 1 || 3 || 5 || 7 || 9)
{
GPIOSetValue( 1, 9, LED_ON );
sec++;
}
else if(sec == 0 || 2 || 4 || 6 || 8)
{
GPIOSetValue( 1, 9, LED_OFF );
sec++;
}
else
{
GPIOSetValue( 1, 9, LED_OFF );
sec2++;
sec = 0;
}
}
/* LED2 */
if(sec == 2 || 3 || 6 || 7)
{
GPIOSetValue( 1, 10, LED_ON);
}
else
{
GPIOSetValue( 1, 10, LED_OFF);
}


As i step through the debugging it reaches

if ( (timer32_0_counter%LED_TOGGLE_TICKS) < (LED_TOGGLE_TICKS/2) )
{
  GPIOSetValue( 1, 9, LED_OFF );
}


then jumps to

                        /* LED2 */
if(sec == 2 || 3 || 6 || 7)
{
GPIOSetValue( 1, 10, LED_ON);
}

but it shouldn't because initially sec = 0

so LED2 is turned on indefinitely along with every other LED because there in similar if statements...

I didn't put all the code in here because the everything works... just not the if statements... I don't have much experience at all with LPCXpresso whats wrong!?
0 Kudos
Reply
13 Replies

2,171 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Sun Dec 23 05:18:16 MST 2012

Quote:
I've enjoyed your website and some of your project

Thanks. We just got the PCBs for the Arduino/LPC sensor network boards, can't wait to get them running.


Quote:
Most of my programs access the peripheral's registers directly.

I'm going the other way and writing a HAL to hide all that stuff, sort of Arduino++ on LPC :)


Quote:
have a look at the gpio example project in lpc13xx.new.zip

Will do.


Quote:
I guess I'm one of those nomads who got stuck with tiny microcontrollers a few years ago

Likewise, although I was into uCs first then had a mid-life crisis and hit the road.
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Sun Dec 23 04:36:27 MST 2012

Quote: graynomad
GPIOSetValue();
How come no examples I see here ever compile for me? This is not a known function using LPCXpresso and redlib :confused:



GPIOSetValue() is not a redlib function. redlib and newlib are just (standard) C-library variants. The GPIOSetvalue() function is used in some of the examples that come with the LPCXpresso IDE. I do not use those functions myself that much. Most of my programs access the peripheral's registers directly.
This results in faster and compacter code.

I guess I'm one of those nomads who got stuck with tiny microcontrollers a few years ago :eek:

Edit: have a look at the gpio example project in lpc13xx.new.zip (in C:\nxp\LPCXpresso_4.1.5_219\lpcxpresso\Examples\NXP\LPC1000\LPC13xx\examples.lpc13xx.new.zip\gpio\src)

Rob
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Sun Dec 23 03:27:06 MST 2012
GPIOSetValue();
How come no examples I see here ever compile for me? This is not a known function using LPCXpresso and redlib :confused:

Anyway FWIW here's my take on the code

int seconds = 0;
int minutes = 0;
int hours = 0;

while(1)    {
    if ( (timer32_0_counter%200) < (200/2) ) {  // don't know what these magic numbers are,  
                                                // but I assume this is true every second
        if (++seconds == 60) {
            minutes++;
            seconds = 0;
        }
        if (minutes == 60) {
            hours++;
            minutes = 0;
        }

        write_to_port ((hours << 16) | (minutes << 8) | seconds);   // made up function, you could
                                                                    // write directly to the port
                                                                    // if the hardware is set up right

    }
}
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Sun Dec 23 03:04:18 MST 2012
You have 4 LEDs connected to 4 GPIOs with each of those LEDs representing one bit of the variable 'sec'.

So why not just use the knowledge that microprocessors use binary numbers.

You could easily write:
GPIOSetValue( 2, 3, sec & 1);
GPIOSetValue( 1, 11, sec & 2);
GPIOSetValue( 1, 10, sec & 4);
GPIOSetValue( 1, 9, sec & 8);
Same for sec2 etc. This makes your code much cleaner and faster.
Also it's a lot less typing and less error prone so this would have got you to a working solution faster.

I would spilt setting the LEDs from the counting of the clock: use the if construction only for updating the time each second and do all the GPIO stuff after this.
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by vasanth on Sun Dec 23 02:47:29 MST 2012
Hi perez1028,
Cryptography is not my field but you could have named this an "Encrypted Clock".
O.K. you have just passed the first one. Congrats.
Now try the next exercise. Control all the rows and columns with only 10 pins (one allocated for each row/column). :)
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by perez1028 on Sat Dec 22 21:40:28 MST 2012
Oh I completely agree greynomad, but I'm rusty on C also I just needed to get it done and I knew this would work so I went with it! You can basically do it with a few for loops but meh... =)
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Sat Dec 22 20:28:19 MST 2012
Good on you for getting it working, but that's about the most convoluted way I've ever seen to print what appears do be just a binary sequence of hours, minutes and seconds.

Unless I've missed something (quite possible) if you couldn't do that in 10 lines of code I'd be gobsmacked. Less if you can organise the LEDs to be on a single port.
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by perez1028 on Sat Dec 22 20:13:22 MST 2012
I got it to work, my code was not correct anyways.

If anyone was wondering the binary clock looks like the following image

http://heldermonteiro.files.wordpress.com/2010/11/binary-clock-method-300.jpg

here's my code it works perfectly, based off the blinky example code from NXP

while(1)
{
/*
 * ***********************************
 * Seconds 0-9 (LED1 - LED7)
 * ***********************************
*/
/* Use timer here to set LED1 to on and off each second */
if ( (timer32_0_counter%200) < (200/2) )
{
if(sec == 1)
{
GPIOSetValue( 2, 3, LED_ON );
GPIOSetValue( 1, 11, LED_OFF );
GPIOSetValue( 1, 10, LED_OFF );
GPIOSetValue( 1,9, LED_OFF );
sec = 2;
}
else if(sec == 3)
{
GPIOSetValue( 2, 3, LED_ON );
GPIOSetValue( 1, 11, LED_ON );
GPIOSetValue( 1, 10, LED_OFF );
GPIOSetValue( 1,9, LED_OFF );
sec = 4;
}
else if(sec == 5)
{
GPIOSetValue( 2, 3, LED_ON );
GPIOSetValue( 1, 11, LED_OFF );
GPIOSetValue( 1, 10, LED_ON );
GPIOSetValue( 1,9, LED_OFF );
sec = 6;
}
else if(sec == 7)
{
GPIOSetValue( 2, 3, LED_ON );
GPIOSetValue( 1, 11, LED_ON );
GPIOSetValue( 1, 10, LED_ON );
GPIOSetValue( 1,9, LED_OFF );
sec = 8;
}
else if(sec == 9)
{
GPIOSetValue( 2, 3, LED_ON );
GPIOSetValue( 1, 11, LED_OFF );
GPIOSetValue( 1, 10, LED_OFF );
GPIOSetValue( 1,9, LED_ON );
sec = 10;
}
}

else
{
if(sec == 0)
{
GPIOSetValue( 2, 3, LED_OFF );
GPIOSetValue( 1, 11, LED_OFF );
GPIOSetValue( 1, 10, LED_OFF );
GPIOSetValue( 1,9, LED_OFF );
sec = 1;
}
else if(sec == 2)
{
GPIOSetValue( 2, 3, LED_OFF );
GPIOSetValue( 1, 11, LED_ON );
GPIOSetValue( 1, 10, LED_OFF );
GPIOSetValue( 1,9, LED_OFF );
sec = 3;
}
else if(sec == 4)
{
GPIOSetValue( 2, 3, LED_OFF );
GPIOSetValue( 1, 11, LED_OFF );
GPIOSetValue( 1, 10, LED_ON );
GPIOSetValue( 1,9, LED_OFF );
sec = 5;
}
else if(sec == 6)
{
GPIOSetValue( 2, 3, LED_OFF );
GPIOSetValue( 1, 11, LED_ON );
GPIOSetValue( 1, 10, LED_ON );
GPIOSetValue( 1,9, LED_OFF );
sec = 7;
}
else if(sec == 8)
{
GPIOSetValue( 2, 3, LED_OFF );
GPIOSetValue( 1, 11, LED_OFF );
GPIOSetValue( 1, 10, LED_OFF );
GPIOSetValue( 1,9, LED_ON );
sec = 9;
}
else if(sec == 10)
{
GPIOSetValue( 2, 3, LED_OFF );
GPIOSetValue( 1, 11, LED_OFF );
GPIOSetValue( 1, 10, LED_OFF );
GPIOSetValue( 1,9, LED_OFF );
if(sec2 == 5)
{
min++;
sec2 = 0;
if(min == 10)
{
min2++;
min = 0;
if(min2 == 5)
{
hour++;
min2 = 0;
if(hour == 10)
{
hour2++;
hour = 0;
if(hour2 == 5)
{
hour2 = 0;
hour = 1;
}
}
}
}
}
else
{
sec2++;
}
sec = 1;

}
} // end clock pulse


the rest of the code is setting the rest of the LEDs depending on what the counters are. (sec2, min, min2 etc...)
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Rob65 on Fri Dec 21 08:28:06 MST 2012
Still don't get it ...

A binary clock should, I guess, just use the binary values:

GPIOSetValue( 1,  9, (sec & 0x01) ? LED_ON : LED_OFF );
GPIOSetValue( 1, 10, (sec & 0x02) ? LED_ON : LED_OFF);
GPIOSetValue( ...... (sec & 0x04) .....);
GPIOSetValue( ...... (sec & 0x08) .....);

etc.


In case you wonder: "&" is the binary AND operator and "?" is an embedded if then else statement. If the result of the operation left of the ? is non zero, execute the part left of the ":" else the part at the right;

Rob
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Fri Dec 21 01:57:46 MST 2012

Quote:
if you are just testing for the number being odd:


I thought that, but then he has that third option. I think the whole thing needs a rethink.
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Fri Dec 21 01:40:12 MST 2012

Quote:

if(sec == 1 || 3 || 5 || 7 || 9)

This code is always true. Perhaps you meant to write
[Code]
if ((sec ==1) || (sec ==3) || (sec ==5) || (sec == 7) || (sec ==9))
[/Code]Or, if you are just testing for the number being odd:
[Code]
if ((sec &1)==1) // check bottom bit to see if it is set
[/Code]
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by graynomad on Thu Dec 20 23:44:34 MST 2012
if(sec == 1 || 3 || 5 || 7 || 9)
I'm actually not sure what that does but it ain't right. Rather that a mess of if/elses you could use a switch
switch (sec) {
     case 1:
     case 3:
     case 5:
     case 7:
     case 9:
          GPIOSetValue( 1, 9, LED_ON );
         sec++;
         break;

     case 0:
     case 2:
     case 4:
     case 6:
     case 8:
          GPIOSetValue( 1, 9, LED_OFF );
          sec++;
         break;

default:
     GPIOSetValue( 1, 9, LED_OFF ); 
      sec = 0;
      sec2++; 
}
That said the whole thing looks a bit sus. Is this just to count in 2 decimal digits?

_____
Rob
0 Kudos
Reply

2,172 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by perez1028 on Thu Dec 20 22:40:24 MST 2012
got it figured out. didn't like using or I'll just do it with bunch of else ifs. just wana get this done! suppose to be a c-mas present don't care how ugly the code is.
0 Kudos
Reply