Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] help me in programming :) detecting the input LOW bits..

Status
Not open for further replies.

romel_emperado

Advanced Member level 2
Joined
Jul 23, 2009
Messages
606
Helped
45
Reputation
132
Reaction score
65
Trophy points
1,318
Location
philippines
Activity points
6,061
guys, do you have any hint on how to program this?

this is the problem.
example if i have input in POR1 = 1111 1010

i need to know which bit is low. as seen in the example there are two low/0 values in the input... how to detect that??

actually from the example P1.0 and P1.2 are low.


i can do that using switch case but i think there is another method to do that. imagine that is 8bits i cant manage to put all that case combination by using switch case from 0 - 255. :sad:
 

simply run in a for loop,

example,

for(i = 0; i<=8; i++)
{
if(POR1 & (0x01 << i))
BIT IS SET
else
BIT CLEAR
}
 
hi raco, what do you mean by bit is set and bit is clear?

how does it actually work?

as i understand it is scanning from P1.0 to P1.7

but i dont actually get the application of bit is set? its my first time to know about that :sad:

im thinking to output the data to another port.. let say if i detected that P1.0 is low then it will output as 1 then if the last bit(P1.7) is low the output is 8..
 
Last edited:

Bit set and clear is another term for saying that bit is 1 and 0, respectively.

It becomes even easier if you want to output to another port, simply output the value of "i" when you detect a high bit.


for(i = 0; i<8; i++)
{
if(POR1 & (0x01 << i))
PORTX = i + 1;
}
 
i dont realy get it.. i tried it using proteus but it only counts 1 to 8 ;(

this line if(P1 & (0x01 << i)) dont take it's effect

for(i = 0; i<8; i++)
{
Delay_ms(2000);
if(P1 & (0x01 << i))
P3 = i + 1;
}
 

so probably you could try,

for(i=1; i<256; i= i*2)
if(P1 & i)
P3 = i;

But logically, they both should work fine and are same.
 

for(i = 0; i<8; i++)
{
if(POR1 & (0x01 << i))
PORTX = i + 1;
}

If you want to set the bit of the other port then PORTX = i + 1; will not do much, it can only take values from 1 to 8.
If you want to set or clear the bits of the other port then you should use


Code C - [expand]
1
2
3
4
5
6
7
for(i = 0; i<8; i++)
{
    if(POR1 & (0x01 << i)) 
        PORTX |= (1<<i); // set bit
    else 
        PORTX &= ~(1<<i);   // clear bit
}



but if you just want to copy a port to another port then why not use PORY=PORX;

Alex
 
Both of the replies from raco_rage's posts are spot on and totally correct. However, if I may add, romel_emperado mentioned that he wants to output a numerical value everytime he encounters a low bit. The following code (actually 99.99% replica of raco_rage's code) should work. If not, then I believe the problem should be somewhere else in the code.

for(i = 0; i<8; i++)
{
if(!(POR1 & (0x01 << i))) // TRUE for bit = 0
PORTX = i + 1;
}

(Note: Are you sure you want to output an integer to the port at ever occurence of a corresponding low bit? Normally, one would have to bitmask a byte on to a port.....)
 
(Note: Are you sure you want to output an integer to the port at ever occurence of a corresponding low bit? Normally, one would have to bitmask a byte on to a port.....)

yes i that exactly i want to do. i need to detect what pins of P1 is low or high.....


guys, thanks for your help.. i will try your suggestions...
 
Last edited:

yea, exactly as Klen says, there should be a ! for low.

As the OP says that he wants to output values only from 1 to 8, corresponding to each bit position, he wouldn't need a output value more than 8 unless he is checking a 16 or 32 bit number.
 
I'm still not clear what exactly you are trying to do,
do you want to have

input out
00000001 -> 00000001
00000010 -> 00000010
00000100 -> 00000011
00001000 -> 00000100
00010000 -> 00000101

please give an example of the input and output you expect.

Alex
 
please give an example of the input and output you expect.

example if i have this input:

P1 = 1011 1110

