PDA

View Full Version : Could I please ask for some help on my code



opticaltrigger
- 20th January 2010, 00:17
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

http://i749.photobucket.com/albums/xx133/opticaltrigger/SD530507.jpg
http://i749.photobucket.com/albums/xx133/opticaltrigger/SD530510.jpg

Darrel Taylor
- 20th January 2010, 01:46
bit0 and bit1 are aliases to bits in the B0 byte variable

So when you do something like ...
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,

opticaltrigger
- 20th January 2010, 02:06
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.

Darrel Taylor
- 20th January 2010, 02:24
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.


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

opticaltrigger
- 20th January 2010, 22:41
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.

Darrel Taylor
- 20th January 2010, 23:35
And if we ever meet face to face, I will have you buy me that beer. :D


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,

opticaltrigger
- 21st January 2010, 00:34
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.

Acetronics2
- 22nd January 2010, 09:26
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

opticaltrigger
- 24th January 2010, 23:45
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.