Jump to content
Sign in to follow this  
Pierre

How To Use Ra4 As An Output With 16f628

Recommended Posts

Hi,

I know that ra4 is an open drain output and can only sink current but that is not the problem. I would like to know if ra4 can be used as an ordinary and direct output like the other output or is it used as the output of the comparator? Here is my problem. I have attached to ra4 a led in series wiht a resistance to vdd but the led stays on all the time. It should go on only when ra4 is set (bsf porta,4) and go off when ra4 is cleared (bcf porta,4). I'm I doing something wrong?

Share this post


Link to post
Share on other sites

Pierre,

 

I have attached to ra4 a led in series wiht a resistance to vdd but the led stays on all the time. It should go on only when ra4 is set (bsf porta,4) and go off when ra4 is cleared (bcf porta,4). I'm I doing something wrong?

Yes you are doing something wrong! Your logic is inverted. With the LED tied to Vdd if ra4 is set i.e. Logic 1 then your LED will be off. Clear ra4 i.e. Logic 0 and it will be on.

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Thankyou David for trying to find the problem, I appreciate. I'm aware that the output is inverted but in my test program I toggle between on and off with a pause between but the led stays on all the time. So it means that the open-drain is conductive all the time. I suspect the problem coming from a configuation but I don't know where. I have attached my test program. If you see any problem with it, please let me know. I have desable the comparator. Thanks

 

 

 

 

Pierre,

 

I have attached to ra4 a led in series wiht a resistance to vdd but the led stays on all the time. It should go on only when ra4 is set (bsf porta,4) and go off when ra4 is cleared (bcf porta,4). I'm I doing something wrong?

Yes you are doing something wrong! Your logic is inverted. With the LED tied to Vdd if ra4 is set i.e. Logic 1 then your LED will be off. Clear ra4 i.e. Logic 0 and it will be on.

 

Regards

 

davidb

test_open_drain.txt

Share this post


Link to post
Share on other sites

Hi David,

It seems that my reply was not sent so I'm trying again.

 

Thank for helping me but I'm aware of the inversion. In my test program I toggle between on and off but the led stay on. I beleive the problem is somewhere else, maybe in configuration. Comparator are desable.

 

 

Pierre,

 

I have attached to ra4 a led in series wiht a resistance to vdd but the led stays on all the time. It should go on only when ra4 is set (bsf porta,4) and go off when ra4 is cleared (bcf porta,4). I'm I doing something wrong?

Yes you are doing something wrong! Your logic is inverted. With the LED tied to Vdd if ra4 is set i.e. Logic 1 then your LED will be off. Clear ra4 i.e. Logic 0 and it will be on.

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Pierre,

 

First take a look at your configuration code:

;configuration des ports

movlw 0xff ;port rb = entrées

tris portb

clrf portb

movlw 0x00

tris porta ;port ra = sorties (bit 0=direction A; 1=direction :)

clrf porta

I am not sure what you intended your code to do but if you want to set the direction of a port or port pin you must write a value to the appropriate TRIS register e.g.

 

movlw 0xff

movwf TRISB ; Set all Port B pins to inputs

 

clrf TRISA ; Set all Port A pins to outputs

 

movlw 0b11101111

movwf TRISA ; Set Port Pin RA4 to output, rest to inputs

 

bcf TRISA,4 ; Set Port Pin RA4 to output, rest as set previously

 

I haven't shown it but you also need to manipulate the bank bits as the TRIS registers are in bank 1. Don't forget to change back again.

Regards

 

davidb

Share this post


Link to post
Share on other sites

Thank you David,

 

I know that using tris the way I do is not recomended but it works for all the other ports exept ra4 (in my program I do the test for other ports at the same time). But I will try to do your way to see if it works. I will come back to you later to give you the result. Thanks again.

 

Pierre

 

 

Pierre,

 

First take a look at your configuration code:

;configuration des ports

movlw 0xff ;port rb = entrées

tris portb

clrf portb

movlw 0x00

tris porta ;port ra = sorties (bit 0=direction A; 1=direction B)

clrf porta

I am not sure what you intended your code to do but if you want to set the direction of a port or port pin you must write a value to the appropriate TRIS register e.g.

 

movlw 0xff

movwf TRISB ; Set all Port B pins to inputs

 

clrf TRISA ; Set all Port A pins to outputs

 

movlw 0b11101111

movwf TRISA ; Set Port Pin RA4 to output, rest to inputs

 

bcf TRISA,4 ; Set Port Pin RA4 to output, rest as set previously

 

I haven't shown it but you also need to manipulate the bank bits as the TRIS registers are in bank 1. Don't forget to change back again.

Regards

 

davidb

Share this post


Link to post
Share on other sites

Pierre,

 

When I first started PIC programming in asm I forced myself to do everything the preferred Microchip way and obviously did such a good job I completely forgot about the 'old' non-preferred way of using tris! As far as I can see you have correctly disabled the comparators and your code does appear to toggle RA4 on the simulator. Without trying it on real hardware myself I can only suggest that you check your hardware:

 

1. Check that you haven't connected the LED and resistor to the wrong port pin. Been there and done that.

2. If you haven't already done it connect the LED to one of the other outputs and check that it toggles.

3. Use a meter with the PIC power off to confirm that the open drain transistor on RA4 is not permanently shorted.

4. Try another PIC.

 

BTW what resistor are you using to limit the LED current? One in the range 180R to 680R should be OK.

 

Regards

 

davidb

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
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...
Sign in to follow this  

×