I learned a concept that when changing bit value of PIC I/O pin, PC would first read the value of the I/O port, and change the value, and write the changed value to the register.
I validate this argument by using this circuit:
The code I wrote in mikroC for PIC:
void main() {
TRISB = 0;
PORTB.F0 = 1;
PORTB.F1 = 1;
while(1) ;
}
I learned that because LED D1 only need 0.7 V, so RB0's voltage is about 0.7V. When my program set RB1 to 1, PIC first read the value of port B, get 0000 0000. And since RB0 is 0.7V, it was interpreted as low. So after changing the value(set prot B as 0000 0010), only D2 is on.
So I connect resistors between LED and IO pins, and changed the code:
void main() {
TRISB = 0;
PORTB.F0 = 1;
Delay_ms(500);
PORTB.F1 = 1;
while(1) ;
}
At the beginning of simulation, the circuit act as I expected, voltage of RB0 is 4.5V.
However, after the 500ms delay, only D2 is on. It seems weird to me, I thought D1 and D2 would be on at the same time.(Read should get 0000 0001, so after changing, port B should be 0000 0011)