Simple(?) 08M2 input problem

Hello ever helpful experts. I'm totally baffled by what should be a really simple circuit/software using a centre-off toggle switch to select up in one direction and down in the other. Basically, the 'Down' direction of the switch works fine but not the 'Up', using the serial-in pin, C.5.
HT 08M2 circuit.gif
This is the relevant bit of the circuit and (being short on pins as ever because I like the 08M2 and have lots of them) I've had to use the C.5-Serial In pin as one of my inputs. I discovered some time ago that it's vital to use DISCONNECT in the code when doing this but it still doesn't work.

These are the relevant bits from the code:
Code:
; Version 1 using a PICAXE 8M2+ on an AXE092 board.
; Displaying on a 2-line x 16 chr. LCD.
;
#picaxe 08M2
PAUSE 2000
DISCONNECT
#NO_DATA
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Outputs
SYMBOL LCDout = C.0
; C.1 - I2C SCL to RTC board with DS3231 & 24C32 memory.
; C.2 - I2C SDA to RTC board with DS3231 & 24C32 memory.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Inputs
SYMBOL tdown = pinC.3
SYMBOL tempsensor = C.4
SYMBOL tup   = pinC.5
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main:
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    IF tup = 1 THEN
    SEROUT LCDout,N2400,(254,143,"U")       ; "U" at end of line one.
    ELSE
    SEROUT LCDout,N2400,(254,143," ")       ; " " at end of line one.
    ENDIF
    IF tdown = 1 THEN
    SEROUT LCDout,N2400,(254,143,"D")       ; "D" at end of line one.
    ELSE
    SEROUT LCDout,N2400,(254,143," ")       ; " " at end of line one.
    ENDIF
    
GOTO main

END
And to make sure I wasn't doing anything silly, I tried a simulation (v.6.0.6.2 running in Windows XP Pro) with this code:
Code:
; Version 1 using a PICAXE 8M2+ on an AXE092 board.
; Displaying on a 2-line x 16 chr. LCD.
;
#picaxe 08M2
PAUSE 2000
DISCONNECT
#NO_DATA
; 640/2048 bytes
; Last save date 29/11/2020
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Outputs
SYMBOL LCDout = C.0
; C.1 - I2C SCL to RTC board with DS3231 & 24C32 memory.
; C.2 - I2C SDA to RTC board with DS3231 & 24C32 memory.
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
; Inputs
SYMBOL tdown = pinC.3
SYMBOL tup   = pinC.5

main:
    IF tup = 1 THEN
    sertxd("Up",cr,lf)
;    SEROUT LCDout,N2400,(254,143,"U")       ; 
    ELSE
    sertxd("Not up",cr,lf)
;    SEROUT LCDout,N2400,(254,143," ")       ; 
    ENDIF
    IF tdown = 1 THEN
    sertxd("Down",cr,lf)
;    SEROUT LCDout,N2400,(254,143,"D")       ; 
    ELSE
    sertxd("Not down",cr,lf)
;    SEROUT LCDout,N2400,(254,143," ")       ; 
    ENDIF
    
GOTO main
and of course it runs perfectly doing exactly what I expected the live code to do.

I assumed that maybe the hardware was at fault but putting a meter on pin 2 confirms that it's getting a logic high when the switch is up and low otherwise.

Having ruled out a hardware problem I assume that it's my software but I'd be most grateful if someone could please point out what I'm doing wrong?

Thanks.
 

Technical

Technical Support
Staff member
If tup = 1 then you print a 'U' and then immediately print a space in the same place via the next else if, probably not what you intended.
 

PhilHornby

Senior Member
Just looking back at some earlier code I wrote, I note the following comment I left myself: "NOTE: The Picaxe will not boot with SERIN held high"

Do you get the same effect? - i.e. setting switch to "Target UP" stops it booting. This might offer a clue as to what the Picaxe is seeing...

Otherwise- all I can think of, is the mild confusion around the SEROUT, SERTXD and the I²C LCD.

As @Technical intimates, it might actual be working - just not displaying the expected output.
 
If tup = 1 then you print a 'U' and then immediately print a space in the same place via the next else if, probably not what you intended.
Thanks for your reply. It is exactly what I intended. With the other direction of the switch, it repeatedly shows 'D' while the switch is held in that direction and is blanked as soon as I release it, but nothing happens in the 'up' direction.
 
