PDA

View Full Version : Problem with PIC basic code *.bas



chai98a
- 11th May 2005, 14:02
I have try to make car robot and have problem with my code need all friend to help.

Pls help to verify why did not work thank


'************************************************* ***************
'* Name : IR_car3.BAS *
'* Author : *
'* : *
'* Notice : Copyright (c) 2005 *
'* : ALL Rights Reserved *
'* Date : 10/3/2003 *
'* Version : 1.0 *
'* Notes : *
'* : *
''************************************************ ****************

@ DEVICE PIC16F84a, WDT_OFF,PWRT_ON, PROTECT_OFF

TRISA = %00011111
TRISB = %11111111
DEFINE OSC 4

IRpulse_length var word
xx var Byte
Command Var Byte
Device Var Byte
sw_l var PORTB.0
sw_r var PORTB.1


left var PORTA.0 'turn left
right var PORTA.1 'turn right
fwd var PORTA.2 'go
bkd var PORTA.3 'back

n var Byte
n = 0
'pause 150

enable INTERRUPT

START:
LOW BKD
HIGH FWD
PAUSE 150
If sw_l = 0 then
HIGH BKD
LOW FWD
PAUSE 800
LOW BKD
LOW FWD

else
iF SW_r = 0 THEN
HIGH BKD
LOW FWD
PAUSE 800
LOW BKD
LOW FWD

ENDIF
endif

'Ir decoder

PULSIN Portb.2,0,IRpulse_length(0)
IF IRpulse_length(0) = 0 THEN start

for xx =1 to 18
pulsin Portb.2,0,IRpulse_length(xx)
next xx

disable INTERRUPT
displaybits:
if IRpulse_length(1) < 100 then
Command.bit0 = 0
Else
Command.bit0 = 1
endif
if IRpulse_length(2) < 100 then
Command.bit1 = 0
Else
Command.bit1 = 1
endif
if IRpulse_length(3) < 100 then
Command.bit2 = 0
Else
Command.bit2 = 1
endif
if IRpulse_length(4) < 100 then
Command.bit3 = 0
Else
Command.bit3 = 1
endif
if IRpulse_length(5) < 100 then
Command.bit4 = 0
Else
Command.bit4 = 1
endif

if IRpulse_length(6) < 100 then
Command.bit5 = 0
Else
Command.bit5 = 1
endif

if IRpulse_length(7) < 100 then
Command.bit6 = 0
Else
Command.bit6 = 1
endif
Command.bit7 = 0
Command = Command + 1
If Command = 10 then
Command = 0
Endif

if IRpulse_length(8) < 100 then
Device.bit0 = 0
Else
Device.bit0 = 1
endif
if IRpulse_length(9) < 100 then
Device.bit1 = 0
Else
Device.bit1 = 1
endif
if IRpulse_length(10) < 100 then
Device.bit2 = 0
Else
Device.bit2 = 1
endif
if IRpulse_length(11) < 100 then
Device.bit3 = 0
Else
Device.bit3 = 1
endif
'Device.bit1 = 0

low portb.7

'SEROUT Porta.3,sbaud,[#Device," - ",#Command,13,10]
iF COMMAND = 13 THEN
high left
PAUSE 800

ENDIF
RESUME
ENABLE INTERRUPT
'pause 100
goto start

mister_e
- 11th May 2005, 17:34
Hi chai98a

schematic and a few more details on what you want to do will be appreciated. Tell us what's working and what's not.

chai98a
- 13th May 2005, 06:40
Here is circuit of my project

mister_e
- 13th May 2005, 14:50
O.K. few things, there's no Pull-ups attach to yiur switches, but you can use the internal one... it can help


OPTION_REG.7=0 ' turn on internal Pull-up.

since you didn't declare any interrupt stuff you don't need to write Enable Interupt and Disable Interupt.

Your TRISA definition is not right BUT High and Low do the job for you to correct it. I MEAN TRISA%00001111 will set PORTA<3:0> to input and PORTA<7:4> to output.

what is the actual current needed by you motor? Is there any driver between your PIC and you motors? Are they DC type, Step, Servo?

That's what i see as now ;)

chai98a
- 14th May 2005, 07:45
We use dc motor 3 vdc and driver with Transister (see circuit on attached)
Let you mark new code sample for me too thank you. Plsssss.

mister_e
- 14th May 2005, 14:15
ssssssssssffffff BANG!

I'm looking to your schematic and few things are wrong. Now all your transistors and/or few PIC i/o could be destroyed. by not using any resistor to your transistor base, you ask to PIC to drive as much current as I=E/R... aboute infinite. AND no clamping diode on Transistor while driving inductive load is for sure going to destroy your transistors really soon.

can you, before changing everything, remove your transistor and use Leds, mini lamp, multimeter on PIC output to see if there's any signal that goes out.

you can also use some H-Bridge driver to drive your Motor directly and make your design smaller on the final-Board.

Do you also know the current needed by your Motor... not suppose to be really high. If you plan to use transistor i can do some schematic for you if you need or if you don't find any all over the net... wich i doubt!

