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
for(i = 0; i<8; i++)
{
Delay_ms(2000);
if(P1 & (0x01 << i))
P3 = i + 1;
}
for(i = 0; i<8; i++)
{
if(POR1 & (0x01 << i))
PORTX = i + 1;
}
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 }
(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.....)
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.. ;(
do u mean you want,
P3 = ~P1 ??
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.
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))
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
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); }
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
#define SETBIT(ADDRESS,BIT) (ADDRESS |= (1<<BIT))
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?