Tap Tempo on M2

Hugh_Jorifice

New Member
I'm trying to work out how to do tap tempo on the M2 chip. Basically I need to time the gap between two or more (usually more) presses of a footswitch and then work out the average reading in milliseconds. I have this working very well on a 28X2, using settimer and preloading the minor tick variable to give me a major tick and increment the timer variable every millisecond.
The problem is that I need to get this working on a 08M2 chip (for many reasons including board space and data table size). I can't find an equivalent function on the M2 chip (just the time variable, which increments ever second or half second depending on clock). Is there a wizard's way of accessing the timer register to preload it like you can on the X2 and getting a faster timer tick?
Sorry if I have missed this on another post and thanks in advance for any help.
 

premelec

Senior Member
Have a look at PULSIN command - and you can change ranges by SETFREQ - not sure I understand your device... :) I don't have any minor or major ticks in my yard just now to try out :)
 

hippy

Technical Support
Staff member
When it works on the X2, doesn't on an M2, or seems it would require a lot of effort to achieve the same result and to match what already works; my first suggestion would be to reassess moving from X2 to M2, reassess if size constraints can be worked round or if using an X2 plus M2 would solve the problem.

Hammering an X2 flat, cutting off unused legs, wrapping in insulating tape with flying wires from the pins used on the chip to the circuit, is one potential solution for getting a chip into an inconveniently small space. If you can get it working on a 20X2 you could even cut some of the body off, get it down to 14-pin or less perhaps.
 

Hugh_Jorifice

New Member
Thanks for the replies, pulsin would be good but I need to time intervals of up to one second and pulsin will only do 655 ms. I guess I'll just have to shoehorn the 20X2 and make do with a smaller data table.
 

hippy

Technical Support
Staff member
I guess I'll just have to shoehorn the 20X2 and make do with a smaller data table.
It's not clear exactly what you mean by data table. All PICAXE M2, X1 and X2 have 256 bytes of internal data EEPROM. While 14M2, 18M2 and 20M2 have TABLE storage, the 08M2 does not support that.
 

Hugh_Jorifice

New Member
Yes my mistake, I would like to use a lookup table larger than the 255 locations supported by X2 and was reading from the manual, TABLE command: "M2 parts have 512 locations (0-511). These are separate to the 2048 bytes of program memory, so do not affect program length.".
I just didn't spot that the 08M2 is not included!
 

premelec

Senior Member
You can set frequency [SETFREQ] much lower to get much longer pulsin time reading on M2 parts...
 

Hugh_Jorifice

New Member
Yay, didn't spot that one either! Looks like I'll use eeprom memory for my data table & I'm away (only 256 locations but I'll have to make do). Thanks to you both.
 

Goeytex

Senior Member
Here is something I did a while back to measure the time that a switch was pressed. It uses PWM to generate a 1000 Hz clock signal. The PWM is fed back to an input pin where Pulsin is used as a simple counter. The Pulsin value is irrelevant as we are only interested in the number of pulses where each pulse represents 1 ms. It is accurate to within + - 2 ms. 2 seconds is no problem and there is no need to slow the Picaxe down.

This could possibly be modified to meet your needs. Give it a try.

Code:
[color=Navy]#Picaxe [/color][color=Black]08M2[/color]
[color=Navy]#no_data
#terminal 19200[/color]

[color=Blue]Setfreq m16 [/color][color=Green]'// Necessary for this code[/color]
[color=Blue]Pause [/color][color=Navy]1000  [/color][color=Green]'// Allow Time for Terminal to open [/color]
[color=Blue]Symbol [/color][color=Purple]footswitch [/color][color=DarkCyan]= [/color][color=Purple]PinC.4[/color]
[color=Blue]symbol countpin [/color][color=DarkCyan]= [/color][color=Blue]C.3

Symbol Pressed [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]Symbol Released [/color][color=DarkCyan]= [/color][color=Navy]1[/color]

[color=Blue]Symbol [/color][color=Purple]milliseconds [/color][color=DarkCyan]= [/color][color=Purple]W1[/color]
[color=Blue]symbol [/color][color=Purple]dummy [/color][color=DarkCyan]= [/color][color=Purple]W10[/color]

