Hi Gerad,
Peter's proposal works, but requires OPTION CPUSPEED 252000 or OPTION CPUSPEED 378000 to manage 12 count inputs in MMBasic. The standard picomite speed of 133000 is not sufficient for 12 inputs.
Below is the program to test this:
'count events on 12 GPIO pins
nummeters%=12
dim counter%(nummeters%-1)
openpins
'measurement speed
settick 1,readpins
'main loop
do
'just simulating all the math for 100ms
pause 100
'print how much time the interrupt routine takes (out of 1ms tick)
'print t
loop
'read all the pins
sub readpins
't=timer
a%=port(1,2,4,4,9,4,14,2) 'GP0...GP11
new%= (a% XOR last%) and a% 'pins that have changed to ON
last%=a%
for i%=0 to nummeters%-1
if new% and 1 then inc counter%(i%)
new%=new%>>1
next i%
't=timer-t
end sub
sub openpins
setpin 1,din
setpin 2,din
setpin 4,din
setpin 5,din
setpin 6,din
setpin 7,din
setpin 9,din
setpin 10,din
setpin 11,din
setpin 12,din
setpin 14,din
setpin 15,din
end sub
When you "un-comment" lines 15,20,28 it gives you the time consumed in the interrupt routine. At 252MHz (CPUSPEED 252000) that is 62% (0.62ms of 1ms), and that will be barely enough to run a responsive main program.
Most time is consumed in this section of the interrupt routine 0.5ms of 0.62ms.
for i%=0 to nummeters%-1
if new% and 1 then inc counter%(i%)
new%=new%>>1
next i%
Maybe there is a faster way of doing this, that would be good. But I have no alternative yet. Minor speed up can be achieved by
NEXT
in stead of
NEXT i%
And defining nummeters% as 11 (in stead of 12) to ommit the math in the interrupt routine.
for i%=0 to nummeters%
in stead of
for i%=0 to nummeters%-1
That would bring you to 0.56ms (from 0.62ms) every 1ms interrupt
Volhout
EDIT: this brute force interrupt routine is faster (0.44ms)....
sub readpins
t=timer
a%=port(1,2,4,4,9,4,14,2)
new%= (a% XOR last%) and a%
last%=a%
inc counter%(0),(new% and 1=1)
inc counter%(1),(new% and 2=2)
inc counter%(2),(new% and 4=4)
inc counter%(3),(new% and 8=8)
inc counter%(4),(new% and 16=16)
inc counter%(5),(new% and 32=32)
inc counter%(6),(new% and 64=64)
inc counter%(7),(new% and 128=128)
inc counter%(8),(new% and 256=256)
inc counter%(9),(new% and 512=512)
inc counter%(10),(new% and 1024=1024)
inc counter%(11),(new% and 2048=2048)
t=timer-t
end sub
Edited 2024-03-31 02:22 by Volhout