Controlling servo with pic16f1789 for car backseat table
Servo motor :
Servo motor is a dc motor with a derive circuit and position sensor for precise movements. position sensor used in servo motor is normally potentiometer attached to gears of motor.
Derive circuit typically consists of
- pulse width modulation to voltage converter
- feedback from potentiometer
- supply voltages
Control signal :
control signal is most complex signal out of three signal required to operate DC Servo motor . Other two signals are 0 v and 5 volts out of battery. To generate control signal we are using Pic 16f1789 micro-controller in pwm (pulse width modulation ) mode.
Setting Pic 16f1789 in PWM mode :
To configure micro-controller in pwm mode first we have to configure oscillator of Pic 16f1789 up-to our requirements . configure oscillator to 125 KHz internal oscillator. configure device for pwm mode and interrupt on change mode. Load values of wave's period and duty cycle in pr2 and corresponding CCPRxL registers .
Motor used :
Motor used in this project is dc servo motor having rotation 0-90 degrees. 1 ms pulse for minimum rotation and 2 ms pulse for full rotation operating at 50 Hz .
Code for Controlling Servo using Pic 16f1789 :
This code is written to operate servo motor according to open/close position of control button of Car backseat tray system . you can use code under the main heading if only fixed pulse is required
#include "p16F1789.inc"
; __config 0x2FE4
__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
; CONFIG2
; __config 0x3EFF
__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOR_OFF & _LVP_ON
ORG 000H
GOTO MAIN
ORG 004H
GOTO INTERRUPT_SELECT
ORG 020H
INTERRUPT_SELECT
BTFSS INTCON,IOCIF
RETFIE
BANKSEL INLVLA
BTFSC IOCAF,0
GOTO OPEN_CLOSE
NOP
RETFIE
OPEN_CLOSE
BANKSEL PORTA
BTFSC PORTA,0
GOTO OPEN
BTFSS PORTA,0
GOTO DEFAULT
RETURN
OPEN
BANKSEL ODCONA
BCF CCP1CON,DC1B1
BCF CCP1CON,DC1B0
MOVLW B'00000100'
MOVWF CCPR1L
BANKSEL INLVLA
BCF IOCAF,0
RETFIE
DEFAULT
BANKSEL ODCONA
BCF CCP1CON,DC1B1
BCF CCP1CON,DC1B0
MOVLW B'00000010'
MOVWF CCPR1L
BANKSEL INLVLA
BCF IOCAF,0
RETFIE
MAIN
BSF INTCON,GIE
BSF INTCON,PEIE
BSF INTCON,IOCIE
BANKSEL ANSELA
BCF ANSELA,0
BANKSEL INLVLA
BSF IOCAP,0
BSF IOCAN,0
BANKSEL TRISA
BSF TRISA,0
MOVLW B'00101000'
MOVWF OSCCON
CLRF OSCTUNE
BSF TRISC,2
BANKSEL ODCONA
MOVLW B'00001100'
MOVWF CCP1CON
MOVLW B'00000010'
MOVWF CCPR1L
BANKSEL PORTA
MOVLW .38
MOVWF PR2
MOVLW B'00000110'
MOVWF T2CON
BANKSEL TRISA
BCF TRISC,2
BANKSEL INLVLA
BCF IOCAF,0
HERE
GOTO HERE
END
Comments
Post a Comment