1) it is more than 15 years I'm not using PICs, so I hope I remember correctly the I/O port syntax...
2) I modified the code without trying to compile it, so maybe there are some errors in it
3) You never mentioned you was checking an input port (PORTB) and required to read its status: I hope to understand there can be a bit set to 0 and you have to identify it...
4) i removed your 'rando()' function because you need to call it only 1 time...
void main (void)
{
TRISD=0x00; // PORTD as output
TRISB=0xff; // PORTB as input
ADCON1 = 0b00010000;
ADCON0 = 0b00000001;
DelayUs(1);
GO_DONE = 1;
while (GO_DONE)
;
srand(ADRESH);
unsigned char randval=0, oldval=0, out, i; // they are UNSIGNED CHAR!!!!
randval=rando();
while (1) // infinite loop
{
randval = rnd() & 0x07 ; // only values from 0 to 7
if (randval != oldval)
{
// assign value to the 'old' variable
oldval = randval ;
// light the 'n'th led
PORTD = 1 << randval ;
// scan the 8 input ports
// 'out' is set to 0xFF to avoid undefined cases
for (out=0xFF, i=0 ; i<8 ; i++)
{
// check if input port 'i' is set to '0'
// the loop will be exited at the first occurrence of '0' on PORTB
// be careful! if every bit of PORTB is set to '1', 'out' value is UNDEFINED!!!
if (!(PORTB & (0x01 << i)))
{
out = i ; // set 'out' to number of port found to 0
break ; // exit from the loop: I have found a bit set to 0
}
}
}
}
}
[added some hours later]
Corrected the code to use Bitwise AND operator (&), instead of Logical AND operator (&&)