Announcement

Collapse
No announcement yet.

new to PBP, looking for some feedback...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • new to PBP, looking for some feedback...

    so, first some back history...
    I have a modest Home Theater setup that shares the same physical space as my family room due to the minor detail of me not being in danger of ever becoming wealthy enough to buy a large house with a separate dedicated HT room...
    in order to facilitate the sharing of space, I built a cabinet thingy to hide the TV in when I want to use the projector/big screen...
    so it looks something like this:

    just without the battery charger (a nice 12Vdc supply for debugging at the time) and minus all the dust...
    and it is all controlled by my home automation PC, and a weedtech (I liked the name...) digital I/O module (basically a PIC with 14 I/O's and a simple RS232 interface)
    and that works fine as such... but times change, HT equipment comes and goes... and where once I was actually using most of those 14 I/O's for controlling lots of stuff, now I only use a couple for the TV up/down control... seems like a waste...

    so it seemed like making my own up/down controller thingy was the perfect PIC project to undertake... I have always wanted to make something with a PIC...

    so, I have the easy part all done and assembled (ie the hardware...) so all that is left is the scary/hard part... (ie the software)
    while I did do some 8086/8088ASM back in like the 80's or 90's... I really don't want to go back to moving things into and out of registers again... I vaguely remember it being really tedious/painful to get anything useful done...
    as far as C goes, I can usually look at and understand what a program is doing, but when it come to writing one, I just end up staring cluelessly at a blank screen...
    so that leaves just BASIC...
    wow, that ended up being longer than I thought it would be...
    anyway, the TL;DR , can someone look at my code and make sure I didn't make any really stupid mistakes that will cause my poor defenseless PIC to burst into flames and explode? I don't want him to explode...

    Code:
     #CONFIG
        __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_ON & _CP_OFF
    
     #ENDCONFIG 
     
     rly1 var portc.0
     rly2 var portc.1
     led var portc.2
     limu var porta.2
     limd var porta.4
     rxin var byte
     txout var byte
     ANSEL = 0
     
    trisc.0 = 0
    trisc.1 = 0
    trisc.2 = 0
    trisa.2 = 1
    trisa.4 = 1
    low rly1
    low rly2
    low led
    
    
    DEFINE OSC 4 
    DEFINE HSER_RCSTA 90h
    DEFINE HSER_TXSTA 20h
    DEFINE HSER_BAUD 9600
    DEFINE HSER_CLROERR 1
    pause 500
    high led
    pause 1000
    low led
    pause 1000
    high led
    hserout ["I live AGAIN!",10,13]
    pause 500
    low led
    hserout ["CQC-MB1 v1.0",10,13]
    pause 500
    
    mainloop:
    high led
    hserin [rxin]
    low led
    if rxin = $53 then gosub stat
    if rxin = $55 then gosub up
    if rxin = $44 then gosub down
    goto mainloop
    
    
    stat:
        if limu = 0 then
            txout = $55
            low rly2
            pause 500
            low rly1
        endif 
     
        if limd = 0 then 
            txout = $44
            low rly2
            pause 500
            low rly1
        endif 
    
        if rly2 = 1 then
            if rly1 = 0 then txout = $3e
            if rly1 = 1 then txout = $3c
        endif
        hserout [txout]
    return
    
    up:
        low rly2
        pause 500
        low rly1
        pause 500
        high rly2
        txout = $3e
        hserout [txout]
    return
    
    down:
        low rly2
        pause 500
        high rly1
        pause 500
        high rly2
        txout = $3c
        hserout [txout]
    return

  • #2
    what pic chip are you using ? , some are a bit particular about the rx/tx port tris settings.
    it looks like rly1 sets the direction and rly2 controls the power but I can't see where you check the limit switches when its moving do you need to ? , what stops the motor when it gets to the end ?

    Comment


    • #3
      I am using a pic 16f688.
      and yes, rly1 controls direction, and rly2 controls power/output.
      I have a LED that should blink every now and then just because blinky LED's are cool, and it seemed like a good way to visually verify it was alive...
      the "Limit switches" are somewhat miss named, they really are more of just a position feed back... the linear actuator I use has it's own internal limit switches that cut power once it is fully extended or retracted...

      Comment


      • #4
        you seem to have it hand .
        ps some linear actuators have a pulse output that could be monitored so that the relays could be shut off when motion stops ( mine has about 4 pulses per second when driven by 12v)

        Comment


        • #5
          turning off the relays in my application is pretty much non-time critical...

          actually the only reason I am turning them off is just because the thought of wasting ~0.4W each bothers me...

          when my HA pc polls the pic (not sure on the timing yet, but I will probably aim for once every couple of seconds... again not really time critical...), and the pic sees that one of the limit switches is triggered, it will then turn off the relays...

          only issue I have now is that I can't seem to program the pic...
          I have a Pickit 3, and I am just using the MPlabs thingy.... but I had a bit of a physical location/interference issue, and I accidentally ripped up the pads for the data/clock programing lines...
          I think that may have something to do with why I can't program it...
          Last edited by SomeWhatLost; 02-04-2014, 08:40 AM.

          Comment


          • #6
            So, I finally got my pic programed... that was much harder than I anticipated...
            all due to the fact that the pickit 3 seems very picky about what PC it will actually decide to work on... ended up needing to bring it to work and use my lab PC... what a PITA...

            anyway, so all programed up, now I have a couple issues...
            1st, it seems my baud is off for my serial port by just a bit...
            the hello message comes out like:
            Originally posted by 16F688
            ÿÉ ìiöå ÁGÁÉÎ!
            ÃÑC-ÍÂ1 v1.0
            it managed to get the ! and the v1.0 right though... that's something at least...
            2nd issue I have is that pressing D or U (ie down or up) should turn on 1 or more relays, and keep them on until I send and S (status) and it checks the limit switches to see if one of them have tripped... instead, the relay turn on for ~35ms then turns off... why it doing that?

            anyway, the 1st issue is the one I think I should tackle and get out of the way 1st, so what is going on with the baud/serial thing? well I am guessing it is a baud rate issue, seems to work mostly, it does seem to see the characters sent to it correctly, just the TX side seems a bit 'off'...

            Comment


            • #7
              With a 4Mhz OSC, 9600 baud and BRGH = 0, the error rate is too high (~7%)
              Try using BRGH = 1 ...
              Code:
              DEFINE HSER_TXSTA 24h
              Try this program for baud rate calculations ...
              http://www.picbasic.co.uk/forum/showthread.php?t=4994
              PBP3 Manual : Microchip Datasheets - 10F, 12F, 16F, 18F
              Never download a PIC datasheet from anywhere but microchip.com

              Comment


              • #8
                Hi,
                Regarding the PICKit3, I'm pretty sure it's a voltage issue with USB port(s) on the "offending" PC's, in conjunction with not so great USB cables resulting in too low voltage at the PK3 end of the cable.

                Make sure you use a good quality USB cable, no longer than it needs to be. I've had issues, at home and work, when using the long red cable that comes with the PK3. Using a shorter cable fixed it.

                /Henrik.

                Comment


                • #9
                  so, after getting distracted by boring stuff for a while (aka work) and then getting distracted by my truck breaking (I guess GM never thought anyone would use a 4wd truck in the snow? guess I should take my bike when it is cold/wet/snowy out, and only take my truck on nice dry sunny days... but that is a rant for a different forum...)
                  anyway, that calc Darrel is wonderful... also tried out the config util thingy that was just posted here, that seems to work great too...

                  so anyway, I am down to problem #2... the relays only staying on for a short period of time...
                  I ended up putting a bunch of pauses and hserouts in my code to try and see what was going on, and I believe I found the issue, but a)I don't really understand it, and that leads to B) I don't really know how to fix it...
                  the issue is: if I turn on either of the relays, as soon as I write (high or low, level makes no difference) to either the other relay or to the LED, then the first relay will turn off...
                  so for example:
                  high rly1 <- turns on relay 1 just fine.
                  high rly2 <-turns on relay 2, and turns off relay 1
                  low LED <-turns on LED (active low) and turns off relay 2

                  I believe I followed the instructions adequately, and my alias setups are correct
                  rly1 var portc.0
                  rly2 var portc.1
                  led var portc.2

                  so is there something strange about the 16f688?
                  do I need to need to treat port C like a register or something and set all the 0's and 1's each time I try and write to it?

                  Comment


                  • #10
                    looks like the classic read modify write issue
                    check out this video for an explaination
                    http://www.microchip.com/webinars.mi...cName=en556253
                    since the p16f688 has no latx regs the ideas in the video will give you a software solution ,that will be easier to implement rather than changing the relay drivers

                    Comment


                    • #11
                      Hi,
                      On the 16F688 RC0 and RC1 (your relay outputs) are inputs to one of the comparators. When configured as analog inputs (which they are at power on/reset) they read as '0' which leads to the read-modify-write issue that Richard pointed out.

                      Try turning the comparator off, CMCON = 7

                      /Henrik.

                      Comment


                      • #12
                        thanks,
                        in the interest of not just getting my current code working, but also understanding why it works, could someone point out to me where this cmcon command is documented?
                        I vaguely remember coming across it when doing my initial research, but when it came time to build my actual code I could not find it again, so I just assumed since I couldn't find it that I either didn't need it, or I just imagined it in the first place...

                        Comment


                        • #13
                          Hi,
                          That would be in the datasheet for the PIC in question, specifically the section covering the comparator(s).

                          CMCON is not a command, it's a register in the PIC and just like TRIS and ANSEL etc it defaults to a certain state and in this case that state is with the comparators enabled. If that state doesn't suit your needs you need to change it.

                          And, just because CMCON=7 turns the comparators off on this particular PIC doesn't mean it's the same on the next one you're using. Some defaults to ON, some defaults to OFF and so on.

                          When pins doesn't act the way you expect, Always check that you've got any analog functionality for those pins disabled.

                          /Henrik.

                          Comment


                          • #14
                            sorry for the excessively NOOB questions,
                            but looking at the datasheet, I am a bit lost on how you got CMCON=7
                            I get the =7 part, but I still have some issue with the CMCON part...
                            in the datasheet it is CMCON0, is it because it is a 0 (number 0, not o) at the end you can just drop the 0 and get to CMCON?
                            or is there some other rule of thumb or something that tells you when to drop parts off the end of the reg name?

                            again, sorry for the stupid questions, I am just trying to learn this semi scary software stuff...

                            Comment


                            • #15
                              it should be cmcon0 = 7
                              dt. has a useful program called alldigital.pbp which can be found on this forum,it can simplify some of these issues

                              Comment

                              Working...
                              X