News:

;) This forum is the property of Proton software developers

Main Menu

Setting for PORTD.3 Input on 18F47K40

Started by Dave-S, Feb 20, 2025, 11:32 AM

Previous topic - Next topic

Dave-S

Trying to set PORTD.3 to input on a 18F47K40.
There is no voltage measured with volt meter on the pin have tryed setting pull down and also connected 10K to negative but I still get value 1 in the code,
SerOut PORTD.1, 84, ["portd.3 = ", Dec PORTD.3, 13], shows "portd.3 = 1"

So where am I going wrong?
Thanks David

Device = 18F47K40
Declare Xtal = 64 

 TRISD.3 = 1
 WPUD.3 = 1
 INLVLD.3 = 0
 PORTD = %00001000
 RD3PPS = %00011011
 
 
 DelayMS 2500
 SerOut PORTD.1, 84, ["boot", 13]       
  '***************     MAIN    *****************************************************************
DelayMS 50
Main:
SerOut PORTD.1, 84, ["Main", 13]
 
       SerOut PORTD.1, 84, ["portd.3 = ", Dec PORTD.3, 13]
       DelayMS 8000
GoTo Main

'-------------------------------------------------------------------------------
'**** Added by Fuse Configurator ****
' Use the Fuses Tab to change these settings

Config_Start
  FEXTOSC = OFF               ;HS (crystal oscillator) above 8 MHz; PFM set to high powe
  RSTOSC = HFINTOSC_64MHZ    ;HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:
  CLKOUTEN = OFF             ;CLKOUT function is disable
  CSWEN = On                 ;Writing to NOSC and NDIV is allowe
  FCMEN = On                 ;Fail-Safe Clock Monitor enable
  MCLRE = INTMCLR            ;If LVP = 0, MCLR pin function is port defined function; If LVP =1, RE3 pin fuction is MCL
  PWRTE = OFF                ;Power up timer disable
  LPBOREN = OFF              ;ULPBOR disable
  BOREN = SBORDIS            ;Brown-out Reset enabled , SBOREN bit is ignore
  BORV = VBOR_2P45           ;Brown-out Reset Voltage (VBOR) set to 2.45
  ZCD = OFF                  ;ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCO
  PPS1WAY = OFF               ;PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycl
  STVREN = On                ;Stack full/underflow will cause Rese
  Debug = OFF                ;Background debugger disable
  XINST = OFF                ;Extended Instruction Set and Indexed Addressing Mode disable
  WDTCPS = WDTCPS_31         ;Divider ratio 1:65536; software control of WDTP
  WDTE = OFF                 ;WDT Disable
  WDTCWS = WDTCWS_7          ;window always open (100%); software control; keyed access not require
  WDTCCS = SC                ;Software Contro
  WRT0 = OFF                 ;Block 0 (000800-003FFFh) not write-protecte
  WRT1 = OFF                 ;Block 1 (004000-007FFFh) not write-protecte
  WRT2 = OFF                 ;Block 2 (008000-00BFFFh) not write-protecte
  WRT3 = OFF                 ;Block 3 (00C000-00FFFFh) not write-protecte
  WRT4 = OFF                 ;Block 4 (010000-013FFFh) not write-protecte
  WRT5 = OFF                 ;Block 5 (014000-017FFFh) not write-protecte
  WRT6 = OFF                 ;Block 6 (018000-01BFFFh) not write-protecte
  WRT7 = OFF                 ;Block 7 (01C000-01FFFFh) not write-protecte
  WRTC = OFF                 ;Configuration registers (300000-30000Bh) not write-protecte
  WRTB = OFF                 ;Boot Block (000000-0007FFh) not write-protecte
  WRTD = OFF                 ;Data EEPROM not write-protecte
  SCANE = On                 ;Scanner module is available for use, SCANMD bit can control the modul
  LVP = OFF                   ;Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignore
  Cp = OFF                   ;UserNVM code protection disable
  CPD = OFF                  ;DataNVM code protection disable
  EBTR0 = OFF                ;Block 0 (000800-003FFFh) not protected from table reads executed in other block
  EBTR1 = OFF                ;Block 1 (004000-007FFFh) not protected from table reads executed in other block
  EBTR2 = OFF                ;Block 2 (008000-00BFFFh) not protected from table reads executed in other block
  EBTR3 = OFF                ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other block
  EBTR4 = OFF                ;Block 4 (010000-013FFFh) not protected from table reads executed in other block
  EBTR5 = OFF                ;Block 5 (014000-017FFFh) not protected from table reads executed in other block
  EBTR6 = OFF                ;Block 6 (018000-01BFFFh) not protected from table reads executed in other block
  EBTR7 = OFF                ;Block 7 (01C000-01FFFFh) not protected from table reads executed in other block
  EBTRB = OFF                ;Boot Block (000000-0007FFh) not protected from table reads executed in other block
