Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

This stuff is tricky.. Help with my code please

Status
Not open for further replies.

piersuk

Junior Member level 2
Joined
May 10, 2009
Messages
22
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,427
Sorry for being such a newbie and having no idea what I'm doing, but I'm trying to work things through.

What I'm trying to do is drive a DC motor at different voltages with a 2 second delay inbetween each step. I have tried the below which I hoped would be two steps 3.8v and 6v but alas no.

Any help with the code would be appreciated. In addition I would like to know what and where to add each voltage step piece of code.

This is my code
Code:
LIST   P=PIC16F819, r=hex, f=INHX8M    

    INCLUDE "p16f819.inc"

    __CONFIG 3909H

;********* program proper starts here *************************************

    ORG     00H            ; reset vector
    GOTO     START

    ORG     010H

START    
        ; these 3 lines set the period of the pulsing (39 kHz)
        MOVLW    01FH
        BSF     STATUS,RP0        
        MOVWF    PR2
    
        ; make all ports outputs (only B1 is used)
        CLRF    TRISB
        BCF        STATUS,RP0

        ; configure as PWM mode
        MOVLW     B'00001100'
        MOVWF    CCP1CON
 
        ; the value of CCPRIL sets the duty cycle and therefore the voltage 
        ;MOVLW    010H        ; gives 2.52 V
        MOVLW    018H        ; gives 3.8 V
       ;MOVLW    022H 	      ; 6v
        MOVWF    CCPR1L        

        ; turn the PWM on
        BSF      T2CON, TMR2ON    ;and start the timer running

		; Delay = 2 seconds
; Clock frequency = 4 MHz

; Actual delay = 2 seconds = 2000000 cycles
; Error = 0 %

	cblock
	d1
	d2
	d3
	endc

Delay
			;1999996 cycles
	movlw	0x11
	movwf	d1
	movlw	0x5D
	movwf	d2
	movlw	0x05
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;4 cycles (including call)


        ; these 3 lines set the period of the pulsing (39 kHz)
        MOVLW    01FH
        BSF     STATUS,RP0        
        MOVWF    PR2
    
        ; make all ports outputs (only B1 is used)
        CLRF    TRISB
        BCF        STATUS,RP0

        ; configure as PWM mode
        MOVLW     B'00001100'
        MOVWF    CCP1CON
 
        ; the value of CCPRIL sets the duty cycle and therefore the voltage 
        ;MOVLW    010H        ; gives 2.52 V
        ;MOVLW    018H        ; gives 3.8 V
         MOVLW    022H        ; gives 6.0v
        MOVWF    CCPR1L        

        ; turn the PWM on
        BSF      T2CON, TMR2ON    ;and start the timer running


FIN    NOP
    GOTO    FIN

    END

Thanks

A Slow Learner ! :cry::cry:
 

You could make the delay a subroutine.
You need to put a starting address in data memory on the end of your cblock directive.
Use mplab sim, it is very good. Enable Trace all in Debugger settings and use the logic analyzer window.
The pwm output is on Pin RB2.


Code:
LIST   P=PIC16F819, r=hex, f=INHX8M    

    INCLUDE "p16f819.inc" 

    __CONFIG 3909H 

;********* program proper starts here ************************************* 

        cblock 0x20 
        d1 
        d2 
        d3 
        endc 

        ORG     00H            ; reset vector 
        GOTO    START 

        ORG     010H 

START    
        ; these 3 lines set the period of the pulsing (39 kHz) 
        MOVLW   01FH 
        BSF     STATUS,RP0        
        MOVWF   PR2 
    
        ; make all ports outputs (only B1 is used) 
        CLRF    TRISB 
        BCF     STATUS,RP0 

        ; configure as PWM mode 
        MOVLW   B'00001100' 
        MOVWF   CCP1CON 
        clrf    CCPR1L
        BSF     T2CON, TMR2ON    ;and start the timer running 

Loop
        ; the value of CCPRIL sets the duty cycle and therefore the voltage 
        MOVLW   010H        ; gives 2.52 V
        MOVWF   CCPR1L
        call    Delay 

        MOVLW   018H        ; gives 3.8 V 
        MOVWF   CCPR1L 
        call    Delay       

        goto    Loop
        
;--- 2 second delay    

Delay   movlw   0xff 
        movwf   d1 
        movlw   0xff 
        movwf   d2 
        movlw   0x0b 
        movwf   d3 

Delay_0 decfsz  d1,f 
        goto    Delay_0 
        decfsz  d2,f 
        goto    Delay_0  
        decfsz  d3,f 
        goto    Delay_0 
        return
        
        END
 

Thanks - I am complete newbie with this stuff, a friend wrote the PWM bit for me and I had a go at the delay.

If I wanted to add a third voltage after a two second delay where would that site in the revised code?

Thanks
 

In the loop bit
Code:
Loop 
        ; the value of CCPRIL sets the duty cycle and therefore the voltage 
        MOVLW   010H        ; gives 2.52 V 
        MOVWF   CCPR1L 
        call    Delay 

        MOVLW   018H        ; gives 3.8 V 
        MOVWF   CCPR1L 
        call    Delay        

        MOVLW   ??H        ; new voltage here 
        MOVWF   CCPR1L 
        call    Delay        

        goto    Loop
 

    piersuk

    Points: 2
    Helpful Answer Positive Rating
Thanks, I had a play and tried that, was just about to post an update when you replied. :D Thanks for taking the time though.

Next step is to drive two motors in the same way, at the same time (though with different voltages at different times).

Is that possible with the 16F819?

Ultimately I want to drives three motors in this way, again using different voltages, independent of each other...

This is good stuff.

Piers
 

The Pic 819 only has 1 pwm module and 1 pwm output.
To drive 3 motors independantly of each other with different powers you will need to use a micro designed for motor control.
You will be looking at the Pic18 or the 16-bit dsPics.
The dsPic30f2010 is a good micro for motor control, and cheap enough.
You can download the C compiler for free (Student version) and move into programming in C. The 2010 is a true 16-bit micro, a quantum leap in power.
A whole new ball game.
 

    piersuk

    Points: 2
    Helpful Answer Positive Rating
Thanks for the info...

A whole new ball game, this whole thing is a season of ball games :D:D

Maybe I'll stick with controlling this voltage with a serial connection first.

I doubt it will be too long before I'm posting for those details.

Thanks again

Piers
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top