Code problems

Test_soft…txt (6984Bytes)

Hi

I am using the LMR Mr. Basic robot chassie and motors.

I have now tried to write a program using aseambler code. It is for a PIC16F876A IC. My program involve the use of the A/D converter, and the PWM (Both PWM outputs) I want my robot to be able to slow down when its IR sensor (sharp) detects someting at about 20cm away from it. And i want it to stop when someting is about 10cm away form it, and then drive in reverse.

(Later i want it to be able to turn, but will have to wait)

Here is my problem, it all worked when i triede it induvidual, but when i put it all together, it wont work. Well if i try and turn my robot ON and OFF maybe 10-20 times, i might be lucky to get either the 2left wheels or the 2right wheels to move. It might be my PWM that is not set correct, but if it is, then i am not sure what i have done wrong. Hope that you can help me.

Please be free if you got any comment about how i can improve my code.

The first thing I noticed is

The first thing I noticed is that the two subroutines ‘fuldf’ and ‘fuldb’ are identical, not sure if this is an error or just inefficient coding.
Why does the subroutine ‘stop’ set the duty cycle to zero? Perhaps it would help if you posted your circuit so we can see how the motors are being driven.

The set of instructions here is quite odd:
bank0
bcf CCP1CON,0
bcf CCP1CON,1
bcf CCP1CON,2
bcf CCP1CON,3

bsf CCP1CON,2
bsf CCP2CON,3

The result is that CCP1CON = B’xx000100’ and CCP2CON = B’xx001000’. This causes CCP1 to enter capture mode, and CCP2 to enter compare mode… probably not the desired effect.

Anyway, I’m out of time for now, I’ll have a look again when I get the chance.

circuit

Thanks for the respond, will try and look at those tings.

Circuit on the way. Will post it when i get my labtop hook up to the internet to night.

The “fuldf” and “fuldb” are where i set the speed for, full speed forward, and full speed reversed.

Maybe you should condense

Maybe you should condense fuldf and fuldb into a single subroutine to save program memory and possible confusion.

sorry for the delay

hi, sorry for the delay.

Here is the curcuit, think you might to click on it before it work.

 

til_lmr_fuldt_diagram_fors__g.pdf|x

• You need to rearrange

• You need to rearrange your relay contacts, current both sets of contacts go to either 4.5V or transistors bridge at the same time.

• Once the relays connections are corrected, those 1μF bipolar capacitors will be subjected to reverse polarity which may damage them.

• Probably a good idea to put limiting resistors on the bases of your TIP31A transistors.

resistor

Wont it destroy the darlington kopling effekt if i add a resistor at the base of TIP31 and Emitter on BC547?

Thanks dident see that i had made a drawning error at the relays.

The bipolar elektrolyt dont have a + or - so dont think anyting will happen to it.

Do you mean Darlington? Your

Do you mean Darlington? Your transistors aren’t connected in the Darlington Pair configuration here, as the collectors are not joined together. In any case adding a resistor wouldn’t destroy the effect - the base current into the BC547 is still being amplified by both transistors in series, for a multiplied total gain. If appropriately chosen the resistor won’t decrease the saturated collector current through the TIP31A, but it will prevent the transistors from exceeding their max current ratings. It might actually be better to put the resistor on the collector of the BC547 instead of the base of the TIP31A.

If the electrolytic capacitor doesn’t have +/- markers it’s probably not a bipolar, but a non-polarised capacitor.

my foult

Hmm damm thought that i had made it correct. Well it should still work. So my BC547 is then almost like a driver for the TIP31.

Might be a good idea with that ekstra resistor.

On second thoughts, will the bc547 still be able to handle the PWM with help from TIP31?

Yep, the BC547 will PWM

Yep, the BC547 will PWM fine, but once again you’ll need to keep the frequency at a suitable maximum for the transistor to work properly.

frequency question

Hi again

When i am looking on the datasheet over the TIP31A, then it says someting about duty cycle