[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.2[/color][color=Black], [/color][color=Navy]249[/color][color=Black], [/color][color=Navy]99  [/color][color=Green]'//1000 Hz - 10 Percent Duty[/color]

[color=Black]Main:[/color]
[color=Green]'// Foot Switch on Pin C.4 
'// Wait for switch press[/color]
[color=Blue]do while [/color][color=Purple]footswitch [/color][color=DarkCyan]= [/color][color=Blue]released 
loop  

pause [/color][color=Navy]20 [/color][color=Green]'// 5 ms debounce  [/color]

[color=Blue]do while [/color][color=Purple]footswitch [/color][color=DarkCyan]= [/color][color=Blue]pressed  
   Pulsin countpin[/color][color=Black], [/color][color=Navy]1[/color][color=Black], [/color][color=Purple]dummy
   [/color][color=Blue]inc [/color][color=Purple]milliseconds  [/color][color=Green]'//increment the ms count for each pulse[/color]
[color=Blue]loop   [/color]

[color=Purple]w1 [/color][color=DarkCyan]= [/color][color=Purple]W1 [/color][color=DarkCyan]+ [/color][color=Navy]5  [/color][color=Green]'//compensate for debounce time [/color]
[color=Blue]sertxd ([/color][color=Red]"Time Pressed = "[/color][color=Black],#[/color][color=Purple]W1[/color][color=Black], [/color][color=Red]" ms"[/color][color=Black],[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)[/color]

[color=Purple]w1 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Green]'//clear the variable [/color]
[color=Blue]pause [/color][color=Navy]1000[/color]
[color=Blue]goto [/color][color=Black]main[/color]

[color=Blue]END[/color]
 

Attachments

Hugh_Jorifice

New Member
Wow that is really neat, I wouldn't have thought of something like that. Certainly worth a try I think, thank you.
Here is something I did a while back to measure the time that a switch was pressed. It uses PWM to generate a 1000 Hz clock signal. The PWM is fed back to an input pin where Pulsin is used as a simple counter. The Pulsin value is irrelevant as we are only interested in the number of pulses where each pulse represents 1 ms. It is accurate to within + - 2 ms. 2 seconds is no problem and there is no need to slow the Picaxe down.

This could possibly be modified to meet your needs. Give it a try.

Code:
[color=Navy]#Picaxe [/color][color=Black]08M2[/color]
[color=Navy]#no_data
#terminal 19200[/color]

[color=Blue]Setfreq m16 [/color][color=Green]'// Necessary for this code[/color]
[color=Blue]Pause [/color][color=Navy]1000  [/color][color=Green]'// Allow Time for Terminal to open [/color]
[color=Blue]Symbol [/color][color=Purple]footswitch [/color][color=DarkCyan]= [/color][color=Purple]PinC.4[/color]
[color=Blue]symbol countpin [/color][color=DarkCyan]= [/color][color=Blue]C.3

Symbol Pressed [/color][color=DarkCyan]= [/color][color=Navy]0[/color]
[color=Blue]Symbol Released [/color][color=DarkCyan]= [/color][color=Navy]1[/color]

[color=Blue]Symbol [/color][color=Purple]milliseconds [/color][color=DarkCyan]= [/color][color=Purple]W1[/color]
[color=Blue]symbol [/color][color=Purple]dummy [/color][color=DarkCyan]= [/color][color=Purple]W10[/color]

[color=Blue]pwmout pwmdiv16[/color][color=Black], [/color][color=Blue]C.2[/color][color=Black], [/color][color=Navy]249[/color][color=Black], [/color][color=Navy]99  [/color][color=Green]'//1000 Hz - 10 Percent Duty[/color]

[color=Black]Main:[/color]
[color=Green]'// Foot Switch on Pin C.4 
'// Wait for switch press[/color]
[color=Blue]do while [/color][color=Purple]footswitch [/color][color=DarkCyan]= [/color][color=Blue]released 
loop  

pause [/color][color=Navy]20 [/color][color=Green]'// 5 ms debounce  [/color]

[color=Blue]do while [/color][color=Purple]footswitch [/color][color=DarkCyan]= [/color][color=Blue]pressed  
   Pulsin countpin[/color][color=Black], [/color][color=Navy]1[/color][color=Black], [/color][color=Purple]dummy
   [/color][color=Blue]inc [/color][color=Purple]milliseconds  [/color][color=Green]'//increment the ms count for each pulse[/color]
[color=Blue]loop   [/color]

[color=Purple]w1 [/color][color=DarkCyan]= [/color][color=Purple]W1 [/color][color=DarkCyan]+ [/color][color=Navy]5  [/color][color=Green]'//compensate for debounce time [/color]
[color=Blue]sertxd ([/color][color=Red]"Time Pressed = "[/color][color=Black],#[/color][color=Purple]W1[/color][color=Black], [/color][color=Red]" ms"[/color][color=Black],[/color][color=Blue]cr[/color][color=Black],[/color][color=Blue]lf)[/color]

[color=Purple]w1 [/color][color=DarkCyan]= [/color][color=Navy]0 [/color][color=Green]'//clear the variable [/color]
[color=Blue]pause [/color][color=Navy]1000[/color]
[color=Blue]goto [/color][color=Black]main[/color]

[color=Blue]END[/color]
 
Top