Does'nt work in Serial Communicator to send string

Started by Aries_Ryan, Today at 12:28 PM

Previous topic - Next topic

Aries_Ryan

I have issue I did'nt get a receive text when I sending in Serial Communicator to PIC, I used USB TTL and porting to PIC Pin.
Hope somebody here to helping what is a problem.
 
Device = 18F46K22
Declare Xtal = 16

Config_Start
    FOSC = INTIO67     
    PLLCFG = OFF       
    PRICLKEN = On       
    FCMEN = OFF         
    IESO = OFF         
    PWRTEN = OFF       
    BOREN = SBORDIS     
    BORV = 190         
    WDTEN = OFF         
    MCLRE = EXTMCLR     
Config_End

Declare Auto_Heap_Arrays = On       
Declare Auto_Heap_Strings = On     
Declare Auto_Variable_Bank_Cross = On

' ---(PORTC - Pin 25 TX1) -----
Declare Hserial_Baud = 9600
Declare Hserial_RCSTA = %10010000
Declare Hserial_TXSTA = %00100100
Declare Hserial_Clear = On

' --- To (PORTB - Pin 40 RX2) -----
Declare Hserial2_Baud = 9600
Declare Hserial2_RCSTA = %10010000
Declare Hserial2_TXSTA = %00100100
Declare Hserial2_Clear = On

Dim PC_Char         As Byte     
Dim Header_State    As Byte       
Dim PC_Idx          As Byte       
Dim i               As Byte
Dim Heartbeat       As Word
Dim Loop_Tick       As Word

Dim Live_Msg[121]   As Byte       

OSCCON = %01110000                 
ANSELB = 0                         
ANSELC = 0

TRISB  = %10000000  ' Pin 40 (RB7) set strictly as Input
TRISC  = %10000000  ' Pin 26 Input, Pin 25 Output

SPBRG2 = 103       
TXSTA2 = %00100100 
RCSTA2 = %10010000 

DelayMS 1200       

Header_State = 0
PC_Idx = 0
Heartbeat = 0
Loop_Tick = 0

Main_Loop:
    While 1 = 1
       
        While PIR3.5 = 1
            PC_Char = RCREG2

            If Header_State = 0 Then
                If PC_Char = "T" Then
                    Header_State = 1
                EndIf
                GoTo Next_Byte
            EndIf
           
            If Header_State = 1 Then
                If PC_Char = "X" Then
                    Header_State = 2
                Else
                    Header_State = 0
                EndIf
                GoTo Next_Byte
            EndIf
           
            If Header_State = 2 Then
                If PC_Char = "T" Then
                    Header_State = 3
                Else
                    Header_State = 0
                EndIf
                GoTo Next_Byte
            EndIf
           
            If Header_State = 3 Then
                If PC_Char = ":" Then
                    Header_State = 4
                    PC_Idx = 0
                    For i = 0 To 120 : Live_Msg[i] = " " : Next
                Else
                    Header_State = 0
                EndIf
                GoTo Next_Byte
            EndIf

            If Header_State = 4 Then
                If PC_Char = 13 Or PC_Char = 10 Or PC_Char = 0 Or PC_Idx >= 115 Then
                    Live_Msg[PC_Idx] = 0
                   
                   
                    HSerOut ["t13.txt=", 34, Str Live_Msg, 34, 255, 255, 255]
                    Header_State = 0
                   
               
                    Loop_Tick = -10000
                Else
                    Live_Msg[PC_Idx] = PC_Char
                    Inc PC_Idx
                EndIf
            EndIf
Next_Byte:
        Wend
       
        If RCSTA2.1 = 1 Then
            RCSTA2.4 = 0
            RCSTA2.4 = 1
        EndIf

       
        Inc Loop_Tick
        If Loop_Tick >= 1000 Then   ' 1000 ticks * 1ms = 1 Second
            Loop_Tick = 0
            Inc Heartbeat
           
            If Header_State = 0 Then
                HSerOut ["t13.txt=", 34, "PIC SEARCHING PC: ", Dec Heartbeat, 34, 255, 255, 255]
            EndIf
        EndIf

        DelayMS 1
    Wend

