Problems with Serrxd on 18M2

Armand

New Member
Hi.
I am new to picaxe, and working on a project where the 18M2 is controlling a PWM fan depending on temperature. It's all good so far, but now I want to be able to send commands to the 18M2 to change the behavior of the fan.
My plan is to send a byte with two different qualifiers to adjust the MIN_temp and the MAX_temp and store them i EEPROM.
For now I am testing this code by sending and receiving using the picaxe terminal, storing the variables in b2 and b3, display them on my LCD and sending them to the terminal. It works, but I get a lot of errors. I have tried with and without the "add <CR>" in the terminal
Below is the output from a test.

DEDEDEDE3E3E3E3E3E3E3E3E383838383838383838383838383838ðððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð¥+DEðððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððððð¥+DEDED5D5D5D5

* First the program terminal receives the expected "DE" as the MIN and MAX is set to 68 and 69 (ASCII D and E)
* Then I enter MN3 in the terminal and we can see the MIN value changes to "3" on both my LCD and the terminal output.
* Then I enter MX8 in the terminal and we can see the MAX value changes to "8" on both my LCD and the terminal output.
* -THEN- when I enter MX9 to change the MAX value again to "9" everything goes tits up... we can see that a lot of "ð" characters are sent and it seems that the 18M2 resets because the next two values are are "DE"
* I try to send a "MN5" and now it works again, and I abort the program.

This happens in a random fashion. Sometimes i can enter many values before things go wrong. If I decrease the serrxd timeout to 100 it get the error almost all the time... Anyone who can shed some lights on what is going on?

Another thing is that I dont want to have long timeouts because that would slow down the program code too much. Any tip of how to solve that without using a digital input?

Here is my code:

Code:
symbol CHAR = b0
symbol INST = b1
symbol MIN_temp = b2
symbol MAX_temp = b3

Init:
MIN_temp = 68
MAX_temp = 69
let dirsc = %11000000
let dirsb = %01011111
gosub initLCD

Main:
pause 500
sertxd (MIN_temp,MAX_temp)	;Send value to PC
pause 50
disconnect
serrxd [1000],("MN"),MIN_temp	;Read MIN value from PC with MN qualifier
serrxd [1000],("MX"),MAX_temp	;Read MAX value from PC with MX qualifier
reconnect
CHAR = MIN_temp			;Put the MIN value into CHAR for display on LCD
gosub wrchr
CHAR = 32				;Put 32 (space) into CHAR for display on LCD
gosub wrchr
CHAR = MAX_temp
gosub wrchr
INST = 128				;Go to start of line 1 on LCD
gosub wrins
goto Main

;=======================================================================
;==== Subroutines ======================================================
;=======================================================================

;This code is standard initialization of a LCD display in 4-bit mode
initLCD:
low c.7
let pinsb = 0 		; Clear all output lines
pause 200 			; Wait 200 ms for LCD to reset.
let pinsb = 3 		; Set to 8-bit operation.
pulsout c.6,1 		; Send data by pulsing ;enable’
pause 10 			; Wait 10 ms
pulsout c.6,1 		; Send data again
pulsout c.6,1 		; Send data again
let pinsb = 2 		; Set to 4-bit operation.
pulsout c.6,1 		; Send data.
pulsout c.6,1 		; Send  data again.
let pinsb = 16 		; Set to two line operation
pulsout c.6,1		; Send data.
let INST = 12 		; Screen on, cursor off instruction
gosub wrins 		; Write instruction to LCD
return

;This code puts bits 0-7 out to the pins connected to DB4-DB7 on the LCD
;Note that we first send the four highest bits, and then the four lowest.
;bits 0-7 are the 8 bits in the first byte (b0) we have renamed to CHAR in our code.
wrchr:
high c.7 			; Make sure RS is high to set the LCD in character mode
let pinb.4 = bit7
let pinb.2 = bit6
let pinb.1 = bit5
let pinb.0 = bit4
pulsout c.6,1 		; Pulse the enable pin to send data.
let pinb.4 = bit3
let pinb.2 = bit2
let pinb.1 = bit1
let pinb.0 = bit0
pulsout c.6,1 		; Pulse the enable pin to send data.
return

;This code puts bits 8-15 out to the pins connected to DB4-DB7 on the LCD
;Note that we first send the four highest bits, and then the four lowest.
;bits 0-7 are the 8 bits in the first byte (b0) we have renamed to CHAR in our code.
wrins:
low c.7 			; Make sure RS is low to set the LCD in instruction mode
let pinb.4 = bit15
let pinb.2 = bit14
let pinb.1 = bit13
let pinb.0 = bit12
pulsout c.6,1 		; Pulse the enable pin to send data.
let pinb.4 = bit11
let pinb.2 = bit10
let pinb.1 = bit9
let pinb.0 = bit8
pulsout c.6,1 		; Pulse the enable pin to send data.
return
 

