PDA

View Full Version : Problem Keeping PORTD.1 LOW



koossa
- 31st August 2005, 18:20
Hi Picers

I am designing a project to switch my circuit on with a membrane keypad using a MAX1672 (see attached drawing) with a PIC16F877 @ 20MHz.

The ONB Pin on the MAX1672, is by default HIGH, to keep the circuit off, if I press the ON button on my keypad it should switch it ON and then in my code I would put PORTD.1 LOW to keep it on, but this does not work.
When I press the ON button it switch the circuit on, but when I release the button it switch it off again, so my PORTD.1 is not kept LOW.

I have asked someone on the microchip mailing list if this is a bug, and this is what he has said:

<hr>
<i>
No, it's not a bug. Read-modify-write is just how the chip works:

When you do a BSF 0x30, 3 the chip does like this:

1) Read the value at register 0x30 in the ALU.
2) Change the value of bit 3 (set)
3) write back the modified value to register 0x30.

But when a read of a port register takes place, the pin voltage is read, instead of the internal value written to the port. If you have a pullup on the pin, and set it LOW, when you read the pin's value you will get HIGH, because the pullup resistor is forcing it HIGH.

So, for BSF PORTD,3, the chip does like this:

A) Read the pins of PORTD in the ALU;
B) Modify the value of bit 3 (set)
C) Write back the modified value to register 0x30.

See? you need to buffer to avoid reading the pins in a r-m-w.

Eventually they changed the PORT circuitry in the 16bits PICs, to include another register, called LATD, which does the same thing as our PORTD_BUFFER above.
</i>
<hr>

Is there a solution for this in PICBASIC?

CocaColaKid
- 31st August 2005, 18:43
Have to tried switching a transistor which switches on the MAX1672? The IO might be getting too loaded.

Bruce
- 31st August 2005, 18:47
It would help if you posted your code. This doesn't sound like a read-modify-write problem.

koossa
- 31st August 2005, 18:54
I have made quite a lot of changes in my code to see if I can get it to work, so there might be some things that's not supposed to be there:

<hr>
&nbsp;&nbsp;INCLUDE "modedefs.bas" 'Standard Include for some variables.
&nbsp;&nbsp;DEFINE OSC 20 'Oscilator speed.

&nbsp;&nbsp;Symbol SO = PortA.0 ' Serial Out

&nbsp;&nbsp;Key_Col Var BYTE ' Keypad column.
&nbsp;&nbsp;Key_Row Var BYTE ' Keypad row.
&nbsp;&nbsp;Key_Val Var BYTE ' Key value.
&nbsp;&nbsp;sKey Var BYTE ' Key String Value.
&nbsp;&nbsp;SWITCHPIN VAR PORTD.1

Main:
&nbsp;&nbsp;ADCON1 = 7 ' PortA = digital I/O
&nbsp;&nbsp;OPTION_REG.7 = 0 ' Enable PORTB pull-ups
&nbsp;&nbsp;PORTD = %01000000
&nbsp;&nbsp;TRISD = %10000000 ' Set Pin to Output
&nbsp;&nbsp;LOW SWITCHPIN

