Log in

View Full Version : 16F887 - PORT pins interfere with each other when set high/low



CuriousOne
- 20th April 2020, 21:28
Hello.

Run into strange problem. I have button connected to PORTA.0 (pulled up to 5v via 10k resistor) and buzzer to PORTB.7 Below is the shortened code.



OSCCON=%01110101 'SET INTOSC TO 8MHZ
ANSEL=%00000000
ANSELH=%00000000 'disable ADC


TRISC=%00000000 'set PORTC as output all
TRISD=%00000000 'set PORTD as output all
TRISB=%00000000 'set PORTB as output all
TRISA=%00000001 'set PORTA 0 as input, others as output
TRISE=%00000000 'same here
BUZ var PORTB.7
BUTN var PORTA.0



DEFINE OSC 8 'OSC SPEED
'turn off all digits


PORTA=0: PORTB=0: PORTC=0: PORTD=0 : PORTE=0 'turn off all outputs

CYCLER:
if BUTN=0 then
high BUZ
pause 100
low BUZ
pause 100
endif
goto CYCLER


A simple code, when you press the button and keep it pressed, buzzer should rapidly beep. But it does not exactly works. It makes 5 normal length beeps and then 5 silent - When looked with scope, feels like first PAUSE 100 statement is being skipped - only a small impulse appears on scope on buzzer pin. After these 5 silent (short) pulses, there will come trail of 5 normal length pulses and so on. The first thoughts might be that button is faulty. But I've removed it, soldered porta.0 pin directly to GND - no change. If I remove reading of BUTN, and just leave high buz pause 100 low buz pause 100 code, it all works just fine. So it might appear that PORTA is doing something. So I changed code to the following



CYCLER:
IF BUTN=0 then
high PORTB.7
TRISB.7=1
pause 100
TRISB.7=0
pause 100
goto CYCLER:


It works fine.

It may appear that problem is solved. But wait, fun just begins. Let's slightly modify the code and add flashing led, connected to say PORTB.4



CYCLER:
IF BUTN=0 then
high PORTB.7
TRISB.7=1
TRISB.4=1
pause 100
TRISB.7=0
TRISB.4=0
pause 100
goto CYCLER:


We run into same problem. Led flashes as it should, but buzzer is outputting 5 long and 5 short pulses in the loop. If I remove "IF BUTN=0" then buzzer works again fine.

I thought that this might be Pickit 3 interfering with ports and tried to unplug it - nothing helps.

Any ideas?

Ioannis
- 20th April 2020, 21:38
I don't understand why you make the portb.7 input (since it should be an output to buzzer), wait 100ms and then output again.

Ioannis

CuriousOne
- 20th April 2020, 21:51
As far as I understand PBP manual and in practice before, HIGH PORTX.Y makes that pin high and LOW PORTX.Y makes that pin low.

Ioannis
- 20th April 2020, 22:24
TRISB.7=1 makes portb.7 an input

TRISB4.=1 makes portb.4 an input

TRISB.7=0 makes portb.7 an output

TRISB4.=0 makes portb.4 an output

Is this really what you want?

Ioannis

CuriousOne
- 21st April 2020, 07:23
No. I want to make PORTB.7 output logical "1" and then logical "0". PBP statements like HIGH PORTB.7 work, but only if I do not read PORTA.0 as input.

Ioannis
- 21st April 2020, 10:51
So, why in your loop, you use TRIS to make the ports input then output? You should use High Portb.7 or Low Portb.7 and not TRIS. TRIS is only to define the input or output operation.

Ioannis

CuriousOne
- 21st April 2020, 10:58
If you check my post, initially I was using HIGH/LOW. But it does not work. TRIS works, but not completely.

Ioannis
- 21st April 2020, 11:19
You insist on TRIS. The purpose of TRIS is completely different than what you trying to do...

You set once on the top of your program what every port will be (input or output) with the TRIS command and then you do not touch it again. Except if you want to change a port from output to input intentionally.

If you want to control the state of an output port you only use High and Low of that specific port.

Ioannis