i must know that the two zeros in P1 are located in PIN1 of port1 and the other zero is in port7 of port1... that's what i want to do.. i dont have idea how to do that.. ;(
 

example if i have this input:

P1 = 1011 1110

i must know that the two zeros in P1 are located in PIN1 of port1 and the other zero is in port7 of port1... that's what i want to do.. i dont have idea how to do that.. ;(

The easiest way I can think of right now is to light some LEDs in active low logic. The code is already provided so it shouldnt be difficult :)
 
do u mean you want,

P3 = ~P1 ??

nope.. sorry for causing you trouble guys..

this is the problem.. my port1 is monitoring for possibe input.. port1 has 8bits and then for example if P1 detected a value of 253. 253 in binary is 1111 1101

now, i want to know which PIN is low.... obviously if we look at the example input the low bit is in P1.1. let say for example i want to display like this "the zero value of 253 is located at P1.1" :)

something like that guys..


here is an example code i am using but it's not perfect it cant detect the whole 8bits..


Code C - [expand]
1
2
3
4
5
6
7
8
9
int x = 10100101;
// First bit
(x >> 0) % 2; // 1 is detected that it is in  P1.0
// Second bit
(x >> 1) % 2; // 0 is detected that it is in  P1.1
// Third bit
(x >> 2) % 2; // 1 is detected that it is in  P1.2
...
etc.

 
Last edited:

There are some useful defines that you can use , just put them in the start of your code and you can use them after that


Code C - [expand]
1
2
3
4
5
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT)) 
#define CLEARBIT(ADDRESS,BIT) (ADDRESS &= ~(1<<BIT)) 
#define FLIPBIT(ADDRESS,BIT) (ADDRESS ^= (1<<BIT)) 
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT)) 
#define WRITEBIT(RADDRESS,RBIT,WADDRESS,WBIT) (CHECKBIT(RADDRESS,RBIT) ? SETBIT(WADDRESS,WBIT) : CLEARBIT(WADDRESS,WBIT))



To use them

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
SETBIT(PORTX,2);  // set bit2 of portx (set to 1)
SETBIT(PORTY,0);  // set bit0 of porty
 
CLEARBIT(PORTX,5); // clear bit5 of portx (set to 0)
 
FLIPBIT(PORTX,2);  // invert bit2 of portX, if it was 1 it becomes 0, and if 0 becomes 1
                    // example for portx 0x10101010 the result is 0x10101110
                    
if (CHECKBIT(PORTX,4)) {} else {} // result is true if bit4 of portx is 1, false if it is 0
 
WRITEBIT(PORTX,0,PORTY,2);   // copy the bit0 of portx to bit2 of porty



Alex
 
hi alexan_e,

thanks for that.. i will surely try that...

---------- Post added at 16:54 ---------- Previous post was at 16:48 ----------

thanks alexan_e, :)

it solves my problem.. excellent!!! :-D:-D:-D:-D

this is my code actually


Code C (Mac) - [expand]
1
2
3
4
5
6
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
void main()
{
  SETBIT(P1,2);
  P3 = SETBIT(P1,0);
}



---------- Post added at 16:59 ---------- Previous post was at 16:54 ----------

to all of your.. thank you so much and sorry for causing your trouble :)
 

SETBIT(P1,2); sets P1 bit2 to 1

SETBIT(P1,0); sets P1 bit0 to 1 and copies the result byte to P3, is this what you want?

Alex
 

even if he was able to solve the problem, I am still not clear what he really wanted?
 

SETBIT(P1,2); sets P1 bit2 to 1

SETBIT(P1,0); sets P1 bit0 to 1 and copies the result byte to P3, is this what you want?

Alex


yes that's really what i want.. i million thanks to you :)

actually the logic behind this solves my problem

#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))


---------- Post added at 17:17 ---------- Previous post was at 17:09 ----------

oops sorry raco_rage , actually its not totally solved. im thinking of another but alexan_e give me idea..

its hard to explain how i solve my problem due to trial and error... hehe Im new to programming and Im learning just myself relying on some tut found in google.. :) thanks for helping guys :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top