Menu

Help with serial data

Help
2016-02-24
2016-02-25
  • Edward LaBudde

    Edward LaBudde - 2016-02-24

    Hi all, looking to have some of you smart guys to review my code for a project I am working on. Ihave tried to encorprate you previous comments. Please review my code. Question do I need to put in the "Wait 9890 us 'To allow last two bytes Tx" on the send data sub?
    Thanks, in advance Ed.

    'Chip model
    #chip 18F4455, 40
    #config  MCLRE = On
    'Configure Usart
    #define USART_BAUD_RATE 2400
    #define USART_BLOCKING
    
    'Transmit data
    Sub SendData                'Send power monitor data
        Call SendCRC            'Caculate CRC
        Set SPEN = Off      'Turn Usart off to prevent interferance from pulseout
        Set T/R = Off           'Turn Off Rx turn on Tx
        wait 1000 us            'Transmitter delay Typical = 490 us
        pulseout PortC.6, 12 ms 'Init Reciever
        Set SPEN = On           'Turn Usart on
        HSerSend Solar1A_h
        HSerSend Solar1A
        HSerSend Solar2A_h
        HSerSend Solar2A
        HSerSend WindA_h
        HSerSend WindA
        HSerSend TEGA_h
        HSerSend TEGA
        HSerSend BattAVG_h
        HSerSend BattAVG
        HSerSend ETimeHrs_h
        HSerSend ETimeHrs
        HSerSend S1TimeHrs_h
        HSerSend S1TimeHrs
        HSerSend S2TimeHrs_h
        HSerSend S2TimeHrs_h
        HSerSend WindTimeHrs_h
        HSerSend WindTimeHrs
        HSerSend TEGTimeHrs_h
        HSerSend TEGTimeHrs
        HSerSend CRC
        Wait 9890 us            'To allow last two bytes Tx
        Set T/R = On            'Turn off Tx turn on Rx
        set CREN = On           'Turn Receiver On
        wait 400 us             'Receiver delay Typical = 180 us
    Return
    
    
    'Recieve data
    Sub ReceiveData
     Solar1A_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with signal
     Solar1A = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     Solar2A_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     Solar2A = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     WindA_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     WindA = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     TEGA_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     TEGA = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     BattAVG_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     BattAVG = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     ETimeHrs_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     ETimeHrs  = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     S1TimeHrs_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     S1TimeHrs = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     S2TimeHrs_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     ETS2_hrs = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     WindTimeHrs_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     WindTimeHrs = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     TEGTimeHrs_h = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     TEGTimeHrs = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
     Cs = HserReceive1
     RSSIS = ReadAD10(AN0)  'Read RSSI with  signal
     RSSISAvg = (RSSISAvg+RSSIS)/2
    
     Call RECCRC
     If Cs <> CRC then
        DataErrors = DataErrors + 1
      Set CRCRED = On
      Else set CRCGREEN = on
        Wait 10 ms
        End If
        Set CREN = Off                  'Turn Receiver off
        wait 10 ms                        'Wait for RSSI to settle
        RSSISampleNS = ReadAD10(AN0)    'Read RSSI with no signal
        RSSISamplesNSAVG = (RSSISamplesNSAVG+RSSISamplesNS)/2
        RSSISampleNS = ReadAD10(AN0)    'Read RSSI with no signal
        RSSISamplesNSAVG = (RSSISamplesNSAVG+RSSISamplesNS)/2
        RSSISampleNS = ReadAD10(AN0)    'Read RSSI with no signal
        RSSISamplesNSAVG = (RSSISamplesNSAVG+RSSISamplesNS)/2
        set CREN = On
    Return
    
     
  • mmotte

    mmotte - 2016-02-25

    Some formating errors. Each "Sub" needs an "End Sub" instead of a "return".
    The if then else needs to rearrange. Look these up in help. Also when you "call" a sub you don't need to use "Call", just use the name of the sub.

    I assume these two subs are in different processors?

    The setup of the HSer routines looks right but is missing the "dir" of the Hserial ports. Remember to set them up.

    In the sending , you are using "Blocking", so the usart is waiting for the input register to be empty before putting the next byte in.

    In receiving, with "blocking", you are waiting for the next new byte to come in, for each of your variables.

    You asked if you should wait 9.8 ms for the transmit USART to clear. If USART was always enabled on, I would not think you would need the "wait" because the USART will clear itself by sending what is in the shift register, transfering the byte from the Tx input register, and shifting that out.. But you are turning the USART on and off, so i think you need to wait for the USART to finish before shutting it off.

    I like the " linear programming" of the HSerSends. it makes it easy to read. But a coder would probably put all the variables into an array and then use a loop to send the array. Same for the receive.

    Any questions? please ask!

    GL
    73
    Mike

     
  • Anobium

    Anobium - 2016-02-25

    Also, this will not complile under v.0.95.006 (due out this weekend). These types of errors are not found in earlier releases of GCB - I am improving the pre-processor all the time.

    08:47:05 G+Stool-COMPILE/ASSEMBLE, processing C:\GCB@Syn\G+Stools\makeHEX.bat
    Source-File = C:\GCB@Syn\GreatCowBasic\Demos\code.gcb
    6.0 Sec. <<< WARNINGs / ERRORs while compiling!
    Doubleclick on errormessage below to go to sourcecode-line:
    first-start-sample.gcb (9): Error: Missing Function/Sub definition
    first-start-sample.gcb (114): Error: If-Else line has continous code - not permitted. Place code on next line

    and,

    first-start-sample.gcb (44): Error: Missing Function/Sub definition

    Line 41 and the same at line 127 - needs to be a End Sub. Whilst Return will work you may have issues with code using Exit Sub
    Line 114 - you need to put the code after the else on the next line. I think the 'set CRCGREEN = on' is as risk of not being compiled. I would correct rather than wade thru the ASM.

     
  • Edward LaBudde

    Edward LaBudde - 2016-02-25

    Thanks, Mmotte and Evan. Have made the changes. Regards, Ed.

     

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.