CuriousOne
- 21st April 2020, 14:06
I absolutely do not care about what statement it will be.
I'm asking why it does not works, whenever it is HIGH/LOW or TRIS.

Ioannis
- 21st April 2020, 14:55
OK. Lets stick on your first code that looks OK to me.

What is connected to your buzzer output? Can you connect only a LED with a resistor for testing purposes?

And I would also consider having decoupling capacitors the power rails, as close as possible to the PIC power pins.

Maybe you have an interference of the buzzer if it is electromagnetic?

Ioannis

Dave
- 21st April 2020, 15:17
Hey CuriousOne, Where is the ENDIF to finish the "if BUTN=0 then" statement?

Ioannis
- 21st April 2020, 15:50
Assuming we refer to the first program, the ENDIF it there at the end.

On the snippets it is missing, but I doubt that this can compile anyway.

Ioannis

mpgmike
- 21st April 2020, 20:09
I don't use this particular Model T PIC, so I reserve the right to miss some quirks. Here are things I would check:

1. You don't list your CONFIGs, but try disabling the WDT. It could be resetting on you.
2. Try enabling CLKOUT and verify oscillator. On one project I did years ago, I had the crystal too far from the OSC pins and it would work for a few seconds then Peter out.
3. Not sure which PORT PIN is MCLR, but you only have one Input in your TRISx Registers. MCLR should be a "1" (even though it probably defaults to Input Only). Don't forget the pull-up resistor.
4. Make sure there are no other default settings triggering things unexpectedly (like Comparator or something).
5. I think you already tried removing the PICkit3 programmer before trying the code. Using PORTB.7 probably ties into a PICkit3 programming pin.

Some food for thought, things to try. Hope it helps.

CuriousOne
- 22nd April 2020, 07:06
#CONFIG
cfg1 = _INTRC_OSC_NOCLKOUT ; INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
cfg1&= _WDT_ON ; WDT enabled
cfg1&= _PWRTE_OFF ; PWRT disabled
cfg1&= _MCLRE_OFF ; RE3/MCLR pin function is digital input, MCLR internally tied to VDD
cfg1&= _CP_OFF ; Program memory code protection is disabled
cfg1&= _CPD_OFF ; Data memory code protection is disabled
cfg1&= _BOR_OFF ; BOR disabled
cfg1&= _IESO_ON ; Internal/External Switchover mode is enabled
cfg1&= _FCMEN_ON ; Fail-Safe Clock Monitor is enabled
cfg1&= _LVP_OFF ; RB3 pin has digital I/O, HV on MCLR must be used for programming
cfg1&= _DEBUG_OFF ; In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
__CONFIG _CONFIG1, cfg1


cfg2 = _BOR40V ; Brown-out Reset set to 4.0V
cfg2&= _WRT_OFF ; Write protection off
__CONFIG _CONFIG2, cfg2

CuriousOne
- 22nd April 2020, 07:13
Ok here are answers.

1. PORTB.7 is connected via 10K resistor to MPSA42 transistor, which drives buzzer.
2. I tried replacing it with led, different buzzer, no difference. Sometimes it works, sometimes it does not.
3. I've used another PIC16F887 - no change.
4. Of course I've tried disconnecting pickit 3 - no difference.
5. This problem ONLY occurs with PORTB.7 as output and PORTA.0 as input. Say if I don't query PORTA.0 for input and just make PORTB.7 high/low from the code, it works fine.
6. All remaining ports (except PORTA 1 & 2 which are connected to DS3231) are connected via 10K resistors to MPSA42 transistors, which drive nixie tube cathodes and work fine.
7. MCLR, WDT and others are disabled, as you can see from above config.

Ioannis
- 22nd April 2020, 09:04
WDT is ON on your code but I doubt that this is causing troubles as the compiler takes care of this.

MCLR: is it tied to Vss or Vdd?

Ioannis

CuriousOne
- 22nd April 2020, 12:08
MCLR is disabled and used as button input - tied to VDD via 10K resistor and button pulls it down to VSS. It works fine.

