PDA

View Full Version : 16F628 problem



Brown
- 20th March 2005, 20:43
Hi, I just built an infrared robot using the 16F628's PWM module to activate the IR LEDs. The 2 IR LEDs are placed on either side of an IR receiver. I used 2 servos to make it move. I'm using 4 AA batteries for power. At first I couldn't get the servos to change direction, so I added a 1000uF capacitor between vdd and ground. Then it started working. Now, here is the new problem. When the robot encounters a wall, (lets just say the wall is on the robot's right side) it doesn't turn long enough to get away from the wall. As a result the robot ends up nearly parallel to the wall. I figured that I should add a "For...Next" statement to make the turning period longer. However, I just cannot get it to work! I have no idea what i'm doing wrong. Here, look at my program.
Symbol cmcon = $1F
Symbol ccp1con = $17
Symbol ccpr1l = $15
Symbol pr2 = $92
Symbol t2con = $12
Symbol trisb = 134
Symbol portb = 6
Poke cmcon, 7
Poke ccpr1l, 5 ' duty cycle to 20%
Poke pr2, 25 ' PWM to 38.4kHz
Poke t2con, 4
Poke trisb, 4
Low 6 ' this is the left servo
Low 7 ' this is the right servo
main:
B0 = 0
B3 = 0
B4 = 0
Poke ccp1con, 12
Low 0 ' this turns on right IR LED
High 1 ' this turns off left IR LED
Pause 1
Peek portb, B0 ' this finds value of IR receiver output
IF bit2 = 0 Then setleft ' 0 = something detected, 1 = nothing
B1 = 1
GoTo here1
setleft:
B1 = 0
here1:
High 0
Low 1
Pause 1
Peek portb, B0
IF bit2 = 0 Then setright
B2 = 1
GoTo here2
setright:
B2 = 0
here2:
High 0
High 1
Poke ccp1con, 0
IF B1 = 1 AND B2 = 1 Then forward ' nothing there
IF B1 = 0 AND B2 = 1 Then left ' something on right side
IF B1 = 1 AND B2 = 0 Then right ' something on left side
IF B1 = 0 AND B2 = 0 Then backward ' both
forward:
B3 = 200
B4 = 100
GoTo pulse
left:
High 5 ' turn on indicator LED
B3 = 100
B4 = 100
GoTo pulse
right:
High 4 ' turn on indicator LED
B3 = 200
B4 = 200
GoTo pulse
backward:
High 4 ' turn on indicator LED
High 5 ' turn on indicator LED
B3 = 100
B4 = 200
GoTo pulse
pulse: ' WHY WON'T THIS PART WORK?
B5 = 0 ' it never goes longer that a single pulse
For B5 = 0 to 10
PulsOut 6, B3
PulsOut 7, B4
Pause 20
Next B5
Low 4
Low 5
GoTo main

Please help.
Thanks.

Dwayne
- 6th May 2005, 19:56
Hello Brown,

Brown>>
B5 = 0 ' it never goes longer that a single pulse
For B5 = 0 to 10
PulsOut 6, B3
PulsOut 7, B4
Pause 20
Next B5
Low 4
Low 5
<<

A couple of things to look at..

1. the Pause 20... change it to a higher value... 20 milliseconds is only 1 50th of a second. pretty short time to turn. maybe you want something like 200 or 300. for a pause. That is 1/5 of a second. I see that you try to adjust the timeing to 200 mils with the pause 20 and counter. Since I do not know how your setup is.... I am assuming you want a little longer of a pulse. Thus, I would do something a little different....like the following....You will have a hard time convincing me that only a single pulse is happening... I think all your pulses are happening, just way to quick for your project to react the way you want.

B5 = 0 ' it never goes longer that a single pulse
For B6= 0 to 10
For B5 = 0 to 60 'change this value and see what happens....200?
PulsOut 6, B3
PulsOut 7, B4
Pause 1
Next B5
Pause 20
NEXT B6.
Low 4
Low 5


Dwayne