Hsersetup & interrupt

techElder

Well-known member
Does anyone see what I'm doing incorrect here? PE6 Terminal is receiving the text within the DO/LOOP, and I have an LED on my AXE091 board showing the heartbeat of portB.

I can't seem to get an interrupt to function. It works in the simulator in another program. I copied into a new program to get the hardware comms working, but seem to have lost the interrupt function.

Code:
[color=Navy]#picaxe [/color][color=Black]40X2      [/color][color=Green]; Set the compiler mode, also the equivalent of #define xxx.[/color]
[color=Navy]#no_table         [/color][color=Green]; Do not download table or EEPROM data (X1 and X2 parts only).
                  ;     This automatically also enables #no_data'[/color]
[color=Navy]#slot 0           [/color][color=Green]; Select internal program slot (0-3) or i2c slot (4-7) on X2 parts.[/color]
[color=Navy]#define [/color][color=Black]CLOCK [/color][color=Blue]EM32 [/color][color=Green];SEE NOTE  ; EM must be there or runs on internal resonator[/color]
[color=Navy]#terminal 9600    [/color][color=Green]; off/300/600/1200/4800/9600/19200/38400
'[/color]
[color=Blue]SETFREQ EM32[/color]
[color=Purple]dirsB       [/color][color=DarkCyan]= [/color][color=Navy]%11111111                   [/color][color=Green]; all outputs PERF bus[/color]
[color=Blue]Hsersetup B9600_32[/color][color=Black], [/color][color=Navy]%111[/color]
[color=Blue]setintflags [/color][color=Navy]%00100000[/color][color=Black],[/color][color=Navy]%00100000  [/color][color=Green]'// interrupt on hserial receive   [/color]


