Incrementing a variable at a certain frequency
smbaker
Posts: 164
I have a variable, and I'd like it continuously incremented at a frequency (frequency to vary between 1 Hz and 100,000 Hz, as specified from another cog). Currently I have this implemented in a cog:
The code works pretty well, but ties up a cog. I can see from the manual that the prop has several different counter modes, although I don't really understand how to use them, nor do I see an example that does exactly this sort of thing. Any suggestions?
Thanks,
Scott
VAR long freq PUB setFreq(x) freq := x PRI countLoop repeat if (freq>0) value := value + 1 waitcnt(clkfreq/freq + cnt) else ' freq = 0, wait a while to see if someone changes it waitcnt(clkfreq/10 + cnt)
The code works pretty well, but ties up a cog. I can see from the manual that the prop has several different counter modes, although I don't really understand how to use them, nor do I see an example that does exactly this sort of thing. Any suggestions?
Thanks,
Scott
Comments
Each cog does have two counters and each of them can be incremented at the system clock rate or in response to an external input. You can set up the two counters so that one of them toggles an I/O pin at a frequency related to the system clock and the the second counter counts the positive (or negative) transitions of the I/O pin. The I/O pin doesn't have to be connected to anything, just configured as an output. Just don't try this at a frequency close to the system clock rate (80MHz usually).