(puls duration = 300us, duty cycle ≤2%) How do i calculate its min. pwm and its max. pwm?

And do the bc547 got any ting to say, about the min and max pwm signal it can handle?

Min PWM duty cycle is 0%,

Min PWM duty cycle is 0%, max is 100%. If you look at it closely that note in the datasheet is just a reference to the test conditions used when calculating the transistor’s electrical properties.

If you set the duty cycle to 100% it’s the same as just turning your transistors on with no PWM. If your transistor can handle being fully on continuously, then there is no max duty cycle.
The only time you need to worry about max duty cycle is if the transistor is running overcurrent.

reset?

Hi

Do you need to define the /MCLR (reset) in the code, or is that only if you want to use it?

I have put my /MCLR direct to +5V.

In my code it start with:

org 0x00 --> Isent this the adres for reset?

goto main

org 0x0a --> I am not completly sure what this is for, it where in a aplication i read, but no info about what it is for.

On some PIC micros you can

On some PIC micros you can reassign !MCLR and use the pin for something else, but this is not the case for the 16F876A. !MCLR/Vpp is always assigned to that pin, and since it can never be anything else you don’t need to assign it. Tying it to +5V is fine for almost every application, since you can just toggle the power switch to cause the micro to reset.

ORG 0x00 is indeed the Reset Vector address, so whenever your micro resets it will start executing code from there, which causes the program to jump straight to the main subroutine which is the easiest thing to do if you don’t care what the cause of the reset was. ORG 0x0A isn’t really useful for the 16F876A, it just starts the main code a little way into the program memory. If you change 0x0A to 0x05 you’ll be able to use a little more space in the program memory before you run out, since 0x05 is the first free space you can write to.

• All of this

• All of this code:
“bcf CCP1CON,0 ;Reset modul 1
 bcf CCP1CON,1
 bcf CCP1CON,2
 bcf CCP1CON,3
  
 bsf CCP1CON,2 ;PWM mode, modul 1
 bsf CCP1CON,3

 bcf CCP2CON,0 ;Reset modul 2
 bcf CCp2CON,1
 bcf CCP2CON,2
 bcf CCP2CON,3

 bsf CCP2CON,2 ;PWM mode, modul 2
 bsf CCP2CON,3”

…can be replaced by this:
“MOVLW   b’00001100’
MOVWF    CCP1CON
MOVWF    CCP2CON”

• Since both CCPRxL registers are cleared, as well as bits 4 & 5 from the CCPxCON register, the PWM duty cycle for both PWM modules is zero. This means that your outputs aren’t doing anything very exciting at the moment.

• The instructions “movlw 0xff, movwf PR2” set Timer2’s Period Register, which is used to determine the PWM period and therefore frequency. 0xFF is the maximum value, meaning that the PWM period will be the longest, and so the PWM frequency will be the slowest.

Nice

So with other words, the reason why it wont drive any where, is becaus my pwm dosent work…

Thought i had it right this time :0(

Damm only 3 lines, that does the same as the 12 lines i wrote. Damm nice, thanks!

Is there any page in the datasheet over the PIC, where i can read about how to set that value b’00001100’?

Look up section 8.0,

Look up section 8.0, “Register 8-1: CCP1CON Register/CCP2CON Register”. All the info for setting up the CCPxCON registers is in there, and further down in section 8.3 is the PWM specific stuff.
This should be your first reference for any questions on PWM.

jep

Jep have looked at those pages. Still they are alittle confusing to use, when you have never tried it before.

I used them together with a tutorial from winpicprog.co.uk. Great side.

Also got alot of help from that page, for my A/D konverter.

And what i can read from those pages, my PWM should work.

timer2?

The line where i start timer2, can i wirte it like this:     bsf   T2CON,2      

Or does it have to be like this:                                   bsf  T2CON, TMR2ON   ?

If you’re using the standard

If you’re using the standard includes then TMR2ON is defined as 2, so both instructions are the same. You can open up the include file and see what all the assignments are.