BUT if i need to drive a motor and i don't have any H-Bridge driver IC or they're current unsufficient, i'll prefer to use mosfet instead of bjt transistor due to the lowest voltage drop across and many more other reason but that's another story.

chai98a
- 14th May 2005, 15:56
Hi Mister_e
Your right !!! on base of transister need to add R 1k .

I will explan for my problem
1. If we used code #1 (show as below) only the function of software will ok no problem
if push sw_l ,sw_r motor backword and forword ...ok

2. If we add code #2 (show as below) the function not ok
a. the motor forword only not
b. No repond if we push sw_l and sw_r

I think may some error druing pic runing and may hung due to time over .....but i cannot find the problem
...I more exprien for pic (first study)

thank you for advise


My problem :

we used microcode studio for careted code basic code and complier with PBP2.45 .

#1
'================================================= ==
@ DEVICE PIC16F84a, WDT_OFF,PWRT_ON, PROTECT_OFF

OPTION_REG.7=0 ' turn on internal Pull-up.
TRISA = %00001111
TRISB = %11111111

DEFINE OSC 4

IRpulse_length var word
xx var Byte
Command Var Byte
Device Var Byte
sw_l var PORTB.0
sw_r var PORTB.1

left var PORTA.0
right var PORTA.1
fwd var PORTA.2
bkd var PORTA.3

n var Byte
n = 0
pause 150

START:
LOW BKD
HIGH FWD
PAUSE 150
If sw_l = 0 then
HIGH BKD
LOW FWD
PAUSE 800
LOW BKD
LOW FWD
else
iF SW_r = 0 THEN
HIGH BKD
LOW FWD
PAUSE 800
LOW BKD
LOW FWD

ENDIF
endif
goto start





'===========================
#2
'==========================
@ DEVICE PIC16F84a, WDT_OFF,PWRT_ON, PROTECT_OFF

OPTION_REG.7=0 ' turn on internal Pull-up.
TRISA = %00001111
TRISB = %11111111

DEFINE OSC 4

IRpulse_length var word
xx var Byte
Command Var Byte
Device Var Byte
sw_l var PORTB.0
sw_r var PORTB.1

left var PORTA.0
right var PORTA.1
fwd var PORTA.2
bkd var PORTA.3

n var Byte
n = 0
pause 150

START:
LOW BKD
HIGH FWD
PAUSE 150
If sw_l = 0 then
HIGH BKD
LOW FWD
PAUSE 800
LOW BKD
LOW FWD
else
iF SW_r = 0 THEN
HIGH BKD
LOW FWD
PAUSE 800
LOW BKD
LOW FWD

ENDIF
endif

' Ir decode

PULSIN Portb.2,0,IRpulse_length(0)
IF IRpulse_length(0) = 0 THEN start

high portb.7
for xx =1 to 18
pulsin Portb.2,0,IRpulse_length(xx)
next xx

displaybits:
if IRpulse_length(1) < 100 then
Command.bit0 = 0
Else
Command.bit0 = 1
endif
if IRpulse_length(2) < 100 then
Command.bit1 = 0
Else
Command.bit1 = 1
endif
if IRpulse_length(3) < 100 then
Command.bit2 = 0
Else
Command.bit2 = 1
endif
if IRpulse_length(4) < 100 then
Command.bit3 = 0
Else
Command.bit3 = 1
endif
if IRpulse_length(5) < 100 then
Command.bit4 = 0
Else
Command.bit4 = 1
endif

if IRpulse_length(6) < 100 then
Command.bit5 = 0
Else
Command.bit5 = 1
endif

if IRpulse_length(7) < 100 then
Command.bit6 = 0
Else
Command.bit6 = 1
endif
Command.bit7 = 0
Command = Command + 1
If Command = 10 then
Command = 0
Endif

if IRpulse_length(8) < 100 then
Device.bit0 = 0
Else
Device.bit0 = 1
endif
if IRpulse_length(9) < 100 then
Device.bit1 = 0
Else
Device.bit1 = 1
endif
if IRpulse_length(10) < 100 then
Device.bit2 = 0
Else
Device.bit2 = 1
endif
if IRpulse_length(11) < 100 then
Device.bit3 = 0
Else
Device.bit3 = 1
endif
'Device.bit1 = 0

low portb.7

iF COMMAND = 13 THEN
high left
PAUSE 800

ENDIF

goto start

mister_e
- 14th May 2005, 16:45
Many times you use something like:
IRpulse_length(0)

I never see that kind of variable use so the question is...What should it do??? OR what do you want to do. I guess you want to store your results in an array so.. you'll have to declare your variable as an array
something like

IRpulse_length var word [20]

and when you want to store/read from, use
IRpulse_length[ArrayLocation]


The reason why your switches don't work is because of those pulsin. look into your manual section 5.59 DEFINE PULSIN_MAX. This can help but not as efficient as you need to decode IR signal.

In your case i'll prefer measuring incoming pulses by using Timer in an interrupt routine. Interrupt will be given by your IR receiver.. DUH!

chai98a
- 15th May 2005, 04:56
thank you for your advise
i will try it again.