Shop OBEX P1 Docs P2 Docs Learn Events
Frequency Counter — Parallax Forums

Frequency Counter

Gerry KeelyGerry Keely Posts: 75
edited 2008-07-03 13:34 in Propeller 1
''Demonstration of the counter used as a frequency counter
CON 
        _clkmode = xtal1 + pll16x
        _XinFREQ = 5_000_000
OBJ
        txt : "TV_Text"
PUB Go | freq
      
        txt.start(12) 
        cognew(@entry, freq)
        
repeat
  txt.out($00)                  'clear the screen
  txt.dec(freq)                 'display the value (in Hz)                  
DAT
        org      
entry   mov     ctra, ctra_             'establish mode and start counter
        mov     frqa, #1                'increment for each edge seen
        mov     cnt_, cnt               'setup time delay
        add     cnt_, cntadd
:loop   waitcnt cnt_, cntadd            'wait for next sample
        mov     new, phsa               'record new count
        mov     temp, new               'make second copy
        sub     new, old                'get delta
        mov     old, temp               'set next delta's base
        wrlong  new, par
        jmp     #:loop
ctra_   long    %01010 << 26 + 1        'mode + APIN
cntadd  long    80_000_000              'wait 1 second, answer in Hz 
cnt_    res     1
new     res     1
old     res     1
temp    res     1


When this program is run the TV displays value for 1 second only(ie value in cntadd).If I change cntadd to 160_000_000 it displays it for 2 seconds etc.

If I change the line "org" to "org 20" the program runs as expected.

Does anybody know why this is happening ??

Regards

Gerry

Comments

  • BeanBean Posts: 8,129
    edited 2007-06-18 21:25
    Gerry,
    Try "cognew(@entry, @freq)".

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    “The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    www.hittconsulting.com
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-18 21:38
    Argh the frequency counter example comes up again, Ive been working on a revision to the docs, so I've held off on fixing that specific error. But since I've been distracted by several higher priority projects in the course of the revision, I'll push a .spin file correction as a band-aid.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • Gerry KeelyGerry Keely Posts: 75
    edited 2007-06-18 21:39
    Bean

    Thank you for you very quick response that did the trick smile.gif

    regards

    Gerry
  • Paul BakerPaul Baker Posts: 6,351
    edited 2007-06-18 23:32
    Ok, code example in download is now correct. Code in pdf is still incorrect, that will be changed along with the new revision, besides who reads printed code anyways? wink.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
  • KaioKaio Posts: 253
    edited 2007-06-19 10:27
    Gerry Keely said...

    If I change the line "org" to "org 20" the program runs as expected.

    Does anybody know why this is happening ??
    I'm wondering that this should be work properly. The compiler calculates only the addresses depending of the value given on org. But the code will not be moved at this location. Perhaps it will run once a time but it should not count the real frequency, because the value of ctra_ should be bad. And then the jmp would be to location outside of the code.

    I will try this with POD today for myself and will report.

    Thomas
  • Gerry KeelyGerry Keely Posts: 75
    edited 2007-06-19 11:33
    Actually it only to continued to work for approx. 5 mins and then the display went blank.Also I was not measuring any frequency at the time- just testing the display.I was a bit puzzled by this myself as it would appear that the TV code is being overwritten as otherwise why should the display stop working ?

    Regards

    Gerry
  • KaioKaio Posts: 253
    edited 2007-06-19 12:29
    Gerry,

    you are right. The Spin code or related stack will be overwritten by the assembly Cog after some minutes running. If it would be working properly the display routine should not end.

    Thomas
  • KaioKaio Posts: 253
    edited 2007-06-20 13:15
    Gerry,

    I have debugged the assembly code with org 20 or some whatelse and it will not work properly as I was mention before.
    I have also made little changes in Spin code on the loop so it will print out each second a value. And I was using the PC_Text driver for PropTerminal for my tests. With these changes the Spin code is running 28 seconds, then never a print out occur again.

    I have also added some lines of code in assembly which you can uncomment for test if you have a demo board. Then you could see a blinking LED if the code is working properly. But if you use the org 20 statement after a short time several LED's will be illuminated and after 28 seconds the display loop will be run 4 times slower.

    Thomas
  • pgbpsupgbpsu Posts: 460
    edited 2008-07-02 18:16
    What is the maximum frequency that the Frequency Counter code included in this thread and in the Propeller Counters PDF is capable of capturing?

    I count 7 instructions (one being a hub operation) in the assembly loop, so it seem like it takes at least 350ns (2.86Mhz) to get back around. Although, I thought the counters were able to run on their own so that PHSA is accumulating even while this cog is busy getting through the loop.

    I'm trying to count a signal that should be ~20.48Mhz. I'm happy to get an average over one second. My results with this code suggest some kind of aliasing. Am I asking too much of the prop here?

    Thanks,
    pgb
  • AleAle Posts: 2,363
    edited 2008-07-02 20:50
    Maybe you can divide it with a D-flip-flop or with a decimal counter like the 74*90. At ~ 3 MHz it should work ok.
  • pgbpsupgbpsu Posts: 460
    edited 2008-07-03 13:34
    Ale-

    Thanks for your response. With a bit of fiddling, I was able to get this working. The problems were with my circuit, not the code. I've confirmed that my signal is operating at 20,480Khz.

    I'm now looking for stability in that signal to better than 30bbp, which I'm not sure I can get from the prop because that depends on the stability of the prop's clock. But I'm happy to see that I'm getting something close to 20.48Mhz from my signal.

    Thanks,
    pgb
Sign In or Register to comment.