Author Topic: Novice XC8 question  (Read 6738 times)

0 Members and 1 Guest are viewing this topic.

Offline DTJTopic starter

  • Super Contributor
  • ***
  • Posts: 1024
  • Country: au
Novice XC8 question
« on: February 25, 2016, 06:46:08 am »
Playing with a p18lf25k22, XC8 on MPLABx


When I press a button it triggers an interrupt and sets a flag bit in a register (two bits actually, B7 and B0):

Like this: Button Press --> Interrupt ---> 0b10000010 ==> flag_register

In the main code loop I then test for the flag and if it's set I run some other code:


                if ((status_flags & 0x00000010)) //if button pressed do this code...
                {
                   
                   __delay_ms(30);
                   __delay_ms(30);
                   //do some stuff.......

                 }

My problem is the execution never passes the "if" test, it is as if the "if" always tests false.

If I step through the program and mouse over the status_flags variable it shows the contents as 0x82 = 0b10000010 which is what
I expect. I'm trying to test if bit1 is set. Why doesn't it work?

Thanks.   :(

 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4566
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: Novice XC8 question
« Reply #1 on: February 25, 2016, 06:54:06 am »
Your code says 0x00000010, not 0b00000010.

You're comparing against the value 16, not 2.

Offline DTJTopic starter

  • Super Contributor
  • ***
  • Posts: 1024
  • Country: au
Re: Novice XC8 question
« Reply #2 on: February 25, 2016, 08:27:17 am »
 :palm:

Oh FFS! Thank you very much!

So obvious, I've been looking at it for hours......
 

Offline Howardlong

  • Super Contributor
  • ***
  • Posts: 5524
  • Country: gb
Re: Novice XC8 question
« Reply #3 on: February 25, 2016, 05:35:32 pm »
Been there, done that...
 

Offline AndyC_772

  • Super Contributor
  • ***
  • Posts: 4566
  • Country: gb
  • Professional design engineer
    • Cawte Engineering | Reliable Electronics
Re: Novice XC8 question
« Reply #4 on: February 25, 2016, 05:38:29 pm »
Me too!  :-DD

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Novice XC8 question
« Reply #5 on: February 25, 2016, 05:47:34 pm »
Binary notation isn't terribly helpful, as this example shows.
================================
https://dannyelectronics.wordpress.com/
 

Offline DTJTopic starter

  • Super Contributor
  • ***
  • Posts: 1024
  • Country: au
Re: Novice XC8 question
« Reply #6 on: February 26, 2016, 04:43:39 am »
I just find using binary for flag registers more intuitive.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: Novice XC8 question
« Reply #7 on: February 26, 2016, 08:07:27 am »
I just find using binary for flag registers more intuitive.

Don't use "Magic" number like this.  Define your bitmask using the BV() macro (which may already be defined in the compiler library with a leading underscore) and it's all much less error prone

Code: [Select]
#define BV(x) (1U<<x)

/* Bit mask for bit 3 */
#define MY_BITMASK BV(3)
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Novice XC8 question
« Reply #8 on: February 26, 2016, 11:28:35 am »
Code: [Select]
#define BV(x) (1U<<x)

Seems to be a very dangerous way of writing a macro.
================================
https://dannyelectronics.wordpress.com/
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: Novice XC8 question
« Reply #9 on: February 26, 2016, 11:50:14 am »
Code: [Select]
#define BV(x) (1U<<x)

Seems to be a very dangerous way of writing a macro.

Seems to be a very typical way of writing a macro.  How would you write it, given the use to which it will be put?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Novice XC8 question
« Reply #10 on: February 26, 2016, 11:59:42 am »
Quote
Seems to be a very typical way of writing a macro.

Just because it is popular or typical doesn't mean it is right.

The mistake is very basic, probably the first thing they tell you NOT to do when learning to write a macro.

Someone like your caliber will figure it out quickly, I am sure.
================================
https://dannyelectronics.wordpress.com/
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Novice XC8 question
« Reply #11 on: February 27, 2016, 12:31:10 pm »
"#define BV(x) (1U<<x)"

It shows how important it is to be able to go back to basics, no matter how good you think you are.

The most dangerous code is one that's often overlooked and one that works most of the time - because of that, it is widely used yet least suspected.

When it fails, it tends to fail spectacularly.

Every once in a while, great work like that happens to highly accomplished people who may have done X projects, sold Y dollars, and written Z lines of flawless code.

I guess being always humble has its places.
================================
https://dannyelectronics.wordpress.com/
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: Novice XC8 question
« Reply #12 on: February 27, 2016, 12:54:06 pm »
Code: [Select]
#define BV(x) (1U<<x)

Seems to be a very dangerous way of writing a macro.

Yes, quite understandable for a beginner doing this as a not knowing any better, but I just hate when I see this in so called professional code. The lack of p********* is just a sign of ignorance or just plain lazyness.
« Last Edit: February 27, 2016, 01:00:43 pm by Kalvin »
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Novice XC8 question
« Reply #13 on: February 27, 2016, 02:56:43 pm »
Agreed. Another example is to load up the 24bit systick counter with a value of 25M.

If the person who wrote that piece of garbage had run his code, he would realize that mistake instantly.

================================
https://dannyelectronics.wordpress.com/
 

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2178
  • Country: fi
  • Embedded SW/HW.
Re: Novice XC8 question
« Reply #14 on: February 27, 2016, 04:11:04 pm »
Code: [Select]
#define BV(x) (1U<<x)

Seems to be a very dangerous way of writing a macro.

Seems to be a very typical way of writing a macro.  How would you write it, given the use to which it will be put?

You may want to see this page, for example: http://www.cs.yale.edu/homes/aspnes/pinewiki/C%282f%29Macros.html "1. Macros with arguments". You should find the answer and the rationale there. While at it, see also the part "3.3. Multiple statements in one macro" how to combine multiple statements in a macro.
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3671
  • Country: gb
Re: Novice XC8 question
« Reply #15 on: March 04, 2016, 02:29:50 pm »
You may want to see this page, for example: http://www.cs.yale.edu/homes/aspnes/pinewiki/C%282f%29Macros.html "1. Macros with arguments". You should find the answer and the rationale there. While at it, see also the part "3.3. Multiple statements in one macro" how to combine multiple statements in a macro.

I'm very well aware of the dangers of function-like macros, I'm simply waiting for the great DannyF to show us all how this should be correctly implemented given it's intended use.  I suspect it will be an extended wait.
 

Offline macboy

  • Super Contributor
  • ***
  • Posts: 2410
  • Country: ca
Re: Novice XC8 question
« Reply #16 on: March 04, 2016, 06:55:08 pm »
You may want to see this page, for example: http://www.cs.yale.edu/homes/aspnes/pinewiki/C%282f%29Macros.html "1. Macros with arguments". You should find the answer and the rationale there. While at it, see also the part "3.3. Multiple statements in one macro" how to combine multiple statements in a macro.

I'm very well aware of the dangers of function-like macros, I'm simply waiting for the great DannyF to show us all how this should be correctly implemented given it's intended use.  I suspect it will be an extended wait.
I too would expect no less. A six-line response without even a hint at the suggested problem or a solution.

Here's a better way:
Code: [Select]
#define BV(x) (1U<<(x))The parenthesis around x ensure that the entire expression passed as x will be first evaluated, then bitshifted left. If the parenthesis are not there, imagine what happens when you call "BV(i-1)".
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf

 

- - -
-->