Learning how to use USART


Closed Thread
Results 1 to 14 of 14

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Learning how to use USART

    Quote Originally Posted by richard View Post
    how can it work ?
    char's 0 and 1 are non printing characters on a lcd

    lcdout $fe, $94, dec character
    Adding the dec modifier didn't help. I tried adding it to hserout as well and it didn't work. Maybe it's my hardware. This is what I'm trying to use.

    Name:  midi-schem.jpg
Views: 1226
Size:  21.4 KB

    Can you recommend something else that's better for connecting two PICs?

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,644


    Did you find this post helpful? Yes | No

    Default Re: Learning how to use USART

    Can you recommend something else that's better for connecting two PICs?
    it can't get simpler than this depending on distance / speed .
    using serial tty ie hserin/out
    Attached Images Attached Images  
    Warning I'm not a teacher

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Learning how to use USART

    I would get the two talking directly without any MIDI interface. Just connect the TX from the PIC running the transmission code, to the RX on the receiving PIC using a simple jumper wire. If you need to send data back the connect the receiving PICs TX pin to the RX pin on the other PIC. I would then use the HSEROUT and HSERIN commands to send data.

    You could write a short test routine so that the receiving PIC sends a character to the transmitting PIC when its ready to receive data (say by pressing a button), the transmission is made and the data displayed on the LCD.

    So your transmission PIC has something like
    Code:
    RCIF              VAR PIR1.5                    ' USART receive flag
    GIE               VAR INTCON.7                  ' Comms
    TempWD            VAR WORD                      ' temporary WORD variable
    nTest var byte
    
    
    FOR TempWD = 0 TO 1000
        IF RCIF=1 THEN GOTO coms                   ; Check to see if somethings in the buffer, if there is something goto Comms   
        PAUSE 2
    NEXT TempWD  
    
    coms:
    HSERIN [nTest]
        SELECT CASE nTest    
        CASE "Q"                    ; if Q then send data 
        GOTO Term_TX:
    
    Term_TX
    Hserout [DEC3 insert variable here] ; sends three digits of variable eg 123
    You would then have your receiving PIC set up so that when you want to receive data it sends a Q to the transmitting PIC. The transmitting PIC is always looking at the serial port buffer to see if there is something in it, and when it finds something jumps to the comms subroutine, which checks to see what character has been received an for a match. If its Q then it transmits the data. You could use other characters to jump to other routines and do other things if you wanted.

    The only caveat here is that I've only ever sent numeric data this way. Sending ASCII or strings may need more work.

    Whilst the code is clunky it could always be tidied up and made more efficient once you have the two talking.

  4. #4
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Learning how to use USART

    Strange baudrate !

    Using a EUSART calculator for your baudrate it gives these values for the port settings

    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 39  ' 31250 Baud @ SPBRGH = 0
    BAUDCON.3 = 1         ' Enable 16 bit baudrate generator
    
    
    RCSTA = $90   ' Enable serial port & continuous receive
    TXSTA = $20   ' Enable transmit, BRGH = 0
    SPBRG = 39    ' 31250 Baud @ 0.0%
    SPBRGH = 0
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator
    I don't have PBP3 - so no idea if the formatting is the same or needs changing

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,132


    Did you find this post helpful? Yes | No

    Default Re: Learning how to use USART

    Please make sure that PIC to PIC directly connected work as expected.

    Then move to MIDI interface.

    Also be sure to connect TX to RX. Looks trivial but...

    Ioannis

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Learning how to use USART

    I removed the midi connections and just connected the TX of the transmitting PIC to the RC of the receiving PIC. Here is the code I'm using for each:

    transmitting:
    Code:
    define OSC 20
    define HSER_RCSTA 90h
    define HSER_TXSTA 20h
    define HSER_BAUD 31250
    DEFINE HSER_SPBRG 39  ' 31250 Baud @ SPBRGH = 0
    
    
    
    ANSEL = %00000000
    ANSELH = %00000000
    TRISD = %00000000
    TRISE = %00000000
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %10000000
    
    main:
    
    hserout [dec 1]
    PORTA.0 = 1
    pause 300
    hserout [dec 0]
    PORTA.0 = 0
    pause 300
    
    goto main
    receiving:
    Code:
    CLEAR
    define LOADER_USED 1
    define OSC 20
    define HSER_RCSTA 90h
    define HSER_TXSTA 20h
    define HSER_BAUD 31250
    define HSER_CLROERR 1
    DEFINE HSER_SPBRG 39  ' 31250 Baud @ SPBRGH = 0
    
    define LCD_DREG PORTD
    define LCD_DBIT 0
    define LCD_RSREG PORTE
    define LCD_RSBIT 0
    define LCD_EREG PORTE
    define LCD_EBIT 1
    define LCD_RWREG PORTE
    define LCD_RWBIT 2
    define LCD_BITS 4
    define LCD_LINES 4
    define LCD_COMMANDUS 2000
    define LCD_DATAUS 50
    
    ANSEL = %00000000
    ANSELH = %00000000
    TRISD = %00000000
    TRISE = %00000000
    TRISA = %00000000
    TRISB = %00000000
    TRISC = %10000000
    
    character var byte
    
    pause 250
    lcdout $FE, 1
    lcdout $fe, $d4, "ready to receive"
    pause 500
    lcdout $fe, 1
    pause 250
    
    main:
    
    hserin [character]
    lcdout $fe, 1
    lcdout $fe, $94, dec character
    
    goto main
    So now it's sorta working. The LCD does display something. It displays "216" when it should be displaying "0" and it displays "217" when it should be displaying 1.

  7. #7
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Learning how to use USART

    Try setting a variable.
    Code:
    character  var word
    then use something like

    Code:
    hserout [dec character]
    And similar for the receiving pic. I'm no expert, but I think that just having DEC 1 as you had simply means its setting the DEC command to use just 1 digit as decimal value of a variable, not a value
    Last edited by Scampy; - 14th May 2018 at 20:44.

Similar Threads

  1. remote code learning
    By Bruce in forum Code Examples
    Replies: 20
    Last Post: - 21st February 2021, 15:38
  2. Replies: 7
    Last Post: - 12th January 2018, 17:02
  3. USB Learning
    By ruijc in forum USB
    Replies: 19
    Last Post: - 26th July 2014, 21:09
  4. Learning Basic
    By bartman in forum General
    Replies: 14
    Last Post: - 19th November 2004, 00:45

Members who have read this thread : 2

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