However it doesn't work, and I know why, but I don't know how to make it work!
if you have MPlab run it in the simulator and see what happens...
PORTB does this:
00000001
00000010
00000100
00001000
00010000
00100000
01000000
10000000
01000000
10000000
01000000
because after getting to 10000000 it shifts right, so I get 01000000, but then it shift left again, so I have 10000000 again, I have wasted about the whole afternoon trying to get this to work...
goncaloc’s code works because it executes a full right shift sequence followed by a full left shift sequence.
If you wanted the program to work the way you were writing it, you’d need to add an extra boolean/bit variable to indicate the direction the PORTB register is to be shifted. When the active LED hits one end of the register, you flip the boolean value and the LED takes off in the opposite direction. Something like this:
They’re about the same really, both require a value to act as ‘memory’ (aux_var, d) so the PIC knows what part of the sequence it’s up to.
If you want to free up some resources the first thing you should look at is replacing that delay loop with a timer based routine. Start by polling the timer complete flag, and then give the timer interrupt a try.