Config_End

'**** End of Fuse Configurator Settings ****
'--------


trastikata

Hello Dave-S,

what is the purpose of this code: PORTD = %00001000

top204

Also, the line:

RD3PPS = %00011011

Is setting the PPS (Peripheral Pin Select) for the pin to something that is not in the datasheet, so it may not now be a general purpose I/O line.

Instead of the code you have that alters SFRs for the pin, change it to the single command:

PinMode PORTD.3, Input_Pullup

Also, is there a reason for using the Serout command, as opposed to the HSerout command? The Serout command is now very outdated and is actually a 'keep backward compatability' command, and on devices that have a USART, it is very much recommended to use the HRsout or HSerout commands, because they are a lot more reliable and a use a lot less code space.

The compiler sets up the TTL levels and ADC disabling etc for the devices.

Also, do not test the code in Proteus, as the 18FxxK40 devices do not simulate correctly... Surprise, surprise. :-)


Dave-S

Thanks for the replies.
I thought the "PORTD = %00001000" set the PORTD.3 pin to input.

Have changed code. to the single command:, but still get 1 how do you then set input for a pps device
Device = 18F47K40
Declare Xtal = 64 
PinMode PORTD.3, Input_PullUp

 DelayMS 2500
 SerOut PORTD.1, 84, ["boot", 13]       
 ' *****************************************************************
DelayMS 50
Main:
SerOut PORTD.1, 84, ["Main", 13]
 
       SerOut PORTD.1, 84, ["portd.3 = ", Dec PORTD.3, 13]
       DelayMS 8000
GoTo Main

Have always used Serout for debugging via IDE Serial comuicator.
Tried the following but get nothing shown in IDE Serial comuicator
Declare Hserial_Baud = 9600
Declare HSerout_Pin = PORTD.1
HSerOut ["portd.3 = ", Dec PORTD.3, 13]

Thanks David



trastikata

It is set as input, but the pull-up is enabled, thus unless something pulls down the input, it will read 1 if left floating.

Dave-S

Originally I set it Pull down and tried a 10K resister to ground, I wanted to register a positive being applied to pin.  The pinMode suggested only gives Pullup.

Stephen Moss

Quote from: Dave-S on Feb 20, 2025, 01:23 PMI thought the "PORTD = %00001000" set the PORTD.3 pin to input.

Shouldn't that be TrisD?
Usually for 18F devices...
LATx is used for writes to the port
PORTx is used for Reads from the port
and as usual with PIC's TRISx set the input/output state of the pins. But unless you specifically wanted to set the other pins as output the datasheet shown that input is the default state anyway.

Thus in your original code example you appear to be setting the pin to an input (TrisD.3 = 1)
But then I cannot see where you are actually reading the state of the input pin, PORTD = %00001000 is setting the value of PortD read register is probably why it is being displayed as 1, I cannot see where you are then reading the PortD to update the value of the input pin.
Try...
TRISD = $08
 WPUD.3 = 1
 INLVLD.3 = 0
 RD3PPS = %00011011
 
 Dim X as byte

 DelayMS 2500
 SerOut PORTD.1, 84, ["boot", 13]      
  '***************     MAIN    *****************************************************************
DelayMS 50
Main:
SerOut PORTD.1, 84, ["Main", 13]
X = portD
       SerOut PORTD.1, 84, ["portd.3 = ", Dec X.3, 13]
       DelayMS 8000
GoTo Main