checkKeyDown:
&nbsp;&nbsp;PAUSE 50 ' Debounce key-input
WaitForKeysUp: ' Wait for all keys up
&nbsp;&nbsp;PORTB = $f ' All output-pins high
&nbsp;&nbsp;PAUSE 50 ' Debounce key-input
checkKeyPress: ' Wait for keypress
&nbsp;&nbsp;FOR Key_Row = 0 TO 3 ' 4 rows in keypad
&nbsp;&nbsp;&nbsp;&nbsp;PORTB = $f ' All output-pins high
&nbsp;&nbsp;&nbsp;&nbsp;TRISB = (DCD Key_Row) ^ $ff ' Set one row pin to output
&nbsp;&nbsp;&nbsp;&nbsp;Key_Col = PORTB >> 4 ' Read columns
&nbsp;&nbsp;&nbsp;&nbsp;IF Key_Col != 0 THEN ' If any keydown, exit
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Key_Val = (Key_Row * 4) + (NCD (Key_Col ^ 0))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LOOKUP Key_Val,[" 147S<ORE369.2580"],sKey
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Serout SO, N9600, [#Key_Val] ' Send Character
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;goto checkKeyDown
&nbsp;&nbsp;&nbsp;&nbsp;endif
&nbsp;&nbsp;NEXT Key_Row
&nbsp;&nbsp;GOTO checkKeyPress
End
<hr>

Darrel Taylor
- 31st August 2005, 18:56
Hi koossa.

That guy's explaination doesn't make much sense. If you're holding the ON button, the pin is being held low. &nbsp;How is the Pull-Up resistor going to cause a problem with R-M-W. The Pin will be set low in the program long before the button is released.

How are you setting the output?

LOW PORTD.1
-or-
PORTD.1 = 0

If it's the second one. Are you also setting the appropriate TRIS bit?

TRISD.1 = 0 ' Set PORTD.1 to output
-or-
OUTPUT PORTD.1
<br>
oops, caught you in the middle of posting. I'll take abother look.

koossa
- 31st August 2005, 18:58
I'm using LOW PORTD.1

Darrel Taylor
- 31st August 2005, 19:16
After power up, the pin gets set low, and then immidiately it starts scanning the keypad. With your finger still on the button, you now have a dead short from the 5v being output on PORTB to the 0v being output on PORTD.1.

This will probably cause the MAX1672 to shut-down. Or to oscillate between on and off untill you release the button.
<br>

Maybe not, I see that 2k resistor now. rats.

koossa
- 2nd September 2005, 16:24
The following code Keep my Portd.1 High
<HR>
<p>&nbsp;&nbsp;&nbsp; INCLUDE &quot;modedefs.bas&quot; 'Standard Include for some
variables.<br>
&nbsp;&nbsp;&nbsp; DEFINE OSC 20 'Oscilator speed. <br>
<br>
&nbsp;&nbsp;&nbsp; Symbol SO = PortA.0 ' Serial Out<br>
<br>
&nbsp;&nbsp;&nbsp; Key_Col Var BYTE ' Keypad column.<br>
&nbsp;&nbsp;&nbsp; Key_Row Var BYTE ' Keypad row.<br>
&nbsp;&nbsp;&nbsp; Key_Val Var BYTE ' Key value.<br>
&nbsp;&nbsp;&nbsp; sKey Var BYTE ' Key String Value.<br>
&nbsp;&nbsp;&nbsp; SWITCHPIN VAR PORTD.1<br>
<br>
Main:<br>
&nbsp;&nbsp;&nbsp; ADCON1 = 7 ' PortA = digital I/O<br>
&nbsp;&nbsp;&nbsp; OPTION_REG.7 = 0 ' Enable PORTB pull-ups<br>
&nbsp;&nbsp;&nbsp; PORTD = %00000010<br>
&nbsp;&nbsp;&nbsp; TRISD = %10000000 ' Set Pin to Output<br>
&nbsp;&nbsp;&nbsp; LOW SWITCHPIN<br>
<br>
END</p>

<HR>

But when I put a loop in or any other code it does not keep my PORTD.1 High.
As soon as I release the ON button it turn off again?

<HR>
<p>&nbsp;&nbsp;&nbsp; INCLUDE &quot;modedefs.bas&quot; 'Standard Include for some
variables.<br>
&nbsp;&nbsp;&nbsp; DEFINE OSC 20 'Oscilator speed. <br>
<br>
&nbsp;&nbsp;&nbsp; Symbol SO = PortA.0 ' Serial Out<br>
<br>
&nbsp;&nbsp;&nbsp; Key_Col Var BYTE ' Keypad column.<br>
&nbsp;&nbsp;&nbsp; Key_Row Var BYTE ' Keypad row.<br>
&nbsp;&nbsp;&nbsp; Key_Val Var BYTE ' Key value.<br>
&nbsp;&nbsp;&nbsp; sKey Var BYTE ' Key String Value.<br>
&nbsp;&nbsp;&nbsp; SWITCHPIN VAR PORTD.1<br>
<br>
Main:<br>
&nbsp;&nbsp;&nbsp; ADCON1 = 7 ' PortA = digital I/O<br>
&nbsp;&nbsp;&nbsp; OPTION_REG.7 = 0 ' Enable PORTB pull-ups<br>
&nbsp;&nbsp;&nbsp; PORTD = %00000010<br>
&nbsp;&nbsp;&nbsp; TRISD = %10000000 ' Set Pin to Output<br>
&nbsp;&nbsp;&nbsp; LOW SWITCHPIN<br>
&nbsp;</p>
<p>checkKeyPress:</p>
<p>&nbsp;&nbsp;&nbsp; GOTO checkKeyPress</p>
<p><br>
END</p>
<HR>
<HR>

victorf57
- 5th September 2005, 01:27
I have been bit the portD being parallel.
I believe you have to set the trisE register to control it.
I can't remember exactly but you may want to check it out.
victor

sougata
- 5th September 2005, 08:59
Hi there,

You cannot use the pin (portd.1) for any other purpose like a keyboard scanning routine (which you r doing as far as your schematic goes ).

Please take note of the following. Why don't you use the ONA pin on the MAX1672.That is positive edge triggered. By default when a PIC wakes up the pin in an input (tristated). Do the following

1. Turn of the AD and make the pin digital (if it has a muxed analogue in)
2. Set the DATA direction register
3. Throw the value to the port

This will set your circuit kicking till you make the pin high and kill the power supply.

A detailed schematic and complete code would be appreciated.

Regards

Sougata

Acetronics2
- 5th September 2005, 09:59
Hi there,

1. Turn of the AD and make the pin digital (if it has a muxed analogue in)
2. Set the DATA direction register
3. Throw the value to the port

Sougata

Hi, Sougata

If you want to have safe outputs ( no "transitory levels" ) at startup, use the following :

1. Turn of the AD and make the pin digital (if it has a muxed analogue in)
2. Throw the value to the port
3. Set the DATA direction register

Old " unlogic" trick, when high power outputs used ... Easy to show with a PIC electronic ignition i.e.

Alain

koossa
- 5th September 2005, 10:06
Attached is my circuit, and my picbasic code not working and the one who is working?

koossa
- 5th September 2005, 10:31
This code does not work
<hr>
INCLUDE "modedefs.bas"
DEFINE OSC 20

SWITCHPIN VAR PORTD.1

Main:
ADCON1 = 7
OPTION_REG.7 = 0
PORTD = %00000010
TRISD = %00000010
LOW SWITCHPIN

checkKeyPress:
GOTO checkKeyPress

End
<hr>

but if I remove the loop:
checkKeyPress:
GOTO checkKeyPress

Then it is working??

sougata
- 5th September 2005, 12:56
Hi there,

Your working and not working code has the difference of only one loop. That's absurd why it should not work. If the portd.1 goes low okay then there should not a problem of level difference of the 5volt level at the micro side and maximum permissible level of 3v at the MAX side. However try inserting a diode (1n4148) with its anode coonected to the portd side in series with the 2k resistor. So you receive only the ground (negative) going pulse from portb (the on switch).

try inserting some oscilloscope check routine like toggling a pin when you enter or exit the loop and find the relation of portd alongwith it. If possible post the entire code, the asm and the .cod file. I haven't worked much on the 16F877 as my choice of MCU is the 18F series still I will spend some time reading the manual and sort out any R-M-W issues. Would you mind spending a few miliamps ? Then go for a opto. Alternatively use the ONA pin and a 3volt zener in series with limiting resistor to translate the 5 volt level from the MCU to a 3 volt for the boost converter.

Regards

Sougata

koossa
- 5th September 2005, 14:42
Hi Sougata


Yes, I agree, that is absurd?

I think the MAX1672 maximum voltage to switch on ONB is 0.4v.
I think the 1n4148 would not take it that low, or would it?

I have changed the portd.1 to another pin to see if it is working.
Have tried portc.0 and porta.0, but it do exactly the same?

Attached is the source code

Thank you!!

Melanie
- 5th September 2005, 19:57
> I think the 1n4148 would not take it that low, or would it?

Have you actually tried it?

koossa
- 5th September 2005, 20:01
Hi Melanie

I have blown my Max Chip.
Will have to get some new ones tomorrow, will then try it.
Thx

sougata
- 6th September 2005, 11:36
Hi ,

How did you blow that chip ? Did it happen when you inserted the diode (1n4148) ? Or it just said goodbye. I think that so long we were concentrating on the software but consider this.

1. The PIC consumes much greater current when active (no in sleep a.k.a END)

2. MAX1672 is designed to deliver much more in order of a few hundred miliamps so no issues of overload even when active in the loop.

3. The boost converter itself was unstable and was following a shutdown due to internal protection circuitry.

4. Excessive ringing (bad pcb layout) can cause over dissipation of the chip following a thermal shutdown.


When you get the chip do the following :

Use a dedicated supply for the micro and use the logic to make the MAX switch on and stay then switch-off via code.

Use the ONB as a momentary input(via switch) and a port of the chip with high level to the ONA to keep it on. Use high value pull-up and pull-down resistors. (1M) Use a series resitor of 10K from the port to the MAX ONA input. Ground a green LED and tie its anode to the ONA input. This should keep the levels safe for the MAX (2.4 volt for a green LED).

Regards

Sougata

Acetronics2
- 6th September 2005, 12:34
Hi, Koosa

The reason why is written in the first lines of the max specs ...

Referring your design : Overvoltage on control pins ...

Alain

Melanie
- 6th September 2005, 12:45
You know this thread started off on 26 August here...

http://www.picbasic.co.uk/forum/showthread.php?t=2273

After all this time, are you telling me you haven't even tried the original solution?

Acetronics2
- 6th September 2005, 12:55
The design I looked at is this one : http://www.picbasic.co.uk/forum/attachment.php?attachmentid=467

The nearest ...

Alain

PS: Sorry, Mel ...a BAT85-like schottky diode must be used to reach the .4v max. low level spec ...

If I could ...."RTFS" ... hi, hi,hi ...

koossa
- 6th September 2005, 15:43
I have tried 2 different germanium diodes, but that does not work, so I went on to the alternative solution.

I wanted to connect the ONA to VDD, but accidentally connected the PGND to VDD and that blown up my chip.

Acetronics2
- 6th September 2005, 17:20
Looking further on the datasheet I got a good question ... how do your scheme reach any sleep ???
Regulator output is disconnected when reg.disabled ...so pic disabled too !!!

I suppose then regulator re-enabled ( enable input becomes high ...) so that's why your PIC port doesn't want to stay on the low state ...

Good luck with it ... seems you've re-engineered the blinking PIC ...

Alain

arniepj
- 6th September 2005, 18:03
Tie the input ONB to V+ and the input ONA to com both thru 1 meg resistors as in the data sheet, this will ensure the chip is off during power up.Tie portd.1 to the ONA input and remove the 2 k resistor between the input and output of the pic.Install regular pullup resistors for the keypad scanner,instead of using the portb weak pullups.Weak means very low current sourcing.
Using your keypad scanner,you know what bit represents the ON button,create a routine that stores the state of that bit and compares it to the input bit.Toggle that bit on then off each time the ON button is depressed,ensuring you are de-bouncing properly.Have your portd.1 output toggle on/off with this bit.Also on your hardware initialization,be sure to tris portb.I also noticed you set the portd.1 = 1 then a line later low switchpin setting portd.1 = 0.

koossa
- 6th September 2005, 18:06
Thank you, I will try that!!

koossa
- 7th September 2005, 15:08
Don't know what to do anymore?
Attached are my new circuit and my code.

I have now also connected the ONA pin of the MAX1672 to PortD.0 of the PIC16F877.
According to the datasheet if I put ONA HIGH my circuit should stay on, no matter what ONB’s value is.

It does work (Press ON button, Max1672 stays on) if I remove the highlighted piece of code in the attached screenshot.

As soon as I put the highlighted code back, it does not work.
When I press the ON button it turns on, but as soon as I release the ON button it turns off again?

PLEASE HELP ME!!

CocaColaKid
- 7th September 2005, 15:21
Why not post that part of the code then. There is obviously something in the code causing this issue.

Acetronics2
- 7th September 2005, 15:21
Koossa

Did you really read my last post ???????????????????????????????????????????

explanation there

Alain

You've just confirmed ...

koossa
- 7th September 2005, 15:59
Hi Alain

Sorry, I have thought you and Melanie's example was the same principal, so I have tried what she has suggest.

A couple of questions:
1) ONB is connected to V+ through a 1M resistor.
What is the reason it is not connected directly to V+?
2) "Install regular pullup resistors for the keypad"
Will 470R do?
3) Must I leave the rest of my circuit with the 10Ks and 2Ks as on the design?