Jamster

Senior Member
as a guess i would say the picaxe terminal was interpereting it as Ascii

Add a # before variables

Jamster
 

Technical

Technical Support
Staff member
You can't 'reconnect' if keeping the computer serial port open with the terminal application, as the PICAXE will then keep resetting as it thinks a new download is occurring (and output this random data when it does so).

In this type of use you need one 'disconnect' at the start of your program, and then leave it disconnected. To then do a new PICAXE program download you will need to do a 'hard reset'.
 

hippy

Ex-Staff (retired)
Also ...

disconnect
serrxd [1000],("MN"),MIN_temp ;Read MIN value from PC with MN qualifier
serrxd [1000],("MX"),MAX_temp ;Read MAX value from PC with MX qualifier

You are setting yourself only a small window in which to receive the data, and if you send "MX" data while it's waiting for "MN" data it will ignore it, and vice versa.

A better option is something similar to ...

Do
SerRxd ("M"), b0, b1
Loop Until b0 = "N" or b0 = "X"
Select Case b0
Case "N" : MIN_Temp = b1
Case "X" : MAX_Temp = b1
End Select
 

Armand

New Member
SOLVED

Adding a # before the variables only makes them appear as their ASCII-number in the terminal. 68696869 etc. instead of DEDEDE etc... I tried, but still the same problem.

I tried to remove the timeout function of the serrxd and then everything is working as expected. :)
Code:
disconnect
serrxd ("MN"),MIN_temp	;Read MIN value from PC with MN qualifier
serrxd ("MX"),MAX_temp	;Read MAX value from PC with MX qualifier
reconnect
Before finishing this post I read the manual once more and a little more careful, and it all makes more sense now. From the manual:

Timeout is an optional variables/constants which sets the timeout period in
milliseconds (not available on M parts)
This option is not available!!! A bit strange that it's "kindof" working since I got my messages through sometimes. And changing the timeout value also had an effect :confused:
I guess I have to use serin then. This is a bit unfortunate because this fan controller is going to sit inside the computer and I wanted to have the option of not changing over the cable if I wanted to re-program the chip.
 

hippy

Ex-Staff (retired)
I tried to remove the timeout function of the serrxd and then everything is working as expected. :)
Possibly not. It will only work if you send MN data then MX data. If you send only MN data or send MX data first then it won't work.
 

Armand

New Member
Possibly not. It will only work if you send MN data then MX data. If you send only MN data or send MX data first then it won't work.
That's true... (I was composing my post while you guys wrote yours..)
I have now implemented both your tips and everything is OK :)
* Timeout DOES work on the 18M2 if I remove the "reconnect" command. Thanks Technical (IMHO I don't think the manual is clear on this issue)
* Your code Hippy is a good idea too. But without a timeout it would be stuck in the for loop.

I have now made a new code without the the "reconnect" which enables me to use the timeout function. I also used part of your code Hippy to do only ONE serrxd.
Thanks guys. I love this forum :) Now it's time to do some VB programming.
PS! I am the teacher for the guys with the fan project and I have to stay a little ahead of them... ;)

Code:
Main:
sertxd (MIN_temp,MAX_temp)	;Send value to PC
b6=0
SerRxd [100],("M"), b4, b5
Select Case b4
Case "N"
 MIN_Temp = b5
 CHAR = MIN_temp 	;Put the MIN value into CHAR for display on LCD
 gosub wrchr
Case "X"
 MAX_Temp = b5
 INST = 20		;move to the right to place max temp right of min temp on LCD
 GOSUB wrins	;
 CHAR = MAX_temp
 gosub wrchr
End Select 
INST = 128		;Go to start of line 1 on LCD
gosub wrins

goto Main
 
Last edited:

hippy

Ex-Staff (retired)
Be careful with timeouts - If the SERRXD timesout, then you send the data while the PICAXE is doing something else, and isn't yet back to the SERRXD, it won't receive what you sent.
 

Armand

New Member
Be careful with timeouts - If the SERRXD timesout, then you send the data while the PICAXE is doing something else, and isn't yet back to the SERRXD, it won't receive what you sent.
Yes, I am planning to make a program where you enter the MIN and MAX value and then send over the string when I push a button. On a button push, I will re-send the telegram enough times for the 18M2 to run a couple of full loops and also set the timeout value to double the time it takes to send a complete telegram. I hope that will do the trick.
Again, thanks. And I am sure I will be back with other questions later.
It was in fact one of my students who mentioned the Picaxe last autumn. We have been using Basic Stamp for a couple of years, but the cost is too high for the students to use the chip in their projects if they want to keep it. Programming Microchip PIC controllers is a bit difficult with all the registers and the programming interface. Picaxe is just as easy to program and faster than the BS, and with the price difference the choice is easy.
 
Top