Shop OBEX P1 Docs P2 Docs Learn Events
TIMER (Please Help) — Parallax Forums

TIMER (Please Help)

albertalbert Posts: 10
edited 2011-04-13 13:18 in General Discussion
I'm trying to make a timer that counts while somepin is high, in this example its supposed to count up at a frequency determined by a constant named PRECITION and then get read and reset each time somepin goes low, the value the gets read is put on a memory address to get read by another cog. My problem is that it isn't counting up at all. Is there some error in this code or is the problem somewhere else? I can't find any tutorials for timers.
ctra[30..26] := %11010
ctra[5..0] := somepin
frqa := clkfreq/PRECITION

'some code

phsa~

'some more code

repeat
  if not somepin
    LONG[someaddress] := phsa
    phsa~

Comments

  • ElectricAyeElectricAye Posts: 4,561
    edited 2011-04-12 10:08
    albert wrote: »
    ... I can't find any tutorials for timers.

    ....

    You might want to look at the file for using counter modules. See attachment.

    You might want to post your entire code, too, so people can see exactly what's going on.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-12 10:08
    The main problem is that, whenever somepin is low, you're continually reading and resetting phsa and LONG[someaddress]. You'll very very briefly see a non-zero value for LONG[someaddress], then it will quickly be replaced with a zero from phsa.

    What you want to do is read phsa only once for each time somepin goes high. In other words, look for a change in somepin from 1 to 0.
  • albertalbert Posts: 10
    edited 2011-04-12 10:32
    I guess i should have posted my whole code, I had done what you suggested accually, but i rewrote the code for my message because i had done lots of changes in my file to see if the timer works, I changed it back now how its supposed to be, and here is a straight copy:
    PRI Read(Pin, PositionAddr)
       
      dira[Pin]~                                  
      ctra[30..26] := %11010    
      ctra[5..0] := Pin
      frqa := clkfreq/PRECITION     
      repeat while ina[Pin]         
      phsa~   
          
      repeat        
        if not ina[Pin]   
          LONG[PositionAddr]:= phsa 
          phsa~   
          repeat while not ina[Pin]
    
  • Mike GreenMike Green Posts: 23,101
    edited 2011-04-12 12:50
    Remember that FRQA will be added to PHSA on every clock cycle where the pin is high. Also remember that the counter gets activated when the mode is set. The pin # will be zero until it's set to something else.

    Use PULSIN_Clk from "BS2 Functions" in the Object Exchange as an example of how to do this:
    PUB PULSIN_Clk(Pin, State) : Duration 
    {{
      Reads duration of Pulse on pin defined for state, returns duration in 1/clkFreq increments - 12.5nS at 80MHz
      Note: Absence of pulse can cause cog lockup if watchdog is not used - See distributed example
        x := BS2.Pulsin_Clk(5,1)
        BS2.Debug_Dec(x)
    }}
    
      DIRA[pin]~
      ctra := 0
      if state == 1
        ctra := (%11010 << 26 ) | (%001 << 23) | (0 << 9) | (PIN) ' set up counter, A level count
      else
        ctra := (%10101 << 26 ) | (%001 << 23) | (0 << 9) | (PIN) ' set up counter, !A level count
      frqa := 1
      waitpne(State << pin, |< Pin, 0)                         ' Wait for opposite state ready
      phsa:=0                                                  ' Clear count
      waitpeq(State << pin, |< Pin, 0)                         ' wait for pulse
      waitpne(State << pin, |< Pin, 0)                         ' Wait for pulse to end
      Duration := phsa                                         ' Return duration as counts
      ctra :=0                                                 ' stop counter
    
    You'd simply call this like
    PRI Read(Pin, PositionAddr)
       repeat
          LONG[PositionAddr] := PULSIN_Clk(Pin, 1) / PRECISION
    
  • albertalbert Posts: 10
    edited 2011-04-13 10:54
    Oh? frqa gets added to phsa every clockpulse? I assumed that it worked like when i worked with ARM, that it increases phsa every frqa clockpulses. I guess it was counting up way to fast for me to be of any use for me then. I think I will be able to fix my progam now. Thanks for the help.
  • albertalbert Posts: 10
    edited 2011-04-13 13:18
    Thank you so much Mike Green. I´ve solved it now. Sincerely Albert
Sign In or Register to comment.