A_Ryan

RGV250

Hi,
Do you know if the comms is working, if it were me I would first link RX<->TX on the USB TTL and make sure that works first.
Then just have a very simple example with the PIC just echoing the characters.

Regards,
Bob

trastikata

@RGV250

RGV250 correctly stated that first you have to see if the USB TTL is sending something to the PIC.

Easiest way is an oscilloscope. If not available you can connect a LED to the USB TTL output. If you send 1200 times 0x00 then 0xFF and again 0x00 at 9600 baud, the LED should stay OFF-ON-OFF for about 1 second at each state.


Aries_Ryan

Quote from: RGV250 on Today at 12:41 PMHi,
Do you know if the comms is working, if it were me I would first link RX<->TX on the USB TTL and make sure that works first.
Then just have a very simple example with the PIC just echoing the characters.

Regards,
Bob
No worries about it, I have done some my project to communication into VB. Net for controller relay and wireless.
My question are around that my post, to loop TX to RX.

A_Ryan

Aries_Ryan

Quote from: trastikata on Today at 12:52 PM@RGV250

RGV250 correctly stated that first you have to see if the USB TTL is sending something to the PIC.

Easiest way is an oscilloscope. If not available you can connect a LED to the USB TTL output. If you send 1200 times 0x00 then 0xFF and again 0x00 at 9600 baud, the LED should stay OFF-ON-OFF for about 1 second at each state.


I did it to LED, I don't have an osciloscope.

A_Ryan.

top204

A few things I have noticed in your code Aries.

First... Always implement 'all' config fuses, and never leave defaults, because microchip have a nasty habit of defaulting to the most obscure settings. Especially the XINST setting.

Second... For the past decade, or more, there is no need to use the Hserial2_RCSTA, and Hserial2_TXSTA Declares because the compiler sorts them out for the user, depending on the Xtal value and the Baud rate set.

Third... After the declares, the SFRs themselves are altered again in your code.

Below is a code listing template to show a test I carried out on a simulation of a PIC18F46K22 device using USART2 to receive and re-transmit the data received. I tried the code on a real PIC18F26K22 device as well, and it all works (with a fuse change for the 18F26K22):

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' USART2 receive and transmit test.
'
' Written by Les Johnson for the Positron8 BASIC Compiler.
' https://sites.google.com/view/rosetta-tech/positron-compilers-experimenters-notebook.
'
    Device = 18F46K22                                                   ' Tell the compiler what device to compile for
    Declare Xtal = 16                                                   ' Tell the compiler what frequency the device is operating at (in MHz)
    Declare Auto_Heap_Arrays = On                                       ' Make all arrays "Heap" types, so they always get placed after standard variables
    Declare Auto_Heap_Strings = On                                      ' Make all Strings "Heap" types, so they always get placed after standard variables
    Declare Auto_Variable_Bank_Cross = On                               ' Make sure all multi-byte variables remain within a single RAM bank
'
' Setup USART1
'
    Declare HSerout1_Pin  = PORTC.6                                     ' Tell the compiler what pin for USART1 TX
    Declare HSerin1_Pin   = PORTC.7                                     ' Tell the compiler what pin for USART1 RX
    Declare Hserial1_Baud = 9600                                        ' Tell the compiler what Baud rate to set for USART1
    Declare Hserial1_Clear = On                                         ' Enable Error clearing on received bytes by USART1
'
' Setup USART2
'
    Declare HSerout2_Pin  = PORTD.6                                     ' Tell the compiler what pin for USART2 TX
    Declare HSerin2_Pin   = PORTD.7                                     ' Tell the compiler what pin for USART2 RX
    Declare Hserial2_Baud = 9600                                        ' Tell the compiler what Baud rate to set for USART2
    Declare Hserial2_Clear = On                                         ' Enable Error clearing on received bytes by USART2
'
' Create some global variables for the demo program
'
    Dim ByteIn As Byte                                                 ' Holds the data received from USART2

