View previous topic :: View next topic |
Author |
Message |
piko
Joined: 11 Nov 2008 Posts: 3
|
16 bits mask |
Posted: Tue Nov 11, 2008 11:52 am |
|
|
Hi. I'm trying to write a small application that is running 16 leds turning them on and off on a specific sequence. I made an array of 16bit values that is masked to reveal if a given led should be on or off.
Eg.:
Code: |
int16 sequence[] = {
0b0001000000000001,
0b0000010000000011
};
int16 mask[] = {
0b1000000000000000,
0b0100000000000000,
0b0010000000000000, ..... etc
} |
Below is a function call repeated over and over. The ptr variable indicates the position in the sequence.
Code: | for (i=0; i<16; i++) // scan all 16 bits
{
if ((sequence[ptr] & mask[i]) == mask[i])
{
... code to turn led on ...
}
else { ... turn the led off ... }
}
ptr++; |
Actually, this works on half of the leds (the 8 last ones) while the first 8 leds are constantly turned on.
I searched the forum and I can't find any example of masking 16bit values so I was wondering if it was possible or if there was a trick to get it to work properly.
Any help would be appreciated.
Thanks in advance |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Tue Nov 11, 2008 1:33 pm |
|
|
I don't see anything wrong with your code. I would look at the .lst file and see what the assembly code for that section is doing. That usually tells me what I have done wrong in my C code. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 11, 2008 1:59 pm |
|
|
If you can't make it work, post a small version of the program that
demonstrates the problem. It must be compilable without errors.
It must have the #include, #fuses, and #use delay() statements
and a main(). It must be short (50 lines max).
Post a description of the LED circuits, including component values
and part numbers (if relevant).
Post your compiler version.
Tell us if this code is being tested on actual hardware, or if it's a Proteus
simulation. |
|
|
piko
Joined: 11 Nov 2008 Posts: 3
|
|
Posted: Tue Nov 11, 2008 11:44 pm |
|
|
Thanks guys. Actually I discovered that my problem was the PIC itself. I swapped to another chip (same model) and it was working fine...
I can't believe I wasted 1 hour on that while it was only a bugged pic
I will post my code when I'm done for the community to use.
Thanks again. |
|
|
Ttelmah Guest
|
|
Posted: Wed Nov 12, 2008 9:51 am |
|
|
As a separate comment, why not look at the inbuilt 'bit test' function, rather than using the mask?.
Best Wishes |
|
|
piko
Joined: 11 Nov 2008 Posts: 3
|
|
Posted: Wed Nov 12, 2008 1:58 pm |
|
|
Quote: | why not look at the inbuilt 'bit test' function, rather than using the mask?. |
I come from a foreing world where it is normal not to look at the documentation because you know 'C' doesn't offer what you want !
I never get used to all the bling blings that comes with CCS that is meant to ease my life so I do it the hard way
Actually, only because I'm used to masks and the concept in my head involved one, I didn't look further. Your way would be nice as well. Thank you :D |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Wed Nov 12, 2008 4:51 pm |
|
|
C at least offers a shift operator that also can do the job. |
|
|
|