jean12
Advanced Member level 2
- Joined
- Aug 27, 2013
- Messages
- 529
- Helped
- 5
- Reputation
- 12
- Reaction score
- 6
- Trophy points
- 18
- Activity points
- 5,497
Hi,my codes are as follows
;unfortunately I am not able to adjust the speed of asynchronous Motor and also find below the PWM signals I am getting(from CCP1,CCP2 and CCP3) I think they can`t be used for three phase system,
please help!!
PHP:
LIST P = P16F1936
__CONFIG H'8007',H'3FAA'
__CONFIG H'8008',H'3FBC'
INCLUDE <P16F1936.INC>
INCLUDE <PARAMETERS.INC>
;**************************************************************************************
UDATA_SHR
W_TEMP RES 1 ;LOCATION FOR SAVING 'WREG' DURING INTERRUPT
STATUS_TEMP RES 1 ;LOCATION FOR SAVING 'STATUS' DURING INTERRUPT
FLAGS RES 1 ;FLAGS REGISTERS USED TO INDICATE DIFFERENT STATUS
FREQ_REF_H RES 1 ;REFERANCE FREQUENCY INPUT(MSB) IN TIME COUNTS
FREQ_REF_L RES 1 ;REFERANCE FREQUENCY INPUT(LSB) IN TIME COUNTS
SET_FREQ RES 1 ;REFERANCE FREQUENCY INPUT IN COUNTS
TEMP_LOC RES 1 ;GENERAL PURPOSE TEMPORARY LOCATION
TEMP_LOC_1 RES 1 ;GENERAL PURPOSE TEMPORARY LOCATION
TEMP_LOC_2 RES 1 ;GENERAL PURPOSE TEMPORARY LOCATION
NO_1_LSB RES 1 ;NUMERATOR(LSB) AND QUOTIENT(LSB) OR MULTIPLIER(LSB)
NO_1_MSB RES 1 ;NUMERATOR(MSB) AND QUOTIENT(MSB) OR MULTIPLIER(MSB)
NO_2_LSB RES 1 ;DENOMINATOR(LSB) OR MULTIPLICAND(LSB)
NO_2_MSB RES 1 ;DENOMINATOR(MSB) OR MULTIPLICAND(MSB)
RESULT_LSB RES 1 ;RESULT OF MULTIPLICATION(LSB)/REMAINDER(LSB)
RESULT_MSB RES 1 ;RESULT OF MULTIPLICATION(MSB)/REMAINDER(MSB)
VDC_COUNT RES 1 ;DIGITAL COUNT OF DC BUS VOLTAGE
UDATA
TABLE_OFFSET1 RES 1 ;PHASE1 OFFSET TO THE SINE TABLE(0)
TABLE_OFFSET2 RES 1 ;PHASE2 OFFSET TO THE SINE TABLE(120)
TABLE_OFFSET3 RES 1 ;PHASE3 OFFSET TO THE SINE TABLE(240)
TMR2_INT_COUNT RES 1 ;COUNTER TO MEASURE 4MS USING TMR2 OV INT
SINE_TABLE_RAM RES 0X14 ;SINE TABLE
EXTERN SET_ADC_GO
EXTERN NEW_FREQ
EXTERN FLAG_FREQ
EXTERN DEC_COUNTER_LSB
EXTERN DEC_COUNTER_MSB
EXTERN ACC_COUNTER_LSB
EXTERN ACC_COUNTER_MSB
EXTERN TEMP_COUNTER_LSB
EXTERN TEMP_COUNTER_MSB
EXTERN TEMP_FREQ
GLOBAL SET_FREQ
GLOBAL FLAGS
GLOBAL NO_1_LSB
GLOBAL NO_1_MSB
GLOBAL NO_2_LSB
GLOBAL NO_2_MSB
GLOBAL SET_FREQ
GLOBAL FREQ_REF_L
GLOBAL FREQ_REF_H
GLOBAL TEMP_LOC
GLOBAL TEMP_LOC_1
GLOBAL TEMP_LOC_2
GLOBAL RESULT_MSB
GLOBAL RESULT_LSB
;**************************************************************************************
;MACRO FOR MULTIPLICATION - 8X8 UNSIGNED
;**************************************************************************************
MULT MACRO BIT ;MACRO FOR UNSIGNEDMULTIPLICATION
BTFSC NO_1_LSB,BIT
ADDWF RESULT_MSB,F
RRF RESULT_MSB,F
RRF RESULT_LSB,F
ENDM ;END OF MACRO FOR MULTIPLICATION
;**************************************************************************************
;POWER ON RESET VECTOR LOCATION
;**************************************************************************************
STARTUP CODE 0X00 ;RESET VECTOR ADDRESS
GOTO START
CODE 0X04 ;INTERRUPT VECTOR LOCATION
GOTO ISR_INT ;GOTO INTERRUPT SERVICE ROUTINE
;**************************************************************************************
PROG0 CODE
START
;**************************************************************************************
;INITIALIZATION OF THE PORTS AND TIMERS
BANKSEL WDTCON
CLRF WDTCON
CLRF FLAGS ;CLEAR ALL FLAGS
BANKSEL FLAG_FREQ
CLRF FLAG_FREQ ;CLEAR ALL FLAGS
BANKSEL TRISB
BSF TRISB,3
BSF TRISB,5
BSF TRISC,2
MOVLW 0XD7
MOVWF TRISB
BANKSEL TRISA
MOVLW 0X0F
MOVWF TRISA ;Set RA0,RA1,RA2,RA3,RA4 as input pins on PORTA
BANKSEL TRISC
MOVLW 0x03
MOVWF TRISC ;Set PORTC as output PORT
BANKSEL PORTC
BCF PORTC,4
BCF PORTC,5
BCF PORTC,7
;BSF TRISC,2
; BSF TRISB,3
; BSF TRISB,5
BANKSEL PR2
MOVLW PR2_VALUE ;LOAD PR2 VALUE TO PR2 REGISTER
MOVWF PR2
MOVLW 0X0C
BANKSEL CCP1CON
MOVWF CCP1CON ;CCP1,CCP2 AND CCP3 CONFIGURED TO PWM
BANKSEL CCP2CON
MOVWF CCP2CON
BANKSEL CCP3CON
MOVWF CCP3CON
BANKSEL CCPTMRS0 ;select the TMR2 on which CCP1,CCP2 and CCP3 are based
MOVLW 0x00
MOVWF CCPTMRS0
BCF T2CON,0
BCF T2CON,1
BANKSEL APFCON
MOVLW 0XFF
MOVWF APFCON
MOVLW 0X10
BANKSEL PWM1CON
MOVWF PWM1CON
BANKSEL PWM1CON
MOVWF PWM2CON
BANKSEL PWM3CON
MOVWF PWM3CON
BANKSEL CCP1AS
MOVLW 0X0F
MOVWF CCP1AS
BANKSEL CCP2AS
MOVWF CCP2AS
BANKSEL CCP3AS
MOVWF CCP3AS
BANKSEL PSTR1CON
MOVLW 0X1F
MOVWF PSTR1CON
BANKSEL PSTR2CON
MOVWF PSTR2CON
BANKSEL PSTR3CON
MOVWF PSTR3CON
CALL STOP_MOTOR ;STOP MOTOR
BANKSEL T2CON
BSF T2CON,TMR2ON ;TIMER2 ON
CALL COPY_TABLE_TO_RAM ;COPY SINE TABLE FROM PROGRAM MEMORY TO RAM FOR FASTER ACCESS
;CALL STOP_MOTOR ;STOP MOTOR
;**************************************************************************************
;INITIALIZE ADC REGISTERS
;**************************************************************************************
BANKSEL ADCON0
MOVLW 0b00000111
;MOVLW 0X91
MOVWF ADCON0 ;CONFIGURE FOR 32TOSC AND CHANNEL FOR CONVERSION - RA2 (DC BS VOLTAGE)
BANKSEL ADCON1
MOVLW 0B00100111
;MOVLW 0x0B ;CONFIGURE RA0-RA3 AS ANALOG INPUT
MOVWF ADCON1
BANKSEL ANSELA
MOVLW d'0'
MOVWF ANSELA
;MOVWF ADCON2
;MOVLW 0X31 ;SETTING OF ACQUISITION TIME FOR ADC (~25usec)
;MOVWF ADCON0 ;TO BE CHECKED for equivalence/////////////////////////////////////////////////////////////////////////////
MOVLW 0XFF ;RA0-RA3 INPUT and RA4-RA7 OUTPUT
MOVWF TRISA
;**************************************************************************************
;TIMRE1 INITIALIZATION WITH PRESCALER - USED FOR SETTING OF MOTOR FREQ.
;**************************************************************************************
MOVLW 0X31 ;LOAD THE T1CON WITH CONTROL WORD
MOVWF T1CON ;FOR TMR1 ON AND PRESCALAR IS 1:8, INTERNAL CLOCK
;**************************************************************************************
;TIMRE0 INITIALIZATION WITH PRESCALER - USED FOR ACCELERATION,DECELERATION
;AND ADC TRIGGER FOR FREQ. CONV.
;**************************************************************************************
BANKSEL INTCON
CLRF INTCON ;DISABLE ALL INTERRUPTS AND FLAGS ASSOCIATED WITH
CLRF PIR1 ;DISABLE ALL INTERRUPT FLAGS
CLRF PIR2 ;DISABLE ALL INTERRUPT FLAGS
BANKSEL PIE1
BANKSEL PIE2
CLRF PIE1 ;DISABLE ALL INTERRUPTS
CLRF PIE2 ;DISABLE ALL INTERRUPTS
MOVLW 0X03 ;SET #POR AND #BOR FLAGS
MOVWF PCON
BSF INTCON,INTE ;ENABLE RB PORT CHANGE INTERRUPT FOR RB4
BSF INTCON,PEIE ;PERIPHERAL INTERRUPTS ENABLE
BSF INTCON,GIE ;GLOBAL INTERRUPT ENABLE
;**************************************************************************************
;INFINITE LOOP
;**************************************************************************************
LOOP
BTFSS FLAGS,TIMER1_OV_FLAG ;IS TMR1 OV FLAG SET?
GOTO BYPASS ;NO
BCF FLAGS,TIMER1_OV_FLAG ;CLEAR TMR1 OV FLAG
CALL UPDATE_PWM_DUTYCYCLES ;YES, UPDATE THE PWM DUTY CYCLE WITH NEW VALUE
CALL UPDATE_TABLE_OFFSET ;UPDATE 3 OFFSETS
BYPASS
CALL SET_ADC_GO ;START AD CONVERSION AND CALCULATE TMR1 RELOAD VALUES BASED ON POT SETTING
CALL KEY_CHECK ;CHECK KEYS CHANGE
GOTO LOOP ;GO BACK TO MAIN LOOP
;******************************************************************
;INTERRUPT SERVICE ROUTINE
;******************************************************************
ISR_INT
MOVWF W_TEMP ;COPY W TO A TEMPORARY REGISTER
SWAPF STATUS,W ;SWAP STATUS NIBBLES AND PLACE INTO W REGISTER
MOVWF STATUS_TEMP ;SAVE STATUS TO A TEMPORARY REGISTER IN BANK0
BANKSEL PIR1
BTFSC INTCON,INTF ;RB INTERRUPT? External interupt test
GOTO CHECK_FAULT ;YES - OVER CURRENT FAULT
nop
BTFSC PIR1,TMR1IF ;TIMER1 OVERFLOW INTERRUPT?
GOTO TIMER1_OVERFLOW ;YES - INTERRUPT FOR UPDATING TMR1 REGISTERS BASED ON POT SETTING
BTFSC PIR1,ADIF ;ADC CONVERSION COMPLETE INTERRUPT?
GOTO AD_CONV_COMPLETE ;YES - CONVERSON RESULT 'F - MOTOR FREQUENCY' OR 'V - DC BUS VOLTAGE'
BTFSC PIR1,TMR2IF
GOTO TMR2_ISR
POPUP ;RETRIEVE SAVED WREG AND STAUS REGISTER VALUES
SWAPF STATUS_TEMP,W ;SWAP ORIGINAL STATUS REGISTER VALUE INTO W (RESTORES ORIGINAL BANK)
MOVWF STATUS ;RESTORE STATUS REGISTER FROM W REGISTER
SWAPF W_TEMP,F ;SWAP W_TEMP NIBBLES AND RETURN VALUE TO W_TEMP
SWAPF W_TEMP,W ;SWAP W_TEMP TO W TO RESTORE ORIGINAL W VALUE WITHOUT AFFECTING STATUS
RETFIE ;RETURN FROM INTERRUPT
;******************************************************************
TIMER1_OVERFLOW ;TMR1 OVERFLOW ISR
MOVF FREQ_REF_H,W
MOVWF TMR1H ;LOAD THE HIGHER BYTE OF SPEEDCOMMAND TO TMR1H
MOVF FREQ_REF_L,W
MOVWF TMR1L ;LOAD THE LOWER BYTE OF SPEEDCOMMAND TO TMR1L
BSF FLAGS,TIMER1_OV_FLAG
BCF PIR1,TMR1IF ;CLEAR TMR1IF
GOTO POPUP
;******************************************************************
CHECK_FAULT
MOVF PORTB,F ;READ PORTB TO ITSELF TO END MISMATCH CONDITION
BTFSS FLAGS,MOTOR_RUNNING
GOTO NO_ACTION
BTFSS PORTB,FAULT_BIT ;CHECK FOR FAULT BIT
GOTO THERE_IS_FAULT ;YES, FAULT OCCURRED, STOP MOTOR
CALL RUN_MOTOR_AGAIN ;FAULT CLEARED?
NO_ACTION
BCF INTCON,INTF ;RUN MOTOR AGAIN
GOTO POPUP
THERE_IS_FAULT ;YES,FAULT IS THERE
CALL STOP_MOTOR ;STOP MOTOR
BCF INTCON,INTF
GOTO POPUP
;*****************************************************************
AD_CONV_COMPLETE ;ADC INTERRUPT
BCF PIR1,ADIF ;ADIF FLAG IS CLEARED FOR NEXT INTERRUPT
BTFSS FLAGS,V_F_FLAG ;DOES CONVERSION RESULT REFLECT DC BUS VOLTAGE?
GOTO CONV_IS_FREQ ;NO - READ RESULT AS POT SETTING OF FREQ.
GOTO CONV_IS_BUS_VOLTAGE ;YES - READ RESULT AS DC BUS VOLTAGE
CONV_IS_FREQ
BANKSEL NEW_FREQ
MOVF ADRESH,W ;READ AD CONVERSION RESULT
MOVWF NEW_FREQ ;CHECK FOR LOWER AND UPPER ALLOWED LIMIT OF FREQ.
SUBLW 0X13 ;MINIMUM FREQUENCY SET TO 5HZ (SCALING FACTOR X4)
BTFSS STATUS,C ;IS POT SETTING FOR FREQ, MORE THAT LOWER SET LIMIT?
GOTO CHECK_UPPER_LIMIT_FREQUENCY ;YES - NOW CHECK UPPER LIMIT
MOVLW 0X14 ;NO - SET FREQ. TO LOWER ALLOWED LIMIT - 5HZ (X4)
MOVWF NEW_FREQ
GOTO POPUP
CHECK_UPPER_LIMIT_FREQUENCY
MOVLW 0XF1
SUBWF NEW_FREQ,W
BTFSS STATUS,C ;IS POT SETTING MORE THAN ALLOWED UPPER LIMIT OF FREQ?
GOTO POPUP ;NO - RETURN FROM INTERRUPT
MOVLW 0XF0 ;YES - SET FREQ TO UPPER ALLOWED LIMIT - 60HZ (X4)
MOVWF NEW_FREQ
GOTO POPUP
CONV_IS_BUS_VOLTAGE
MOVF ADRESH,W
MOVWF VDC_COUNT
SUBLW LIMIT_V_LOW ;UV LIMIT SET TO 20% OF 115VAC = 92VAC
BTFSS STATUS,C ;IS DC BUS VOLTAGE LESS THAN LOWER LIMIT?
GOTO CHECK_UPPER_LIMIT_VOLTAGE ;NO - NOW CHECK UPPER LIMIT
BANKSEL TRISC
BSF TRISC,4 ;YES - TURN ON LED TO INDICATE SAME
CALL STOP_MOTOR ;STOP MOTOR
BCF FLAGS,MOTOR_RUNNING ;CLEAR MOTOR_RUNNING FLAG
GOTO POPUP ;RETURN FROM INTERRUPT
CHECK_UPPER_LIMIT_VOLTAGE
MOVLW LIMIT_V_HIGH ;OV LIMIT SET TO 20% OF 115VAC = 138VAC
SUBWF VDC_COUNT,W
BTFSS STATUS,C ;IS DC BUS VOLTAGE MORE THAN UPPER LIMIT?
GOTO VOLTAGE_WITHIN_LIMIT ;NO
BANKSEL TRISC
BSF TRISC,5 ;YES - TURN ON LED TO INDICATE SAME
CALL STOP_MOTOR ;STOP MOTOR
BCF FLAGS,MOTOR_RUNNING ;CLEAR MOTOR_RUNNING FLAG
GOTO POPUP ;RETURN FROM INTERRUPT
VOLTAGE_WITHIN_LIMIT
BANKSEL TRISC
BSF TRISC,4 ;DC BUS VOLTAGE WITHIN SPECIFIED LIMIT
BCF TRISC,5
GOTO POPUP ;RETURN FROM INTERRUPT
;******************************************************************
TMR2_ISR
BCF PIR1,TMR2IF
BANKSEL TMR2_INT_COUNT
DECFSZ TMR2_INT_COUNT,F
GOTO POPUP
MOVLW 0X50
MOVWF TMR2_INT_COUNT
BANKSEL FLAG_FREQ
BSF FLAG_FREQ,DELAY_4MS ;SET 4MS DELAY FLAG
#IFNDEF RAMP
GOTO POPUP
#ELSE
BTFSS FLAG_FREQ,CHANGE_FREQ ;IS CHANGE IN FREQ REQUIRED?
GOTO POPUP ;NO - RETURN TO MAIN ROUTINE
BTFSC FLAG_FREQ,DEC_ACC_F ;IS DECELEARTION REQUIRED?
GOTO ACC_FREQ ;NO - ACCELERATION IS REQUIRED
DEC_FREQ
BANKSEL TEMP_COUNTER_LSB
DECFSZ TEMP_COUNTER_LSB,F ;IS DEC_COUNTER_LSB ZERO AFTER DECREMENT?
GOTO POPUP ;NO - COME OUT OF INTERRUPT
BANKSEL TEMP_COUNTER_MSB
MOVF TEMP_COUNTER_MSB,F
BTFSS STATUS,Z ;IS DEC_COUNTER_MSB ZERO?
GOTO DECREASE_TEMP_DEC_COUNTER_MSB ;NO
DECF SET_FREQ,F ;DECREASE SET_FREQ COUNT BY 1
BANKSEL DEC_COUNTER_LSB ;RELOAD DEC_COUNTER TO TEMP_COUNTER
MOVF DEC_COUNTER_LSB,W
BANKSEL TEMP_COUNTER_LSB
MOVWF TEMP_COUNTER_LSB
BANKSEL DEC_COUNTER_MSB
MOVF DEC_COUNTER_MSB,W
BANKSEL TEMP_COUNTER_MSB
MOVWF TEMP_COUNTER_MSB
GOTO POPUP
DECREASE_TEMP_DEC_COUNTER_MSB
DECF TEMP_COUNTER_MSB,F ;DECREASE DEC_COUNTER_MSB BY 1
GOTO POPUP
ACC_FREQ
BANKSEL TEMP_COUNTER_LSB
DECFSZ TEMP_COUNTER_LSB,F ;IS ACC_COUNTER_LSB ZERO AFTER DECREMENT?
GOTO POPUP ;NO - COME OUT OF INTERRUPT
BANKSEL TEMP_COUNTER_MSB
MOVF TEMP_COUNTER_MSB,F
BTFSS STATUS,Z ;IS ACC_COUNTER_MSB ZERO?
GOTO DECREASE_TEMP_ACC_COUNTER_MSB ;NO
INCF SET_FREQ,F ;INCREASE SET_FREQ COUNT BY 1
BANKSEL ACC_COUNTER_LSB ;RELOAD ACC_COUNTER TO TEMP_COUNTER
MOVF ACC_COUNTER_LSB,W
BANKSEL TEMP_COUNTER_LSB
MOVWF TEMP_COUNTER_LSB
BANKSEL ACC_COUNTER_MSB
MOVF ACC_COUNTER_MSB,W
BANKSEL TEMP_COUNTER_MSB
MOVWF TEMP_COUNTER_MSB
GOTO POPUP
DECREASE_TEMP_ACC_COUNTER_MSB
DECF TEMP_COUNTER_MSB,F ;DECREASE ACC_COUNTER_MSB BY 1
GOTO POPUP
#ENDIF
;*************************************************************************
;THIS ROUTINE WILL UPDATE THE PWM DUTY CYCLE ON CCPX ACCORDING TO THE
;OFFSET TO THE TABLE WITH 0-120-240 DEGREES.
;THIS ROUTINE SCALES THE PWM VALUE FROM THE TABLE BASED ON THE FREQUENCY TO KEEP V/F
;CONSTANT AND LOADS THEM IN APPROPRIATE CCPX REGISTER DEPENDING ON SETTING OF FWD/REV FLAG
;*************************************************************************
UPDATE_PWM_DUTYCYCLES
MOVLW LOW(SINE_TABLE_RAM)
MOVWF FSR1L ;BASE ADDRESS OF SINE TABLE IN RAM IS LOADED TO FSR /added by me
MOVLW HIGH(SINE_TABLE) ;Added by me
MOVWF FSR1H
BANKSEL TABLE_OFFSET1
MOVF TABLE_OFFSET1,W ;TABLE_OFFSET1 IS COPIED TO WREG
MOVIW 0[FSR1]
ADDWF FSR1,F ;ADRESS TO BE READ=SINE TABLE BASE ADRESS + TABLE_OFFSET1
BANKSEL SINE_TABLE_RAM
MOVF INDF1,W ;COPY SINE TABLE VALUE, POINTED BY FSR, TO WREG
BTFSC STATUS,Z ;CHECK IS VALUE READ ZERO?
GOTO PWM1_IS_0 ;YES, GOTO PWM1_IS_0
MOVWF NO_1_LSB ;NO, SINE TABEL VALUE X SET_FREQ TO SCALE TABLE VALUE BASED ON FREQUENCY SETTING
CALL MUL_8X8 ;CALL ROUTINE FOR UNSIGNED 8x8 BIT MULTIPLICATION
MOVF RESULT_MSB,W ;8 MSB OF 16 BIT RESULT IS STORED
MOVWF TEMP_LOC ;AT TEMP_LOC - THIS REPRESENT PWM DUTY CYCLE VALUE FOR PHASE 1
GOTO UPDATE_PWM2 ;GO FOR UPDATING PWM DUTY CYCLE FOR 2ND PHASE
PWM1_IS_0
CLRF TEMP_LOC ;CLEAR PWM DUTY CYCLE VALUE FOR PHASE 1
UPDATE_PWM2
MOVLW LOW(SINE_TABLE_RAM)
MOVWF FSR1L ;BASE ADDRESS OF SINE TABLE IN RAM IS LOADED TO FSR /added by me
MOVLW HIGH(SINE_TABLE) ;Added by me
MOVWF FSR1H
BANKSEL TABLE_OFFSET2
MOVF TABLE_OFFSET2,W ;TABLE_OFFSET1 IS COPIED TO WREG
MOVIW 0[FSR1]
ADDWF FSR1,F ;ADRESS TO BE READ=SINE TABLE BASE ADRESS + TABLE_OFFSET1
BANKSEL SINE_TABLE_RAM
MOVF INDF1,W ;COPY SINE TABLE VALUE, POINTED BY FSR, TO WREG
BTFSC STATUS,Z ;CHECK IS VALUE READ ZERO?
GOTO PWM2_IS_0 ;YES, GOTO PWM2_IS_0
MOVWF NO_1_LSB ;NO, SINE TABEL VALUE X SET_FREQ TO SCALE TABLE VALUE BASED ON FREQUENCY SETTING
CALL MUL_8X8 ;CALL ROUTINE FOR UNSIGNED 8x8 BIT MULTIPLICATION
MOVF RESULT_MSB,W ;8 MSB OF 16 BIT RESULT IS STORED
MOVWF TEMP_LOC_1 ;AT TEMP_LOC_1 - THIS REPRESENT PWM DUTY CYCLE VALUE FOR PHASE 2
GOTO UPDATE_PWM3 ;GO FOR UPDATING PWM DUTY CYCLE FOR 3RD PHASE
PWM2_IS_0
CLRF TEMP_LOC_1 ;CLEAR PWM DUTY CYCLE VALUE FOR PHASE 2
UPDATE_PWM3
MOVLW LOW(SINE_TABLE_RAM)
MOVWF FSR1L ;BASE ADDRESS OF SINE TABLE IN RAM IS LOADED TO FSR /added by me
MOVLW HIGH(SINE_TABLE) ;Added by me
MOVWF FSR1H
BANKSEL TABLE_OFFSET3
MOVF TABLE_OFFSET3,W ;TABLE_OFFSET1 IS COPIED TO WREG
MOVIW 0[FSR1]
ADDWF FSR1,F ;ADRESS TO BE READ=SINE TABLE BASE ADRESS + TABLE_OFFSET1
BANKSEL SINE_TABLE_RAM
MOVF INDF1,W ;COPY SINE TABLE VALUE, POINTED BY FSR, TO WREG
BTFSC STATUS,Z ;CHECK IS VALUE READ ZERO?
GOTO PWM3_IS_0 ;YES, GOTO PWM3_IS_0
MOVWF NO_1_LSB ;NO, SINE TABEL VALUE X SET_FREQ TO SCALE TABLE VALUE BASED ON FREQUENCY SETTING
CALL MUL_8X8 ;CALL ROUTINE FOR UNSIGNED 8x8 BIT MULTIPLICATION
MOVF RESULT_MSB,W ;8 MSB OF 16 BIT RESULT IS STORED
MOVWF TEMP_LOC_2 ;AT TEMP_LOC_2 - THIS REPRESENT PWM DUTY CYCLE VALUE FOR PHASE 3
GOTO SET_PWM12 ;GO FOR CHECKING DIRECTION OF MOTOR ROTATION REEQUIRED
PWM3_IS_0
CLRF TEMP_LOC_2 ;CLEAR PWM DUTY CYCLE VALUE FOR PHASE 3
SET_PWM12
BANKSEL CCPR1L
BTFSS FLAGS,MOTOR_DIRECTION ;IS MOTOR_DIRECTION FLAG SET FOR FORWARD ROTATION?
GOTO ROTATE_REVERSE ;NO - GO FOR REVERSE ROTATION
MOVF TEMP_LOC,W
MOVWF CCPR1L ;COPY TEMP_LOC AND TEMP_LOC_1 VALUES TO
MOVF TEMP_LOC_1,W ;CCPR1L AND CCPR2L RESPECTIVELY FOR
MOVWF CCPR2L ;FOR FORWARD ROTATION OF MOTOR
BANKSEL TEMP_LOC_2
MOVF TEMP_LOC_2,W
MOVWF CCPR3L ;COPY TEMP_LOC_2 TO CCPR3L
BANKSEL TRISC
BSF TRISC,3 ;TURN ON FWD_REV_LED TO INDICATE FORWARD ROTATION OF MOTOR
RETURN
ROTATE_REVERSE
MOVF TEMP_LOC_1,W ;COPY TEMP_LOC_1 AND TEMP_LOC VALUES TO
BANKSEL CCPR1L
MOVWF CCPR1L ;CCPR1L AND CCPR2L RESPECTIVELY FOR
MOVF TEMP_LOC,W ;FOR REVERSE ROTATION OF MOTOR
BANKSEL CCPR2L
MOVWF CCPR2L
BANKSEL TEMP_LOC_2
MOVF TEMP_LOC_2,W
BANKSEL CCPR3L
MOVWF CCPR3L ;COPY TEMP_LOC_2 TO CCPR3L
BANKSEL TRISC
BCF TRISC,3 ;TURN OFF FWD_REV_LED TO INDICATE REVERSE ROTATION OF MOTOR
RETURN
;*******************************************************************************
;THIS ROUTINE UPDATES THE OFFSET POINTERS TO THE TABLE AFTER EVERY ACCESS
;*******************************************************************************
UPDATE_TABLE_OFFSET
BANKSEL TABLE_OFFSET1
BTFSS FLAGS,OFFSET1_FLAG ;IF SET INCR. ON TABLE
GOTO DECREMENT_OFFSET1
MOVLW (SINE_TABLE_ENTRIES-1) ;CHECK FOR THE LAST VALUE ON THE TABLE
SUBWF TABLE_OFFSET1,W
BTFSC STATUS,C
GOTO CLEAR_OFFSET1_FLAG
INCF TABLE_OFFSET1,F ;INCREMENT OFFSET1
GOTO UPDATE_OFFSET2
CLEAR_OFFSET1_FLAG
BCF FLAGS,OFFSET1_FLAG
DECREMENT_OFFSET1
DECFSZ TABLE_OFFSET1,F ;DECREMENT OFFSET1
GOTO UPDATE_OFFSET2
BSF FLAGS,OFFSET1_FLAG
UPDATE_OFFSET2
BANKSEL TABLE_OFFSET2
BTFSS FLAGS,OFFSET2_FLAG ;IF SET INCR. ON TABLE
GOTO DECREMENT_OFFSET2
MOVLW (SINE_TABLE_ENTRIES-1) ;CHECK FOR THE LAST VALUE ON THE TABLE
SUBWF TABLE_OFFSET2,W
BTFSC STATUS,C
GOTO CLEAR_OFFSET2_FLAG
INCF TABLE_OFFSET2,F ;INCREMENT OFFSET2
GOTO UPDATE_OFFSET3
CLEAR_OFFSET2_FLAG
BCF FLAGS,OFFSET2_FLAG
DECREMENT_OFFSET2
DECFSZ TABLE_OFFSET2,F ;DECREMENT OFFSET2
GOTO UPDATE_OFFSET3
BSF FLAGS,OFFSET2_FLAG
UPDATE_OFFSET3
BANKSEL TABLE_OFFSET3
BTFSS FLAGS,OFFSET3_FLAG ;IF SET INCR. ON TABLE
GOTO DECREMENT_OFFSET3
MOVLW (SINE_TABLE_ENTRIES-1) ;CHECK FOR THE LAST VALUE ON THE TABLE
SUBWF TABLE_OFFSET3,W
BTFSC STATUS,C
GOTO CLEAR_OFFSET3_FLAG
INCF TABLE_OFFSET3,F ;INCREMENT OFFSET3
RETURN
CLEAR_OFFSET3_FLAG
BCF FLAGS,OFFSET3_FLAG
DECREMENT_OFFSET3
DECFSZ TABLE_OFFSET3,F ;DECREMENT OFFSET3
RETURN
BSF FLAGS,OFFSET3_FLAG
RETURN
;*******************************************************************************
;THIS ROUTINE CHECKS FOR THE KEYS STATUS. 2 KEYS ARE CHECKED, RUN/STOP AND
;FORWARD(FWD)/REVERSE(REV)
;*******************************************************************************
KEY_CHECK
BANKSEL PORTB
;BTFSS PORTB,RUN_STOP_KEY ;IS RUN_STOP_KEY PRESSED?
BTFSS PORTB,3
GOTO RUN_STOP_KEY_PRESSED ;YES
BTFSC FLAGS,MOTOR_RUNNING ;IS MOTOR RUNNING?
GOTO FWD_REV_CHECK ;YES
RETURN
RUN_STOP_KEY_PRESSED
BTFSS FLAGS,MOTOR_RUNNING ;IS MOTOR ALREADY RUNNING?
GOTO RUN_MOTOR ;MO
CALL STOP_MOTOR ;STOP MOTOR
BANKSEL TRISC
;BCF LED_PORT,RUN_STOP_LED
BCF TRISC,7
BCF FLAGS,MOTOR_RUNNING ;CLEAR FLAG TO INDICATE THE SAME
WAIT_1
;BTFSS PORTB,RUN_STOP_KEY ;INFINITE LOOP TILL THE TIME PRESSED SWITCH IS RELEASE3
BTFSS PORTB,3
GOTO WAIT_1
RETURN
RUN_MOTOR
CALL RUN_MOTOR_AGAIN
BANKSEL TRISC
BSF TRISC,7
BSF FLAGS,MOTOR_RUNNING
WAIT_2
;BTFSS PORTB,RUN_STOP_KEY ;INFINITE LOOP TILL THE TIME PRESSED SWITCH IS RELEASED
BTFSS PORTB,3
GOTO WAIT_2
FWD_REV_CHECK
BANKSEL PORTB
;BTFSC PORTB,FWD_REV_KEY ;FWD/REV KEY PRESSED?
BTFSC PORTB,1
RETURN ;NO
CALL STOP_MOTOR
MOVLW 0X64 ;ADJUST THIS PARAMETER TO SET TIME BETWEEN CHANGE OF DIRECTION OF ROTOATION OF MOTOR
MOVWF TEMP_LOC_2
WAIT_FOR_MOTOR_TO_STOP
CALL DELAY
DECFSZ TEMP_LOC_2,F
GOTO WAIT_FOR_MOTOR_TO_STOP
CALL RUN_MOTOR_AGAIN
BANKSEL PORTB
MOVF FLAGS,W
ANDLW 0X01
BTFSS STATUS,Z
GOTO SET_REV_DIRECTION
SET_FWD_DIRECTION
BSF FLAGS,MOTOR_DIRECTION
WAIT_3
;BTFSS PORTB,FWD_REV_KEY ;INFINITE LOOP TILL THE TIME PRESSED SWITCH IS RELEASED
BTFSS PORTB,1
GOTO WAIT_3
RETURN
SET_REV_DIRECTION
BCF FLAGS,MOTOR_DIRECTION
WAIT_4
;BTFSS PORTB,FWD_REV_KEY ;INFINITE LOOP TILL THE TIME PRESSED SWITCH IS RELEASED
BTFSS PORTB,1
GOTO WAIT_4
RETURN
;*******************************************************************************
;THIS ROUTINE STOPS THE MOTOR BY DRIVING THE PWMS TO 0% DUTY CYCLE. ALSO DISABLE
;SELECT INTERRUPT TO MAINTAIN STOP CONDITION OF MOTOR
;*******************************************************************************
STOP_MOTOR
BANKSEL PIE1
BCF PIE1,TMR1IE
BCF PIE1,TMR2IE
BCF PIE1,ADIE
BCF FLAGS,TIMER1_OV_FLAG
CLRF CCPR3L
BANKSEL PORTA
BCF PORTA,DRIVER_ENABLE
CLRF CCPR1L
CLRF CCPR2L
BANKSEL TABLE_OFFSET1
CLRF TABLE_OFFSET1
BANKSEL TABLE_OFFSET2
CLRF TABLE_OFFSET2
BANKSEL TABLE_OFFSET3
CLRF TABLE_OFFSET3
MOVLW 0X14
MOVWF SET_FREQ ;RESET MOTO FREQUENCY TO 5Hz
BANKSEL NEW_FREQ
MOVWF NEW_FREQ
BANKSEL TEMP_FREQ
MOVWF TEMP_FREQ
RETURN
;*******************************************************************************
;THIS ROUTINE STARTS MOTOR FROM PREVIOUS STOP WITH MOTOR PARAMETERS INITIALIZED
;*******************************************************************************
RUN_MOTOR_AGAIN
CALL INIT_MOTOR_PARAMETERS
BANKSEL PIE1
BSF PIE1,TMR1IE
BSF PIE1,ADIE
BSF PIE1,TMR2IE
BANKSEL TMR2_INT_COUNT
MOVLW 0X50
MOVWF TMR2_INT_COUNT
RETURN
;*******************************************************************************
;THIS ROUTINE INITIALIZES THE PARAMETERS REQUIRED FOR MOTOR INITIALIZATION.
;*******************************************************************************
INIT_MOTOR_PARAMETERS
BANKSEL TABLE_OFFSET1
MOVLW 0X09 ;INITIALIZE THE TABLE OFFSETS TO 3 REGISTERS
MOVWF TABLE_OFFSET1 ;SUCH THAT THEY HAVE 120 DEG. PHASE SHIFT WITH EACH OTHER
BANKSEL TABLE_OFFSET2
MOVLW 0X03
MOVWF TABLE_OFFSET2
BANKSEL TABLE_OFFSET3
MOVLW 0X0F
MOVWF TABLE_OFFSET3
BSF FLAGS,OFFSET1_FLAG ;OFFSET FLAGS INITIALIZATION
BCF FLAGS,OFFSET2_FLAG
BCF FLAGS,OFFSET3_FLAG
BANKSEL TMR1H
MOVLW 0XF9 ;TIMER1 INITIALISATION
MOVWF FREQ_REF_H
MOVWF TMR1H
MOVLW 0X37
MOVWF TMR1L
MOVWF FREQ_REF_L
MOVLW 0XB1
MOVWF TMR0
BSF FLAGS,TIMER1_OV_FLAG
BSF PORTA,DRIVER_ENABLE
RETURN
;*******************************************************************************
;ROUTINE FOR 8*8 BIT MULTIPLICAION
;*******************************************************************************
MUL_8X8
CLRF RESULT_MSB
CLRF RESULT_LSB
MOVF SET_FREQ,W ;MOVE THE MULTIPLICAND TO W REG.
BCF STATUS,C ;CLEAR THE CARRY BIT IN THE STATUS REG.
MULT 0
MULT 1
MULT 2
MULT 3
MULT 4
MULT 5
MULT 6
MULT 7
RETLW 0
;*******************************************************************************
;UPON INITIALIZATION THE SINE TABLE CONTENTS ARE COPIED TO THE RAM FROM
;PROGRAM MEMORY
;*******************************************************************************
COPY_TABLE_TO_RAM
BANKSEL SINE_TABLE_RAM
MOVLW LOW(SINE_TABLE_RAM)
MOVWF FSR1
MOVLW 0X13
MOVWF TEMP_LOC
CLRF TEMP_LOC_1
COPY_AGAIN
MOVLW HIGH(SINE_TABLE)
MOVWF PCLATH
MOVF TEMP_LOC_1,W
CALL SINE_TABLE
MOVWF INDF1
INCF TEMP_LOC_1,F
INCF FSR1,F
DECFSZ TEMP_LOC,F
GOTO COPY_AGAIN
MOVLW LOW(SINE_TABLE_RAM) ;FSR POINTS TO THE STARTING OF THE TABLE
MOVWF FSR1
RETURN
;*******************************************************************************
;GENERAL PURPOSE DELAY ROUTINE - PRESENT DELAY TIME ~38msec
;*******************************************************************************
DELAY
MOVLW DELAY_COUNT1
MOVWF TEMP_LOC
DEC_TEMP_LOC
MOVLW DELAY_COUNT2
MOVWF TEMP_LOC_1
DEC_TEMP_LOC_1
DECFSZ TEMP_LOC_1,F
GOTO DEC_TEMP_LOC_1
DECFSZ TEMP_LOC,F
GOTO DEC_TEMP_LOC
RETURN
;*******************************************************************************
;EQUATION USED FOR CALCULATION OF SINE TABLE ENTRIES = (SIN(ANGLE)+1)*FF/2
;ANGLES ARE FROM 270 DEG. TO 90 DEG. STEP SIZE = 10 DEG.
;*******************************************************************************
TABLE CODE 0X0005
SINE_TABLE
ADDWF PCL,F
RETLW 0X00
RETLW 0X01
RETLW 0X07
RETLW 0X11
RETLW 0X1D
RETLW 0X2D
RETLW 0X3F
RETLW 0X53
RETLW 0X69
RETLW 0X7F
RETLW 0X95
RETLW 0XAB
RETLW 0XBF
RETLW 0XD1
RETLW 0XE1
RETLW 0XED
RETLW 0XF7
RETLW 0XFD
RETLW 0XFF
;*******************************************************************************
;END OF FILE
;*******************************************************************************
END
please help!!