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.
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.
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
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
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
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.
Hiya
Well intermittent read as nothing at all .
Clock works ok ....The IR works ok as do the LEDs ....Proves processor is running
The pauses were put in as a desperate attempt to see something ....
The Bluetooth is working as I have had a terminal monitor on both TX and RX ....again all good 😁.
Hserout working fine as I can see Timeout message
Andy
Which device and out of curiousity, what's up with the slightly odd baudrate?
Well nothing and intermittent isn't quite the same :-)
Are you SURE the serial data is actually getting to the correct pin on the PIC? Have you checked? Scope, logic analyzer?
Write up a simple program to make sure you have a working serial connection both ways, something like:EDIT: Sorry, didn't realise serial was over the BT...I guess data IS comming to the correct pin then.Code:Main: HSERIN 5000, timeout,[char] HSEROUT[char] Goto main Timeout: HSEROUT["Nope, not this time. Lets try again",13] Goto Main
Bookmarks