Jump to content

Recommended Posts

I am trying to toggle an LED using a Timer interrupt and the NOT operator, with the expression

portb.4=~portb.4

When I run the code through the simulator the LED comes on and stays on. Could someone

please explain what I am doing wrong.

 

adam

Link to post
Share on other sites

Hi

 

This kind of operations is OK for RAM variables but be careful when addressing I/O ports.

The PIC micros implement the bit operations in a three step process.

1 - The register value is read to a temporary buffer (whole byte)

2 - The bit operations is done in this buffer

3 - The result is stored in the register (whole byte).

 

When it comes to I/O ports the read is from the "pins" and "write" is to the output drivers buffer.

Its not guaranteed that the "read" retuens what was previously writen as the pin state can be forced by external hardware or suffer from electrical noise.

Another issue is when doing bit operations in a fast sequence, like updating diferent bits in a row. The output drivers take some time to settle so if you operate one bit of the port and then another one, the secfond operation might read a previous value from the first bit and not teh new one.

This is known as the "R-M-W" issue and documented in Microchip datasheets.

 

If the PIC has accessible output latches (LATx registers) use these instead of the port registers for this operations, if not, setup your own internal shadow register and write to the port the whole byte at a time.

 

AFAIK simulators don't implement this R-M-W issue so, from time to time, someone complains that a program working in a simulator doens't work in the actual device.

 

 

 

Best regards

Jorge

Link to post
Share on other sites
  • 2 weeks later...

......

AFAIK simulators don't implement this R-M-W issue so, from time to time, someone complains that a program working in a simulator doens't work in the actual device.

.....

For other simulators I'm not sure of but for Labcenter Proteus VSM it's entirely not true.

The Microchip read-modify-write mechanism is fully modeled in Proteus VSM such that if you have pins that share analog features and you cannot disable them then you will get 0 readings for that pin when you implement bit operations. Also, when you implement bit operations you'll get a pin state that also depends by the input logical states exactly as in the reality. Even the electrical noise falls in this category; it (the noise) may always be simulated by a digital pattern generator connected to the relevant pins.

 

Just FYI.

Link to post
Share on other sites

Hi

 

...when you implement bit operations you'll get a pin state that also depends by the input logical states exactly as in the reality....

What I call the R-M-W issue, is not the R-M-W behaviour in presence of inputs. Is the problem with multiple successive bit operations that corrupt outputs due to the lack of time to stabilize the output buffers.

 

A code example that shows this kind of issue might be something like this:

.
clrf TRISB
.
bsf PORTB, 0
bsf PORTB, 1
bsf PORTB, 2
bsf PORTB. 3
bsf PORTB, 4
bsf PORTB, 5
bsf PORTB, 6
bsf PORTB, 7

In theory you should have PORTB = 0xff at the end of this sequence.

Due the the so called R-M-W issue you will end up with an impredictable state for each output pin.

 

Another example is trying to implement a fast oscillation on a pin using bit operations.

I've been bitten by this one myself when trying to output a 32 KHz square wave.

.
.
bcf  TRISB, 2
bcf  PORTB, 2
.
main_loop
  movlw  DELAY_VAL
  movwf  DelayCnt
loop_1
  decfsz  DelayCnt, F
  goto  loop_1
  bsf  PORTB, 2  
  movlw  DELAY_VAL
  movwf  DelayCnt
loop_2
  decfsz  DelayCnt, F
  goto  loop_2
  bcf  PORTB, 2
  goto main_loop
.

The output pin (RB2) never reached the logical high or low state, the scope only showed noise.

It worked after I changed the code to operate on a RAM register and copied the register as as whole to the port.

 

I've used a number of simulators that don't mimic this behaviour, but I never used PROTEUS. if it does, I might consider moving in that direction.

Thanks for the info.

 

 

Best regards

Jorge

Edited by JorgeF
Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...