PDA

View Full Version : Please explain about this program



wafagenius
- 5th October 2006, 10:59
I need someone to help me explain about this program and tell me if there is error occur. I will attached the circuit altogether if someone know about the circuit error if any.I am new in picbasic and electronic,I beg all expert out there, please help me......!

' PicBasic Pro program
' 10-bit A/D conversion
' Balancing robot using Sharp GP2D12
' Use a 16f876 pic mcu from microchip

define osc 20
' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result was 10
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS was 50
define DEBUG_REG PORTB
define DEBUG_BIT 5
define DEBUG_BAUD 9600
define DEBUG_MODE 1
define DEBUG_PACING 500


tiltfor var word ' ET Sensor Forward
tiltbac var word ' ET sensor Back
lwheel var word ' Left wheel zero speed
rwheel var word ' Right wheel zero speed
speed var word ' speed variable
k con 7 ' k factor
smax con 500 ' speed limiter

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %10000010 ' Set PORTA analog and right justify result


'lwheel=747 'zero calibrate left wheel
'rwheel=747 'zero calibrate right wheel

speed=0
start:

debug "Balbot ET"
pause 1000
high portc.2
pause 1000
low portc.2

adcin 1, lwheel 'read trimmer for left wheel zero speed

adcin 2, rwheel 'read trimmer for right wheel zero speed

loop:

adcin 0, tiltfor ' Read channel 0 to adval

adcin 4, tiltbac

speed = abs (tiltfor - tiltbac)* k

if speed > smax then speed = smax

if tiltbac > tiltfor then back


forward:

low portc.0
pulsout portc.0,(lwheel + speed) 'left wheel
pause 5
low portc.1
pulsout portc.1,(rwheel - speed) 'right wheel
pause 5

goto loop

back:

low portc.0
pulsout portc.0,(lwheel - speed) 'left wheel
pause 5
low portc.1
pulsout portc.1,(rwheel + speed) 'right wheel
pause 5

goto loop

End

HenrikOlsson
- 5th October 2006, 13:16
Hi,
You say please explain this program but you need to be more specific. What is it that doesn't work? Does the PIC run? Does it have power? Do you get the debug message? Do you get the reading from the ADC? Does the robot run but will not balance?

You say that you are new to PBP, and that's OK, we've all been there, but if you need us to explain the whole program you are aiming to high for a beginner. If that's the case start with the standard LED blinking and hello world projects, then go from there.

If you have specific qestions, don't hesitate.

/Henrik Olsson.

wafagenius
- 6th October 2006, 01:20
The PIC is running,it have power,there is a reading on ADC,there is no error message while compiling.However after burn it to PIC only one of the motor working.I have no idea what is going on. in configuration bits the setting is "oscillator is HS mode" and "watchdog timer is ON" and other configuration is off.I have attached the circuit,I really need help

HenrikOlsson
- 6th October 2006, 06:32
I looked at the code and it seems that if one wheel is turning the other should too, codewise. I think you have an electrical problem or perhaps a bad servo.

Try swapping the left and right servo connectors to see if it's the actual servo that doesnt work. If the one that doesn't work now start working when you swap places it's either something in the code or a bad connection on the circuitboard. Do you have 6V and GND on both servo connectors?

It can also be a bad pin on the PIC, can you try moving the servo control lines from PortC.0 and PortC.1 to PortB.0 and PortB.1 for example. Don't forget to change the code accordingly.

/Henrik Olsson.

wafagenius
- 7th October 2006, 10:56
Thanks anyway MR HenrikOlsson due to your explaination. I really appreciated it. I will look into what you have suggested.

Regards

Hasnul Wafa

Archangel
- 8th October 2006, 04:07
wafagenius,
Addition to other's suggestions: Tie all unused PIC pins to ground or power, do not allow pins to float, many strange and unwanted things occur with open pins.
Your schematic shows many unused pins.
Joe

wafagenius
- 10th October 2006, 01:25
Thanks Bro for the suggestion.