have a look at my pwm motor controller on 12f675

Started by shaunundergrad August 19, 2003
hi guys,
just completed my pwm motor control program, please have look and see
if there is any obvious mistakes, it was written on mplab uses
special instruction mnemonics so probably only looks correct in
mplab. it works ok but the frequency a bit low. gives reasonable
output range about 5% to 96% of vdd. any advice on improving
appreciated.

best regards

shaun
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;;
;;;
ANALOGUE INPUT AN0 ;;;
;;; PWM
OUTPUT ON GP1 ;;;
;;; TO "H"
BRIDGE MOTOR DRIVER ;;;
;;;
WRITTEN BY SHAUN COLLIER ;;;
;;; 09-08-
03 ;;;
;;; 1K
POT AND 100 OHM RESISTOR ;;;
;;; USED AS
POTENTIAL DIVIDER ;;;
;;; TO GIVE
0.5V TO 2.9V ON AN0 ;;;
;;;
STEADY 960 Hz ;;;
;;;
BOTTOM OF H BRIDGE RANGE ;;;
;;;
;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; LIST PF675, RIMAL
#INCLUDE <P12F675.INC>

ERRORLEVEL -302 ;MICROCHIP RECOMMENDED (KNOWN
BUG)

;CONFIG
SETTINGS
;CODE
PROTECTION DISABLED
;WATCHDOG
TIMER DISABLED
;MASTER CLEAR
INTERNAL ENABLED
;POWER UP
TIMER DISABLED
;BROWN OUT
DETECT DISABLED
;INTERNAL RC
OSCILLATOR ENABLED 4Mhz
__CONFIG _CP_OFF & _WDT_OFF & _MCLRE_OFF & _PWRTE_OFF
&_BODEN_OFF & _INTRC_OSC_NOCLKOUT
ORG 000
GOTO MAIN

;EQUATES TABLE
DELAY_TIMER EQU 0X20
PWM_TIMER EQU 0X21
PWM_CYCLE EQU 0X22
ON EQU 0X23
OFF EQU 0X24

DELAY
MOVLW 25
MOVWF DELAY_TIMER
DECFSZ DELAY_TIMER,F
GOTO $-1
RETURN INITIALISE_AD ;INITIALISES THE A/D
CONVERSION MODULE

BANKSEL TRISIO ;BANK 1
MOVLW B'00001001' ;MAKE GPIO,0 INPUT, GPIO,3
INPUT ONLY
MOVWF TRISIO
BSF PIE1,ADIE ;BANK 1
;PERIPHERAL
INTERUPT ENABLE
;A/D
INTERUPT, 1=ENABLE, 0=DISABLE
MOVLW B'01110001'
MOVWF ANSEL ;BANK 1
;ANALOG
SELECT REGISTER
;BIT 7,
UNIMPLEMENTED
;BITS 6-4,
A/D CONVERSION CLOCK SELECT, x11=FRC, DEDICATED INTERNAL CLOCK,
500KHz MAX
;BITS 3-0,
ANALOG SELECT BITS, 0=DIGITAL OR SPECIAL FUNCTIONS, 1=ANALOG INPUT
BANKSEL ADCON0
MOVLW B'00000001'
MOVWF ADCON0 ;BANK 0
;BIT 7, ADFM,
A/D RESULT JUSTIFICATION, 0=LEFT, 1=RIGHT
;BIT 6, VCFG,
VOLTAGE REF, 0=Vdd, 1=EXTERNAL REF
;BITS 5-4,
UNIMPLEMENTED
;BITS 3-2,
ANALOG CHANNEL SELECT 00=AN0,01=AN1,10=AN2,11=AN3
;BIT 1,
GO_DONE, A/D STATUS BIT, 1=START AQUISITION, AUTOMATICALLY CLEARED
WHEN FINISHED
;BIT 0, A/D
CONVERSION STATUS, 1=A/D ON, 0=A/D OFF
BSF INTCON,PEIE ;BANK 0
;INTERUPT
CONTROL REGISTER
;PERIPHERAL
INTERUPT ENABLE,1=ENABLES UNMASKED INTERUPTS, 0=DISABLED

BCF PIR1,ADIF ;A/D CONVERSION
COMPLETE FLAG, 1=COMPLETE, 0=INCOMPLETE, MUST CLEARED IN SOFTWARE
CALL DELAY ;TIME PERIOD REQUIRED TO
ACQUIRE SIGNAL
RETURN INITIALISE_DIGITAL
BANKSEL TRISIO
CLRF TRISIO ;CLEAR PORT DIRECTION REG
BANKSEL PIE1 ;CLEAR PERIPHERAL INTERUPT
ENABLE
CLRF PIE1
BANKSEL ADCON0
CLRF ADCON0 ;CLEAR ANALOGUE CONFIG REG
MOVLW 0x07
BANKSEL CMCON
MOVWF CMCON ;SWITCH OFF COMPARATOR
BANKSEL ANSEL
CLRF ANSEL ;SWITCH ALL ANALOGUES OFF
RETURN

OUTPUT
MOVLW 255 ;PWM TIME PERIOD
MOVWF PWM_CYCLE
CLRF OFF ;CLEAR OFF TIMER
CLRF ON ;CLEAR PULSE PERIOD
TIMER
BANKSEL ADRESH
MOVFW ADRESH ;MOVE MSB'S ANALOGUE INPUT
INTO W REG
MOVWF OFF ;MOVE ANALOGUE INTO
OFF REGISTER
NEGF OFF,W ;COMPLEMENT AND INCREMENT,
SAVE BACK IN FILE
MOVFW OFF ;MOVE NEGATIVE
ANALOGUE INTO W REG
ADDWF PWM_CYCLE,W ;ADD TO PWM TIME PERIOD TO
GAIN OFF TIME
MOVWF OFF
INCF OFF,F ;INCREMENT TO ENSURE NO ROLL
OVER
BANKSEL GPIO
BCF GPIO,1 ;ENSURE PULSE OFF
MOVFW OFF
MOVWF PWM_TIMER
DECFSZ PWM_TIMER,F ;COUNTDOWN OFF PERIOD
GOTO $-1
BANKSEL GPIO
BSF GPIO,1 ;SWITCH PULSE ON
BANKSEL ADRESH
MOVFW ADRESH
;MOVWF ON
;MOVFW ON
BANKSEL PWM_TIMER
MOVWF PWM_TIMER
INCF PWM_TIMER,F ;INCREMENT TO ENSURE NO ROLL
OVER
DECFSZ PWM_TIMER,F
GOTO $-1
BCF GPIO,1 ;SWITCH PULSE OFF
RETURN
MAIN
CALL INITIALISE_AD ;SWITCH A/D MODULE ON AND
CONFIGURE
BANKSEL ADCON0
BSF ADCON0,GO ;START CONVERSION OF
ACQUIRED ANALOGUE SIGNAL
BTFSC ADCON0,GO ;TEST TILL CONVERSION
FINISHED THEN SKIP
GOTO $-1
CALL INITIALISE_DIGITAL ;SWITCH ANALOGUE A/D MODULE
OFF
CALL OUTPUT ;PWM ROUTINE
GOTO MAIN ;KEEP ON GOING
END