Could I please ask for some help on my code


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2009
    Location
    Somerset England
    Posts
    10

    Default Could I please ask for some help on my code

    Im trying to write a program to switch on four LEDs in response to HI/LO (5v) pulses from two optical sensors.Im at this point with it where Im just staring at it and I cant see whats wrong at all and I despratley need to ask for help with it.
    The problem is when I power everything up all that hapens is the LED connected to RB0 comes on and it shouldnt and my optical sensors do nothing.In short it just dosent work and I cant understand why.Ive included the program that Ive writen and some pictures of the sensors and if somone could look at it and let me know what Ive done wrong I would be very greatfull.

    The nuts and bolts is,it's intended ultimatley to fire four ignition coils sequentialy on an engine but for now four LEDs will do.
    There are two infrared optical sensors.One has a single interuption per revolution and the other has four interuptions per revolution.This is achived with a segmented wheel runing through each sensor.As in the picture the four segment wheel is on the top and the single segment one is on the bottom of the same shaft.Ive included the scope readings from the sensors and you can see that when the sensor is interupted the voltage drops and goes low (0v).It can also be seen that the single segment sensor overlaps slightly the four and you can see it in the first part of the program also at Label "loop".The program has to see the signal from the lower single segment sensor first before it can enter the section to check the four segment section.
    Its on the low (0v) from the sensors that I need my LEDs to come on.

    Please,if anybody could read through this post and help me with where Im going wrong.
    Its like a wall at the moment that I cant climb over,dig under,or go round.
    Thanks very much everybody
    All the best O.T.

    PIC16F876
    Compiler version = PICBASIC 1.45


    SYMBOL ADCON1 = $9f
    SYMBOL TRISA = $85
    symbol TRISB = $86
    symbol PortA = 5
    symbol PortB = 6
    Init: poke ADCON1, 7
    POKE TRISA, %11111111
    POKE TRISB, %00000000
    loop: peek portA,B0
    if Bit0 = 0 then chk1 ----------------------------------------------Lower single segment sensor
    GOTO LOOP
    chk1: peek portA,B1
    if bit1 = 0 then fire1 ----------------------------------------------Upper four segment sensor
    goto chk1
    if bit1 = 0 then fire3
    goto chk3
    chk4: peek portA,B3
    if Bit1 = 0 then fire4
    goto chk4
    chk2: peek portA,B4
    IF BIT1 = 0 THEN fire2
    goto chk2
    fire1: poke PortB, %00000001
    peek portA,B1
    if Bit1 = 0 then fire1
    poke PortB, %00000000
    goto chk3
    fire3: POKE pORTb, %00000010
    peek portA,B2
    if Bit1 = 0 then fire3
    POKE PortB, %00000000
    goto chk4
    fire4: poke PortB, %00000100
    peek portA,B3
    if Bit1 = 0 then fire4
    poke PortB, %00000000
    goto chk2
    fire2: poke PortB, %00001000
    peek portA,B4
    if Bit1 = 0 then fire2
    poke PortB, %00000000
    goto loop
    end


    Last edited by opticaltrigger; - 20th January 2010 at 00:25.

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    bit0 and bit1 are aliases to bits in the B0 byte variable

    So when you do something like ...
    Code:
    chk4: peek portA,B3
    if Bit1 = 0 then fire4
    goto chk4
    It will get locked in the loop forever, because Bit1 can never change.

    hth,
    DT

  3. #3
    Join Date
    Jun 2009
    Location
    Somerset England
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel and thankyou very much for the reply.
    Can I ask Darrel how I should fix thiss problem,what should I type instead.
    Please forgive my uninformed aproach to all this but its all quite new to me and Ive only had myself as a teacher so there are some serious gaps for me,but I love it all the same.

    All the very best
    O.T.
    Last edited by opticaltrigger; - 20th January 2010 at 02:20.

  4. #4
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by opticaltrigger View Post
    Can I ask Darrel how I should fix thiss problem,what should I type instead.
    Well ... typing in your order for PicBasic Pro would probably be the easiest.

    But, in lieu of that ... put your values in B0 if you want to test the bits.

    Code:
    chk4: peek portA,B0
    if Bit1 = 0 then fire4
    goto chk4
    <br>
    DT

  5. #5
    Join Date
    Jun 2009
    Location
    Somerset England
    Posts
    10


    Did you find this post helpful? Yes | No

    Default thankyou Darrel

    Thanks very much Darrel for helping me with that.
    I just fired it up with the amended B0s and worked exactly as it should do straight away.I wish I could tell you how excited I feel seing it work.Its my first proper program for doing a job.
    If I lived closer I would buy you a beer Darrel.Thanks so much.

    Im going to swap the LEDs for transistors tomorow and see how it all gets on runing proper ignition coils and making sparks at the spark plugs.

    One last thing if I may.As said its my first proper program so I have no bench mark to go by in terms of its efficiency,and I noticed that you had mentioned BASIC PRO might be better.My question is does the code look ok or do you see a way it could be better,or perhaps is it as good as can be with ordinary BASIC?
    I want to try and get it to run as fast as possible really.
    Any comments would be great but once again thankyou very much for helping me with it Darrel.

    Best regards
    O.T.
    Last edited by opticaltrigger; - 20th January 2010 at 22:45.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    And if we ever meet face to face, I will have you buy me that beer.

    Quote Originally Posted by opticaltrigger View Post
    Im going to swap the LEDs for transistors tomorow and see how it all gets on runing proper ignition coils and making sparks at the spark plugs.
    Well now that you put it that way ... That's the strangest distributor I've ever seen.

    One last thing if I may.As said its my first proper program so I have no bench mark to go by in terms of its efficiency,and I noticed that you had mentioned BASIC PRO might be better.My question is does the code look ok or do you see a way it could be better,or perhaps is it as good as can be with ordinary BASIC?
    When it comes to regular PicBasic ... If it does exactly what you wanted it to do, then it's the perfect program.

    You seem to be on the right track, so I'm sure you'll get the "Perfect" program for the next step too.

    Best regards,
    DT

  7. #7
    Join Date
    Jun 2009
    Location
    Somerset England
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    Well now that you put it that way ... That's the strangest distributor I've ever seen.


    Yeah,it does look kinda strange.
    I removed the rotor arm and put the segmented wheel in its place,the wheel itself is made from the cylinder head off a radio controled nitro car engine.

    Thanks again for your help with it.

    Best regards
    O.T.

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Wink

    Hi, OT

    I do not want to bash ...

    But using Pure Basic to trigger your coils did not need to have killed this poor engine head ... Generally speaking, only asm interrupts can do the job ...

    as your four cylinders ( do not joke ... it's not a flat 8 ??? ) distributor never will have enough accuracy in machining to give precise and separate advance values for the sparks ...

    I send you an example vor a V twin engine ( site has halas been modified ... no more download ! )

    Alain
    Attached Images Attached Images
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  9. #9
    Join Date
    Jun 2009
    Location
    Somerset England
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    Hi Acetronics,and thanks for the input.
    I hear what your saying on the machining tolerences but the system dosent relly on it.
    Fortunatly for me this Boch distributor comes with a reluctor inside of it which sends an AC pulse to the ignition amplifier which in turn switches the coil.So its actualy still runing the factory triggering system.
    I am going to conect the wire from the reluctor to four ignition amps and coils instead of just the one.
    The system that I have constructed on top with the RC cylinder head is used to decide which ignition amp and coil should recive power.They are all actualy being pulsed by the reluctor at once.Ive posititioned the blades on the head to overlap the reluctor by a healthy margin on both sides also.
    I have spun the distributor and tried the program and it does work,should I say it flashes LEDs anyway and all in the right places.
    I have to now try applying 12v to the collectors on my transistors and conecting the emitters up to ignition amps and coils and see what a real world bench test goes like.
    First before I do that (probably tomorow now) I want to conect the scope up to the factory reluctor output from the distributor and the output from my optical sensor with the four blades and see how central I was in my positioning of the head over the reluctor.

    This is my first day at this stuff and Im very aware that my programing skills and understanding are seriously lacking but I thought the only way to learn about somthing is to try.Im an engine guy really and the ability to use this type of thing in that field is fascinating to me,when I think about the applications and possibilities for an engine I can get quite carried away with it.But at the moment my ideas for engine applications with this far outstrip my abbilities with programing.Still,Im having fun learning.

    All the very best wishes and many many thanks for your thoughts Acetronics and please let me have any thoughts you have on it.
    O.T.

Similar Threads

  1. Reading in Manchester code
    By brid0030 in forum Code Examples
    Replies: 0
    Last Post: - 10th March 2009, 21:55
  2. How much code space do PBP statements use.
    By Darrel Taylor in forum Code Examples
    Replies: 5
    Last Post: - 13th February 2009, 21:31
  3. Loop with two motor and 2 sensors
    By MrRoboto in forum mel PIC BASIC
    Replies: 4
    Last Post: - 8th December 2008, 23:40
  4. Making Program Code Space your playground...
    By Melanie in forum Code Examples
    Replies: 15
    Last Post: - 19th July 2008, 08:26
  5. Re-Writing IF-THEN-AND-ENDIF code?
    By jessey in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 18th August 2006, 17:23

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts