2nd hardware UART


+ Reply to Thread
Results 1 to 28 of 28

Hybrid View

  1. #1
    Join Date
    Aug 2011
    Posts
    453


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    You might want to post what you've tried so we can give you some 'tips'

  2. #2
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    169


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    Quote Originally Posted by tumbleweed View Post
    You might want to post what you've tried so we can give you some 'tips'
    Code:
    OSCFRQ = %1000 '64Mhz
    
    Define OSC 64
    
    ' Set receive register to receiver enabled
    ' Set transmit register to transmitter enabled
    DEFINE HSER_RXREG PORTC
    DEFINE HSER_RXBIT 4
    DEFINE HSER_TXREG PORTC
    DEFINE HSER_TXBIT 5
    DEFINE HSER_BAUD 9600
    
    ' Set receive register to receiver enabled
    ' Set transmit register to transmitter enabled
    'DEFINE HSER2_RXREG PORTB
    'DEFINE HSER2_RXBIT 3
    'DEFINE HSER2_TXREG PORTB
    'DEFINE HSER2_TXBIT 0
    'DEFINE HSER2_BAUD 9600
    
    'This works
    hSerout ["Test",13,10]
    
    'This doesn't
    hSerout2 ["Test",13,10]

  3. #3
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    169


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    Apologies, here is the actual code:

    Code:
    OSCFRQ = %1000 '64Mhz
    
    Define OSC 64
    
    PRECISION  CON 8 SYSTEM  '8 byte = 64 bit precision
    INCLUDE "N-Bit_Math.pbp"
    
    DEFINE HSER2_RXREG PORTB
    DEFINE HSER2_RXBIT 3
    DEFINE HSER2_TXREG PORTB
    DEFINE HSER2_TXBIT 0
    DEFINE HSER2_BAUD 9600
    
    DEFINE HSER_RXREG PORTC
    DEFINE HSER_RXBIT 4
    DEFINE HSER_TXREG PORTC
    DEFINE HSER_TXBIT 5
    DEFINE HSER_BAUD 9600
     
    ANSELA = %00000000 'Everything Digital
    ANSELB = %00000000 'Everything Else Digital
    ANSELC = %00000000 'Everything Else Digital
    
    TRISB.0 = 0 
    TRISB.3 = 1 
    TRISC.4 = 1 
    TRISC.5 = 0 
    
    'works
    hSerout ["Test",13,10]
    
    'doesn't work
    hSerout2 ["Test",13,10]

  4. #4
    Join Date
    Nov 2005
    Posts
    35


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    I'm far from an expert in this space. And haven't used the second USART port on a PIC
    But maybe PMD5 register for UART2 isn't being set correctly by picbasic??

    bit 5 U2MD: Disable UART2 bit
    1 = UART2 module disabled
    0 = UART2 module enabled
    bit 4 U1MD: Disable UART1 bit
    1 = UART1 module disabled
    0 = UART1 module enabled

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    I'm far from an expert in this space. And haven't used the second USART port on a PIC
    But maybe PMD5 register for UART2 isn't being set correctly by picbasic??
    extremely unlikely

    note power on defaults for PDM5
    Name:  Untitled.jpg
Views: 825
Size:  135.6 KB

    did you try setting uart regs like mcc suggests ?
    note uart registers

    Name:  rocket.jpg
Views: 736
Size:  175.2 KB
    Warning I'm not a teacher

  6. #6
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    169


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    Regarding setting the regs directly: this is what I've tried (note this is set to 115200 baud as that was the original objective)

    Code:
    OSCFRQ = %1000 '64Mhz
    
    Define OSC 64
    
    PRECISION  CON 8 SYSTEM  '8 byte = 64 bit precision
    INCLUDE "N-Bit_Math.pbp"
    
    'Use for hardware serial comms to SD card module
    '******************************************************************************
    'Configure UART Control Register 0
    U2CON0.7 = 1 'BRGS = 1 (high speed baud generator)
    U2CON0.6 = 0 'ABDEN = 0 Auto-baud Detect Disabled 
    U2CON0.5 = 1 'TXEN: Transmit Enabled
    U2CON0.4 = 1 'RXEN: Receive Disabled
    U2CON0.3 = 0 '0000 = Asynchronous 8-bit UART mode
    U2CON0.2 = 0 '0000 = Asynchronous 8-bit UART mode
    U2CON0.1 = 0 '0000 = Asynchronous 8-bit UART mode
    U2CON0.0 = 0 '0000 = Asynchronous 8-bit UART mode
    
    U2CON1.7 = 0 'Turn Serial port 2 off for baud setting
     
    U2BRGL = 138  'Baud Divisor low byte
    U2BRGH = 0    'Baud Divisor high byte
    
    U2RXPPS = %1011 'Assign UART RX pin to RB3
    RB0PPS = %010110  'Assign UART2 TX pin to RB0
     
    U2CON1.7 = 1 'Turn Serial port 2 on
    '******************************************************************************
     
    'Configure AN2 as Analog Inputs (PortA2)
    ANSELB = %00000000 'Everything Digital
    
    TRISB.0 = 0 'SD_Card_TX Hardware UART
    TRISB.3 = 1 'SD_Card_RX Hardware UART
    
    hSerout2 ["Test",13,10]
    That doesn't provide any joy either.
    Last edited by rocket_troy; - 13th August 2024 at 16:45.

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    I was going to say that you need to unlock the PPS before trying to configuring it but reading the datasheet it looks like it is unlocked at POR - I've always thought it was the other way around.

    Anyway, here's the relevant parts from a project of mine which uses both UARTS, in this particular case I even use the PPS peripheral to switch UART2 pins around to two different devices. Using HSEROUT / HSEROUT2 in the code it worked out fine. This is for a 67K40 so it won't match your setup but perhaps it can provide some pointers.
    Code:
    '----------------------------------------------------
    ' PPS Unlock
    PPSLOCK = $55
    PPSLOCK = $AA
    @ BCF PPSLOCK, PPSLOCKED
    
    RC6PPS = $0C                                ' We want PORTC.6 as EUSART1 TX
    RX1PPS = %00010111                          ' We want PORTC.7 as EUSART1 RX
    
    RG7PPS = $0E                                ' We want EUSART2 TX on PortG.7 
    RX2PPS = %00110010                          ' We want EUSART2 RX on PortG.2 
    
    ' Since we're switching UART pins around during runtime we need to leave the PPS lock open.
    'PPSLOCK = $55
    'PPSLOCK = $AA
    '@ BSF PPSLOCK, PPSLOCKED
    '----------------------------------------------------
    
    ANSELA = %00110000                          ' RA4, 5 analog, rest digital 
    ANSELB = 0
    ANSELD = 0
    ANSELE = 0
    ANSELF = 0
    ANSELG = 0
    
    
    ' Setup for EUSART1 
    RCSTA1 = $90                                ' Enable serial port & continuous receive
    TXSTA1 = $24                                ' Enable transmit, BRGH = 1
    SPBRG1 = 64                                 ' 19200 Baud @ 64MHz, 0,04%
    SPBRGH1 = 3
    BAUDCON1.3 = 1                              ' Enable 16 bit baudrate generator
    
    
    ' Setup for EUSART2 
    RCSTA2 = $90                                ' Enable serial port & continuous receive
    TXSTA2 = $24                                ' Enable transmit, BRGH = 1
    SPBRG2 = 138                                ' 115200 Baud @ 64MHz, -0,08%
    SPBRGH2 = 0
    BAUDCON2.3 = 1                              ' Enable 16 bit baudrate generator
    
    
    TRISC.6 = 0                                 ' UART1 TX
    TRISC.7 = 1                                 ' UART1 RX
    
    TRISG.7 = 0                                 ' UART2 TX 
    TRISG.2 = 1                                 ' UART2 RX
    /Henrik.

  8. #8
    Join Date
    May 2013
    Location
    australia
    Posts
    2,643


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    you don't appear to have set the portb pins used to digital [anselb =$f6]

    or not

    mcc sets the involved registers like this to match your settings

    TRISB = 0xFE;
    ANSELB = 0xF6;
    U2RXPPS = 0x0B; //RB3->UART2:RX2;
    RB0PPS = 0x16; //RB0->UART2:TX2;
    U2P1L = 0x00;
    U2P1H = 0x00;
    U2P2L = 0x00;
    U2P2H = 0x00;
    U2P3L = 0x00;
    U2P3H = 0x00;
    U2CON0 = 0xB0;
    U2CON1 = 0x80;
    U2CON2 = 0x00;
    U2BRGL = 0x82;
    U2BRGH = 0x06;
    U2FIFO = 0x00;
    U2UIR = 0x00;
    U2ERRIR = 0x00;
    U2ERRIE = 0x00;
    Last edited by richard; - 13th August 2024 at 01:05. Reason: oh you posted again when i reading the data sheet
    Warning I'm not a teacher

  9. #9
    Join Date
    Dec 2010
    Location
    Melbourne Australia
    Posts
    169


    Did you find this post helpful? Yes | No

    Default Re: 2nd hardware UART

    I'm guessing this is a reply to my 1st code post which I inadvertently cut the TRIS settings in my (hastily) attempt for reader friendly pruning, but I can assure you every port was set to digital all the time. What's more, the 2nd UART was originally configured to the same pins as the 1st. When my head was too sore from banging against that wall, I switched the 1st UART to those pins and voila, they worked.

    Troy

    Quote Originally Posted by richard View Post
    you don't appear to have set the portb pins used to digital [anselb =$f6]

    or not

    mcc sets the involved registers like this to match your settings

    TRISB = 0xFE;
    ANSELB = 0xF6;
    U2RXPPS = 0x0B; //RB3->UART2:RX2;
    RB0PPS = 0x16; //RB0->UART2:TX2;
    U2P1L = 0x00;
    U2P1H = 0x00;
    U2P2L = 0x00;
    U2P2H = 0x00;
    U2P3L = 0x00;
    U2P3H = 0x00;
    U2CON0 = 0xB0;
    U2CON1 = 0x80;
    U2CON2 = 0x00;
    U2BRGL = 0x82;
    U2BRGH = 0x06;
    U2FIFO = 0x00;
    U2UIR = 0x00;
    U2ERRIR = 0x00;
    U2ERRIE = 0x00;

Similar Threads

  1. PID-filter routine (2nd try).
    By HenrikOlsson in forum Code Examples
    Replies: 131
    Last Post: - 3rd October 2018, 07:53
  2. Setup baudrate on hardware uart on 18F4520
    By Pedro Pinto in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 25th August 2010, 00:31
  3. crucial error in a 90% working hardware uart
    By mimmmis in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 23rd September 2008, 14:18
  4. Serial Port Complete 2nd Edition
    By Bruce in forum Serial
    Replies: 4
    Last Post: - 8th November 2007, 15:28
  5. 2nd Order Digital Filter for 24-bit
    By sefayil in forum mel PIC BASIC
    Replies: 0
    Last Post: - 2nd December 2005, 21:55

Members who have read this thread : 13

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts