Potentiometer A2D and PIC 16F877A for DC motor control with PWM

Discussion in 'Embedded Systems and Microcontrollers' started by zackattach, Apr 23, 2009.

  1. zackattach

    Thread Starter New Member

    Apr 23, 2009
    I want to use a PIC16F877A (I have a boot loader on it) to control the speed of an electric motor. I also want to use the PIC for a few other things not related to this problem so I want to program it in assembly for better programming control (and for the fun of it). Motor speed will be controlled using a potentiometer as input in a voltage divider. The voltage will be measured using RA0/AN0 (or which ever) of the analog pins on the PIC . The measured voltage will used to set the value of a PWM signal that will go to an H-bridge. My oscillator is 16 MHz.

    How can I program the PIC16F877A in assembly code to measure voltage at one of the analog pins and output a PWM signal to an H-bridge? I've managed to put together some code from what I remember and have been able to get from the internet but I'm sure that it needs correcting. What else do I need to add or correct?

    LIST P=16F877A

    org 0x1F00 ; For the boot loader

    ; 01 = B1
    bsf STATUS, RP0
    bcf STATUS, RP1

    ; Set PORTA to input
    movlw B'11111111'
    movwf TRISA

    ; Set PORTE to input
    movlw B'11111111'
    movwf TRISE

    ; Justify left the A2D inputs
    ; All possible pins are analog, VREF+ = VDD, VREF- = VSS
    clrf ADCON1

    ; 00 = B0
    bcf STATUS, RP0

    ; Fosc / 8, RA0/AN0, AD on
    movlw B'01000001'
    movwf ADCON0

    ; 00 = not used, 11 = LSB, 1100 = PWM
    movlw B'00111100'
    movwf CCP1CON

    ; Goto Main
    goto Main

    ; Move the new values from the A2D, ADRESH and ADRESL,
    ; to the PWM values, CCPR1H and CCPR1L, respectively
    movf ADRESH, CCPR1H
    movf ADRESL, CCPR1L

    ; Get the AD measurement
    bsf ADCON0, GO
    btfsc ADCON0, GO
    goto Pause

    call GetAD ; Get the AD measurement
    call SetPWM ; Set the PWM values
    goto Main ; Loop back