Strange behavior of RA4

I have connected RA4 to Vcc through 10K register on PIC16F84A. This RA4 was connected base, Vcc - LED - 330 ohm - collector of C3202. RA0 - RA3 was connected to LCD DB4-DB7.

the following code lights LED.

ORG 0 BSF 03H,5 ; BANK 1 CLRF PORTA BCF 03H,5 ; BANK 0 BSF PORTA,4 GOTO $

but when I access other RA ports, the LED goes out. The following code does not lights LED.

ORG 0 BSF 03H,5 ; BANK 1 CLRF PORTA BCF 03H,5 ; BANK 0 BSF PORTA,4 BSF PORTA,0 ;==> just inserted this code GOTO $

What's wrong? What should I do for the LED light on while accessing other RA ports?

- QHHong

Reply to
qhhong
Loading thread data ...

"qhhong" schreef in bericht news: snipped-for-privacy@o13g2000cwo.googlegroups.com...

It looks like a RMW problem. Use an extra RAM adress to store data and write that register to Port A. During a BSF the Port is first read (actual value), after that the modifications are made, and finally written to the Port.

In your case the voltage on RA4 may be detected as low because of a too small resistor. Changing the resistor may solve the problem but is not the only way to solve it. Always take care of RMW problems in your code!

Alexander

Reply to
Alexander

BCF STATUS, RP0 ; CLRF PORTA ; Initialize PORTA by ; clearing output ; data latches BSF STATUS, RP0 ; Select Bank 1 MOVLW 0x0F ; Value used to ; initialize data ; direction MOVWF TRISA ; Set RA as inputs ; RA4 as output ; TRISA are always ; read as ?0?.

the above is cut from pic16f84.pdf so it wont help you as part of porta is set inputs but a small edit should sort it out.

;----------------------------------------------- org 0 ; org4 ; RETFIE ;return if an interupt happend org10 ; BCF STATUS, RP0 ; CLRF PORTA ; Initialize PORTA by ; clearing output ; data latches BSF STATUS, RP0 ; Select Bank 1 MOVLW 0x00 ; Value used to ; initialize data ; direction MOVWF TRISA ; ; All output ; TRISA are always ; read as ?0?. BCF STATUS, RP0 ; Back to page 0

;----------------------------------------------- ; now aleast here you know the state of the pic ; might not make any diffrence :-) ;----------------------------------------------- BSF PORTA,4 ; BSF PORTA,0 ;==> just inserted this code nop ; remove these nop's if it works ? nop ; nop ; GOTO $

well thats my guess let us know if you solve it anyway. David

Reply to
david

RA4 has open drain output (see page 15 in the datasheet) so you need an external pull-up resistor (to +5V) for this pin to work.

Johnny

"qhhong" skrev i en meddelelse news: snipped-for-privacy@o13g2000cwo.googlegroups.com...

Reply to
X-Nerd

errm...see below...

Reply to
JJ

Sorry, as JJ points out, I missed you allready did connect RA4 to Vcc.

Johnny

"JJ" skrev i en meddelelse news:43fda0cf$0$82628$ snipped-for-privacy@ptn-nntp-reader03.plus.net...

Reply to
X-Nerd

A.

too

the

I think that's it. He doesn't seem to have a resistor between the PIC pin (RA4) and the base of the transistor. This means that the voltage at the pin when driven high is still only about .6V. This is obviously going to result in a RMW problem.

A "shadow register" is the correct way to solve this, but replacing the pull-up with something like a 2.2K pull-up and then a 7.5K resistor to the base may solve it too. Of course when RA4 is driven low, it will waste allot more power than the 10K pull-up did. I'd go with the shadow register.

Reply to
Anthony Fremont

Thank you all for your replies.

I have changed the resistor value from 10K to 100K. ==> The same symptom. I have changed the code as David have recommanded. ==> The same symptom.

As Alexander have indicated, BSF also have the RMW problem. The only way was to save the RA4 value(10H or 00H) on another place and when PORTA modification is required, I have ORed this saved value and "MOVE" this value to PORTA. I found no other way than MOV instruction works.

Reply to
qhhong

You didn't try my way. Change the pull-up to 2.2K and place another

7.5K resistor from the PIC pin to the base of the transistor. This will allow the PIC to see a much higher voltage on RA4 when it is driving the LED. With the emitter of the transistor grounded, the base can only be .6V higher than ground. This will cause the PIC to interpret the port pin as being low since it is at such a low voltage.

But, the correct way to do this is to always use a shadow register and only MOV the contents of the shadow register to the PORTx.

Reply to
Anthony Fremont

not convinced a shadow register is needed but is good practice. but this problem on RA4 does interest me a lot never having seen such a simple problem causing so much trouble :-)

when driving an Led from RA 4 which is quite often I tend to just put a resistor (about 1k) and an led to 5 volts. This does then work in reverse setting ra4 turns off the led, clearing ra4 turns the led on.

but I do think Anthony's way should also work. If this goes on much longer I will be building your circuit to try an recreate problem :-)

another segment of working code FLASH btfss KEY_FLG,6 ; if set then light fully goto FLS_LED bcf PORTA,4 goto RECLAIM

FLS_LED btfss TMR_FLG,4 ; LED flasher flag goto NO_FLSH btfsc INS_FLG,2 ; is exit delay in process goto FAST_FL ; fast flash btfss TIME_CNT2,1 ; check bit in counter goto NO_FLSH bcf PORTA,4 ; drive LED goto RECLAIM

NO_FLSH bsf PORTA,4 ; LED off goto RECLAIM FAST_FL btfss TIME_CNT2,0 ; check bit in counter goto NO_FLSH bcf PORTA,4 ; LED on

the cut n paste did not make it look pretty

but just to show a shadow register is not aways the answer

David

Reply to
david

As was mentioned above, the transistor base to emitter voltage will hold the port low regardless of the pull up resistor. You must place a resistor in series with the port and the base to allow the port to register high. A simpler way to do this is to eliminate the transistor all together and drive the LED off of the port. Since the port does not pull up, connect it in a pull down fashion. Anode of LED to +V, Cathode of LED to 150 ohm resistor, other end of resistor to port pin. That's it. The code needs to be inverted: Shadowing the port will not be necessary.

;Fragment for turn on and port direction: bcf porta,4 ;turn on port before trissing, low on bsf status,rp0 ;bank1 bcf trisa,4 ;porta bit 4 to output or clrf trisa bcf status,rp0 ;bank0 ;continue

;Led off: bsf porta,4 ;high off ;continue

Good luck. Bob

Reply to
Bob Eldred

An alternative solution is to use a MOSFET to drive the LED where the gate won't pull the port low. Not sure if the pinouts will be compatable?

Reply to
Fred

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.