Novice XC8 question - Page 1
Forum section: electronics / general

author: DTJ
author_rank: Super Contributor
timestamp: February 25, 2016, 06:46:08 am
content: Playing with a p18lf25k22, XC8 on MPLABxWhen 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_registerIn 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 whatI expect. I'm trying to test if bit1 is set. Why doesn't it work?Thanks.

author: AndyC_772
author_rank: Super Contributor
timestamp: February 25, 2016, 06:54:06 am
content: Your code says 0x00000010, not 0b00000010.You're comparing against the value 16, not 2.

author: DTJ
author_rank: Super Contributor
timestamp: February 25, 2016, 08:27:17 am
content: Oh FFS! Thank you very much!So obvious, I've been looking at it for hours......

author: Howardlong
author_rank: Super Contributor
timestamp: February 25, 2016, 05:35:32 pm
content: Been there, done that...

author: AndyC_772
author_rank: Super Contributor
timestamp: February 25, 2016, 05:38:29 pm
content: Me too!

author: dannyf
author_rank: Super Contributor
timestamp: February 25, 2016, 05:47:34 pm
content: Binary notation isn't terribly helpful, as this example shows.

author: DTJ
author_rank: Super Contributor
timestamp: February 26, 2016, 04:43:39 am
content: I just find using binary for flag registers more intuitive.

author: mikerj
author_rank: Super Contributor
timestamp: February 26, 2016, 08:07:27 am
content: Quote from: DTJ on February 26, 2016, 04:43:39 amDon'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 proneCode:#define BV(x) (1U<<x)/* Bit mask for bit 3 */#define MY_BITMASK BV(3)

author: dannyf
author_rank: Super Contributor
timestamp: February 26, 2016, 11:28:35 am
content: Code:#define BV(x) (1U<<x)Seems to be a very dangerous way of writing a macro.

author: mikerj
author_rank: Super Contributor
timestamp: February 26, 2016, 11:50:14 am
content: Quote from: dannyf on February 26, 2016, 11:28:35 amSeems to be a very typical way of writing a macro. How would you write it, given the use to which it will be put?

author: dannyf
author_rank: Super Contributor
timestamp: February 26, 2016, 11:59:42 am
content: QuoteJust 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.

author: dannyf
author_rank: Super Contributor
timestamp: February 27, 2016, 12:31:10 pm
content: "#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.

author: Kalvin
author_rank: Super Contributor
timestamp: February 27, 2016, 12:54:06 pm
content: Quote from: dannyf on February 26, 2016, 11:28:35 amYes, 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.

author: dannyf
author_rank: Super Contributor
timestamp: February 27, 2016, 02:56:43 pm
content: 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.

author: Kalvin
author_rank: Super Contributor
timestamp: February 27, 2016, 04:11:04 pm
content: Quote from: mikerj on February 26, 2016, 11:50:14 amYou 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.

author: mikerj
author_rank: Super Contributor
timestamp: March 04, 2016, 02:29:50 pm
content: Quote from: Kalvin on February 27, 2016, 04:11:04 pmI'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.

author: macboy
author_rank: Super Contributor
timestamp: March 04, 2016, 06:55:08 pm
content: Quote from: mikerj on March 04, 2016, 02:29:50 pmI 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:#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)".