Unable to get PORTC.7/PORTC.6 working as HSerin/HSerout on PIC18F47K40

Started by Dave-S, Apr 13, 2022, 04:43 PM

Previous topic - Next topic

Dave-S

Unable to get PORTC.7/PORTC.6 working as HSerin/HSerout on PIC18F47K40

This PIC has Peripheral Pin Select (PPS) module, the datasheet states that PORTC.7/PORTC.6
are the default on powerup or reset, therefore I assume I do not have to set them or does the cpmpiler do it anway.  I have set the registers as per datasheet but conecting to the seral terminal and sending I get nothing.  There must be a wrong setting somwhere. Code below. I could not get Serin to work either on on PortC.7 Serout works on PortC.6 ok.


;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F47K40

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

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------
Declare Xtal = 64
All_Digital = True
ANSELC.7 = 1   'disable   digital
ANSELC.6 = 1   'disable   digital
  '*******USART Setup *******************
 TX1STA = %00100100
 RC1STA = %10010000
 BAUD1CON.4 = 1
 BAUD1CON.3 = 0
 BAUD1CON.1 = 0
 BAUD1CON.0 = 0
 SP1BRG = 7
       Declare Hserial_Baud = 115200
       Declare HSerin_Pin = PORTC.7
       Declare HSerout_Pin = PORTC.6
       Declare Hserial_Clear = On
       Dim ESP_Char[4] As Byte
       Dim ESP_Char1 As Byte
       Dim ESP_Buff[20] As Byte
       Dim ESP_CharPos As Byte
       TRISC.6 = 0
       TRISC.7 = 1
       ESP_CharPos = 0
'*************************************************************************************       
 Main:
     GoSub Hser_test     
GoTo Main       
'***************************************************************************************   
Hser_test:
          HSerIn [ESP_Char1]
          HSerOut [Dec ESP_Char1]
         
         ' SerIn PORTC.7, 84, [dec ESP_Char1]
         'SerOut PORTC.6, 84, [dec ESP_Char1, 13]
Return
'*****************************************************************************************
 

Any idea where I am going wrong.
Thanks David.

RGV250

Hi,
I doubt it makes any difference but I don't think you need All_Digital anymore.
I am a bit confused by
ANSELC.7 = 1   'disable   digital
ANSELC.6 = 1   'disable   digital
surely you want these to be digital for serial data?

Bob

Dave-S

These are the Analog- to Digital Converter ANC6 and ANC7
Datasheet states:-
If the RX/DT function is on an analog pin,
the corresponding ANSEL bit must be
cleared for the receiver to function.

David

RGV250


Gabi

btw, check if you have your internal osc initialized. I have it for internal osc @64Mhz like so:

Proc OSCILLATOR_Initialize()
    OSCCON1 = $60       ' NOSC HFINTOSC; NDIV 1
    OSCCON3 = $00       ' CSWHOLD may proceed, SOSCPWR Low power
    ; MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = $00
    OSCFRQ = $08        ' HFFRQ 64_MHz 
    OSCTUNE = $00       ' TUN 0
EndProc


Try this below, as it outputs on RC.6 (tested, should do also without the PPS set):

;-------------------------------------------------------------------------------
Device = 18F47K40
;-------------------------------------------------------------------------------
Declare Xtal = 64                               ' Running at 32 MHz

Declare Hserial_Baud = 115200                   ' Set the Baud rate
Declare HRSOut_Pin = PORTC.6                    ' Select the pin for TX with USART1 
Declare Hserial_Clear = 1                       ' Enable Error clearing on received characters

'-------------------------------------------------------------------------------
' INITIALIZATION -> board specific
'-------------------------------------------------------------------------------
INIT:
    RX1PPS = $17    ' RC7->EUSART1:RX1
    RC6PPS = $09    ' RC6->EUSART1:TX1
'    RX1PPS = $0F                    ' RB7->EUSART1:RX1   
'    RB6PPS = $09                    ' RB6->EUSART1:TX1
    ODCONB = $00
    SLRCONB = $FF
    INLVLB = $FF
    '
    OSCILLATOR_Initialize()
    '
'-------------------------------------------------------------------------------
' Main Loop
'-------------------------------------------------------------------------------
Main:
    While 1=1
        DelayMS 1000
        HRSOutLn "------ START -----\r\n"
    Wend
   

'---------------------------------------------------------
' board specific OSC SETUP
'---------------------------------------------------------
Proc OSCILLATOR_Initialize()
    OSCCON1 = $60       ' NOSC HFINTOSC; NDIV 1
    OSCCON3 = $00       ' CSWHOLD may proceed, SOSCPWR Low power
    ; MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = $00
    OSCFRQ = $08        ' HFFRQ 64_MHz 
    OSCTUNE = $00       ' TUN 0
EndProc


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

other than that, in your code, if no data goes in on the RC.7 it may be waiting in there.


GL & 73
YO4WM

Dave-S

Hi Gabi,
Many thanks for you help, yes I can get it working now with your code, is the cpu running at 32 mhz then, why is this?
In the rest of this project I had SPI, IC2 and Serin/Serout(9600) all working OK so I assumed the CPU was at 64. I had set up a seperate piece code for the UART because I thought there was a conflict with them.

I had relised the orignal PPS were for the B port and had put the correct ones for C, and was getting a gibberish output.



David

Gabi

Quote from: Dave-S on Apr 14, 2022, 10:16 AMHi Gabi,
Many thanks for you help, yes I can get it working now with your code, is the cpu running at 32 mhz then, why is this?
In the rest of this project I had SPI, IC2 and Serin/Serout(9600) all working OK so I assumed the CPU was at 64. I had set up a seperate piece code for the UART because I thought there was a conflict with them.

I had relised the orignal PPS were for the B port and had put the correct ones for C, and was getting a gibberish output.



David

Ignore that line comment please, its a left over from other code.
GL & 73
YO4WM