PDA

View Full Version : Hersin quick question



andybarrett1
- 25th September 2020, 07:40
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

aerostar
- 25th September 2020, 11:28
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.

andybarrett1
- 26th September 2020, 18:01
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

HenrikOlsson
- 26th September 2020, 18:15
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:


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

andybarrett1
- 27th September 2020, 16:38
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

HenrikOlsson
- 27th September 2020, 16:53
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.

andybarrett1
- 27th September 2020, 17:06
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

HenrikOlsson
- 27th September 2020, 17:17
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:


Main:
HSERIN 5000, timeout,[char]
HSEROUT[char]
Goto main

Timeout:
HSEROUT["Nope, not this time. Lets try again",13]
Goto Main

EDIT: Sorry, didn't realise serial was over the BT...I guess data IS comming to the correct pin then.

andybarrett1
- 27th September 2020, 17:23
Hiya
I wondered about the 9615 But manual says is should be that rather than 9600 🤔

Will try the short program when I get the chance and let you know

Thank you for help

Good to know what I am doing should work

Yes processor and TX RX are right
With a dropper circuit for the 3v3 levels

Not scoped yet but good call 👍

Andy

HenrikOlsson
- 27th September 2020, 18:27
Hi Andy,
Which PIC are you targeting (wondering if it's got an USART or EUSART)?
My guess is that there's something weird going on with the baudrate.
If possible, replace the Bluetooth connection with cable or, at the very least, verify with scope/logic analyzer that the data you send gets to the PIC and does ineed do so at the expected 9600 baud.

andybarrett1
- 27th September 2020, 19:10
Sorry it is a 16f628A

Will check in the next few days

Thank you for all help so far

Andy

tumbleweed
- 27th September 2020, 23:53
TRISB=%00000001 'Make RB0 input


For the 16F628A you're setting the RX input (RB1) to output mode.
The datasheet says to set both bits TRISB.2 (RB2/TX) and TRISB.1 (RB1/RX).

andybarrett1
- 29th September 2020, 09:21
Hi All

OK I now have set :-

TRISB=%00000111 As per Data sheet ...... Really thought that would cure my issue


Stripped code back to :-

Main:
HSERIN 5000, timeout,[char]
HSEROUT[char]
Goto main

Timeout:
HSEROUT["Back to loop"]
Goto Main

Connecting Hyperterminal via a Max232 .....

Still nothing ... Hyperterminal is outputting as if i link tx/rx on 5v side I see the echo

Tried different 16f628A...

I am suspecting a config issue ..... As I can output directly to Hyperterminal OK :-


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=%00000111 'Make RB0 input and set RX/TX
PORTA=%00000000 'Port A all low
PORTB=%00000000 'Port B all low

CMCON=7

HSEROUT["This is a test"] ' Works fine




Help..... :-)

Andy

andybarrett1
- 1st October 2020, 10:02
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

richard
- 1st October 2020, 11:52
its not in the code you have posted
this code as suggested by henrik works as expected



#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

andybarrett1
- 4th October 2020, 19:54
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