Hersin quick question


Closed Thread
Results 1 to 16 of 16

Hybrid View

  1. #1
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237

    Default Hersin quick question

    Hi Thank you for reading

    I am using:-

    HSERIN 5000, Timeout, [char]
    IF (char="A") THEN
    GOSUB somewhere
    ENDIF

    Just to check a char and turn on an output (via gosub) if that char is same as recieved ..... In my case "A"

    Should this code work ok?.... Am getting intermittent results that are little hit and miss ?

    All usual Defines are in place thank you for reading

    BR
    Andy
    Last edited by andybarrett1; - 25th September 2020 at 08:55.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: Hersin quick question

    Change the gosub to a goto, as it stands the gosub code will continue on return to the endif and code that follows it, not what you want I think.

  3. #3
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: Hersin quick question

    Hi Thank you for reply

    I think I am missing something in my code... I am not a software engineer at all :-)

    All I am looking to do is read the serial port Looking for a letter "A" No speech marks

    Then do something when seen ??

    Should be easy ??

    LoL

    Andy

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,518


    Did you find this post helpful? Yes | No

    Default Re: Hersin quick question

    I see noting really wrong with what's actually posted but as is usually the case not all the code is posted. Here's an untested example of what I think you're looking for:
    Code:
    Main:
      HSERIN 5000, Timeout, [char]
      IF (char="A") THEN
        GOSUB somewhere
      ENDIF
    Goto Main
    
    Somewhere:
       HSEROUT["I got an A", 13]
    RETURN
    
    Timeout:
       HSEROUT["Nothing for the last 5 seconds, I'll just restart and wait 5 more....",13]
    Goto Main

  5. #5
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Angry Re: Hersin quick question

    Hi Henrik

    That is exactly what I have got .... Really don't know what I am missing ??

    Help very welcome .... I don't do software so go easy on me !

    Andy



    Here is whole code :-

    DEFINE OSC 8
    DEFINE HSER_TXSTA 24H
    DEFINE HSER_RCSTA 90H
    DEFINE HSER_BAUD 9615
    DEFINE HSER_CLROERR 1

    '76543210
    TRISA=%00000010 'Make RA1 input
    TRISB=%00000001 'Make RB0 input
    PORTA=%00000000 'Port A all low
    PORTB=%00000000 'Port B all low

    CMCON=7

    ' Alias pins

    i var byte 'Dummy
    SDA Var PORTB.3 'SDA Line
    SCL Var PORTB.4 'SCL Line
    Led var PORTB.6 'White Light
    BT var PORTA.0 'BlueTooth Enable
    ir var PORTB.0 'Remote Input
    time var word 'Real time on
    realtrig var word 'Real IR Trigger
    char var byte 'Serial in Char
    state var PORTA.1 'State Pin High

    ' Allocate variables
    RTCYear Var Byte
    RTCMonth Var Byte
    RTCDate Var Byte
    RTCDay Var Byte
    RTCHour Var Byte
    RTCMin Var Byte
    RTCSec Var Byte
    RTCCtrl Var Byte

    'Set initial time
    RTCYear = $20
    RTCMonth = $09
    RTCDate = $26
    RTCDay = $07
    RTCHour = $16
    RTCMin = $57
    RTCSec = 1
    RTCCtrl = 0
    'Gosub settime ' Set the time
    Goto start ' Skip over subroutines

    ' Subroutine to write time to RTC

    'settime: I2CWrite SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
    ' Return

    'Subroutine to read time from RTC

    gettime: I2CRead SDA, SCL, $D0, $00, [RTCSec, RTCMin, RTCHour, RTCDay, RTCDate, RTCMonth, RTCYear, RTCCtrl]
    Return

    'Main program loop

    start: high BT 'Turn BlueTooth on

    mainloop: Gosub gettime ' Read the time from the RTC

    pause 100 ' Stabilise

    IF (RTCHour=$19) and (RTCMin=$00) Then
    gosub Led_On ' Led on for a bit
    endif

    pause 100 ' Stabilise

    IF (RTCMin=$00 and RTCsec=$01) or (RTCMin=$15 and RTCsec=$01) or (RTCMin=$30 and RTCsec=$01) or (RTCMin=$45 and RTCsec=$01)Then
    gosub RST_BT
    endif

    pause 100 ' Stabilise

    if (ir=0) then 'if button press
    High PORTB.5
    High PORTB.7
    Pause 10000
    Low PORTB.5
    Low PORTB.7
    'gosub Seconds 'then on 88 Seconds
    endif

    pause 100 ' Stabilise

    if (state=1) then 'if state high
    gosub connected 'then gosub connected
    endif

    Pause 100 ' Stabilise
    Goto mainloop ' Do it forever and ever and ever

    'Light control routines

    Led_On: for time = 1 to 120 'On for 2 Hours
    high led
    pause 65000
    next time
    low led
    return

    Seconds: pause 500 'Pause for 1/2 seconds
    high led
    pause 60000 '60 Seconds
    High led
    pause 28000 ' 28 Seconds (So 60 plus 28 = 88Seconds
    low led
    Pause 500 'delay 1/2 seconds
    return


    connected:
    hserin 5000, timeout,[char]
    if char = "A" then
    gosub Led_On ' Led on for a bit
    endif
    goto connected

    RST_BT: low bt 'Turn BlueTooth off
    pause 1000 'For 1 second
    high bt 'Reset
    return

    Timeout: hserout ["Back to loop..."]
    goto mainloop

    End

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,518


    Did you find this post helpful? Yes | No

    Default Re: Hersin quick question

    Hi Andy,
    Intermittent results you say :-)

    It sits there waiting for a character for 5 seconds, if none is received it goes to mainloop where you spend "considerable" time before you eventually get back the connected subroutine where you again check for an "A". Now, if you happen to send an "A" during the time that HSERIN isn't actually waiting for one it will be missed.

    Might be what's happening.

    Start by removing all the PAUSE statements you have within your main loop and see if it gets "better". Even so, there will be times where HSERIN isn't executing when you send that "A". An interrupt would be a good option here but you could possibly get away with simply polling the receive buffer, if you stick to sending it a single character.

    /Henrik.

  7. #7
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: Hersin quick question

    Well

    Out of desperation.... Swapped to using Serin as opposed to Hserin.

    All is working as I thought... So pins are right and drivers etc etc

    Can only be Config ...(I Think)

    But what and where ?

    Andy

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


    Did you find this post helpful? Yes | No

    Default Re: Hersin quick question

    its not in the code you have posted
    this code as suggested by henrik works as expected

    Code:
    #CONFIG
      __config   _HS_OSC & _WDT_ON & _PWRTE_OFF & _MCLRE_ON & _BODEN_ON & _LVP_OFF & DATA_CP_OFF & _CP_OFF
    #ENDCONFIG
    
    
    DEFINE OSC 8
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 12  ' 9600 Baud @ 8MHz, 0.16%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    TRISB=%11111111 'Make RB0 input
    char var byte
    
    CMCON=7
    
    
    Main:
      HSERIN 5000, Timeout, [char]
      IF (char="A") THEN
        GOSUB somewhere
      ENDIF
    Goto Main
    
    Somewhere:
       HSEROUT["I got an A", 13]
    RETURN
    
    Timeout:
       HSEROUT["Nothing for the last 5 seconds, I'll just restart and wait 5 more....",13]
    Goto Main
    Warning I'm not a teacher

  9. #9
    Join Date
    May 2012
    Location
    Merseyside, UK
    Posts
    237


    Did you find this post helpful? Yes | No

    Default Re: Hersin quick question

    Hi all

    Have opted for easy way out

    Using Serin no problems ....I still don't know what I was getting wrong

    Thank you for all the help

    Andy

Similar Threads

  1. quick question
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 13th November 2016, 20:02
  2. A quick question.....
    By Megahertz in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th December 2010, 15:25
  3. Quick DS18B20 question
    By gti_uk in forum General
    Replies: 4
    Last Post: - 21st June 2009, 18:24
  4. quick question
    By Srigopal007 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd December 2004, 02:45

Members who have read this thread : 1

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

Tags for this Thread

Posting Permissions

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