This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

bits are not setting

Can anybody tell me what is wrong in the following coding? I am unable to set P1.5 and P1.6 if I take the set memory from the variable and try to assign it to P1.5 or 6. But I am able to set P1.5 and 6 if directly assign from the saved debounce variable. Here below is the code.

 #include <reg51.h>
#include <stdio.h>

#define bitset(a,b) ((a)|=(1<<(b)))
#define bitclear(a,b) ((a)&= (1<<(b)))

sbit LEDp17 = P1^7;
sbit LEDp15= P1^5;
sbit LEDp16=P1^6;
sbit LEDp14=P1^4;
sbit PB0_0 = P0^0;
sbit PB0_1 = P0^1;
static char bdata ibase;
sbit pb00 = ibase^0;
sbit pb01 = ibase^1;
sbit firstscandone = ibase^2;
sbit testbit1 =ibase^3;
sbit testbitp15 = ibase^5;
sbit testbitp16 = ibase^6;

void delay1(int counts)
{
int i, j;
for(i=0; i<1000;i++)
for(j=0; j<counts; j++);
}

void init1(void)
{
delay1(20);
P0=0xff;
P1=0x00;
P0=0x00;
P1=0x00;
ibase=0;
//firstscandone=1;
//bitset(ibase,2);
testbit1=1;
}

void debounce(void)
{
int x, y;
if(PB0_0==1)
{
for(x=0; x<5; x++);
if(PB0_0==1)
{
pb00=1;
pb01=0;
}
}

if(PB0_1==1)
{
for(y=0; y<20; y++);
if(PB0_1==1)
{
pb01=1;
pb00=0;
}
}
}

void main(void)
{
if(firstscandone==0) init1();

while(1)
{

debounce();
if (pb00==1)
{
bitset(ibase,5);
delay1(10);
bitclear(ibase,6);
}

if(pb01==1)
{
bitset(ibase,6);
delay1(10);
bitclear(ibase,5);
}
if(firstscandone==1) bitset(P1,4);
if(testbitp15==1) bitset(P1,5);
else bitclear(P1,5);
if(testbitp16==1) bitset(P1,6);
else bitclear(P1,6);
//When any of P0.0 and P0.1 is switched on then P1.5 or P1.6 are not getting on but P1.4 goes off.
bitset(ibase,2);
}
}

Parents
  • 1) Wny did you create a new thread. What was wrong with your previous thread?
    http://www.keil.com/forum/19275/

    2) In the previous thread, I did ask you about your bitclear() - you didn't respond. Now, we have yet another thread with an identical bitclear(). Have you still not spent any time considering if your bitclear() really does what you intend it to do? Do you really want a bitclear() that clears all bits but one? Wouldn't you expect bitclear() to just clear one single bit, while leaving the other bits untouched? What was your result of trying out the little example I gave you in the previous thread - what does your bitclear() do?

    3) When you start working at the bit level on integers, you should make it a rule to use unsigned integer types to avoid potential problems with the most significant bit potentially being a sign bit. Note that it is compiler-specific if char is signed or unsigned. Much better to use "unsigned char", or (if you have stdint.h) consider using uint8_t.

    4) Are you happy with the indenting of your code? Does it contain any indent at all, or did you use tab characters - tabs do often not work when posting source code on web forums. Whatever the problem is, you should have been able to spot it and made sure your posted code did intent properly to allow readers to easily see what you have written.

    5) If reading unknown code, what valuable insights would you get when you see the variable name "ibase"? Do you fell that it is a very descriptive name? Can you describe how the name describes the meaning of the variable?

    6) Maybe you could spend some time, and count the number of comments you can find in your source code. Do you think that maybe you have too many comments?

    7) How many times do you plan to run the first part of main()?

    void main(void) {
        if(firstscandone==0) init1();
        ....
    }
    


    Is "firstscandone" expected to have different values on different runs of the program?

Reply
  • 1) Wny did you create a new thread. What was wrong with your previous thread?
    http://www.keil.com/forum/19275/

    2) In the previous thread, I did ask you about your bitclear() - you didn't respond. Now, we have yet another thread with an identical bitclear(). Have you still not spent any time considering if your bitclear() really does what you intend it to do? Do you really want a bitclear() that clears all bits but one? Wouldn't you expect bitclear() to just clear one single bit, while leaving the other bits untouched? What was your result of trying out the little example I gave you in the previous thread - what does your bitclear() do?

    3) When you start working at the bit level on integers, you should make it a rule to use unsigned integer types to avoid potential problems with the most significant bit potentially being a sign bit. Note that it is compiler-specific if char is signed or unsigned. Much better to use "unsigned char", or (if you have stdint.h) consider using uint8_t.

    4) Are you happy with the indenting of your code? Does it contain any indent at all, or did you use tab characters - tabs do often not work when posting source code on web forums. Whatever the problem is, you should have been able to spot it and made sure your posted code did intent properly to allow readers to easily see what you have written.

    5) If reading unknown code, what valuable insights would you get when you see the variable name "ibase"? Do you fell that it is a very descriptive name? Can you describe how the name describes the meaning of the variable?

    6) Maybe you could spend some time, and count the number of comments you can find in your source code. Do you think that maybe you have too many comments?

    7) How many times do you plan to run the first part of main()?

    void main(void) {
        if(firstscandone==0) init1();
        ....
    }
    


    Is "firstscandone" expected to have different values on different runs of the program?

Children
No data