Shop OBEX P1 Docs P2 Docs Learn Events
Simple question for first-time propeller user. — Parallax Forums

Simple question for first-time propeller user.

ViktorSiggViktorSigg Posts: 6
edited 2014-02-19 13:22 in Propeller 1
Hi everyone!

I have been lurking these forum since I found out about the propeller MCU a couple of months ago, and recently got one myself (QuickStart board).
I'm new to Spin, but I'm a regular user of PIC MCU's with C and Assambler, so I'm not a newbe with MCU's and coding. Though there is a problem I have bumped in to, and
I would love if someone could give me an answer to this problem of mine. :)
{
Flashes a led on pin 23 with a user selected pwm output.
}

CON

   _clkmode = xtal1 + pll16x     'Define crystal input multiplier, 5 Mhz x16 = 80 Mhz
   _xinfreq = 5_000_000          'Crystal input = 5Mhz                                                                                                                                                                                                      
  
PUB COG_0 | on_led

   dira[7]~       'Direction of pin 7 is input.
   dira[22]~~     'Direction of pin 22 is output.
   dira[23]~~     'Direction of pin 23 is output.


   on_led := 0
   
   repeat
      'Checks the button if it is active.
      if ina[7] == 1
         outa[22]~~
         'on_led := 100_000
            
      'Blinks the led in the desired pwm frequency.
      !outa[23]
      waitcnt([COLOR=#ff0000]on_led[/COLOR] + cnt)
      !outa[23]
      waitcnt(900_000 + cnt)
      
'PRI COG_1
'PRI COG_3
'PRI COG_4

Now, as you might have guessed, I'm trying to get a simple PWM output on pin 23. But, as you also might have guessed, it doesn't work!
The reason is the red marked code on_led at waitcnt (you can't miss it). It seems that you can't use variables with waitcnt(), which appears a bit weird to me.
If I change the on_led to 100_000 then it uses ~10% pwm output. With other words it works if I change the variable.
I'm sure there is a way, and perhaps someone could enlighten me??

/Viktor

Comments

  • Heater.Heater. Posts: 21,230
    edited 2014-02-19 12:22
    In the code as presented you have on_led initilized to zero. The "on_led := 100_000" never happens because it is commented out.

    So the waitcnt will wait for the current value of cnt. Which will come around in 40 seconds or so when the counter rolls over.

    I believe there are some examples in the Propeller manual about this.
  • Heater.Heater. Posts: 21,230
    edited 2014-02-19 12:28
    ViktorSigg,

    Welcome to the forum by the way. I'm sure you will like the Propeller. It's a bit weird compared to run of the mill PIC and AVRs but has powers and ease of use you will come to appreciate. The folks around here are always ready to help out if need be.
  • T ChapT Chap Posts: 4,223
    edited 2014-02-19 12:39
    The minimum waitcnt value is 384, so whatever value you enter, you must insure it doesn't drop below this. If you intend to use a method to ramp up or ramp down the waitcnt value, then look up in the manual Limit Min and Limit Max so you can limit the minimum to 384 and limit the max wait time to the maximum on duty if you need that as well.
  • ViktorSiggViktorSigg Posts: 6
    edited 2014-02-19 12:40
    Haha yeah well now I might look like an idiot, but I'm well aware of that commented-out line. Forgot to change that before I copy-pasted the code from IDE... :D

    The problem is that the program does not work anyway though, with or without the "on_led 100_000". The only thing that makes it work is when I change the "on_led" to a defined value.
    The propeller does not respond to anything at all when the program runs. Atleast it would light up the led at pin 22 when putting pin 7 high, but no response from the board what so ever.
    I'm confused and tired, just though someone would know the answer right away before starting digging through all the PDF's. :P
    Say what you want, but I must say that the sources of knowlede seems to be rather outspread everywere...
  • mindrobotsmindrobots Posts: 6,506
    edited 2014-02-19 12:43
    ViktorSigg,

    The buttons on the QuickStart are a little bit unusual. They aren't on/off switches but capacitive switches. Check out this demo code.

    You actually need to charge them up and then read them as they discharge to see if they have been touched. There were many threads discussing them when the QuickStart first came out.
  • ViktorSiggViktorSigg Posts: 6
    edited 2014-02-19 12:44
    The minimum waitcnt value is 384, so whatever value you enter, you must insure it doesn't drop below this. If you intend to use a method to ramp up or ramp down the waitcnt value, then look up in the manual Limit Min and Limit Max so you can limit the minimum to 384 and limit the max wait time to the maximum on duty if you need that as well.

    Yes, that was something I never thought of, and I will sure keep that in mind! :)

    But the waitcnt works if I manually put value 100_000 there, but not with a varable that is set to 100_000. Strange.
  • ViktorSiggViktorSigg Posts: 6
    edited 2014-02-19 12:47
    mindrobots wrote: »
    ViktorSigg,

    The buttons on the QuickStart are a little bit unusual. They aren't on/off switches but capacitive switches. Check out this demo code.

    You actually need to charge them up and then read them as they discharge to see if they have been touched. There were many threads discussing them when the QuickStart first came out.

    Yes, I figured that out, but the input I'm using is a 5V signal output from a VM167 board, that goes through a voltage divider to 3.3V. And that works with other programs I've tested, but not this one. The whole systems stalls direktly when loaded onto board. :S
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-02-19 12:50
    I've tried to make a list of links to various QuickStart demos and projects.

    I think the last post in the thread has a link to instructions on how to use the touchpads as buttons.

    My QuickStart servo tester is an example of using touchpads as buttons (and I think it's kind of a cool one).
  • Heater.Heater. Posts: 21,230
    edited 2014-02-19 12:51
    So, the issue may not be the waitcnt but the fact that on_led is never set to 100000.
    And from what you say I gather the LED does not come on, i.e. "outa[22]" is not being tweaked.

    Which implies that the ina[7] is never becoming 1.

    Is that really connected properly?
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-02-19 12:53
    ViktorSigg wrote: »
    Yes, I figured that out, but the input I'm using is a 5V signal output from a VM167 board, that goes through a voltage divider to 3.3V. And that works with other programs I've tested, but not this one. The whole systems stalls direktly when loaded onto board. :S

    BTW, You don't really need a voltage divider. As long as you use limit the current through the clamping diodes to 500uA the Prop I/O pins are 5V tolerant. A 3.3K resistor of larger is enough to keep the current at a safe level.
  • ViktorSiggViktorSigg Posts: 6
    edited 2014-02-19 13:09
    Heater. wrote: »
    In the code as presented you have on_led initilized to zero. The "on_led := 100_000" never happens because it is commented out.

    So the waitcnt will wait for the current value of cnt. Which will come around in 40 seconds or so when the counter rolls over.

    I believe there are some examples in the Propeller manual about this.

    Sorry, seems I was not really clear when I described the symptoms. :/

    When I press F10 with the code that I have described above (without the out-commented line), the board fires up,, and led 23 lights up to 100%. Then it all stalls. It won't respond to input (i.e. lighting up led 22 when input 7 is put high).

    I now know a "kind of solution" to the problem. If I declare the "on_led" to 400, it will act more or less normal. Check the code:
    {
    Flashes a led on pin 7 with a user selected pwm output.
    }
    CON
    
       _clkmode = xtal1 + pll16x     'Define the speed
       _xinfreq = 5_000_000          'Set to 80Mhz                                                                                                                                                                                                      
      
    PUB COG_0 | on_led
    
       dira[7]~       'Direction of pin 7 is input.
       dira[22]~~     'Direction of pin 22 is output.
       dira[23]~~     'Direction of pin 23 is output.
    
    
       on_led := 400
       
       repeat
          'Checks the button if it is active.
          if ina[7] == 1
             outa[22]~~
             on_led := 100_000
                
          'Blinks the led in the desired pwm frequency.
          'if on_led >= 500  [B]//Commented out, I know. But it will stall again if I use this If statement as soon as pin 7 is put high.[/B]
             outa[23] := 1
             waitcnt(on_led + cnt)
             
          outa[23] := 0
          waitcnt(900_000 + cnt)
          
    'PRI COG_1
    'PRI COG_3
    'PRI COG_4
    

    This works. BUT, it will only light led 23 for as long as pin 7 is high. It toggles with pin 7, but at least it toggles between ~0 (not visible) and 10%. :P
    And, as mentioned, if I use the IF statement thet is commented out, it will stall the program and lighten up pin 23 to 100% as soon as I put pin 7 high.

    EDIT: STUPIFIED AGAIN! Syntax error with >= operator... Should be => Poop!

    EDIT2: Well you know what? One should not try to learn programming Spin while being tired after a whole day's work. I see now that the If statement is working perfect, just me that is now doing so well anymore... The if statement was under the If statement above, and the result is that the led will turn off as the on command does not happen until I put pin 7 high... sigh! Thanks anyway for the help guys! I learned a lot today. About my incompetence that is. :P
  • ViktorSiggViktorSigg Posts: 6
    edited 2014-02-19 13:12
    Duane Degn wrote: »
    BTW, You don't really need a voltage divider. As long as you use limit the current through the clamping diodes to 500uA the Prop I/O pins are 5V tolerant. A 3.3K resistor of larger is enough to keep the current at a safe level.

    Good to know, thank you! :)

    I must say that the forums seems to be way more friendly than I thought. :) Thanks to all of you! :)
  • Heater.Heater. Posts: 21,230
    edited 2014-02-19 13:19
    This is now very confusing.

    By commenting out that "if on_led ..." you have made the "outa[23] := 1" and following waitcnt conditional on the first "if" statement. That indentation matters.

    Any way, if on_led is zero when you hit that "waitcnt(on_led + cnt)" it is going to be stuck there until cnt rolls over, about 40 seconds I think.

    I would start by taking away all the INA testing and just set on_led to 100_000 or whatever the do the repeat loop and see if the LED toggles.
  • Heater.Heater. Posts: 21,230
    edited 2014-02-19 13:22
    Ha, this is about one of the friendliest forums you will ever find. Sometimes a bit rough, like the school yard, but generally those seeking help will find it.
Sign In or Register to comment.