Hi,
I went quickly through your code.
What you can read here is based on what I understand from
the PicBasic Pro manual and the 16F877 datasheet.
(Not tested with real hardware and software).
==================================
DEFINE HSER_CLROERR 1
If you use "DEFINE HSER_CLROERR 1", PicBasic Pro
will generate the code that in background will take
care of the FIFO RX buffer overrun error. When you
send the AT commands with HSEROUT, the modem will
respond after each single AT command with "OK".
(In your case "0" + CR because of the use of "ATV0").
So while you send the second or third AT command, the
response of the modem to the first or second AT command
will cause an overrun error and therefore the background
code will be executed, which could be the cause of the problem.
What happen with your code:
(From the file you have previously attached).
i=rcreg:i=rcreg
Here you get (clear) the two bytes from the FIFO buffer.
HSerout ["ATV0",13]'Set Numeric Response
The modem sends back 2 bytes. (Ascii 48 and 13).
HSerin 250,no_modem,[i] 'Wait for 0 "zero" (OK)
You read the first byte (Ascii 48) of the two bytes
waiting in the FIFO but one byte will remain in the FIFO.
Hserout ["ats0=2",13,10]
pause 500
The modem sends back the 2 bytes response (Ascii 48 and 13)
and while the UART receives the second byte the overrun error
will occur. (One byte was already there before sending the AT
command so 1+2=3 and with 3 you get the overrun error).
The background code is executed to clear the error. At this
point the FIFO is full.(Two bytes).
hserout ["ATX3",13,10]
pause 500
The modem sends back the first byte of the two bytes response,
(Ascii 48 and 13) and while the UART receives this first byte
the overrun error will occur.
(Two bytes were already there before sending the AT command so
2+1=3 and with 3 you get the overrun error). The background
code is executed to clear the error. After the overrun error
is completed, the execution of your program will resume with the
next statement which is Hserout ["AT&K0",13,10]. The execution
will be interrupted by the overrun error caused by the second byte
of the previous modem response.
Solution 1:
Keep "DEFINE HSER_CLROERR 1" but use a longer pause
between the single AT commands and read the modem
response from the double buffered register RCREG.
This will ensure that the two bytes RX FIFO buffer is
empty before you send the next AT command.
Solution 2:
Remove "DEFINE HSER_CLROERR 1".
In order to be able to receive data in the UART you
will have first to manually clear the overrun error with:
RCSTA.4 = 0
RCSTA.4 = 1
================================
* * *
================================
Also a possible improvement:
Remove this line from your code.
DEFINE HSER_BAUD 9600
(Replaced by the 3 lines below).
Make sure these 3 lines are in your code.
DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRG 129 ' 9600 Bauds, error 0.16%
================================
* * *
================================
Small format errors:
SUB "continue"
pause 15000: (remove the colon : at the end of the line)
SUB "com_device"
i=rcreg:i=rcreg: (remove the last colon : at the end of the line)
================================
* * *
================================
Also a possible improvement:
In your code I see that you use everywhere only CR
after each AT command. Why do you use CR + LF now?
Is ATZ the right AT command?
ATZ means to reset the modem to the settings stored
by the user. You usually have at least one set of
registers for the user to customize their modem with.
AT&F means to reset the modem to the factory default settings.
A few modems take a moment to finish doing an AT&F and
may ignore the next thing sent to them, though not many.
Many will take quite a while to complete an ATZ, and will
ignore the next things sent to them. You really have to
wait for an OKAY response from the modem, before
doing anything after an ATZ or AT&F.
================================
* * *
Best regards,
Luciano
Bookmarks