Acetronics2
- 7th September 2005, 16:21
The solution is quite simple : first think your Pic must ALWAYS be powered ...if it must control one of it's outputs.
so, if your drive pin is floating ( sleep or off mode ) ... your MAX must be off ...

a solution could be to have a supply for the Pic (Through a diode ) direct from the battery.
You should then put your PIC in sleep mode when stopping the MAX supply and wake it by pushing a keyboard button ... have a look to PIC's spec to find the best way to awake it ( reset is the first, but may be a better way ...)

Alain

arniepj
- 7th September 2005, 17:25
Pull up pull down resistors are there for two reasons.First is to satisfy the inputs of the Max ic by driving them to the required state in which you want them to operate.ONA low,ONB high the Max is off on power up.The second reason is to protect the output of the pic.ONA must be driven high to change state,without the resistor you will be connecting high directly to low,V+ to com.Result is smoke ,silent death or if your lucky the 5v regulator will save you and get hot.
As far as your circuit goes,stop connecting i/o pins together,remove the diode,disconnect the portd.1 from the Max,leave ONB pulled up to V+.Leave ONA connected with the pull down to portd.0.Add 10k pull up resistors to all the keypad scanner i/o.In your code create a routine that tests the portd.0 output only.portd.0 =1,pause 1000,portd.0=0,pause 1000,loop endlessly.Check the Max to make sure its operating.The idea is match the truth table for the Max.ONA high,ONB high the Max on.ONA low,ONB high the Max off.Next make sure your keypad scanner only is working.Now that your hardware is working put on your thinking cap,capture the ON bit ,store it,compare it,toggle the output.Or use the ON bit to turn it on and another key to turn it off.This is where the fun is picbasic.

koossa
- 7th September 2005, 18:11
...In your code create a routine that tests the portd.0 output only.portd.0 =1,pause 1000,portd.0=0,pause 1000,loop endlessly.....
As far as I can see, my circuit and PIC would be switched off if I put portd.0 = 0 (ONA)?

Darrel Taylor
- 7th September 2005, 18:33
Koosa,

I see in the last schematic that you have ground tied directly to RB3. This is going to be very bad once you start trying to scan the keypad again. But for now, with the simple turn on test it will be OK.

If you happen to have a couple minutes, could you try loading this HEX file into your project? Just want to test something.

koossa
- 7th September 2005, 18:43
Hi Darrel

I would really appreciate any help.
How do I put it on my PIC.
Must I keep my circuit the same?

Darrel Taylor
- 7th September 2005, 18:53
Yes, Just like you showed it in the last schematic, with the diode and both ONA and ONB connected. You can fix the RB3 thing later.
<br>

koossa
- 7th September 2005, 19:17
Hi Darrel

I have import the Hex file into my project and program the pic.
But it is still the same, when I press the ON button it turn on, but if I release it, it switch off again.

Darrel Taylor
- 7th September 2005, 19:33
OK, now this is just getting cRaZy. I can't stand it anymore!!!

Do you have a schematic of the way you have the MAX1672 set up?

