Menu

Serial does not work

Help
Gigi
2019-04-30
2019-05-01
  • Gigi

    Gigi - 2019-04-30

    Use PIC18F25K22 with two serials and interrupt reception, it seems that the USART1 does not receive anything, the input signal is present. The transmission instead seems correct, I don't understand where I'm wrong, can you give me some help?

     
  • Anobium

    Anobium - 2019-05-01

    I do not think your approach will work. You will need a ring buffer for both USARTs

    Something like this. There are two buffers and in the buffer you simply load the buffer - DO NOT hserprint the incoming data... you dont have time to do that before the next char is in arriving.

    Adapt as you need.

    ;Chip Settings
    #chip 18F25K22
    
    #option Explicit
    
    'USART settings
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    
    dim send_data, Rec_data as byte
    
    'Interrupt Handlers
    On Interrupt UsartRX1Ready Call readUSART1
    On Interrupt UsartRX2Ready Call readUSART2
    
    ' Constants etc required for Buffer Ring
    #define BUFFER_SIZE 64
    #define bkbhit1 (next_in1 <> next_out1)
    ;Variables
    Dim buffer1( BUFFER_SIZE - 1 ) 'we will use element 0 in the array as part of out buffer
    Dim next_in1 as byte: next_in1 = 0
    Dim next_out1 as byte: next_out1 = 0
    
    Dim buffer2( BUFFER_SIZE - 1 ) 'we will use element 0 in the array as part of out buffer
    Dim next_in2 as byte: next_in2 = 0
    Dim next_out2 as byte: next_out2 = 0
    
    dim xpos as Byte
    xpos = 10
    
    Do
      For send_data = 0 to 10
          HSerPrint "AT"
          HSerPrintCRLF
    
          if bkbhit1 then
    
            Rec_data = bgetc1
            IF Rec_data =79 THEN '79 is the ASCII value of O from the "OK" response
                Dir porta.0 out
                PORTA.0 = !PORTA.0
                xpos = 10
            END IF
            if xpos < 16 then
              xpos++
            end if
          end if
      next
    loop
    
    
    Sub readUSART1
        dim temppnt1 as byte
        buffer1(next_in1) = HSerReceive
        temppnt1 = next_in1
        next_in1 = ( next_in1 + 1 ) % BUFFER_SIZE
        if ( next_in1 = next_out1 ) then  ' buffer is full!!
            next_in1 = temppnt1
        end if
    End Sub
    
    function bgetc1
        wait while !(bkbhit1)
        bgetc1 = buffer1(next_out1)
        next_out1=(next_out1+1) % BUFFER_SIZE
    end Function
    
    
    
    Sub readUSART2
        dim temppnt2 as byte
        buffer2(next_in2) = HSerReceive
        temppnt2 = next_in2
        next_in2 = ( next_in2 + 1 ) % BUFFER_SIZE
        if ( next_in2 = next_out2 ) then  ' buffer is full!!
            next_in2 = temppnt2
        end if
    End Sub
    
    function bgetc2
        wait while !(bkbhit2)
        bgetc2 = buffer2(next_out2)
        next_out2=(next_out2+1) % BUFFER_SIZE
    end Function
    
     
  • Gigi

    Gigi - 2019-05-01

    I thank you for your great availability as always, you are right that to do a good job you have to create a buffer that I have already done and it works well, but there is something that does not convince me using the serial in a simple way. This code, for example, looks very simple, but it doesn't work, I use PIC16F628 just text transmission I don't think it works, I don't understand why.
    Thanks for your great help

    '''A demonstration program for EUSART
    '''--------------------------------------------------------------------------------------------------------------------------------
    '''********************************************************************************
    
    ; ----- Configuration
    #chip 16f628,8
    #option explicit
    
    ; ----- Define USART settings
    #define USART1_BAUD_RATE 38400
    #define USART1_TX_BLOCKING
    
    ' -- temp var
    dim Tmp0,Temp,I_tmp       as byte
    dim Tmp1                  as byte
    dim N_pan                 as byte        ' pannello attivo
    dim tempw,tempw1          as word
    
    ; ----- Define Hardware settings
        'Set pin directions
    
        Dir PORTB.2 Out     ' TX USART seriale
        Dir PORTB.1 In      ' RX USART
        dir portb.3 out
        dir portb.0 In
    
    ; ----- Main body of program commences here.
    
        Wait 100 Ms
        'Message after reset
    
    do
    
      portb.3= ! portb.3
      hserprint  "Test-"
      wait 1 S
    
    Loop
    
     

    Last edit: Anobium 2019-05-01
  • Anobium

    Anobium - 2019-05-01

    What is the target of this serial transmission?

     
  • Anobium

    Anobium - 2019-05-01

    TYPO...... #define USART1xxxx should be #define USARTxxxxxx

    Works on test here. Read the datasheet... the error % at 38400 could be very large but it still is workable on test there.

    I only had a 20mhz osc to hand, so, please change the frequency.

    Evan

    '''A demonstration program for EUSART
    '''--------------------------------------------------------------------------------------------------------------------------------
    '''********************************************************************************
    
    ; ----- Configuration
    #chip 16f628,20
    #option explicit
    
    ; ----- Define USART settings
      'USART settings
      #define USART_BAUD_RATE 38400
      #define USART_TX_BLOCKING
    
    
    ' -- temp var
    dim Tmp0,Temp,I_tmp       as byte
    dim Tmp1                  as byte
    dim N_pan                 as byte        ' pannello attivo
    dim tempw,tempw1          as word
    
    ; ----- Define Hardware settings
        'Set pin directions
    
        Dir PORTB.2 Out     ' TX USART seriale
        Dir PORTB.1 In      ' RX USART
        dir portb.3 out
        dir portb.0 In
    
    ; ----- Main body of program commences here.
    
        Wait 100 Ms
        'Message after reset
    
    do
    
      portb.3= ! portb.3
      hserprint  "Test-"
      wait 1 S
    
    Loop
    

    Looks like this on the PC Terminal

     

    Last edit: Anobium 2019-05-01
  • Gigi

    Gigi - 2019-05-01

    I'm doing some tests, a use is the transmission of various information to a PC or other device, I have already done other times with the software serial and it works well but should also go into hardware. If it is not possible I manage the logs manually it is not a very difficult job, I have already done some time ago I still have code in PicBasic.

     
    • Anobium

      Anobium - 2019-05-01

      See my post... you have a typo.

       
  • Gigi

    Gigi - 2019-05-01

    Perfect I tried, it works well. !!!!!!!!!!!!!!!
    Thank you

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.