Menu

Hit Count help please

Keith
2016-06-04
2016-06-15
1 2 > >> (Page 1 of 2)
  • Keith

    Keith - 2016-06-04

    This is driving me nuts' I am trying to make this code work but the solution puzzles me more.

    What I want it to do is count the hits up to three over a time period but if it is taking two long between hits, restart the count back to zero.

    Do While input = 1
    wait 1 S

    hitcount = hitcount + 1

    if hitcount = 3 then

       Call Pattern_1
         Call Pattern_2
       hitcount = 0
    

    end if

    Loop

    I don't know where to start after this. Once again any help would be really appreciated.

    Keith

     
  • Keith

    Keith - 2016-06-04

    I have a feeling I have posted this in wrong place! (Bloody Fool!)

     
  • kent_twt4

    kent_twt4 - 2016-06-04

    Pure speculation not knowing what else is going on.

    1. Use Timer1Overflow interrupt to count up to however many times that constitutes a failed hitcount timeout. Be sure to clear the timer and overflow counts when hitcount = 3 and restart them.
    2. If battery operated, and sleep is involved, then use the WDT or possibly the software watch dog timer SWDTEN to accomplish the task.
     
  • Keith

    Keith - 2016-06-05

    Heck Kent, that is way way above my knowledge and programming skills. Secondly, I don't think the chip I am playing with handles interupts.

    Here is the entire code I am trying to make work and what is happening is it will store the last hitcount indefinitely which is expected. What I want it to do is if the hitcount is sitting at 1 or 2 for longer than 30 Seconds or so I want the hitcount to go back to zero.

    Or in other words to validate a true hit it needs 3 or more consecutive hits in 15 Seconds to prove valid and trigger the light patterns.

    ;Chip Settings
    ~ #chip 12F508,4
    ~#config MCLRE=OFF, CP=OFF, WDT=OFF, OSC=INTRC

    '
    ' 12F508
    ' ------
    ' VDD-|1 8|-Vss
    ' GP5-|2 7|-GP0
    ' GP4-|3 6|-GP1
    ' GP3-|4 5|-GP2
    ' ------

    ;Defines (Constants)

    dir GPIO b'001000'

       ~#define Input GPIO.3 'Output from the IR detector
    ~#define light_1 GPIO.0 'LED 1
    ~#define light_2 GPIO.1' LED 2
    ~#define light_3 GPIO.2' LED 3
    

    ~#define light_5 GPIO.4
    ~#define light_4 GPIO.5

    hitCount = 0

    Main:

    Do While input = 1
    wait 1 S

    hitcount = hitcount + 1

    if hitcount = 3 then

       Call Pattern_1
         Call Pattern_2
       hitcount =0
    

    end if

    Loop

    goto main

    Sub Pattern_1
    Repeat 2
    Set Light_1 On:Pause_1:Set Light_1 Off
    Set Light_2 On:Pause_1:Set Light_2 Off
    Set Light_3 On:Pause_1:Set Light_3 Off
    Set Light_4 On:Pause_1:Set Light_4 Off
    Set Light_5 On:Pause_1:Set Light_5 Off

    Set Light_5 On:Pause_1:Set Light_5 Off
    Set Light_4 On:Pause_1:Set Light_4 Off
    Set Light_3 On:Pause_1:Set Light_3 Off
    Set Light_2 On:Pause_1:Set Light_2 Off
    Set Light_1 On:Pause_1:Set Light_1 Off
    

    End Repeat
    End Sub

    Sub Pattern_2
    Repeat 5
    Set Light_1 On:Set Light_2 On:Set Light_3 On:Set Light_4 On:Set Light_5 On

        Repeat 3
            Pause_1
        End Repeat
    
        Set Light_1 Off:Set Light_2 Off:Set Light_3 Off:Set Light_4 Off:Set Light_5 Off
    
            Repeat 3
            Pause_1
        End Repeat
    
    End Repeat
    

    End Sub

     

    Last edit: Keith 2016-06-05
  • William Roth

    William Roth - 2016-06-05

    What detemines a "hit" in this code. I see an input but it is not commented Is the code ;ooking for a switch press or something else? "Input" is not very descriptive.

    So what is attached to the input? How long does it change states?. .

    You can easily use a software timer to jump out of a loop but I need to understand the application better before I can offer a specific solution.

     
  • Keith

    Keith - 2016-06-05

    It's a Moggie Detector ! ... My neighbours have Moggies and they prefer to use my flower bed as their personal loo. I have found that using scare tactics is better than damaging their hearing with ultra Sonic’s or putting a pellet in its hint end which is a bit cruel and certainly a way to alienate my neighbour, I have found they don't like small flashing lights and scarper. Cats unlike Dogs are not colour blind, and asking my neighbour to have his cats go elsewhere is not an option.

    The hits are from a miniature infra red detector. The output goes high when it receives a shift in the beam pattern.

    The problem is that the spuggies are triggering it also hence I need it to discriminate between hits using a time loop.

    I have added a few comments to code above - hope it makes more sense now.

    A software timer causing it jump out of the loop sounds really good, but I'm not sure how to impliment it.

    Thank you for your time in helping me.

    Keith

     

    Last edit: Keith 2016-06-05
  • Keith

    Keith - 2016-06-05

    I think I solved my own problem. The clue William gave me in a software timer had me thinking (Wow!)

    I wrapped the code inside another loop which cases the hitcount to reset to zero once the set number of laps have expired. It seems to work as I want it. I am testing with with my Real PIC Simulator, and it looks like I have it.

    The code below is 'as is' with the temporary timing indicators and markers in place. As I said, I think I have but I'm sure some of you Guru's and Code Magicians will be able to show how to tidy it up.

    ;Chip Settings
    #chip 12F508,4
    #config MCLRE=OFF, CP=OFF, WDT=OFF, OSC=INTRC
    
    '            12F508
    '         ------
    '     VDD-|1   8|-Vss
    '     GP5-|2   7|-GP0
    '     GP4-|3   6|-GP1
    '     GP3-|4   5|-GP2
    '         ------
    
    ;Defines (Constants)
    
    dir GPIO b'001000'
    
    #define Input GPIO.3
    #define light_1 GPIO.0
    #define light_2 GPIO.1
    #define light_3 GPIO.2
    #define light_5 GPIO.4
    #define light_4 GPIO.5
    
    hitCount = 0
    
    Main:
    
    Do ' Main time Loop Starts Here
    wait 500 mS
    
        Do While input = 1
        wait 1 S
        hitcount = hitcount + 1
    
        if hitcount = 1 then    'Temporary condition to prove step 1 
        Set light_2 on
        Set light_3 off
        End if                  'End of Temporary Condition
    
        if hitcount = 2 then    'Temporary condition to prove step 2 
        Set light_3 on
        Set light_2 off
        End if                  'End of Temporary Condition
    
        if hitcount = 3 then    'Temporary condition to prove step 3
        Set light_2 off
        Set light_3 off         'End of Temporary Condition
    
    Call Pattern_1
    Call Pattern_2
    hitcount =0
    end if
    
      Loop
    
    time_loop = time_loop + 1
    
    if time_loop = 20 Then
    
    time_loop = 0
     hitcount = 0           'Resets hitcount Loop
      Set light_2 off       'Turns of Temporary Test Lights
      Set light_3 off       'Turns of Temporary Test Lights
      Set light_5 on        'Proves timing Loop Function
      wait 500 mS
      Set light_5 off
    
    end if
    
    Loop
    goto main
    
    Sub Pattern_1
        Repeat 2
            Set Light_1 On:Pause_1:Set Light_1 Off
        Set Light_2 On:Pause_1:Set Light_2 Off
        Set Light_3 On:Pause_1:Set Light_3 Off
        Set Light_4 On:Pause_1:Set Light_4 Off
        Set Light_5 On:Pause_1:Set Light_5 Off
    
        Set Light_5 On:Pause_1:Set Light_5 Off
        Set Light_4 On:Pause_1:Set Light_4 Off
        Set Light_3 On:Pause_1:Set Light_3 Off
        Set Light_2 On:Pause_1:Set Light_2 Off
        Set Light_1 On:Pause_1:Set Light_1 Off
       End Repeat
    End Sub
    
    Sub Pattern_2
            Repeat 5
            Set Light_1 On:Set Light_2 On:Set Light_3 On:Set Light_4 On:Set Light_5 On
    
            Repeat 3
                Pause_1
            End Repeat
    
            Set Light_1 Off:Set Light_2 Off:Set Light_3 Off:Set Light_4 Off:Set Light_5 Off
    
                Repeat 3
                Pause_1
            End Repeat
    
        End Repeat
    
    End Sub
    
    Sub Initialise
     Call Pattern_2
    
    End Sub
    

    sub Pause_1
    wait 100 ms
    end sub

     
  • William Roth

    William Roth - 2016-06-05

    I generally use a software timer something like this:

    '// One second timer 
    swtimer = 0
    do
         wait 10 ms    
        '//  test input here      
        swtimer ++   '// increment the timer
        if swtimer > 100 then
            swtimer = 0    '//  housekeeping (clear the timer)
            exit do        '// or jump somewhere else
        end If    
    loop
    

    There are many variations possible. Adapt as needed

     

    Last edit: William Roth 2016-06-05
  • kent_twt4

    kent_twt4 - 2016-06-05

    @Keith
    I would encourage you to have a play with interrupts when you have a chance. It definetely opens up new possibilities with timers and switch presses operating in the background. The new enhanced midrange devices have all kinds of extra features like the 12f1822.

    I had seen a project that differentiates between humans and dogs by placing two IR sensors at different heights on a PVC pipe.

    If the led's don't do the trick you could try using a servo to fashion some sort of noisemaker?

     
  • William Roth

    William Roth - 2016-06-05

    Hey Kent,

    Maybe at some point we need to have a 'Using Interrupts" tutorial that is easy to understand and uses practical examples like button presses. Seems like interrupts scare off a lot of folks.

    Another good tutorial might be on how to create a "State Machine" .
    Err ..What's a State Machine?

    In this case a interrupt would not help, since the 12F508 does not support interrupts. Another limitation is a 2 level deep hardware stack. But still adequate for the task at hand with decent code.

    BTW another cool 8-Pin chip for < $1.00 is the 12F1612 ---- 5+ timers, interrupts. Signal Measurement Timer, Comparator, Zero Cross Detection. etc.

    William

     
  • Keith

    Keith - 2016-06-05

    Thank you Kent and William for your help and support in resolving the issues I had, probably the biggest hurdle was overcoming the confidence to believe that I could resolve this myself, and the knowing that guys like you had my back should I fail miserably. Support like this is priceless.

    I’m going to buy some of these 12F1612 devices and have a play around with them as the spec sheet looks tremendous (the bits I can understand!) internal 32Mhz Clock – wow !!

    No doubt I’ll be back with some more questions but at the moment – Interrupts …. Hell that’s white mans magic!

     
  • William Roth

    William Roth - 2016-06-05

    @Keith

    What Programmer do you have?. Let me verifiy if Pickit 2 will support this chip. I know that it is supported with Pickit 3.

    UPDATE: While there is an entry for 12F1612 in the PK2Devicefile.dat. It appears that whoever originally added the 12F1612 chip, placed in the 32MX family ( won't work). In the latest PK2Devicefile.dat it is in an undefined family ( Won't work) It is highly unlikely that it can be programmed with a PicKit 2 at this time. You will need a Pickit 3 for this chip.

    FYI. The Pickit2 was abandoned by Microchip several years ago and was replaced by the Pickit3. Any new chips released since then are generally not supported by the Pickit2.

    A "clone" Pickit 3 can be purchased for about $12.00 (US) and supports all current Microchip 8-bit microcontrollers.

     

    Last edit: William Roth 2016-06-05
    • Keith

      Keith - 2016-06-06

      Hello again William, sorry for the delay in replying, another friends funeral today, that’s all I seem to do these days – that and try to get my head around this programming language.

      I have both PicKit 2 and 3 programmers hopefully I can talk to them with little problem. My Real PIC simulator doesn’t support it though which is a bit of a pain but not a problem.

       
  • kent_twt4

    kent_twt4 - 2016-06-05

    I just picked up a 12f1571 that has three independent 16 bit PWM's to play with. That will keep me busy for a while. Can no longer use the PICKit 2 or PICKit 3 standalone programmers though. So it's onto the Microchip IDE or IPE..ughhh. Got the IPE finally working but the comms seem really touchy.

     
  • William Roth

    William Roth - 2016-06-05

    Yep. the 12F1571 was also errantly placed in the wrong "family" by whoever added it to the PK2DEfile.dat. Therefore unsupported by Pickit2. I may be able to add these chips so that they are supported, But I will need to get samples for testing/verification. (Next Sample Order)

    I have been using the IPE (Command line via FlashPic.bat) with PK3 for quite some time. Aside from it being agonizingly slow (Java?), I have not seen any coms issues. I am using a $12 clone.

    William

     

    Last edit: William Roth 2016-06-05
  • kent_twt4

    kent_twt4 - 2016-06-05

    @William
    That would be fantastic if you could add the 1571 to the PK2 dat file. I think I read the same post that you did. Apparently it has been done before but the device file editor itself needed to be revised in order to handle the two word device code.

     
  • William Roth

    William Roth - 2016-06-06

    I have not read any posts about 1571 / Pk2Devicefile. But that does not seem quite right. The 12F1571 Device ID is 14-bits the same as most other enhanced midrange chips. Nothing special with this chip in the Programming Guide.

    The trick with adding devices is getting the "ID mask" correct and number of config words correct by placing them in a compatible family.

    If you have a 12F1571 and are willing to edit the device file ( save a copy first) Then open the 1571 "part" and move the part from family 23 ( does not exist) to family 20 and see what happens.

    This just worked for me with 16F1614. But for 1614 you need to get the GCB device file from SVN as it is not included with Feb Release

    I imagine that the 12F1612 can be added in the same manner. I just do not have one to test.

    William

     

    Last edit: William Roth 2016-06-06
  • kent_twt4

    kent_twt4 - 2016-06-06

    What kind of program opens the PK2DeviceFile.dat so that it can be edited? Not seeing anything coherent with notepad.

     
  • William Roth

    William Roth - 2016-06-06

    https://sites.google.com/site/pk2devicefileeditor/home/files/PK2DFE%201.0.0.7.zip?attredirects=0&d=1

    From >>> https://sites.google.com/site/pk2devicefileeditor/

    You may have to "unblock" the exe, using the properties page.

    Open the program then open the pk2devicefile.dat. open the parts list, then open 12F1571, On the second line change family 23 to family 20, Then save the file. Now test the new device file.

    EDIT: Attached are three potential PK2 Devicefiles to try for 12F1571. Please test using PK2 GUI, If one of them works let me know.

    William

     

    Last edit: William Roth 2016-06-06
  • kent_twt4

    kent_twt4 - 2016-06-06

    OK thanks for all the info, much appreciated. Those are quite the device files.

    I got the 12f1571 from the PK2DeviceFile 1.dat to show up in the PicKit 2. It is seen in the Midrange/1.8V Min ID-14 family, not auto detected. It will load the hex but cannot program it i.e. Programming failed at Program Memory address 0x000000. Seemed to be able to read and erase the device OK.

    Odd thing that shows up amongst the Midrange device families "NoDeviceid" along with the standard, 1.8Vmin etc.

    I may not have exhausted all possiblities with the .dat files, as I was not expecting to show up under the 1.8Vmin id-14 group.

     
  • William Roth

    William Roth - 2016-06-06

    "No Device ID" first showed up in the PK2Devicefile.dat from Pic Pascal site. Why it is there, I have no clue. I am tempted to remove it, as it serves no useful pupose that I can tell.

    Some Russian programmers got the PK2 Device File from PicPascal, and then added a bunch of new chips. However, many were placed in the wrong family and do not work. Some do. The only way to tell is to physically test.

    Looks like there may be hope for the 1571. I will just need to get a couple and do some testing.

    William

     
  • William Roth

    William Roth - 2016-06-07

    @Kent

    IF you have time. Try each one of these datfiles with PK2GUI. These are bare minimum files with only one possible part to detect -- 12F1571. If one of these happens to work, please let me know

    William

     
  • kent_twt4

    kent_twt4 - 2016-06-07

    All the test T0-T4 dat files failed to program, thanks.

    Kent

     
  • William Roth

    William Roth - 2016-06-07

    OK Thanks,

    The "scripts" need to be changed for 12F1571 and 12F1572 to work with PK2. I have some chips coming in a few days so I will proceede then. There is still hope. There are some PK2devicefiles linked on the Microchip forum that are claimed to support PIC 12F1572. However, I cannot verify them. without a chip.

    http://www.microchip.com/forums/m841962.aspx

    If in any of the linked files the 1572 addtion is valid, then with the devicefile editor, you could duplicate the 1572 entry, then change the name , Device ID, and memory size and the 1571 should then work.

    I followed the link to the "bert" file on github and I can tell you not to waste with that one as it is full of problems. It looks very similar to the "Russian" file where many of the chips are placed in a family that does not exist. It may even be the soruce of the Russian File.


    On another note: I said I had 16F1614-1619 working with PK2. Well not quite. Parts are detected and can be read and even programed. Seemed to work fine ....However .... in comparing the hex with that produced by PK3/IPE I found that config word 3 is not being read nor is it written with PK2. This is bad.

    Config Word 3 covers the watchdog timer. I will need to write 2 new Pk2 scripts for these chips to work correctly with PK2. Currently, no PK2 script exists that reads and writes 3 config words.

    William

     
  • kent_twt4

    kent_twt4 - 2016-06-07

    Hi William,
    Thanks for the links. Downloaded the PIC12F1572_added.zip. duplicated the PIC12F1572 file, changed name, device id, and program memory. Jackpot! Was able to read, write, erase program memory. Doing a hex compare like you did is on the list. So at the very least it seems promising.

    The 12f1571/2 showed up under the Midrange/USB group.

    EDIT: OK it seems the PK2 config mask isn't outputting the correct config bit reading? IPE proramming results in 3FA4 1EFF, which also matches the IPE config reading after it was programmed by PK2. The PK2 config reading in the GUI says 0EA0 1E03 as evidenced in the attached png.

     

    Last edit: kent_twt4 2016-06-07
1 2 > >> (Page 1 of 2)

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.