[color=Black]Main:[/color]
[color=Blue]do                                             [/color][color=Green]; Main program loop
' Loop while waiting for stuff from the serial hardware interrupt
      [/color][color=Purple]pinsB [/color][color=DarkCyan]= [/color][color=Navy]%11111111 [/color][color=Green]; lamps ON
      [/color][color=Blue]pause [/color][color=Navy]2000 [/color][color=Green]; 1/2 second
      [/color][color=Purple]pinsB [/color][color=DarkCyan]= [/color][color=Navy]%00000000 [/color][color=Green]; lamps OFF
      [/color][color=Blue]pause [/color][color=Navy]2000 [/color][color=Green]; 1/2 second
      'sertxd ("This is something.", cr,lf)
      [/color][color=Blue]hserout [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"hserptr: "[/color][color=Black],#[/color][color=Purple]hserptr[/color][color=Black], [/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)[/color]
[color=Green]'      hserout 0, ("flags: ",#flags, cr,lf)[/color]
[color=Blue]loop                                           [/color][color=Green]; End of main program loop[/color]

[color=Blue]end[/color]

[color=Green]'---------------------------
[PLAIN]'------[ SUBROUTINES ]------[/PLAIN]
'---------------------------
'[/color]
[color=Blue]Interrupt: [/color][color=Green]' COMMAND
' 
      [/color][color=Blue]gosub [/color][color=Black]showSomething                       [/color][color=Green]; Go while interrupted, return here
 
      [/color][color=Purple]flags [/color][color=DarkCyan]= [/color][color=Navy]0                                 [/color][color=Green]; Clear the hserint flag
      [/color][color=Blue]setintflags [/color][color=Navy]%00100000[/color][color=Black],[/color][color=Navy]%00100000           [/color][color=Green]; re-enable interrupt on serial receive [/color]
[color=Blue]return                                          [/color][color=Green]; Back to main[/color]

[color=Black]showSomething:[/color]
[color=Green]'      sertxd (7, cr,lf)

'      sertxd ("Interrupted.", cr,lf)
      [/color][color=Blue]hserout [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"Interrupted..."[/color][color=Black], [/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)
return[/color]
hserConnection_2027.jpg
 
Last edited:

Goeytex

Senior Member
There doesn't seem to be a way for you to see the interrupt. Shouldn't you be using sertxd instead of hserout to display the "interrupt" in the terminal?
 

inglewoodpete

Senior Member
A couple of points.

The first is that, in principle, a well designed interrupt routine should execute and exit as fast as possible. No loops, pauses or 'slow' commands like SerTxd or SerOut.

And, in the case of a PICAXE, don't execute a "return" command before exiting the interrupt routine. Ie: No subroutine calls from within an interrupt routine.

Ultimately you can do anything you like within an interrupt routine, provided it works(!). The best solution is to do as little as possible within an interrupt routine. If you need to debug your code, use SerTxd ("$") rather then SerTxd ("An interrupt has occurred...", CR, LF)
 

techElder

Well-known member
Ok, a little more explanation that I should have included.

The interrupt is only to receive an hser packet. The packet is analyzed for data sent. The data sent is like a Remote Procedure Call and must be completed before more data/commands are accepted. So, no hurry is involved.

I wasn't aware that I couldn't call another subroutine from within the INTERRUPT subroutine. It works fine that way in the simulator. I understand why if one is in a hurry, so as not to miss another event.

Goey, the output is in the showSomething subroutine, unless I misunderstood what you are saying.

I've tested this for a long time in the simulator in a much more complicated fashion. I kinda thought this was cut-n-dry just to get the comm link verified. Then the interrupt didn't work.
 

techElder

Well-known member
I suppose I am assuming that something is coming back from PE6 Terminal to cause the interrupt in the first place. That could be my problem.

I'm using an AXE027 cable on the hser connection as well as the programming connection.
 

Goeytex

Senior Member
The first is that, in principle, a well designed interrupt routine should execute and exit as fast as possible. No loops, pauses or 'slow' commands like SerTxd or SerOut.

And, in the case of a PICAXE, don't execute a "return" command before exiting the interrupt routine. Ie: No subroutine calls from within an interrupt routine.
While these are common memes among many programers, they are many times not practical with a Picaxe and Picaxe Basic. I have abandoned the so-called "rules" with Picaxe due to the relatively long time it takes for the Picaxe to get to the interrupt routine and do anything. Setting a flag and getting out immediately to only return to the calling sub.... and then have to read the flag and do something else takes even more time. State Machines with a Picaxe can be unnecessarily slow. If speed of execution is important these old "rules" can cause missed data and poor performance, especially when using hser background receive.

I do agree however with the idea that you can do anything you like provided it works.
 

inglewoodpete

Senior Member
In order to debug what is going on, you could set ptr = 0 and have the main loop regularly dump (@ptrinc) the contents of the scratchpad via SerTxd to see if anything is being received. Note that SerTxd is bit-banged so disables interrupts while it is executing, so you need to output 1 character at a time.
 

Goeytex

Senior Member
I wasn't aware that I couldn't call another subroutine from within the INTERRUPT subroutine. It works fine that way in the simulator. I understand why if one is in a hurry, so as not to miss another event.
You CAN call another sub. IWP was just referring to what may be considered "proper" code.

Goey, the output is in the showSomething subroutine, unless I misunderstood what you are saying.
You have sertxd remarked out. Unless hserout is connected to a display, or a scope, I didn't see how you could tell if an interrupt occurred. Remarks will help. Or maybe put an LED on the board and add code in the interrupt routine to blink the LED.

When testing hser background , I like to add a short pause at the beginning of the interrupt routine to make sure that all bytes have been received if there are more than just one or two.

As IWP said, you can use sertxd (#hserptr,cr,lf) anywhere in the code to tell if data has been received into the buffer ( scratchpad). IF hserptr does not increment, no data is being received and there will be no interrupt.
 
Last edited:

techElder

Well-known member
Yeah, I should also have stated that I am monitoring the hser port with PE6 Terminal. Again, this isn't my final code ... just test code. Sorry for not commenting the lines.
 

hippy

Technical Support
Staff member
Everything looks like it should work to me. The most obvious cause of not seeing the output within the interrupt would be that it wasn't being executed, and most likely because nothing was being received.

The best way to develop code is to trim it back to the bare minimum, run at default speeds, test a little at a time, test parts separately, and build up from what has been proven to work. For background serial, check serial is being received before expecting serial received interrupts to work.
 

techElder

Well-known member
Right. I assumed since Terminal was RECEIVING the transmission from my project, that when I typed into the "Transmit Buffer" box and click "Send", that I should be transmitting a character for hser to pick up.

Seems like a reasonable assumption.

I don't have any movement in hserptr, so I'm suspicious but at a loss.

Why should I have to dig out a scope to know if Terminal is transmitting what I type when it is receiving what my project sends? Hmmmmm ....

PS. I also tried removing the program cable (for possible interference) and the results didn't change.
 

techElder

Well-known member
Problem Found

I place great stock in every word you guys say ...

So, when looking at why it doesn't seem to receive from the Terminal ...

Wire in wrong hole.

Works fine.

Thanks for helping me work through this.
 
Top