Just looking back at some earlier code I wrote, I note the following comment I left myself: "NOTE: The Picaxe will not boot with SERIN held high"

Do you get the same effect? - i.e. setting switch to "Target UP" stops it booting. This might offer a clue as to what the Picaxe is seeing...

Otherwise- all I can think of, is the mild confusion around the SEROUT, SERTXD and the I²C LCD.

As @Technical intimates, it might actual be working - just not displaying the expected output.
Thanks also.

A quick and easy sanity (and hardware) check which does indeed prevent the 08M2 booting when the switch is held in the 'up' direction. So it does confirm that the hardware is working as designed.

I can't see any confusion with the SEROUT or SERTXD. Obviously I can't use SERTXD with the actual hardware as both serial lines are in use and can't be used for DEBUG.
 
Last edited:

cpedw

Senior Member
Thanks for your reply. It is exactly what I intended. With the other direction of the switch, it repeatedly shows 'D' while the switch is held in that direction and is blanked as soon as I release it, but nothing happens in the 'up' direction.
Something happens (probably!): WIth switch at Up, U is printed by the first IF statement then the second IF immediately causes Blank to be printed where the U was placed. The U disappears and it all happens so quickly that you don't see it. I think you need an IF... ELSEIF structure e.g.

IF tup=1 THEN
write "U"
ELSEIF tdown=1 THEN
write "D"
ELSE
write " "
ENDIF
 
Something happens (probably!): WIth switch at Up, U is printed by the first IF statement then the second IF immediately causes Blank to be printed where the U was placed. The U disappears and it all happens so quickly that you don't see it. I think you need an IF... ELSEIF structure e.g.

IF tup=1 THEN
write "U"
ELSEIF tdown=1 THEN
write "D"
ELSE
write " "
ENDIF
DOH!

Yes of course it does - many thanks for that and the helpful code and apologies to 'Technical' if that's what you were saying but I didn't understand.

What a great forum this is. Thanks everyone.
 

AllyCat

Senior Member
Hi,

Yes, the simulation should have been a "clue" because when the switch was "up" the display would (or should) have been :
Code:
Up   <cr,lf>
Not down  <cr,lf>
You could avoid the problem entirely by applying three levels (High, Low and "High Impedance") to the c.3 pin, as I described in paragraph 3 of post #26 in this recent thread.

But if you want to persevere with the SerIn pin, then I would at least put a resistor in series with the switch. As shown at present, if the switch is "Up" then the power supply is applied directly to the programming input, which might cause damage if/when a programming adapter is plugged in. Or simply connect the switch terminal directly to the PICaxe's input pin, since that pin can "never" become an output.

Cheers, Alan.
 
You could avoid the problem entirely by applying three levels (High, Low and "High Impedance") to the c.3 pin, as I described in paragraph 3 of post #26 in this recent thread.

But if you want to persevere with the SerIn pin, then I would at least put a resistor in series with the switch. As shown at present, if the switch is "Up" then the power supply is applied directly to the programming input, which might cause damage if/when a programming adapter is plugged in. Or simply connect the switch terminal directly to the PICaxe's input pin, since that pin can "never" become an output.

Cheers, Alan.
Thanks very much Alan. I especially like the 3-level approach, it's very elegant.

I've done something similar in the past when I had three float switches with on/off positions but didn't want to tie up three input pins, but by means of varying sized resistors converted the various water levels to different voltages and used the ADC on one pin to read all three switch positions.

Now that I've got it working by simply changing where I write the "U":
Code:
IF tup = 1 THEN
    SEROUT LCDout,N2400,(254,142,"U")       ; "U" near end of line one.
    ELSE
    SEROUT LCDout,N2400,(254,142," ")       ; " " near end of line one.
    ENDIF
    IF tdown = 1 THEN
    SEROUT LCDout,N2400,(254,143,"D")       ; "D" at end of line one.
    ELSE
    SEROUT LCDout,N2400,(254,143," ")       ; " " at end of line one.
    ENDIF
I'll probably keep it simple and use both pins rather than the slightly more confusing software option of turning the PULLUP on and off.

Yes, I was aware that using the switch while the programming lead was attached was probably not a good idea and it looks simple to move the connection to the switch to the other side of the 22k resistor so I'll do that for safety's sake.

Thanks again to all for the timely help.
 
Top