Shop OBEX P1 Docs P2 Docs Learn Events
Reading a pin and increasing a counter, Finally done!!!! — Parallax Forums

Reading a pin and increasing a counter, Finally done!!!!

mosquito56mosquito56 Posts: 387
edited 2008-02-24 03:31 in Propeller 1
I have a subroutine running in another cog which turns on a led every 5 secs. The cog is started first and sets dira and outa. This is in my main program and does not work. Any ideas?

· if outa[noparse][[/noparse]27]==1
····· outa[noparse][[/noparse]27]:=0
····· counterx++

This code works when I turn on the pin in the main program but for some reason it doesn't when the pin is turned on in another cog.



▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·······

······· "What do you mean, it doesn't have any tubes?"
······· "No such thing as a dumb question" unless it's on the internet


Post Edited (mosquito56) : 2/24/2008 5:05:24 AM GMT
«1

Comments

  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-02-21 21:33
    This code is not at fault it is your program in general I think. Try stepping through it in your mind or on paper.

    What makes outa[noparse][[/noparse]27] high? What will stop that making outa[noparse][[/noparse]27] high again before the if statement it next read. That is the sort of thing you need to ask yourself.

    Graham
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-02-21 21:34
    But perhaps at leas it should be

    if outa[noparse][[/noparse]27] == 1
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-21 21:58
    ·If led turns on and only place to turn it off is in main program then that part is not working. The == didn't work but is correct coding anyway, thanx

    ·

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet


    Post Edited (mosquito56) : 2/24/2008 5:07:17 AM GMT
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-21 22:08
    Could it be you mean
    INA[noparse][[/noparse] 27 ] == 1
    

    ?

    Mosquito, you have often been asked to not only post a small part of your problem programs. You are not yet in a position to decide where an error can be, so if you want efficient help we need the FULL code.

    I think you are aware that "== 1" is redundant in cases as those!?
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-21 22:41
    Do not download the code, I didn't save it before I uploaded it. will fix and retry below





    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet


    Post Edited (mosquito56) : 2/21/2008 10:55:55 PM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-02-21 22:50
    deSilva hit the point, outa is internal to a cog, it only pushes out values if a corresponding bit in dira is set. This is a one way (out only), reading it only results in reading the local copy. Use INA instead.

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

    Parallax, Inc.
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-21 23:09
    I have tried ina, outa, dira, excusa, and nothing works.
    Here is the code, I hope this explains the proplem
     term   : "Pc_interface"              'Load in cog 1
     pinon  : "pinon"'
    CON
      _clkmode      = xtal1 + pll16x
      _xinfreq      = 5_000_000
      
    '
    VAR
    byte cntrr
    byte stack[noparse][[/noparse]20]
    byte pin
      
     
    PUB Start  |st, x
    pin:=27
    cntrr:=0
      term.Start(31, 30)
      waitcnt(clkfreq  + cnt) {{wait for terminal program to start}}
      dira[noparse][[/noparse]pin]:=0
     ina[noparse][[/noparse]pin]:=0
      
     repeat
       if outa[noparse][[/noparse]pin]:=1 'I have tried every combo possible to turn pin off
          ina[noparse][[/noparse]pin]:=1
          cntrr++
          term.dec(cntrr)
          term.out(13)
    

    Pinon below
    CON
      _clkmode      = xtal1 + pll16x
      _xinfreq      = 5_000_000
    var 
     byte pin  
     long stack[noparse][[/noparse]20]
    pub go
     cognew(starttt,@stack)
    PUB Starttt|x
     pin:=27
    dira[noparse][[/noparse]pin]:=1
    outa[noparse][[/noparse]pin]:=1
    dira[noparse][[/noparse]pin]:=outa[noparse][[/noparse]pin]:=1
    repeat 7 
      !outa[noparse][[/noparse]pin]
      waitcnt(clkfreq/8+cnt)
    outa[noparse][[/noparse]pin]:=1 
    repeat
     x++     
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet


    Post Edited (mosquito56) : 2/22/2008 12:36:43 AM GMT
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-02-21 23:38
    You've got them turned around, your trying to read from a write-to register and writing to a read only register. Additionally this code reinforces the behavior (ie, if it's on turn it on), you likely want it to be opposite. Also you used the assignment := when you should have used a test equality ==. I haven't checked the rest of your code since these two lines alone have 4 errors.

    if ina[noparse][[/noparse]pin]==1
    ·outa[noparse][[/noparse]pin]:=0

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

    Parallax, Inc.

    Post Edited (Paul Baker (Parallax)) : 2/21/2008 11:44:21 PM GMT
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-21 23:39
    A couple of things ...

    Your "cognew(pinon.go,@stack)" won't work. You're not allowed to start a Spin method in another object. It's not documented in the manual yet, but has been described many times in the "Tricks and Traps" document and others. I can't see why you'd need it anyway since pinon.go just does a COGNEW again. You probably should just do a "pinon.go" call in the main object.

    You've still got an "ina[noparse][[/noparse]pin] := 0" for some reason. It doesn't do anything useful since INA is a a read-only register. When you attempt to change it, you're just modifying the "shadow RAM" value. There are actual RAM cells for all 512 longs in the cog memory even if there are physical registers corresponding to them. For read-only registers like INA and CNT, any attempt to change the register actually stores the value in the shadow RAM cell.

    The expression "outa[noparse][[/noparse]pin] := 1" always is true because you're assigning the value 1 to the output pin, then that value (1) is being used by the IF statement and is considered true. The IF statement will always succeed no matter what the output pin value becomes.
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-02-21 23:46
    mosquito56 said...
    Obviously I have thought of those things first. If it was that simple I wouldn't be posting.

    When I'm programming I find most of the problems I come across are exactly "that simple". I was trying to help, in future I won't bother.
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-22 00:33
    Mike, glad you chimed in. This is a continuation of the timer module we worked out using ctra and ctrb. I have got that working and am just trying to find a way to get the info from cog b to cog a. You had suggested setting a pin high when the ctra rolled over and I am trying to figure a way to do that.

    · Not sure about the cognew not working as it seems to be working fine. The first cognew was a mistake and shouldn't be there. I will switch it and see if it works. I switched it and the video dies.

    ·Paul suggested that

    if ina[noparse][[/noparse]pin]==1
    ·outa[noparse][[/noparse]pin]:=0

    no luck

    If someone could fix the post so it works, I would be eternally grateful. I have been working on this problem for 2 months now.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet


    Post Edited (mosquito56) : 2/22/2008 12:44:50 AM GMT
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-22 16:00
    con
          _clkmode        = xtal1 + pll16x
          _xinfreq        = 5_000_000
    OBJ
    term   : "PC_interface"
    

       
    VAR
    byte cntrr
    byte stack[noparse][[/noparse]20]
    byte pin             'PIN is not a VAR in this context...it is constant..pin is not a good name choice IMO
    

                         ' but would not like to cramp your style,
      
      PUB Start           
      cntrr:=0
     
      term.Start(31,30)              
      cognew(starttt,@stack) 
      waitcnt(clkfreq  + cnt) 
      repeat
        if ina[noparse][[/noparse]pin]==0 
          cntrr++
          term.dec(cntrr)
          term.out(13)
    

     
    PUB Starttt |x
    

    dira[noparse][[/noparse]pin]:=1
    outa[noparse][[/noparse]pin]:=1
    repeat                    ' I removed repeat 7 time to keep cog busy forever
    

      !outa[noparse][[/noparse]pin]
      waitcnt(clkfreq/8+cnt)
    

    'repeat                   'the rest of the code looses me completely
    

     '  X++                    'I suspect that it is to keep the cog alive but for what reason, nothing is going on
    

    Mosquito

    There are so many issues with your code,·its difficult to be sure, but is this what you are trying to achieve ?
    I can't figure out why you would want to do this, but I can't fit the broken bits together in any otherway, so I am really shooting from the hip.
    confused.gif

    I·took the liberty of removing a few lines of code. I·started to·get a headache.

    Not sure·if it was·from reading your code or trying to keep up with the·Code Protect, DOL, Javalin, Forth·threads··here in PROP Land.
    I've·been·AWL for a month·or so·and have a bit of reading to catch up on, looks like.

    Seriously though, pin_num:=27 is being toggled at a fixed rate and pin_num_status is being sampled at a diferent rate, the sample frequency being dependent upon the code in the sample loop,·so what is cntrr(BYTE) telling us about pin_num activity anyway ?· I still don't think we know enough about your objectives to be of much help.


    Ron
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-22 18:27
    Ron, I think it may be so simple it's getting lost.



    A seperate cog keeps a timer going and turns on a led.

    The main program sees the led, turns off the led and cntt++. Thats it.

    Yes the x++ loop was to keep the counter going as the second cog will turn on the pin every x seconds

    ·It must be in a seperate cog as I am using tv and for some reason when I start the cog from main the video doesn't work.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet
    ········
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-22 21:28
    This thread drives me nuts....
    So here is how to use it..
    '' Taking two (unused) timers to make a true long term clock (some 100 years)
    '  As devised and re-devised "from time to time" by Ariba, deSilva and PhiPi 
    CON
      commPin = 0      'unused Pin ?  
    
    PUB main   | oldTimerValue
    
        setupTimer
    
        REPEAT
          IF PHSB<>oldTimerValue
             oldTimerValue  := PHSB
             ' do what you like
    
    
    PRI setupTimer
    ' advantage: an LED at comPin can be used to show the second (even withou a resistor) 
     ctra := %00100<<26  + commPin       ' Set NCO mode i.e. toggle per 25 sec
     frqa := POSX/CLKFREQ*2              ' one second
     ctrb := %01010<<26  + commPin       ' set ctrb to POSEDGE detect
     frqb := 1
     dira[noparse][[/noparse]commPin] := 1
    
    PRI setupAltTimer
    ' just to exercise some other modes
     ctra := %00110<<26  + commPin       ' Set duty cycle i.e. once per 50 sec
     frqa := POSX/CLKFREQ*2              ' one second
     ctrb := %01000<<26  + commPin       ' set ctrb to POS detect
     frqb := 1
     dira[noparse][[/noparse]commPin] := 1
    

    Post Edited (deSilva) : 2/22/2008 9:58:42 PM GMT
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-22 23:06
    Deslivia, Since I have been doing this exact code·for a couple months, thanks to Mike Green it is of no use to me. Sorry you don't·understand the problem but that's to be expected·since you like to answer every post even if it's with garbage. Let me try to make it easier for you, "SIGH".

    Start a seperate cog that turns a pin high, led on.

    Read the pin in MAIN program, increase counter and turn light off.

    P.S. I sent you a msg a while back asking you not to respond to my posts. Since I don't speak German and you have some problems with English, "I am putting you on ignore" Your attitude is making me nuts'

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet
    ········
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-22 23:17
    No problem, mosquito smile.gif
    My posting was not meant for you but for other interested readers!
    There is really no need to start any COG or whatever...
    Sorry you felt highjacked :-(

    Post Edited (deSilva) : 2/23/2008 12:04:28 AM GMT
  • dfletchdfletch Posts: 165
    edited 2008-02-23 00:37
    mosquito56: Wow you think deSilva posts garbage? I've gotten nothing but pure gold from the fellow since day 1.

    deSilva, don't worry I don't think this is a very popular view smile.gif

    Cheers,

    --fletch
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-23 00:48
    Judging by the number of private msgs I have recieved, I would have to disagree. That is one problem with the forum. Someone asks a question and then someone else posts a response that is complely off topic. Hijacking like this is common from some people.

    ·I emailed parallax about this gentlemen months ago and sent him a private msg asking him not to respond to my msgs.

    I informed him today that I was putting him on ignore and yet he still insists on hijacking my threads. I would think a gentleman would take a hint. In my culture, ignoring is the biggest offense.

    · ·I am positive he dislikes me, "He would have to be an idiot not to". Everytime he chimes in everyone else stops. If he continues, I will have to take it up the chain as we said in the military.

    · I have asked him privately to ignore me, yet he continues. Now it is public by his choice.

    I never said it was garbage, just rude.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet
    ········
  • ClemensClemens Posts: 236
    edited 2008-02-23 00:56
    I brought the thread back on top. I suggest you read it again. Paul asked us not to carry these things out in public, that counts you in too in terms of your answers towards people who are trying to help you.
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 01:02
    I shall step into mosquito's - or anybody's - thread when I have the strong impression that other people reading it might get confused. That is what a forum is for.
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-23 02:45

    Mosquito

    This forum as brought together some of the best computer PPL around

    They come from different backgrounds and cultures so its not surprising

    That misunderstanding will occur from time to time.

    This is a classic example of what I mean.

    I think deSilva·feels I am leading you in the wrong direction. I am sure that’s why he posted.



    @deSilva

    I though it was obvious that I had just reworked Mosquitoes own code. Taking into account the issues which Mike, Graham, Paul and you had raised with Mosquito earlier. Changing course to an optimal solution, (to an unknown problem) I think would have been helpful at this stage.

  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 02:55
    Thank you Ron smile.gif
    Yes, in fact I saw you tried well to come to the ground of the problem and then had to stop....
    The reason - mostly likely - that there were no problem at all...

    I tried to post code that should show that, as always a little bit "over-educated"

    That was good, as we now have come to the ground of it: Setting a Pin in one COG and reading it in another. This generally works fine, though it is sometimes forgotten to set the DIRA in the writing COG.

    So I should suggest that mosquito just assembles all those information here, simplifies his code, and tries again, which should then most likely work...
  • Stan671Stan671 Posts: 103
    edited 2008-02-23 04:06
    I was under the impresson that the output pins were wire-ored from all of the cogs output registers and that if one cog turned on an output, that no other cog could turn it off.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Stan Dobrowski
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-23 04:07
    That's true!
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-23 05:24
    @stan
    I was tempted to suggest that Mosquito re visit Page 26 of the Prop manual, Rules A. B and C, in particular. Decided to take it one step at a time.
    Maybe we can get his TV problem sorted out whilst dealing with some of the other outstanding issues here.
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-23 05:37
    Clems, I saw your post and have to ask. If doing it in private doesn't work, what is next? Read my previous msg about private msg for D.

    1. Private msg

    2.?

    3. Parallax for official complaint. I have put him on ignore and only ask he do the same to me. Since I am no longer reading his posts I will not be bothered again but he has a condesending attitude and I have had enough.

    Ron, I tried you code and for the life of me can't understand why it doesn't work. The counter just counts up. Light never goes off. Sorry.
    I think you have to connect an led to see the problem. I don't know what·D is saying anymore.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet
    ········
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-23 06:11
    Ok Mosquito
    I am going to prepare something for you to-nite, and will probably ask you a few questions later.

    Does my code work if you change driver to tv_text instead of PC_interface ? It should just display the value of cntrr and CR nothing else and forever.
    I saw your post in another thread referring to TV Problems when starting a new cog. My guess is that TV will be Ok using the code I posted.
    If I am right and it works Ok then we will go from there and look at your code again line by line and see iIf we can get it sorted.


    Ron
  • mosquito56mosquito56 Posts: 387
    edited 2008-02-23 06:20
    Mike, you code works fine, it just doesn't turn off the led. Seriously, this is so weird if I didn't have an led flashing at me·I wouldn't believe it. It just sounds too simple.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·······

    ······· "What do you mean, it doesn't have any tubes?"
    ······· "No such thing as a dumb question" unless it's on the internet
    ········
  • stevenmess2004stevenmess2004 Posts: 1,102
    edited 2008-02-23 06:28
    I think the problem is this.
    Each cog has an INA, OUTA and DIRA register. If a bit is set (ie set to 1, clear is set to 0) in the DIRA register and the same bit is set in the OUTA register then then the pin will go high (logic state 1 or on). Where you are running into problems is that every cog has its own INA, OUTA and DIRA register. They are not global. So if one cog sets a pin high than the only way to clear it is to either stop the cog that is setting it or to have the same cog that is setting it high to clear it.

    This is all in the propeller manual. Have look where Ron suggested. It goes to great lengths to explain it.
  • Ron SutcliffeRon Sutcliffe Posts: 420
    edited 2008-02-23 06:41
    Ok so my code works with TV_text.

    I'm pleased to hear that. I will get back to you.

    RON
Sign In or Register to comment.