Shop OBEX P1 Docs P2 Docs Learn Events
very simple question — Parallax Forums

very simple question

TCTC Posts: 1,019
edited 2007-08-13 04:11 in Propeller 1
Hello all
I am still learning spin. I am working with a LCD display that has a led backlight. I can control the backlight from the prop. I am trying to make the backlight flash while there is a problem, but the program must continue while the backlight is flashing. this is what I have so far.

PUB Flash (rate,control)
· repeat while control := 1
··· !outa [noparse][[/noparse]backlight]
··· waitcnt(((CLKFREQ * rate)/ 8) + CNT)

Now the program somewhat works, but it will not end when control = 0, and I cant change control. I am making a blacklight driver. I was thinking of running the backlight driver in a new cog (mostly because I dont understand what the manual is saying about "cognew" yet)

Thanks
TC



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
We all make mistakes when we are young………That’s why paste is edible!

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-12 22:30
    If you want your program to continue while the backlight flashes, you will need to have some kind of independent way to do the flashing while your program runs. There are two ways to do this ...
    One is to start another cog to do the flashing and to stop that cog when the flashing is done ... you'll need to make sure the backlight is on after stopping the flashing
    The other solution is to use one of the counters to flash the light. This requires that you not use the counter for anything else like frequency synthesis.

    Along those lines, look at the Frequency Synthesis object from the Propeller Object Exchange. It will do what you want. You just have to call Synth("A",backlight,<frequency Hz>) or Synth("B",backlight,<frequency Hz>) depending on which identical counter you want to use (there's a pair of counters in each cog). When you're done flashing, just clear CTRA := 0 or CTRB := 0 depending on the counter you're using, then make sure the backlight is on by doing OUTA[noparse][[/noparse] backlight ] := 1. Before you call Synth to flash again, be sure to clear the output register bit for the backlight with OUTA[noparse][[/noparse] backlight ] := 0.

    Obviously, you will need to have DIRA[noparse][[/noparse] backlight ] set up properly (set to output ... 1).
  • TCTC Posts: 1,019
    edited 2007-08-12 23:08
    I will give Frequency Synthesis a try after I get a grip on cognew, so it can run by it self. I am having trouble understanding how to get my "rate", & "control" from my main program to the new cog.

    Thanks
    TC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    We all make mistakes when we are young………That’s why paste is edible!
  • hippyhippy Posts: 1,981
    edited 2007-08-13 01:05
    TC said...
    PUB Flash (rate,control)
      repeat while control := 1
        !outa [noparse][[/noparse]backlight]
        waitcnt(((CLKFREQ * rate)/ 8) + CNT)
    

    Should that not be
    repeat while control == 1
    
  • TCTC Posts: 1,019
    edited 2007-08-13 01:14
    Thanks hippy, did not see that

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    We all make mistakes when we are young………That’s why paste is edible!
  • GennadyGennady Posts: 34
    edited 2007-08-13 02:17
    hippy said...
    TC said...
    PUB Flash (rate,control)
      repeat while control := 1
        !outa [noparse][[/noparse]backlight]
        waitcnt(((CLKFREQ * rate)/ 8) + CNT)
    

    Should that not be
    repeat while control == 1
    

    ok, now I have a question. Since 'control' parameter is passed by value, is it going to be constantly monitored inside the Flash method ?
    I beleive (and please correct me if I am wrong) that control should be a global variable for this method to work (unless Flash method is called whenever 'control' changes outside of a method, but it doesn't really make sense).

    Gennady
  • TCTC Posts: 1,019
    edited 2007-08-13 02:29
    Gennady said...
    Since 'control' parameter is passed by value, is it going to be constantly monitored inside the Flash method ?
    Yes, that is my plain. I would like the display to flash while there is a value that is out of it's limit, but keep checking that value. and when that value returns to a safe value, the display will stay on.

    TC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    We all make mistakes when we are young………That’s why paste is edible!
  • GennadyGennady Posts: 34
    edited 2007-08-13 02:46
    TC said...
    Gennady said...
    Since 'control' parameter is passed by value, is it going to be constantly monitored inside the Flash method ?
    Yes, that is my plain. I would like the display to flash while there is a value that is out of it's limit, but keep checking that value. and when that value returns to a safe value, the display will stay on.

    TC
    That is exactly what i am talking about. Since 'control' is passed by value, it's copy is checked only once, when method is called. For what you plan, 'control' should·be a global valiable, but not passed as a parameter.
    In you case if method is called once with control equal 1, repeat loop will go forever.
    Again, that is how I understand it.

    Gennady
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-13 03:06
    Another option would be to pass the address of control to Flash. A typical Flash routine would be:
    VAR long control ' Set to the flash rate.  Zero means "don't flash".
        long stack[noparse][[/noparse]20]
    PUB main
    ' ...
       control := 0
       cognew(Flash(@control),@stack)
    ' ...
       control := 10 ' Flash at 10*1.25s on/off rate
       waitcnt(clkfreq * 4 + cnt) ' Wait 4 seconds
       control := 5 ' Flash at 5 * 1.25s on/off rate
       waitcnt(clkfreq * 4 + cnt) ' Wait 4 seconds
       control := 0 ' Turn off flash ... backlight on
     
    PUB Flash(controlAddr) | delay
       dira[noparse][[/noparse]backlight] := 1 ' Backlight pin is output
       repeat ' This adjusts when control is changed
          outa[noparse][[/noparse]backlight] := 1 ' Make sure is on
          repeat until long[noparse][[/noparse]controlAddr] ' Wait 10ms until
             waitcnt(clkfreq / 10000 + cnt) ' control > 0
          repeat while delay := long[noparse][[/noparse]controlAddr] ' Repeat while control > 0
             !outa[noparse][[/noparse]backlight] ' Toggle backlight
             waitcnt((clkfreq / 8) * delay + cnt) ' Wait requested time
    
    

    Post Edited (Mike Green) : 8/13/2007 3:12:19 AM GMT
  • GennadyGennady Posts: 34
    edited 2007-08-13 03:15
    Would it be sufficient just to replace control by @control (as a parameter and in a method's body) in the original method?
  • Mike GreenMike Green Posts: 23,101
    edited 2007-08-13 03:31
    I've added some comments to the code.
    CON backlight = 4 ' Pin #
    VAR byte control ' Set to 1 to flash, 0 to quit flashing
        long stack[noparse][[/noparse]20]
    PUB main
       control := 1 ' Must be 1 when Flash is called
       dira[noparse][[/noparse]backlight]~~ ' Both main routine and Flash output to backlight
       outa[noparse][[/noparse]backlight]~ ' Turn off backlight output in main cog while flashing
       cognew(Flash(4),@stack) ' Note output state is OR of the two cogs'
       ' ... do stuff while display flashes
       control := 0 ' Signal to stop flashing
       outa[noparse][[/noparse]backlight]~~ ' Turn on backlight output in main cog
       ' Eventually Flash will stop itself.  That will force its cog's DIRA to zero.
    
    PUB Flash (rate)
      dira[noparse][[/noparse]backlight]~~
      repeat while control == 1
        !outa [noparse][[/noparse]backlight]
        waitcnt(((CLKFREQ * rate)/ 8) + CNT)
    


    Note that you need a little more than the original code.
    This implementation has Flash stop its own cog implicitly.
    You have to do a cognew(Flash(4),@stack) whenever you
    want to flash the backlight. When this Flash cog stops itself,
    the backlight will turn off. The main program has to turn it on
    again.

    Post Edited (Mike Green) : 8/13/2007 3:58:30 AM GMT
  • GennadyGennady Posts: 34
    edited 2007-08-13 03:42
    Agree 200%. Thanks for the clarification.
  • TCTC Posts: 1,019
    edited 2007-08-13 04:11
    Thank you all, you have helped me learn more spin today.
    TC

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    We all make mistakes when we are young………That’s why paste is edible!
Sign In or Register to comment.