'--------------------------------------------------------------------------------
' The main program starts here
' Receive data from USART2, and echo it back.
' This will make sure the device is operating at the correct frequency, and that RX and TX connections are OK.
'
Main:
    Setup()                                                             ' Setup the program and any peripherals

    HRSOut2Ln "Type characters and see them echoed back"

    Do                                                                  ' Create a loop
Again:
        HSerIn2 5000, TimedOut, [Bytein]                                ' Receive a byte into Bytein from USART2 (time out after approx 5 seconds)
        HRSOut2 Bytein                                                  ' Transmit what was received
    Loop                                                                ' Do it forever
'
' Jump here if USART1 timed out
'
TimedOut:
    HRSOut2Ln "\rTimed Out"                                             ' Transmit text to a serial terminal
    GoTo Again                                                          ' Jump back to receive more data

'--------------------------------------------------------------------------------
' Setup the program and peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
'
' Any required setups in here
'
EndProc

'--------------------------------------------------------------------------------
' Setup the configuration fuses for a PIC18F46K22 to use the external oscillator.
'
Config_Start
    FOSC     = HSHP                                                     ' HSHP oscillator
    PRICLKEN = Off                                                      ' Primary clock disabled
    MCLRE    = EXTMCLR                                                  ' MCLR pin enabled, RE3 input pin disabled
    WDTEN    = Off                                                      ' Watchdog Timer disabled
    Debug    = Off                                                      ' Background debugger disabled
    PLLCFG   = Off                                                      ' Oscillator used directly
    XINST    = Off                                                      ' Extended Instruction Set Disabled
    FCMEN    = Off                                                      ' Fail-Safe Clock Monitor disabled
    IESO     = Off                                                      ' Oscillator Switchover mode disabled
    PWRTEN   = On                                                       ' Power Up Timer enabled
    BOREN    = Off                                                      ' Brown-Out Reset disabled
    BORV     = 190                                                      ' VBOR set to 1.9 V nominal
    WDTPS    = 128                                                      ' Watchdog ratio is 1:128
    HFOFST   = Off                                                      ' The Access Clock is not held off until the HF-INTOSC is stable
    PBADEN   = Off                                                      ' PORTB<4:0> pins are configured as digital on reset
    CCP2MX   = PORTC1                                                   ' CCP2 input/output is multiplexed with RC1
    CCP3MX   = PORTB5                                                   ' P3A/CCP3 input/output is multiplexed with RB5
    T3CMX    = PORTC0                                                   ' T3CKI is on RC0
    P2BMX    = PORTD2                                                   ' P2B is on RD2
    STVREN   = On                                                       ' Stack full/underflow will cause a reset
    LVP      = On                                                       ' Low voltage ICSP enabled
    Cp0      = Off                                                      ' Block 0 (000800-001FFF) not code-protected
    CP1      = Off                                                      ' Block 1 (002000-003FFF) not code-protected
    CPB      = Off                                                      ' Boot block (000000-0007FF) not code-protected
    CPD      = Off                                                      ' Data EEPROM not code-protected
    WRT0     = Off                                                      ' Block 0 (000800-001FFF) not write-protected
    WRT1     = Off                                                      ' Block 1 (002000-003FFF) not write-protected
    WRTB     = Off                                                      ' Boot block (000000-0007FF) not write-protected
    WRTC     = On                                                       ' Configuration registers (300000-3000FF) write-protected
    WRTD     = Off                                                      ' Data EEPROM not write-protected
    EBTR0    = Off                                                      ' Block 0 (000800-001FFF) not protected from table reads executed in other blocks
    EBTR1    = Off                                                      ' Block 1 (002000-003FFF) not protected from table reads executed in other blocks
    EBTRB    = Off                                                      ' Boot block (000000-0007FF) not protected from table reads executed in other blocks
Config_End

Use the program above to test your interface mechanism, then take each step of your data receiving a stage at a time, with tests in-between. This is important with async serial coms, because it is very time dependant, unless serial buffering is implemented using interrupts.

Below is a screenshot of the simulation running, so you will know what to expect if you run the code yourself:

Serial_Interface_Test.jpg

Regards
Les