serin PICAXE 20X2

Sean87

Member
Hi,

I want to make a communication between my PC and 20X2. For this, I have connected the Serial In pin (which is already connected to the programming cable and works fine) to pin C.7 (is this ok?)

What I am looking for is using a terminal window send the word 'go' to the chip and if chip receives this word it should begin the work.

I came up with the following code, but by considering that I am a total noob, it is not working:
Code:
serin c.7,9600,("go")

if c.7 = "go" then goto main
 

Svejk

Senior Member
There are several issues with your code:

- serial in is not C.7, if you used this without the 22k/10k resistors setup then the chip may be damaged.
- serin waits for variables (it can be used with just qualifiers though)
- c.7 is a boolean, therefore it can hold only 0 and 1, not strings like "go"


A quick fix, apart from reading the manuals:

- to use serrxd instead of serin with the programmer cable
- issue a disconnect before serrxd and a reconnect after it

Sample code:

Code:
disconnect
serrxd ("go")
reconnect

'do something
 

Sean87

Member
I think I kind of killed my chip using this command. The setup was pretty simple the normal configruration for serial downloader cable (10k and 22k resistors) and it was working fine, as soon as I used the serrxd command and tried to send word "go" to the chip using terminal window, it stopped working and it is not programmable anymore.

I have another 20x2 that I replaced for the moment. What did I do wrong?
 

Sean87

Member
I managed to get it to work only one time, I wrote go in terminal and my LED turned on...But I cant reprogram the chip now, could it be Diconnect or Reconnect command? I am sure that the com port is open and accesible.
 
Last edited:

Svejk

Senior Member
You should be able to program using the hard reset procedure described in Manual #1, p. 47.

Several things could go wrong, please post your complete code and setup. Did you use Picaxe terminal?
 

Sean87

Member
You should be able to program using the hard reset procedure described in Manual #1, p. 47.

Several things could go wrong, please post your complete code and setup. Did you use Picaxe terminal?
Thanks, I manged to fix it.

I had to program it after I sent the "go" command using terminal...it is like the chip was blocking any other command ... which now I think it was the problem...chip was only waiting to get "go" command and in this condition it would not allow any communication even from the PICAXE Programmer.

my code was:

Code:
disconnect
serrxd ("go")
reconnect

high 1
 
Last edited:

Svejk

Senior Member
Maybe a beter test code would be:

Code:
#picaxe 20x2

do
  'wait for "go"
  disconnect
  serrxd ("go")
  reconnect

  'go received
  toggle 1
loop
So the picaxe will wait for "go" then toggle 1, restart waiting.
 

Sean87

Member
Yeah this is a better code.


Now what I want to do is storing a variable in memory of 20x2 and then do something based on the value.

My idea is something like this

Code:
disconnect
serrxd b0
if b0 = 1 then 
reconnect
high 1
endif
Can you fix this code please? Then I will get a nice idea of how this thing actually works.
 

ckoehn

Member
I think you want something like this...

Code:
#picaxe 20x2
#no_data
#no_table

main:
    'wait for a "go" followed by a byte, 48 = "0", 49 = "1" ...
    serrxd ("go"),b0    'a disconnect command is sent with a serrxd
    reconnect             'enable serial download again

    select b0              'check for command sent
        case "0"           ' "go0" was sent
            'do something

        case "1"           ' "go1" was sent
            'do something else

        else
            'nothing matched

    endselect
    goto main             'start over again
 

Sean87

Member
Thanks, exactly what I want to do.

However I am looking for something more complicated like this

Code:
main:
    'wait for a "go" followed by a byte, 48 = "0", 49 = "1" ...
    serrxd ("go"),b0    'a disconnect command is sent with a serrxd
    reconnect             'enable serial download again

    select b0              'check for command sent
        case "0"           ' "go0" was sent
            high 0
        case "1"
        	high 1
        case "2"
        	high 2
        else
        	high 0,1,2,3,4,5
        	pause 2000
        	low 0,1,2,3,4,5
    endselect
    
        	goto main
As you can see what I am trying to do is enbale/disable my LED's through keyboard. The code I wrote only turns 1 led on and then it stops somehow.
 

Sean87

Member
Ok, I fixed the problem and now I have a nice program which controls the LED's by PC(entering 0 to 5 will turn on the respective led on A.0 to A.5, Also 6 will do an LED dance :p and 7 will reset the program...Also each action will echo an string to the terminal).

Would be nice if you can check my code and tell me if anything can be optimized/improved etc..

Code:
main:
    low 0,1,2,3,4,5
    b1 = 0
    serrxd ("go")
	b1 = b1 + 1
    reconnect         
    gosub _read
    
_read:    

    if b1 = 1 then
    sertxd("PICAXE 20X2 Connected!")
    endif
    
    serrxd b0

    select b0   
    
        case "0"
      	  sertxd("LED 0 Toggled!")
        	toggle 0


        case "1"
      	  sertxd("LED 1 Toggled!") 
        	toggle 1
        	       	
        	
        case "2"
     	   sertxd("LED 2 Toggled!")   
        	toggle 2
        	     	
        	
        case "3"
     	   sertxd("LED 3 Toggled!")
        	toggle 3
        	  
        	
        case "4"
       	 sertxd("LED 4 Toggled!") 
        	toggle 4
        	 
        	
        case "5"
        	sertxd("LED 5 Toggled!")
        	toggle 5        	     
        	     	        	
        	
        case "6"
	     	sertxd("Dancing!") 
        	goto dance

        case "7"
	        sertxd("Reseting the program!")  
	        b1 = 0
        	goto main 
        	
        else
        	goto _read
        
    endselect
    
    goto _read

dance:
high 0,2,4
pause 50
low 0,2,4
pause 50
high 1,3,5
pause 50
low 1,3,5
high 0,2,4
pause 100
low 0,2,4
pause 100
high 1,3,5
pause 100
low 1,3,5
high 0,2,4
pause 200
low 0,2,4
pause 200
high 1,3,5
pause 200
low 1,3,5
goto _read
 

lbenson

Senior Member
There is no need to "gosub _Read" when "_Read:" immediately follows--just omit the "gosub" and you will fall into "_Read".

But whenever you do use "gosub", it has to be balanced with a return. If you "gosub _Read" in "main:" and within "_Read" do a "goto main", then eventually you will get a stack error and your program will die. If you "gosub" to "_Read" it must end with a "return"; if you fall into it or "goto _Read", then it must not.
 
Last edited:
Top