Announcement

Collapse
No announcement yet.

18f27k40 - usart 2 setup

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 18f27k40 - usart 2 setup

    Hi Charles

    i migrating code to 18f27k40 chip , which uses usart2 on the the default portb , pins 6, 7

    i found your notes indicating possible issues with ISCP port

    the pcb is set and the usart2 and iscp port is required

    can you advise if this will be a problem

    i am still porting code so i have not tried it as yet


    regards


    sheldon

    Code:
    DEFINE HSER_PORT 2          ' Hser port 2 use
        DEFINE HSER2_RXREG PORTB    ' USART 2 RX - Set PPS Register port B
        DEFINE HSER2_RXBIT 7        '
        DEFINE HSER2_TXREG PORTB
        DEFINE HSER2_TXBIT 6
        DEFINE HSER2_RCSTA 90h      ' Hser2 receive status init
        DEFINE HSER2_TXSTA 24h      ' Hser2 transmit status init
        DEFINE HSER2_BAUD 19200     ' Hser2 baud rate
    
    
    
      '---------------- PERIPHERAL PIN SELECT MODULE -----------------------
    asm
    ; Unlock sequence
        BCF INTCON,GIE
        BANKSEL PPSLOCK
        MOVLW 055h
        MOVWF PPSLOCK
        MOVLW 0aah
        MOVWF PPSLOCK
        BCF PPSLOCK,PPSLOCKED
        BSF INTCON,GIE                                                                       
    endasm
      ' RX1PPS = $17'%00010111    'UART1 RX INPUT TO PIN RC7  -not used
      '    RC6PPS = $09'%00001001    'UART1 TX OUTPUT TO PIN RC6 - not used
    
        RX2PPS = $0F'%00001111    'UART2 RX INPUT TO PIN RB7    - sets same as define
        RB6PPS = $0B'%00001011    'UART2 TX OUTPUT TO PIN RB6
    
     '    INT0PPS = %00001000    'PORT B.0    - not used
     '    INT1PPS = %00001001    'PORT B.1
     '     INT2PPS = %00001010    'PORT B.2

  • #2
    Sheldon, You seem astute enough to get some of the deeper programming concepts. Someone once recommended working the registers manually instead of relying on PBP code in certain circumstances. You can always use:

    TX1REG = $34

    to Send a byte and:

    VarByte = RC1REG

    to retrieve. Using the RC1IF, an Interrupt Handler would send you to the retrieve sub. The TXxSTA/RCxSTA/BAUDCONx Registers are pretty much the same as a K22. Using the PPS Registers, you can assign TX/RX to just about any pins that work conveniently for you.

    I find more & more I'm manually working the SFRs and relying less & less on the canned PBP commands, especially with the newer PICs like the K40.
    We can crack this cotton PIC'n thang!

    Comment


    • #3
      Hi mike . Yep i been moving bit more to direct register use . More so with this pic than any other to date.

      It mainly to keep uniformaty through out the included routines for this project that i use all the same pbp command and defines
      Cheers

      sheldon

      Comment


      • #4
        I tested the following successfully. The chip takes programming even when the RX (RB7) is directly connected to an output from another PIC. Some isolation resistors would alleviate any conflicts.

        EDIT: RB7 was actually isolated from the serial transmit PIC with a 4.7K resistor. Sanity restored.

        You don't need to manually manipulate the PPS registers unless your application calls for changing the pin locations on the fly. The unlock sequence is only required if your configuration specifically locks the peripheral.


        Code:
        DEFINE HSER2_RXREG PORTB    ' USART 2 RX - Set PPS Register port B
        DEFINE HSER2_RXBIT 7        '
        DEFINE HSER2_TXREG PORTB
        DEFINE HSER2_TXBIT 6
        DEFINE HSER2_RCSTA 90h      ' Hser2 receive status init
        DEFINE HSER2_TXSTA 24h      ' Hser2 transmit status init
        DEFINE HSER2_BAUD 115200     ' Hser2 baud rate
        
        DEFINE OSC 64
        
        
        char    VAR BYTE
        
        ANSELB = 0
        
        DO
            HSERIN2 500, nodata, [char]
            HSEROUT2 [char]
            nodata:
            TOGGLE PORTB.0
        LOOP
        
        #CONFIG
            CONFIG FEXTOSC = OFF     ;Oscillator not enabled
            CONFIG RSTOSC = HFINTOSC_64MHZ     ;HFINTOSC with HFFRQ = 64 MHz and CDIV = 1:1
            CONFIG CLKOUTEN = OFF     ;CLKOUT function is disabled
            CONFIG CSWEN = ON     ;Writing to NOSC and NDIV is allowed
            CONFIG FCMEN = ON     ;Fail-Safe Clock Monitor enabled
            CONFIG MCLRE = EXTMCLR     ;If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR
            CONFIG PWRTE = ON     ;Power up timer enabled
            CONFIG LPBOREN = OFF     ;ULPBOR disabled
            CONFIG BOREN = ON     ;Brown-out Reset enabled according to SBOREN
            CONFIG BORV = VBOR_245     ;Brown-out Reset Voltage (VBOR) set to 2.45V
            CONFIG ZCD = OFF     ;ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON
            CONFIG PPS1WAY = OFF     ;PPSLOCK bit can be set and cleared repeatedly (subject to the unlock sequence)
            CONFIG STVREN = ON     ;Stack full/underflow will cause Reset
            CONFIG DEBUG = OFF     ;Background debugger disabled
            CONFIG XINST = OFF     ;Extended Instruction Set and Indexed Addressing Mode disabled
            CONFIG WDTCPS = WDTCPS_31     ;Divider ratio 1:65536; software control of WDTPS
            CONFIG WDTE = ON     ;WDT enabled regardless of sleep
            CONFIG WDTCWS = WDTCWS_7     ;window always open (100%); software control; keyed access not required
            CONFIG WDTCCS = LFINTOSC     ;WDT reference clock is the 31.0 kHz LFINTOSC
            CONFIG WRT0 = OFF     ;Block 0 (000800-003FFFh) not write-protected
            CONFIG WRT1 = OFF     ;Block 1 (004000-007FFFh) not write-protected
            CONFIG WRT2 = OFF     ;Block 2 (008000-00BFFFh) not write-protected
            CONFIG WRT3 = OFF     ;Block 3 (00C000-00FFFFh) not write-protected
            CONFIG WRT4 = OFF     ;Block 4 (010000-013FFFh) not write-protected
            CONFIG WRT5 = OFF     ;Block 5 (014000-017FFFh) not write-protected
            CONFIG WRT6 = OFF     ;Block 6 (018000-01BFFFh) not write-protected
            CONFIG WRT7 = OFF     ;Block 7 (01C000-01FFFFh) not write-protected
            CONFIG WRTC = OFF     ;Configuration registers (300000-30000Bh) not write-protected
            CONFIG WRTB = OFF     ;Boot Block (000000-0007FFh) not write-protected
            CONFIG WRTD = OFF     ;Data EEPROM not write-protected
            CONFIG SCANE = ON     ;Scanner module is available for use, SCANMD bit can control the module
            CONFIG LVP = ON     ;Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored
            CONFIG CP = OFF     ;UserNVM code protection disabled
            CONFIG CPD = OFF     ;DataNVM code protection disabled
            CONFIG EBTR0 = OFF     ;Block 0 (000800-003FFFh) not protected from table reads executed in other blocks
            CONFIG EBTR1 = OFF     ;Block 1 (004000-007FFFh) not protected from table reads executed in other blocks
            CONFIG EBTR2 = OFF     ;Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks
            CONFIG EBTR3 = OFF     ;Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks
            CONFIG EBTR4 = OFF     ;Block 4 (010000-013FFFh) not protected from table reads executed in other blocks
            CONFIG EBTR5 = OFF     ;Block 5 (014000-017FFFh) not protected from table reads executed in other blocks
            CONFIG EBTR6 = OFF     ;Block 6 (018000-01BFFFh) not protected from table reads executed in other blocks
            CONFIG EBTR7 = OFF     ;Block 7 (01C000-01FFFFh) 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
        Charles Leo
        ME Labs, Inc.
        http://melabs.com

        Comment


        • #5
          Click image for larger version

Name:	image_847.jpg
Views:	1
Size:	101.9 KB
ID:	7230 thanks charles

          Yes i always use 4k7 isolation resistors on the PGD, ,PGC , to connect for other signals




          Comment


          • #6
            I'm working on a PIC18F27K40 project using both UARTs also. Yesterday I was fighting UART2 and pulled this post up for reference. After way too many hours, I realized I had RX & TX reversed on the board (of my own design). Thanks to PPS it was an easy fix.
            We can crack this cotton PIC'n thang!

            Comment

            Working...
            X