Let's Make Robots!

Code problems


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.

Test_soft...txt6.82 KB

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Thanks, then i had read it correct :0) nice to know that i did someting correct :0)

in my program at etc. "fuldf", that should be the place where i set my duty cycle for fast forward

fuldf   movlw   0xff

        movwf    CCPR1L

        movlw    0x80

        movwf    CCPR2L

The reason they are different is that the motors dont run at the same speed, so it is to correct that. 

In the subrutine  "init"   i   clrf CCPR1L  and clrf CCPR2L,   so they are cleared. Is that wrong?

Or is it in the subrutine "init" that i shall set my duty cycle, if so how do it get it to drive with different speed?

It's not wrong to clear the CCPRxL registers, but for debugging purposes it's not very useful.
Why don't you just try to get your PWM running correctly at a single speed determined in the init subroutine (50% duty is a good starting point), and then once that's sorted you can work on the code that will control the motor speed in the real program. Since the PWM duty cycle is currently being changed in several locations throughout the code, it becomes harder to determine the source of any problems.

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 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.

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.

Have you changed the duty cycle yet? Your PWM should work as far as I can see, but running with zero duty cycle will make it appear to not work, since it isn't doing anything.

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, "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.

Damm, nice to know. Thanks!

But still got problems with it, it still wont work. I think i had conected all the BC547 wrong, so emitter and collektor had switched places.

Here is my code, as it is now. Think i got the PWM right this time.

The PWM sektion is the only ting that have changed


init bcf STATUS,RP1 ; RP1 sættes til 0   ;Not sure what this is for??
    ; bank 0 og 1 vælgws via
    ; mine defines
 clrf TRISC  ; hele C-porten er udad
 bsf TRISA,0  ; indad til analog sp.

;******OPSÆTNING AF PWM**************************************

 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
 clrf CCPR1L  ;PWM bliver sat til 0, så den kører ingen steder
 clrf CCPR2L
 movlw 0xff  ;Not sure what this is for??
 movwf PR2  ;Not sure what this is for??

 bcf TRISC,1  ;Udgang PWM2, hastighed højre motor
 bcf TRISC,2  ;Udgang PWM1, hastighed venstre motor
 bcf TRISC,0  ;Udgang retning højre motor
 bcf TRISC,3  ;Udgang retning venstre motor
 bsf T2CON,1  ;Sætter Timer2 modul til 16prescale
 bcf T2CON,0  ;Sætter timer2 modul til 16prescale

 bsf T2CON,2  ; UPS glemte at starte timeren i første omgang, denne starter timeren
;******SLUT PÅ OPSÆTNING AF PWM******************************


 bsf ADCON0,7 ; 20 MHz cryst.
 bcf ADCON0,6 ; Neddeling med 32

 bcf ADCON0,5 ; En fast kanal til konvert.
 bcf ADCON0,4 ; Der bruges AN0
 bcf ADCON0,3 ; Kanalen styres af 3 bit

 bcf ADCON1,7 ; Venstrestil de 10 bit
 bcf ADCON1,3 ; Der konverteres fra max.
 bcf ADCON1,2 ; 5 kanaler. Jeg bruger 1.
 bsf ADCON1,1 ; Der konverteres ml. 0 og 5 V.
 bcf ADCON1,0 ; Styres af 4 bit.

 bsf ADCON0,0 ; Sæt strøm på converteren

 call delay
 call delay
 ;****** SLUT PÅ OPSÆTNING AF CONVERTER************************

 ;**************init af øvrige variabler***********************

 movlw 0x32  ;værdierne som der trigges på ved 10 og 20 cm.
 movwf value20  ;skal sættes her 
 movlw 0x1e  ;og
 movwf value10  ;her

 ;**************slut på init af øvrige variabler***************


• 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'

• 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.