6
\$\begingroup\$

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:

enter image description here

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.

enter image description here

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)

enter image description here

\$\endgroup\$
4
\$\begingroup\$

PICs have the annoying gotcha of having ANSEL default to ones, which means that ports that can act as analog inputs are so by default. This will force digital reads to zero (see port diagram below taken from the PIC16F833 datasheet to understand why), and that is probably why RB0 gets cleared when setting RB1 with a Read-Modify-Write operation.

enter image description here

In main, clear ANSEL before accessing PORTB and try again.

By the way, LEDs usually drop more than 0.7V, ~2V is common, so it is possible that the first experiment succeeded for the same reason that the second one failed.

\$\endgroup\$
2
\$\begingroup\$

Wait a minute. Aren't RB0 and RB1 TTL outputs? If they go high and are only measuring 0.7 volts then you're pulling too much current from them and possibly damaging the output drivers.

A quick scan of the specs shows RB0/RB1 to have max 25mA output. At 100 ohms (assuming the diode drop is 0.6V). You're going to try to source 4.4V/100 = 44mA. Ouch! Try changing the 100 ohms to 375 ohms or more. You might have already done permanent damage to the drivers so you might need to move to RB2 and RB3.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.