PDA

View Full Version : ? on code



Sam
- 7th March 2004, 16:00
Hi all,
I just started with PBP and an EPIC.

I'm using a 16f84a and I need to have it go high on b.0 for say 1 minute and then go low. All this needs to happen after b.1 goes high and stays high.

Can someone please give me an example code on how to do this?

Thanks,
Sam

NavMicroSystems
- 7th March 2004, 16:12
i Var Byte

Loop:

If PortB.1=1 Then

Hgh PortB.0

for i=1 to 60
Pause 1000
next

Low PortB.0

EndIf

Goto Loop

Sam
- 7th March 2004, 16:37
That did it !

Thank you very much NavMicroSystems.

I'm sure I'll have more ?.

Thanks again,
Sam

Sam
- 7th March 2004, 18:14
Well,
I thought that did it.
Only problem is that the 60 second timing starts (after) high is removed from B.1.

As long as B.1 is high, B.0 stays high.

I've tried modifying but haven't got it yet.

Any suggestions ?

Sam

NavMicroSystems
- 7th March 2004, 18:41
There are many ways, here is one:

this variant sets PortB.0 High for 60 secs. after PortB.1 has gone High.
Then it loops in the DoNothing loop until PortB.1 has gone low again.
Setting PortB.1 high again restarts the process.



i Var Byte

Loop:

If PortB.1=1 Then

High PortB.0

for i=1 to 60
Pause 1000
next

Low PortB.0

EndIf


DoNothing:
If PortB.1=1 Then
Goto DoNothing
EndiF

Goto Loop

Sam
- 7th March 2004, 19:55
OK,
That "almost" does it. But when B.1 goes low again,B.0 reruns the same routine as it does when B.1 goes high.

I tried more code variations on my own and have been through the manual several more times and I've not got it figured out.

Please be patient with me,I WILL learn this.

Your help is much appreciated,
Sam

EDIT: I got b.0 to stay low when B.1 went low by looping IF B.0=0
But, now when B.1 goes high again,B.0 stays low. I need it to re-run each time B.1 goes high.

Thanks

NavMicroSystems
- 7th March 2004, 21:25
How are you setting B.1 low or high ?
are you using a button?

It looks like your "Switch" bounces when toggling.
means it very quickly changes between on and off before it reaches its final position.

The program is that fast that it detects this change.

you need to eliminate that "Key-bounce"

here is one possible way:

i Var Byte

Loop:

If PortB.1=1 Then 'Check HIGH on B.1
Pause 500 ' wait 500ms (you can certainly decrease the Pause time

If PortB.1=1 Then ' Is B1 still HIGH ?

High PortB.0

for i=1 to 60
Pause 1000
next

Low PortB.0

EndIf
EndIf


DoNothing:
If PortB.1=1 Then
Goto DoNothing
EndiF

Goto Loop


Key bounce is actually a "mechanical" problem you have to think of when working with buttons.

Your other questions were regarding basic BASIC stuff.

Now that you have got some examples try to modify the code.
i.e. use the BUTTON statement.

Sam
- 7th March 2004, 22:49
I'm setting B.1 low with a 5k resistor to gnd. and high with wire inserted into breadboard.I'm sure the wire is far worse than a button. I had just tried this:


i Var Byte

Loop:

If PortB.1=1 Then

High PortB.0

for i=1 to 60
Pause 1000
next

Low PortB.0

EndIf


DoNothing:
If PortB.1=1 Then
Goto DoNothing
EndiF

low 0 }I added this
pause 500 } "

if portb.0=0 then} "
Goto Loop

endif

I'm sure you're right about the "bounce" as this fixed the problem.
I'll try your code instead though.
The high to B.1 will actually come from an optoisolator in the final stage. I've got alot more code to figure out on this project and if you or anyone can recomend a book that would help,please do.
Thanks,
Sam

NavMicroSystems,I just sent you a PM.

NavMicroSystems
- 7th March 2004, 23:51
You could try the following books:

Programming PIC Microcontrollers With PicBasic by: by Chuck Hellebuyck
Experimenting with the PicBasic Pro Compiler by: Les Johnson

I have read about them, but I haven't read the books themselves.
(some people may say: it would have been better I had read the books)

Another way of learnig is once you have got a bit familiar with PBP and the PIC Hardware:
look at the many code examples available and try to understand what they are doing (or are supposed to do) rather than just copying them into into your project.

Adittional recommendations:

Try MicroCode Studio http://www.mecanique.co.uk
This is really a great Product and the "Plus" version is worth the money.
The ICD Feature saves a lot of time when debugging.

For small projects try the 16F628 (or even smaller: 12F675)
The 16F628 has more features than the 16F84 and is even cheaper!
And it does not need external parts like a crystal.

If you need more I/O Pins go for the 16F868 / 16F877.

Take care with the 18F series, they appear to be a bit "tricky"
(I'm currently struggling to migrate one of my projects from 16F876 to 18F252.)

Sam
- 8th March 2004, 00:05
Thank you very much,You've been a big help.
I'm sure I'll have more questions though.And I'll check out your recomendations.

Sam

NavMicroSystems
- 8th March 2004, 00:19
SAM, check PM