I'm going to duplicate your circuit and see if I can make it do the same thing. This is just too weird.
<br>

koossa
- 7th September 2005, 19:50
I will qiuckly draw one, thanks!!

koossa
- 8th September 2005, 07:22
Hi Darrel

Attached is my complete circuit as put out on my breadboard.

koossa
- 8th September 2005, 14:03
I have put a LED connected to PortC.3 whith the athached code and my pic do start up, becuase the LED is flickering, but when I release the ON button it stops.

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=476&stc=1" border="1">

koossa
- 8th September 2005, 14:33
I think I'm starting to get some progress.
If I write the code, as below, my ONA does not turn on when measured with a multi-meter.
But if I put a LED over PORTD.0 (ONA) AND GND and I press the on button, it flickers.
So with the loop ONA does not stay high, but flickers
Without the loop ONA does stay high?

<img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=477&stc=1" border=1">

koossa
- 8th September 2005, 16:54
I Think I have found my problem.
The plates in the breadboard creates a capacitance on my circuit, that is why the led was flickering.
I have build a pcb and now it looks like it is working.

mister_e
- 8th September 2005, 17:05
That happen often even if you have a brand new or older breadboard.

Out of curiosity, what about if you remove those capacitor around your crystal on your BreadBoard???

Darrel Taylor
- 8th September 2005, 18:04
Great! Thanks for the schematic. Looks like that took awhile to draw.

I should have the circuit built up later today. Just crawled out of bed. Need Coffee...

I'll get back to you later.
<br>

Darrel Taylor
- 8th September 2005, 18:10
After posting that last one. I saw that you have it working.

Not what I wanted to see.
<br>

koossa
- 8th September 2005, 20:28
Hi Darrel

Thank you very much for your help.

You said that it's not the right way to connect the one pin of the pic directly to ground, what is the correct way of doing this.

Darrel Taylor
- 8th September 2005, 20:49
The problem arises when you try to scan the keypad.

Each ROW is scanned by placing a LOW signal on it, and testing the upper bits of PORTB for the Column results. &nbsp;But, if 1 of the rows ALWAYS has a LOW signal on it, there's no way to distinguish which row had the button press.

So, your stuck between a Rock and a Hard place.

Don't have that "correct way of doing this".
<br>

arniepj
- 9th September 2005, 02:20
Read the attached about keypad scanning.

koossa
- 9th September 2005, 08:51
Thank you arniepj, I will read it!