Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

At my wits end

Status
Not open for further replies.

zanes

New Member
As mentioned in my previous thread, I'm having real trouble with my photovore.I'm using a PIC16F54 connected to 2 h bridges made of BD139 and BD140s. When I tell the PIC to turn on both h bridges (ie, keep two pins high) it's all fine and dandy. When I load up code to take inputs from 2 LDR/resistor dividers and turn on the motors appropriately, nothing happens, although if I spin the motor shaft the motor will turn and run, but slowly. I've rewritten the code, changed pins, tried a BC337 pre amplifier and tried phototransistors as sensors all to no effect.
Boosting the h bridge voltage to 9v had some effect, but a Nicad 9v pack is far too large for my bot. The PIC is running at 5v, as is the LDR sensors. Trying PICKIT2 +5v and a regulated 5v supply from a PP3 made no difference. The hbridge is running at 3volts.
I know it sounds childish, but I'm really beginning to regret ever starting this project. I've had lots of problems, but I've solved them. I've been working on this one for a day, and I've had no luck. It's the last thing in the way of this project, and it's getting to me.
 
Can you post the code? Put
Code:
 before it and [/c[U][/U]ode] after it so it keeps it's format.

Mike.
 
Code:
;*****
;Alex E-S
;27/4/2008
;Version 2 (original did not work at all)
;Revision 3 (reassigned ports to simplify full h-bridge implementation later)
;for PIC 16F54
;Clock 2.4576MHz
;Function: control file for lightseeking robot.

	list P = 16F54
	include "c:\PIC\P16F5X.inc"
	__config  _XT_OSC&_WDT_OFF&_CP_OFF
;==========

;Declarations:

rmotfor equ 00h
rmotrev equ 01h
lmotfor equ 00h
lmotrev equ 01h
rightldr equ 02h
leftldr equ 03h
porta 	equ 05h
portb   equ 06h
	org 1FFh
	goto Start
	org 0
	
	
;==========

; Subroutines:

Init
	clrf porta ;Resets porta
	clrf portb ;Resets portb
	movlw b'1100' ;Sets porta
	tris porta 		;as outputs
	movlw b'00000000' ;sets portb
	tris portb		  ;as i/o
	retlw 0		    ;resets working reg to 0

;==========when dark sensors go low, when light sensors go high
;Program Start
Start 
	call Init
Main
	bcf porta,rmotfor ;turns both 
	bcf porta,lmotfor ;motors off
	btfsc portb,rightldr ;tests light on right LDR
	goto rightlight		;if light,goto rightlight 
	btfsc portb,leftldr ;tests light on left LDR
	goto leftlight		;if light,goto left light
	goto Main
rightlight
	btfsc portb,leftldr ;tests if light on left LDR too
	goto bothlight		;if there is,goto bothlight
	bsf porta,lmotfor ;if not turns on left motor to turn towards light
	goto Main
leftlight
	btfsc portb,rightldr ;tests if light on left LDR too
	goto bothlight 		 ;if there is,goto bothlight
	bsf porta,rmotfor ;if not turns on right motor to turn towards light
	goto Main
;nolight			;behaviour if no light (rotate set amount?(90 degrees?)
	;goto Main
;	bsf porta,rightmotor  initially no action,call from Main when developed.
bothlight		;behaviour if both light
	bsf porta,rmotfor ;turns on 
	bsf porta,lmotfor  ;both motors (later for set time?)
	goto Main
	END
 
You seem to have both the left and right motor forward signals on PortA,0 and the two reverse signals on PortA,1. How is your H-Bridge connected to your pic?

Mike.
 
Ah, that's a relic from troubleshooting, the left motor is connected to porta 0 and 1. Changing that makes no difference, it was merely something I was trying. H-bridge is connected directly to the PIC (was that what you were asking?)
 
zanes said:
Ah, that's a relic from troubleshooting, the left motor is connected to porta 0 and 1. Changing that makes no difference, it was merely something I was trying. H-bridge is connected directly to the PIC (was that what you were asking?)

hi Zanes,
Just put your program on the Oshonsoft sim, had to use 16F84 for sim.

You are setting PORTB as output, yet you are testing for LDR's on the PORTB inputs.

I have had to move the 'org' 's as the program crashes with a stack fault.!

Hope this helps.
 
Thanks. Where did you move the orgs to? I'll try it tomorrow probably, I've had enough for today.
 
zanes said:
Thanks. Where did you move the orgs to? I'll try it tomorrow probably, I've had enough for today.


Hi,

org 0x0;1FFh
goto Start
org 0x04

The declarations are causing problems in the right/left.
For test do this, use numbers in place of all these labels
bsf porta,0; rmotfor ;turns on
bsf porta,1; lmotfor ;both motors (later for set time?)

And it all works OK...:)
 
So because I've defined two things as "01" and "00" it's having problems? Or shouldn't I be using equ for this stuff (I have no problems using numbers, it just helped me write and think about the code)
 
zanes said:
So because I've defined two things as "01" and "00" it's having problems? Or shouldn't I be using equ for this stuff (I have no problems using numbers, it just helped me write and think about the code)

hi,
I didnt dig too deep why, but the problem was that A.0 was the output selected for both rmotfor and lmotfor.
The correct subr was entered but it was always the A.0 bit that was set..?

Likewise when it was the both subr it was still only A.0.

When I changed these two labels to their numeric value, the prog worked OK.

Does this help.?

Dont forget to TRISB as 0xff, Inputs
 
Hmmm... I think I only changed that as a troubleshooting attempt, and then changed part of the program back. I'll try it, and change those orgs. I also need to reread my pic book it seems....
 
zanes said:
Hmmm... I think I only changed that as a troubleshooting attempt, and then changed part of the program back. I'll try it, and change those orgs. I also need to reread my pic book it seems....

Hi zanes,
Post the latest program and I will re-run it in the Oshonsoft Sim.

As I have said, my Sim dosn't have a 16F54 in its options, but the 16F84 option seems to work OK.
 
Right, here we go. Still the same, LEDs connected to the outputs work perfectly, exactly how I programmed then. When I made a mistake in reworking the code, so one hbridge was on all the time, the motor behaved perfectly. It almost as if either a; The microcontroller is not giving full voltage when acting as a decision maker, or b; The code is acting as a very crude PWM, which is dropping the voltage supplied to the h-bridge, which I find unlikely. Anyway, here's the latest version of the code;
Code:
;*****
;Alex E-S
;27/4/2008
;Version 2 (original did not work at all)
;Revision 4 (rework)
;for PIC 16F54
;Clock 2.4576MHz
;Function: control file for lightseeking robot.

	list P = 16F54
	include "c:\PIC\P16F5X.inc"
	__config  _XT_OSC&_WDT_OFF&_CP_OFF
;==========

;Declarations:

porta 	equ 05h
portb   equ 06h
	org 0x0;1FFh
	goto Start
	org 0x04
	
	
;==========

; Subroutines:

Init
	clrf porta ;Resets porta
	clrf portb ;Resets portb
	movlw b'1100' ;Sets porta
	tris porta 		;as i/o
	movlw b'00000000' ;sets portb
	tris portb		  ;as outputs
	retlw 0		    ;resets working reg to 0

;==========when dark sensors go low, when light sensors go high
;Program Start
Start 
	call Init
Main
	bcf porta,0 ;turns both 
	bcf porta,1
	bcf portb,0 ;motors off
	bcf portb,1
	btfsc porta,2 ;tests light on right LDR
	goto rightlight		;if light,goto rightlight 
	btfsc porta,3 ;tests light on left LDR
	goto leftlight		;if light,goto left light
	goto Main
rightlight
	btfsc porta,3 ;tests if light on left LDR too
	goto bothlight		;if there is,goto bothlight
	bsf portb,0 ;if not turns on left motor to turn towards light
	goto Main
leftlight
	btfsc porta,2 ;tests if light on right LDR too
	goto bothlight 		 ;if there is,goto bothlight
	bsf porta,0 ;if not turns on right motor to turn towards light
	goto Main
;nolight			;behaviour if no light (rotate set amount?(90 degrees?)
	;goto Main
;	bsf porta,rightmotor  initially no action,call from Main when developed.
bothlight		;behaviour if both light
	bsf porta,0 ;turns on 
	bsf portb,0  ;both motors (for set time?)
	goto Main
	
	END
 
hi Alex,
Copied it, I will give a spin, let you know.:)
 
Well I'm absolutely clueless now, with LEDs on the outputs working as intended, and the PIC running the H-bridges fine when not using inputs from the sensors. I must be nearly there!!
 
zanes said:
Well I'm absolutely clueless now, with LEDs on the outputs working as intended, and the PIC running the H-bridges fine when not using inputs from the sensors. I must be nearly there!!

Hi,
Running in Sim OK.

Observed Operation:
RA2 and RA3 low NO flashing RB0 and RA0 are both Low
;
RA2 high starts RB0 flashing.
RA3 high starts RA0 flashing
;
RA2 and RA3 high start RB0 and RA0 flashing.

Observed flashing rates are the same for both pins, when operating separately or together.

What are your sensor voltage levels.? going into the PIC pins.
 
Should be flashing very quickly though? Sensor voltage levels I am not sure, but it's enough to trigger an LED, so is obviously triggering the PIC program.
 
zanes said:
Should be flashing very quickly though? Sensor voltage levels I am not sure, but it's enough to trigger an LED, so is obviously triggering the PIC program.

hi,
In real time, not the Sim, they will be flashing very quickly, you have no Wait states.

The input threshold voltage is just over +2V for a +5V supply.
Do you have a DVM.?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top