View Full Version : I/O Pin status during startup
PaulMaker
- 16th January 2025, 12:20
Hi guys,
quick question...
Is there a way (somewhere on the datasheet for example) to know what are the I/O pins state during power up?
I've noticed that some I/O pins (on my 18F2580 MCU) start high even placing "clear" and "PORTA=000000, PORTB=000000, PORTC=000000) at the start of the code (when i say start, i mean after all the configuration lines).
I currently have the "INCLUDE "ALLDIGITAL.pbp"" before the actual code start.
How can I force all the I/Os to be low until the code tells them to go high?
Thanks
Acetronics2
- 16th January 2025, 13:20
no defined I/Os state before you set it ... :rolleyes::rolleyes::rolleyes:
the one and only way is an external pull up or down resistor, as I/Os are placed in an INPUT state ( High impedance ) @ power up ...
moreover, you have to manually set input state before some PBP Commands ( pulsin, RC Time, Toggle ... ) to be sure of what you get.
Alain
PaulMaker
- 16th January 2025, 16:09
Hi Acetronics2,
thanks for your reply.
To be accurate, during power on, these I/Os turn on and off (and then stay off) very quickly (like a flash). The idea is to stop that high-low behavior.
Jerson
- 17th January 2025, 01:57
Hi
To be accurate, during power on, these I/Os turn on and off (and then stay off) very quickly (like a flash). The idea is to stop that high-low behavior.
Simple answer- you cannot do that.
You should design your project with active low outputs to account for the High-Z input state at power on. Almost every microcontroller, I know, behaves similarly unless there is special hardware built in to retain a certain state at power on. Till such time as your program code runs, the microcontroller IO pins are kept in a High-Z state to prevent unintentional damage to itself.
PaulMaker
- 17th January 2025, 08:58
Greetings,
thank you Jerson for your reply.
Regarding "design your project with active low outputs to account for the High-Z input state at power on", what do you mean exactly?
If, for example, I only have LEDs on these I/Os, what can I do?
richard
- 17th January 2025, 10:55
if you have leds that illuminate on power up before the pins have been set as outputs then you are doing something very odd
or you have broken something
schematic and code ?
HenrikOlsson
- 17th January 2025, 16:04
Yeah, this sounds very strange.
As others have said, after power on and reset (POR) all the pins come up as inputs so they should not go either high or low for any duration. Leakage current into the pin is MUCH less than a uA (200nA max) so basically "any" sort of resistance (ie several mega Ohms) to either Vdd or Vss will pull the pin to that.
And it doesn't really matter what you do with the PORT or LAT registers until you also clears the TRIS bits in questions. Ie, at power up the pins are inputs (high impedence), then you "preset" their default state(s) by writing to PORT or LAT and finally you clear the corresponding TRIS bits to enable the output drivers.
The 18F2580 have pull-ups on PORTB (and PORTB only it seems). They are disabled on POR so they should not be able to pull the pin high. The current sourced thru the pull-up resistor is between 50 and 400uA so even WITH them enabled I don't think you'd see a LED light up.
More details please...
Jerson
- 18th January 2025, 03:22
Greetings,
thank you Jerson for your reply.
Regarding "design your project with active low outputs to account for the High-Z input state at power on", what do you mean exactly?
If, for example, I only have LEDs on these I/Os, what can I do?
Many a times, I have used an I/O pin to drive a relay through a transistor. So, assuming I need an active high output to turn on the relay, I have observed (across microcontroller families) that the relay flicks on for the fraction of a second till the program code takes control and the pin get set to the off - state of the relay (0). So, the ideal way to resolve such a situation is to ensure that the relay turns on only when the I/O pin is made to output a LOW.
In case of driving an LED from an IO pin, my thinking is biased with the TTL logic output specificiation where the sinking current limit is larger than the sourcing current limit. So, I usually tend to use LEDS with an active low output configuration. The IO port has to be low for the LED to turn on. This spec thing is no longer true with the current generation of microcontrollers except the 8051 family dies from old.
So, as a best practice, I tend to use active low outputs for any device that needs to be turned on.
Acetronics2
- 18th January 2025, 07:27
I Never saw something like that ....
but, if we consider LEDs as current generators when illuminated ;) ( Yes, the built in input clamp diodes ... )
the question comes ... is the pic in a true OFF state @ "powerup" ???
THE ONE and ONLY thing to have here is ...
SHOW US the full Pic program ( with config ! ) and the Circuit real scheme.
already asked for @ #6 !!!
Alain
PaulMaker
- 21st January 2025, 09:05
Greetings all,
thank you all for your inputs.
Here is the code:
'************************************************* ***************'* Name : Led Controller.PBP *
'* Author : Paul Maker *
'* Notice : Copyright (c) 2024 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 19/12/2024 *
'* Version : 1.0 *
'* Notes : 18F2580 *
'* : *
'************************************************* ***************
'
'
'
;----[18F2580 Hardware Configuration]-------------------------------------------
#IF __PROCESSOR__ = "18F2580"
#DEFINE MCU_FOUND 1
#CONFIG
CONFIG OSC = IRCIO67 ; Internal oscillator block, port function on RA6 and RA7
CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor disabled
CONFIG IESO = OFF ; Oscillator Switchover mode disabled
CONFIG PWRT = OFF ; PWRT disabled
CONFIG BOREN = OFF ; Brown-out Reset disabled in hardware and software
CONFIG BORV = 3 ; VBOR set to 2.1V
CONFIG WDT = OFF ; WDT disabled (control is placed on the SWDTEN bit)
CONFIG WDTPS = 512 ; 1:512
CONFIG PBADEN = OFF ; PORTB<4:0> pins are configured as digital I/O on Reset
CONFIG LPT1OSC = OFF ; Timer1 configured for higher power operation
CONFIG MCLRE = OFF ; RE3 input pin enabled; MCLR disabled
CONFIG STVREN = ON ; Stack full/underflow will cause Reset
CONFIG LVP = OFF ; Single-Supply ICSP disabled
CONFIG BBSIZ = 1024 ; 1K words (2K bytes) boot block
CONFIG XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
CONFIG DEBUG = OFF ; Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
CONFIG CP0 = OFF ; Block 0 (000800-001FFFh) not code-protected
CONFIG CP1 = OFF ; Block 1 (002000-003FFFh) not code-protected
CONFIG CP2 = OFF ; Block 2 (004000-005FFFh) not code-protected
CONFIG CP3 = OFF ; Block 3 (006000-007FFFh) not code-protected
CONFIG CPB = OFF ; Boot block (000000-0007FFh) not code-protected
CONFIG CPD = OFF ; Data EEPROM not code-protected
CONFIG WRT0 = OFF ; Block 0 (000800-001FFFh) not write-protected
CONFIG WRT1 = OFF ; Block 1 (002000-003FFFh) not write-protected
CONFIG WRT2 = OFF ; Block 2 (004000-005FFFh) not write-protected
CONFIG WRT3 = OFF ; Block 3 (006000-007FFFh) not write-protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
CONFIG WRTB = OFF ; Boot block (000000-0007FFh) not write-protected
CONFIG WRTD = OFF ; Data EEPROM not write-protected
CONFIG EBTR0 = OFF ; Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
CONFIG EBTRB = OFF ; Boot block (000000-0007FFh) not protected from table reads executed in other blocks
#ENDCONFIG
#ENDIF
;----[Verify Configs have been specified for Selected Processor]----------------
; Note: Only include this routine once, after all #CONFIG blocks
#IFNDEF MCU_FOUND
#ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
#ENDIF
'************************************************* ****************************
OSCCON =%01110000
OSCTUNE=%11000000
DEFINE OSC 8
'************************************************* ****************************
INCLUDE "ALLDIGITAL.pbp"
DEFINE SHOWDIGITAL 1
'************************************************* ****************************
INCLUDE "modedefs.bas"
'DEFINE DEBUGIN_REG PORTB
'DEFINE DEBUGIN_BIT 5
'DEFINE DEBUGIN_BAUD 1200
'DEFINE DEBUGIN_MODE 0
'************************************************* ****************************
TRISA = %00000000
TRISB = %00100000
TRISC = %00000000
'************************************************* ****************************
'PORTS & PINS
LED1 var PORTC.1 'IC5
LED2 var PORTC.2 'IC5
LED3 var PORTC.0 'IC5
LED4 var PORTA.6 'IC5
LED5 var PORTB.4 'IC5
LED6 var PORTA.7 'IC5
LED7 var PORTB.0 'IC5
LED8 var PORTB.1 'IC5
LED9 var PORTB.2 'IC5
LED10 var PORTB.3 'IC5
LED11 var PORTC.3 'IC5
LED12 var PORTC.4 'IC5
LED13 var PORTC.5 'IC5
LED14 var PORTC.6 'IC5
LED15 var PORTC.7 'IC5
LED16 var PORTA.5 'IC5
LED17 var PORTA.4 'IC5
LED18 var PORTA.3 'IC5
LED19 var PORTA.2 'IC5
LED20 var PORTA.1 'IC5
LED21 var PORTA.0 'IC5
LED22 var PORTB.7 'IC5
LED23 var PORTB.6 'IC5
IC_RX var PORTB.5 'IC5 INPUT
CT VAR BYTE
CT VAR BYTE
PA1 VAR WORD
PA2 VAR WORD
PA3 VAR WORD
PA4 VAR WORD
'************************************************* ****************************
INIT:
clear
pause 100
PORTA=%00000000
PORTB=%00000000
PORTC=%00000000
TRISB.5=1
PA1=300
PA2=600
PA3=900
PA4=1500
CT=0
CT2=0
pause 1000
'-----------------------------------------------------------------
START:
pause 400
high LED3
high LED4
pause 1600
high LED10
high LED15
pause PA4
high LED1
high LED2
high LED6
HIGH LED16
PAUSE 600
PAUSE PA4
GOTO ANIM
'-----------------------------------------------------------------
ANIM:
high LED9
high LED14
PAUSE PA3
high LED8
high LED13
PAUSE PA3
LOW LED13
PAUSE PA3
HIGH LED7
PAUSE PA3
LOW LED7
HIGH LED13
PAUSE PA3
PAUSE PA3
PAUSE PA3
LOW LED8
PAUSE PA3
LOW LED13
PAUSE PA3
PAUSE PA3
PAUSE PA3
HIGH LED8
HIGH LED13
Goto ANIM
Regarding the schematic, I don't have one at the moment but basically it's just the MCU connected to 5V (with 1uF capacitor between VDD and GND pins) and several LEDs connected to each pin (one LED on each pin) in series with a resistor.
richard
- 21st January 2025, 10:28
are the leds connected to gnd or vdd ?
are the leds powered by the same supply?
what size is the led series resistor ?
and what does actually happen compared what you expect to happen
note a schematic can be a picture of a simple hand drawn pencil/paper sketch , or a doodle from paint brush
it does not need to be fancy just readable and complete
HenrikOlsson
- 21st January 2025, 11:16
So, looking at your code....effectively it boils down to
TRISA = %00000000 ' This enables 23 of 24 output drivers, pins goes to whatever state PORT/LAT says
TRISB = %00100000
TRISC = %00000000
clear
pause 100 ' Twiddle thumbs for 100ms
PORTA=%00000000 ' And THEN the pins are set to 0
PORTB=%00000000
PORTC=%00000000
[/code]
You need to do it the other way around.
PaulMaker
- 21st January 2025, 11:17
Hi Richard, thanks for the help.
Regarding the drawing...will try to get it and post here.
In the meantime here are the answers:
are the leds connected to gnd or vdd ?
The LEDs are connected to ground
are the leds powered by the same supply?
Yes, same power supply as the MCU
what size is the led series resistor ?
It's 500ohms for a SMD LED
and what does actually happen compared what you expect to happen
The same moment that i turn the power on, the LEDs turn on and then off very fast.
After some seconds (when the code kicks in) the LED animation starts.
I expect the LEDs not flash when turning the power on and only turn on when the code kicks in.
richard
- 21st January 2025, 12:07
before you go to too much trouble try what henrik suggests
clear all output ports before setting the tris registers and lose that delay in between
Acetronics2
- 21st January 2025, 14:55
did you simply try to add :
INIT:
PORTA=%00000000
PORTB=%00000000
PORTC=%00000000
clear
pause 100
TRISB.5=1
PA1=300
PA2=600
PA3=900
PA4=1500
in the config section ...
tumbleweed
- 21st January 2025, 19:01
did you simply try to add ...
The important part is to set the PORT (or LAT) registers BEFORE setting the TRIS registers to outputs.
Otherwise the pins will be in output mode for a period of time but the outputs aren't specified.
Acetronics2
- 22nd January 2025, 08:12
Hi, Tumbleweed
the funny thing is I had added the port setting lines right before the TRIS statements for a run With MPLAB 8.92 sim ...
'************************************************* ***************'* Name : Led Controller.PBP *
'* Author : Paul Maker *
'* Notice : Copyright (c) 2024 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 19/12/2024 *
'* Version : 1.0 *
'* Notes : 18F2580 *
'* : *
'************************************************* ***************
'
'
'
;----[18F2580 Hardware Configuration]-------------------------------------------
#IF __PROCESSOR__ = "18F2580"
#DEFINE MCU_FOUND 1
#CONFIG
CONFIG OSC = IRCIO67 ; Internal oscillator block, port function on RA6 and RA7
CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor disabled
CONFIG IESO = OFF ; Oscillator Switchover mode disabled
CONFIG PWRT = OFF ; PWRT disabled
CONFIG BOREN = OFF ; Brown-out Reset disabled in hardware and software
CONFIG BORV = 3 ; VBOR set to 2.1V
CONFIG WDT = ON ; WDT disabled (control is placed on the SWDTEN bit)
CONFIG WDTPS = 512 ; 1:512
CONFIG PBADEN = OFF ; PORTB<4:0> pins are configured as digital I/O on Reset
CONFIG LPT1OSC = OFF ; Timer1 configured for higher power operation
CONFIG MCLRE = OFF ; RE3 input pin enabled; MCLR disabled
CONFIG STVREN = ON ; Stack full/underflow will cause Reset
CONFIG LVP = OFF ; Single-Supply ICSP disabled
CONFIG BBSIZ = 1024 ; 1K words (2K bytes) boot block
CONFIG XINST = OFF ; Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
CONFIG DEBUG = OFF ; Background debugger disabled, RB6 and RB7 configured as general purpose I/O pins
CONFIG CP0 = OFF ; Block 0 (000800-001FFFh) not code-protected
CONFIG CP1 = OFF ; Block 1 (002000-003FFFh) not code-protected
CONFIG CP2 = OFF ; Block 2 (004000-005FFFh) not code-protected
CONFIG CP3 = OFF ; Block 3 (006000-007FFFh) not code-protected
CONFIG CPB = OFF ; Boot block (000000-0007FFh) not code-protected
CONFIG CPD = OFF ; Data EEPROM not code-protected
CONFIG WRT0 = OFF ; Block 0 (000800-001FFFh) not write-protected
CONFIG WRT1 = OFF ; Block 1 (002000-003FFFh) not write-protected
CONFIG WRT2 = OFF ; Block 2 (004000-005FFFh) not write-protected
CONFIG WRT3 = OFF ; Block 3 (006000-007FFFh) not write-protected
CONFIG WRTC = OFF ; Configuration registers (300000-3000FFh) not write-protected
CONFIG WRTB = OFF ; Boot block (000000-0007FFh) not write-protected
CONFIG WRTD = OFF ; Data EEPROM not write-protected
CONFIG EBTR0 = OFF ; Block 0 (000800-001FFFh) not protected from table reads executed in other blocks
CONFIG EBTR1 = OFF ; Block 1 (002000-003FFFh) not protected from table reads executed in other blocks
CONFIG EBTR2 = OFF ; Block 2 (004000-005FFFh) not protected from table reads executed in other blocks
CONFIG EBTR3 = OFF ; Block 3 (006000-007FFFh) not protected from table reads executed in other blocks
CONFIG EBTRB = OFF ; Boot block (000000-0007FFh) not protected from table reads executed in other blocks
#ENDCONFIG
#ENDIF
;----[Verify Configs have been specified for Selected Processor]----------------
; Note: Only include this routine once, after all #CONFIG blocks
#IFNDEF MCU_FOUND
#ERROR "No CONFIGs found for [" + __PROCESSOR__ +"]"
#ENDIF
'************************************************* ****************************
OSCCON =110000
OSCTUNE=000000
DEFINE OSC 8
'************************************************* ****************************
INCLUDE "ALLDIGITAL.pbp"
DEFINE SHOWDIGITAL 1
'************************************************* ****************************
INCLUDE "modedefs.bas"
'DEFINE DEBUGIN_REG PORTB
'DEFINE DEBUGIN_BIT 5
'DEFINE DEBUGIN_BAUD 1200
'DEFINE DEBUGIN_MODE 0
'************************************************* ****************************
PORTA = 0
PORTB = 0
PORTC = 0
TRISA = 000000
TRISB = 100000
TRISC = 000000
'************************************************* ****************************
'PORTS & PINS
LED1 var PORTC.1 'IC5
LED2 var PORTC.2 'IC5
LED3 var PORTC.0 'IC5
LED4 var PORTA.6 'IC5
LED5 var PORTB.4 'IC5
LED6 var PORTA.7 'IC5
LED7 var PORTB.0 'IC5
LED8 var PORTB.1 'IC5
LED9 var PORTB.2 'IC5
LED10 var PORTB.3 'IC5
LED11 var PORTC.3 'IC5
LED12 var PORTC.4 'IC5
LED13 var PORTC.5 'IC5
LED14 var PORTC.6 'IC5
LED15 var PORTC.7 'IC5
LED16 var PORTA.5 'IC5
LED17 var PORTA.4 'IC5
LED18 var PORTA.3 'IC5
LED19 var PORTA.2 'IC5
LED20 var PORTA.1 'IC5
LED21 var PORTA.0 'IC5
LED22 var PORTB.7 'IC5
LED23 var PORTB.6 'IC5
IC_RX var PORTB.5 'IC5 INPUT
CT VAR BYTE
CT2 VAR BYTE
PA1 VAR WORD
PA2 VAR WORD
PA3 VAR WORD
PA4 VAR WORD
clear
'************************************************* ****************************
INIT: ' setting the Variables ... )
PA1=300
PA2=600
PA3=900
PA4=1500
pause 1000
'-----------------------------------------------------------------
START:
high LED3
high LED4
pause 1600
high LED10
high LED15
pause PA4
high LED1
high LED2
high LED6
HIGH LED16
PAUSE 600
PAUSE PA4
'-----------------------------------------------------------------
WHILE 1
high LED9
high LED14
PAUSE PA3
high LED8
high LED13
PAUSE PA3
LOW LED13
PAUSE PA3
HIGH LED7
PAUSE PA3
LOW LED7
HIGH LED13
PAUSE PA3
PAUSE PA3
PAUSE PA3
LOW LED8
PAUSE PA3
LOW LED13
PAUSE PA3
PAUSE PA3
PAUSE PA3
HIGH LED8
HIGH LED13
WEND
With some "cleaning" ( wiped double definitions and "enlighted" the never-ending loop )
MPLAB 8.92 ( the last one ! ) is rather useful for those simple programs debugging using checking breakpoints as it is still working fine with W11 ...
PaulMaker
- 22nd January 2025, 08:21
Greetings all,
first of all, thank you so much for all your help.
I tried moving the port settings before the Tris statement like you recommended and it worked!
PORTA=%00000000PORTB=%00000000
PORTC=%00000000
TRISA = %00000000
TRISB = %00100000
TRISC = %00000000
With this change, the LEDs don't flash anymore during startup.
You guys nailed it!
Thanks
Acetronics2
- 23rd January 2025, 08:11
Great !
just remember the processor does what you tell it ... and ONLY what you tell it ! ( thanks to God, PBP doesn't show too many bugs :wink: )
so, it's always you to set ALL the parameters, registers, variables ... etc, etc ... and overall take the greatest care of the default settings !
READ THAT F... MANUAL ( 9904 )as we were used to write here. ) as a punishment, read carefully manual $ 8.1 ...
Alain
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.