Shop OBEX P1 Docs P2 Docs Learn Events
Ctrmode ? — Parallax Forums

Ctrmode ?

anita1984anita1984 Posts: 23
edited 2008-11-17 23:07 in Propeller 1
I have been doing a lot of reading and playing with sample codes( Spin language), but I’m missing something.
I believe the code I need will only be a dozens lines, so I keep reading and trying.I want to control the speed of a motor( in Propeller Assembly language with spin language at the begining) input (1 up to 4KHz and duty cycle 50%).I don't know in this case what is better to choose CTRMODE : %0001X for clock generation or CTRMODE : %0010X NCO ????
NB: i don't need to generate a PWM signal, the synth.spin in library working perfectly to my needs.
Thank you in Advance

Post Edited (anita1984) : 11/9/2008 5:11:06 PM GMT

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-11-10 18:17
    I see you have already answered your question, I too was going to suggest using synth.spin.

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

    Parallax, Inc.
  • anita1984anita1984 Posts: 23
    edited 2008-11-10 20:20
    Hello Paul,Thank you for your answer, i would like to ask you if this code is ideal or enought good for the generation of frequency 1.5KHz on Pin1 and duty cycle 50%?
    CON
      _CLKMODE = XTAL1 + PLL16X
      _XINFREQ = 5_000_000
    VAR
      long  ctr, frq
    PUB Go | freq                
      SynthFreq(1,1_500)           
      CTRA := ctr                       
      FRQA := frq                            
      DIRA~~                        
      repeat                             
        waitcnt(0)                      
    PRI SynthFreq(Pin, Freq) | s, d
      Freq := Freq #> 0 <# 128_000_000     
      if Freq < 500_000                    
        ctr := constant(%00100 << 26)      
        s := 1                          
      else                                 
        ctr := constant(%00010 << 26)      
        d := >|((Freq - 1) / 1_000_000)    
        s := 4 - d                        
        ctr |= d << 23                     
      frq := fraction(Freq, CLKFREQ, s)   
      ctr |= Pin                          
    PRI fraction(a, b, shift) : f
      if shift > 0                      
        a <<= shift                      
      if shift < 0                       
        b <<= -shift 
      repeat 32                           
        f <<= 1
        if a => b
          a -= b
          f++           
        a <<= 1
    
    


    I am trying to convert it to Assembly propeller language , but i found alot of difficult
    Thank you for you answer

    Post Edited (anita1984) : 11/10/2008 8:55:15 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-11-11 00:25
    Yeah, it looks like your code is fine, though I might do DIRA[noparse][[/noparse] 1 ]~~ so you aren't turning all pins to output. Have you taken a look at AN001 on the Propeller Downloads section of our website? It covers the Propeller Counters and the examples are in assembly (been meaning to put spin examples in there too, but I've been too busy with other stuff).

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

    Parallax, Inc.
  • anita1984anita1984 Posts: 23
    edited 2008-11-11 08:16
    Thank you Paul , yes i looked on AN001 , but which one i should tune it for my need ? , i am beginner in PASM :-(
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-11-11 18:09
    Actually after thinking about it, if this is the only thing you'll be doing in a cog, then there isn't much benefit of throwing this into assembly code. The reason for this is that the counter is self running and you are looking to just put out a constant frequency, so once the counter is running it requires no further management from the program. This means you can have the counter running while you are executing other spin code.

    But if you are looking on how to do it to teach yourself,· or you intend to do more complex things in assembly (either with the counter, or not), you would use a modified version of the program listed on page 7. It would be modified to look like:

    ''Demonstration of NCO counter mode
    CON _clkmode = xtal1 + pll16x
       _xinfreq = 5_000_000
    PUB go 
       cognew(@entry, @parameter) 'start assembly cog
       repeat
    DAT
         org
    entry mov dira, diraval 'set APIN to output
         mov ctra, ctraval 'establish counter A mode and APIN
         mov frqa, period  'set counter to flip 1,500 times per second
    :loop jmp #:loop 'loop for next cycle
    
    diraval long |< 1 'APIN=1
    ctraval long %00100 << 26 + 1 'NCO/PWM APIN=1
    period long 80530 '1.5kHz 
    
    

    The value 80530 was obtained by using AN001 equation 1, setting fHz = 1500 and system frequency to 80,000,000 and solving for FRQA.

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

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 11/17/2008 4:45:50 PM GMT
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-15 12:43
    Paul,

    I am doing some counter work and reviewed the AN001 and did some searching and came across this post. In your example above, I see you calculated a period, but it isn't used in the code you posted. Did you mean to include a loop with waitcnt and the period?

    I am trying to use the AN001 page 7 example. I have an assembly routine for an LCD I am working on. The routine has different commands for sending commands or data to the display. One of the commands I am setting up is for the LED back light. I would like to use a counter to control the back light. With this method the counter can be set and the code can go on and do other work. I am searching out examples and posts and the AN001 so I can learn more about the counters. How many years have I been using the chip I haven't done counters yet? There is no excuse.

    Anyway, I looked at page seven of AN001 and tried implementing it in assembly. The pin for the back light is passed to the routine so I can't declare it in the defined data of the DAT section like you did.

    Here is what I have. blPin is brought over from copying in parameters. blPin is the pin value, not the pin mask. Prior to this piece of code the pins are set to outputs or inputs.
                  mov       ctraval,#%00100         'Load in the NCO/PWM configuration
                  shl       ctraval,#26             'Move it to the proper position
                  add       ctraval,blPin           'Add in the pin value
                  mov       ctra,   ctraval         'Load the counter register
                  mov       frqa,   #1              'Load the frequency register
    
    



    In the command section of the code I have this.
                  neg       phsa,   paramA                      'Update the counter with the new value
    
    



    paramA would range from 0 to 100. So far I think something is wrong. When the code is run and paramA is 0, the LED is on. When paramA is 100 the LED is on and the same intensity. To be sure it was not the passing of paramA I replaced it with #0 and #100 and the LED is still on in both cases. I was expecting that with #0 the LED would be off and #100 the LED would be full on.

    Perhaps clarifying what I am doing in my code can help anita1984 too for an example of what to put in his code. I am trying to set the counter to PWM the LED to a certain intensity. My goal is to set the counter, leave and forget about it unless the intensity needs to change.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto fo SunSPOT, BitScope
    www.tdswieter.com
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-16 09:19
    I answered my own questions above by looking into the Counter Lab of the education kit. That document help me to realize I should be using a Duty cycle instead of a PWM. I am sorry for taking this thread slightly off.

    I still don't understand the posted example you made Paul. Should the waitcnt and duty be in the code?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-11-17 16:42
    Oh wow your right Tim, I meant to move period into freqa not 1, i've edited the post.

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

    Parallax, Inc.
  • Timothy D. SwieterTimothy D. Swieter Posts: 1,613
    edited 2008-11-17 23:07
    Makes more sense now, thank you Paul.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Timothy D. Swieter, E.I.
    www.brilldea.com - Prop Blade, LED Painter, RGB LEDs, uOLED-IOC, eProto for SunSPOT, BitScope
    www.tdswieter.com
Sign In or Register to comment.