mpgmike
- 22nd April 2020, 16:13
Hmmm, if you are using a 10k pull-up to VDD and a button to ground, do you have an additional 10k resistor between the button and PORTA.0? If so you have a voltage divider possibility where the Input thresholds occasionally do not exceed min/max, thus the sporadic results. (again, stabbing at unknowns)

Try using 4.7k resistors instead of 10k resistors for isolation. I had a project where I used 10k for MCLR. It was used in a noisy environment where it would occasionally RESET. I replaced the 10k with a 4.7k and the unintended RESET stopped.

Ioannis
- 22nd April 2020, 20:49
Today I checked your code. Changed IESO in the configs and a bit in OSCCON which was read only.

Works just fine. Only thing is that LED on portb.7 is interfering with the programming and causing programming errors!

Besides that everything works just fine on the F887 chip at 8MHz.



#CONFIG
cfg1 = _INTRC_OSC_NOCLKOUT ; INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN
cfg1&= _WDT_ON ; WDT enabled
cfg1&= _PWRTE_OFF ; PWRT disabled
cfg1&= _MCLRE_OFF ; RE3/MCLR pin function is digital input, MCLR internally tied to VDD
cfg1&= _CP_OFF ; Program memory code protection is disabled
cfg1&= _CPD_OFF ; Data memory code protection is disabled
cfg1&= _BOR_OFF ; BOR disabled
cfg1&= _IESO_OFF ; Internal/External Switchover mode is enabled
cfg1&= _FCMEN_ON ; Fail-Safe Clock Monitor is enabled
cfg1&= _LVP_OFF ; RB3 pin has digital I/O, HV on MCLR must be used for programming
cfg1&= _DEBUG_OFF ; In-Circuit Debugger disabled, RB6/ICSPCLK and RB7/ICSPDAT are general purpose I/O pins
__CONFIG _CONFIG1, cfg1


cfg2 = _BOR40V ; Brown-out Reset set to 4.0V
cfg2&= _WRT_OFF ; Write protection off
__CONFIG _CONFIG2, cfg2
#endconfig

OSCCON=%01110001 'SET INTOSC TO 8MHZ

ANSEL = %00000000
ANSELH = %00000000 'disable ADC


TRISC=%00000000 'set PORTC as output all
TRISD=%00000000 'set PORTD as output all
TRISB=%00000000 'set PORTB as output all
TRISA=%00000001 'set PORTA 0 as input, others as output
TRISE=%00000000 'same here
BUZ var PORTB.7
BUTN var PORTA.0



DEFINE OSC 8 'OSC SPEED
'turn off all digits


PORTA=0: PORTB=0: PORTC=0: PORTD=0 : PORTE=0 'turn off all outputs

CYCLER:
if BUTN=0 then
high BUZ
pause 100
low BUZ
pause 100
endif
goto CYCLER

end


I did a short video that shows a working state of the circuit on a PICdem2 Plus board. The PortA.0 is shorted by the yellow wire to ground.

https://youtu.be/pfZLbC3T3bA

Ioannis

CuriousOne
- 23rd April 2020, 07:03
Thanks, will check your code on my hardware today later....

CuriousOne
- 22nd June 2020, 07:28
Found another curiosity. There are two ways of setting PORTA.0 as ADC input on 887.

ANSEL=%00000001
ANSELH=%00000001

Both work equally. However, if ANSELH=%00000001 and ADCin statement is used (if not used, no problems), PORTB.2 becomes high :D

Figured it out by trial and error :)

Ioannis
- 22nd June 2020, 09:49
Obviously you missed that the default is all inputs set to ANALOG IN.

So if you do not do anything all ports with ADC support are, by default, on Power On set analog inputs!

Even if you don't use ANSELx.

But if you use BOTH ANSEL and ANSELH to set the digital and analog inputs, it is not possible to have analog in on PortB.0 using this:

ANSEL=%00000001
ANSEL=%00000000

PortB.0 will be Digital, either in or out defined by TRIS.

Ioannis

CuriousOne
- 22nd June 2020, 15:45
I have all TRIS and other settings set :)

Ioannis
- 22nd June 2020, 16:06
Not enough. If you want it as digital in then you have to reset the appropriate bits in ANSEL/ANSELH.

Ioannis