jean12
Advanced Member level 2
It is not easy for me to provide a complete circuit,I normally use Proteus for schematics drawing but in proteus there is nor IR21362,so I could later tomorrow provide a circuit of PIC and few possible additional components.
The IR21362 when reading its datasheet it normally provide a deadtime,the PIC output voltage is of 0.2V to 2.087V(because the potentiometer on RA0 helps in adjusting the voltage and frequency) at once so that we get V/F constant.
why do we need a driver having more rating than 600V?What are the capabilities of HCPL316 in advancement to the IR21362?could you refer to https://www.microchip.com/wwwAppNotes/AppNotes.aspx?appnote=en012011 of AN843 of microcchip it uses this driver for driving a three phase induction motor.
for HCPL316 ,it is not easy to get a DIP type when trying to search I found only SMD types which I can not handle in my circuit.!!
Find my codes here below
an additional referential file is here
Please help
The IR21362 when reading its datasheet it normally provide a deadtime,the PIC output voltage is of 0.2V to 2.087V(because the potentiometer on RA0 helps in adjusting the voltage and frequency) at once so that we get V/F constant.
why do we need a driver having more rating than 600V?What are the capabilities of HCPL316 in advancement to the IR21362?could you refer to https://www.microchip.com/wwwAppNotes/AppNotes.aspx?appnote=en012011 of AN843 of microcchip it uses this driver for driving a three phase induction motor.
for HCPL316 ,it is not easy to get a DIP type when trying to search I found only SMD types which I can not handle in my circuit.!!
Find my codes here below
PHP:
;----------------------------------------------------------------
include <p18F46K22.inc>
include <3im_vf.inc>
;----------------------------------------------------------------
; CONFIG1H
CONFIG FOSC = HSHP ; Oscillator Selection bits (HS oscillator (high power > 16 MHz))
CONFIG PLLCFG = OFF ; 4X PLL Enable (Oscillator used directly)
CONFIG PRICLKEN = ON ; Primary clock enable bit (Primary clock is always enabled)
CONFIG FCMEN = ON ; Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)
CONFIG IESO = ON ; Internal/External Oscillator Switchover bit (Oscillator Switchover mode enabled)
; CONFIG2L
CONFIG PWRTEN = OFF ; Power-up Timer Enable bit (Power up timer disabled)
CONFIG BOREN = SBORDIS ; Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
CONFIG BORV = 190 ; Brown Out Reset Voltage bits (VBOR set to 1.90 V nominal)
; CONFIG2H
CONFIG WDTEN = OFF ; Watchdog Timer Enable bits (Watch dog timer is always disabled. SWDTEN has no effect.)
CONFIG WDTPS = 32768 ; Watchdog Timer Postscale Select bits (1:32768)
; CONFIG3H
CONFIG CCP2MX = PORTC1 ; CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
CONFIG PBADEN = OFF ; PORTB A/D Enable bit (PORTB<5:0> pins are configured as digital I/O on Reset)
CONFIG CCP3MX = PORTB5 ; P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)
CONFIG HFOFST = ON ; HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status)
CONFIG T3CMX = PORTC0 ; Timer3 Clock input mux bit (T3CKI is on RC0)
CONFIG P2BMX = PORTD2 ; ECCP2 B output mux bit (P2B is on RD2)
CONFIG MCLRE = EXTMCLR ; MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
; CONFIG4L
CONFIG STVREN = ON ; Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
CONFIG LVP = ON ; Single-Supply ICSP Enable bit (Single-Supply ICSP enabled if MCLRE is also 1)
CONFIG XINST = OFF ; Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
; CONFIG5L
CONFIG CP0 = OFF ; Code Protection Block 0 (Block 0 (000800-003FFFh) not code-protected)
CONFIG CP1 = OFF ; Code Protection Block 1 (Block 1 (004000-007FFFh) not code-protected)
CONFIG CP2 = OFF ; Code Protection Block 2 (Block 2 (008000-00BFFFh) not code-protected)
CONFIG CP3 = OFF ; Code Protection Block 3 (Block 3 (00C000-00FFFFh) not code-protected)
; CONFIG5H
CONFIG CPB = OFF ; Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
CONFIG CPD = OFF ; Data EEPROM Code Protection bit (Data EEPROM not code-protected)
; CONFIG6L
CONFIG WRT0 = OFF ; Write Protection Block 0 (Block 0 (000800-003FFFh) not write-protected)
CONFIG WRT1 = OFF ; Write Protection Block 1 (Block 1 (004000-007FFFh) not write-protected)
CONFIG WRT2 = OFF ; Write Protection Block 2 (Block 2 (008000-00BFFFh) not write-protected)
CONFIG WRT3 = OFF ; Write Protection Block 3 (Block 3 (00C000-00FFFFh) not write-protected)
; CONFIG6H
CONFIG WRTC = OFF ; Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
CONFIG WRTB = OFF ; Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
CONFIG WRTD = OFF ; Data EEPROM Write Protection bit (Data EEPROM not write-protected)
; CONFIG7L
CONFIG EBTR0 = OFF ; Table Read Protection Block 0 (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
CONFIG EBTR1 = OFF ; Table Read Protection Block 1 (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
CONFIG EBTR2 = OFF ; Table Read Protection Block 2 (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
CONFIG EBTR3 = OFF ; Table Read Protection Block 3 (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
; CONFIG7H
CONFIG EBTRB = OFF ; Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
;----------------------------------------------------------------------
#define TIMER0_OV_FLAG 0
#define OFFSET1_FLAG 4
#define OFFSET2_FLAG 5
#define OFFSET3_FLAG 6
#define MOTOR_DIRECTION 7
#define MOTOR_RUNNING 1
#define DEBOUNCE 0
#define ON_OFF_FLAG 1
#define DELAY_COUNT1 0xFF
#define DELAY_COUNT2 0xFF
;===============================================================
;RAM locations in Access bank, uninitialized
UDATA_ACS
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)
COUNTER res 1 ;General counters
COUNTER1 res 1
FLAGS res 1 ;Flags registers used to indicate different status
FLAGS1 res 1
FREQ_REF_H res 1 ;Referance Frequency input in counts
FREQ_REF_L res 1
FREQUENCY res 1
CCPR1L_TEMP res 1 ;Temporary locations
CCPR2L_TEMP res 1
CCPR3L_TEMP RES 1
PWM_DUTYCYCLE RES 1
TEMP res 1
TEMP1 res 1
TEMP_LOCATION res 2
SINE_TABLE res 0x14 ;Sine table
;----------------------------------------------------------------
STARTUP code 0x00
goto Start ;Reset Vector address
CODE 0x08
goto ISR_HIGH ;Higher priority ISR at 0x0008
PRG_LOW CODE 0x018
goto ISR_LOW ;Lower priority ISR at 0x0018
;****************************************************************
PROG1 code
Start
;****************************************************************
;Initialization of the Ports and timers
clrf FLAGS ;Clear the flags
clrf TRISC ;PORTC all output
clrf PORTC
CLRF TRISA
CLRF PORTA
; BSF TRISD,0
BANKSEL TRISB
BSF TRISB,0
BSF,TRISB,1
BSF TRISB,2
CLRF PORTD
; clrf TRISB ;PORTB all output
; clrf PORTB
; MOVLW 0X0F
; MOVWF PORTB
; MOVWF TRISB
movlw 0x0F
BANKSEL TRISB
movf TRISB
BSF TRISB,RUN_STOP_KEY
BSF PORTB,FWD_REV_KEY
BSF PORTB,FAULT_BIT
CLRF ECCP1AS
CLRF ECCP2AS
CLRF ECCP3AS
BSF TRISD,RUN_STOP_KEY
bsf TRISB,FAULT_BIT ;Fault input from driver
movlw 0x0C
movwf CCP1CON ;CCP1 and CCP2 configured to PWM
movwf CCP2CON
BANKSEL CCP3CON
movwf CCP3CON
; MOVLW 0x10
; MOVWF PSTR1CON
; MOVWF PSTR2CON
; MOVWF PSTR3CON
; MOVLW 0X80
; MOVWF PWM1CON
; MOVWF PWM2CON
; MOVWF PWM3CON
MOVLW 0X00
MOVWF CCPTMRS0
bsf T2CON,2 ;Timer2 ON
movlw PR2_VALUE ;Load PR2 value to PR2 register
movwf PR2
movlw 0x90
movwf CCPR1L
movwf CCPR2L
BANKSEL CCPR3L
MOVWF CCPR3L
movlw 0x81 ;Timer1 prescaler 1:2
movwf T1CON
call Init_Motor_Parameters ;Initialize motor parameters
call COPY_TABLE_TO_RAM
;******************************************************************
;Initialize ADC registers
movlw ADCON0_VALUE ;From ".inc" file
movwf ADCON0
movlw ADCON1_VALUE ;From ".inc" file
movwf ADCON1
movlw 0x33 ;RA0,RA4,RA5 inputs,RA2&RA3-Outputs
movwf TRISA ;
; bsf DRIVER_ENABLE_PORT,DRIVER_ENABLE_BIT ;Enable the driver chip
;******************************************************************
;Timre0 Initialization with prescaler
;******************************************************************
movlw 0X83 ;Load the T0CON with value
movwf T0CON ;TMR0 ON and prescalar is 1:16
movlw 0xF8 ;Timer0 Initialisation
movwf TMR0H
movlw 0x5E ;
movwf TMR0L
;---------------------------------------
bsf INTCON,TMR0IE ;Timer0 overflow Interrupt enable
bsf PIE1,TMR2IE ;"Timer2 to PR2 match" Interrupt enable
bsf PIE1,TMR1IE ;Timer1 overflow Interrupt enable
bsf PIE1,ADIE ;AD Converter over Interrupt enable
bcf IPR1,ADIP ;Low priority for ADC interrupt
bsf INTCON,RBIE ;PortB interrupt enable with low priority
bcf INTCON2,RBIP ;for Fault checking
movlw 0x093 ;Power ON reset status bit/Brownout reset status bit
movwf RCON ;and Instruction flag bits are set
;Priority level on Interrupots enabled
bsf INTCON,PEIE ;Port interrupts enable
bsf INTCON,GIE ;Global interrupt enable
;******************************************************************
;Main loop where the program will be looping
MAIN_LOOP
btfss FLAGS,TIMER0_OV_FLAG ;back from Timer0 overflow?
bra bypass ;No
call UPDATE_PWM_DUTYCYCLES ;Yes, update the PWM duty cycle with new value
call UPDATE_TABLE_OFFSET ;Update 3 offsets
bcf FLAGS,TIMER0_OV_FLAG ;Clear the flag
bypass
call SET_ADC_GO ;Start AD conversion
call KEY_CHECK ;Check keys change
bra MAIN_LOOP
;******************************************************************
;Higher priority interrupt service routine
;"Timer2 to PR2 match", "Timer1 overflow" and Timer0 overflow are checked
;******************************************************************
ISR_HIGH
btfsc PIR1,TMR2IF ;Timer2 to PR2 match?
bra TIMER2_PR2_Match
; btfsc PIR1,TMR1IF ;Timer1 overflow Interrupt?
; bra TIMER1_OVERFLOW
btfsc INTCON,TMR0IF ;Timer0 overflow Interrupt?
bra TIMER0_OVERFLOW ;Yes
RETFIE FAST
;******************************************************************
;///////////////////////////////////////////////////////////////////
;MODIFIED BY MYSELF FOR REMOVING TIMER 2 NOT USED FOR PWM GENERATION
;//////////////////////////////////////////////////////////////////
TIMER2_PR2_Match
bcf PIR1,TMR2IF ;
; tstfsz CCPR3L_TEMP ;If Software PWM duty cycle=0, then
tstfsz CCPR3L
bra PWM3_NOT_0 ;no need to set the PWM3 port pin
RETFIE FAST
PWM3_NOT_0
movlw 0xFF ;Higher byte of Timer1 loaded with FFh
movwf TMR1H
movff CCPR3L_TEMP,TMR1L
RETFIE FAST
;///////////////////////////////////////////////////////////////////
;MODIFIED BY MYSELF FOR REMOVING TIMER 2 NOT USED FOR PWM GENERATION
;//////////////////////////////////////////////////////////////////
;******************************************************************
;TIMER1_OVERFLOW
; bcf PWM3_PORT,PWM3_PORT_PIN ;PWM3 pin cleared after the duty cycle time expires
; BCF PORTB,5
; bcf PIR1,TMR1IF
; RETFIE FAST
;******************************************************************
TIMER0_OVERFLOW ;TMR0 overflow ISR
movff FREQ_REF_H,TMR0H ;Load the Higher byte of SpeedCommand to TMR0H
movff FREQ_REF_L,TMR0L ;Load the Lower byte of SpeedCommand to TMR0L
bsf FLAGS,TIMER0_OV_FLAG
bcf INTCON,TMR0IF ;Clear TMR0IF
RETFIE FAST
;******************************************************************
;Lower priority interrupt service routine
;Change on PortB(Fault checking) interrupt & "ADConversion over" interrupt are checked
;******************************************************************
ISR_LOW
btfsc INTCON,RBIF ;RB interrupt?
bra CHECK_FAULT ;Yes
btfsc PIR1,ADIF
bra AD_CONV_COMPLETE
RETFIE FAST
;******************************************************************
CHECK_FAULT
movf PORTB,W ;Check for fault bit
btfss WREG,FAULT_BIT
bra THERE_IS_FAULT
call RUN_MOTOR_AGAIN ;Fault cleared?
bcf INTCON,RBIF ;Run motor again
RETFIE FAST
THERE_IS_FAULT ;Yes,fault is there
call STOP_MOTOR ;Stop motor
bcf INTCON,RBIF
RETFIE FAST
;******************************************************************
AD_CONV_COMPLETE ;ADC interrupt
movff ADRESH,FREQUENCY
movlw 0x14 ;Minimum Frequency set to 5Hz (scaling factor X4)
cpfsgt FREQUENCY
movwf FREQUENCY
movlw 0xF0 ;Limiting V/F to F= 60Hz (scaling factor X4)
cpfslt FREQUENCY
movwf FREQUENCY
movwf PORTD ;Out frequency to PORTD for set value
bcf PIR1,ADIF ;ADIF flag is cleared for next interrupt
RETFIE FAST
;*************************************************************************
;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.
;*************************************************************************
UPDATE_PWM_DUTYCYCLES
movf TABLE_OFFSET1,W
movf PLUSW0,W
bz PWM1_IS_0
mulwf FREQUENCY ;Table_value X Frequency
movff PRODH,CCPR1L_TEMP
bra UPDATE_PWM2
PWM1_IS_0
clrf CCPR1L_TEMP ;Clear the PWM duty cycle register
bcf CCP1CON,4
bcf CCP1CON,5
UPDATE_PWM2
movf TABLE_OFFSET2,W
movf PLUSW0,W
bz PWM2_IS_0
mulwf FREQUENCY ;Table_value X Frequency
movff PRODH,CCPR2L_TEMP
bra UPDATE_PWM3
PWM2_IS_0
clrf CCPR2L_TEMP ;Clear the PWM duty cycle register
bcf CCP2CON,4
bcf CCP2CON,5
UPDATE_PWM3
movf TABLE_OFFSET3,W
movf PLUSW0,W
bz PWM3_IS_0
mulwf FREQUENCY ;Table_value X Frequency
movff PRODH,CCPR3L_TEMP
bra SET_PWM12
PWM3_IS_0
clrf CCPR3L_TEMP ;Clear the PWM duty cycle register
bcf CCP2CON,4
bcf CCP2CON,5
;//////////////////////////////////////////////////////
;ABOVE WERE MODIFIED BY MYSELF
;//////////////////////////////////////////////////////
SET_PWM12
btfss FLAGS,MOTOR_DIRECTION
bra ROTATE_REVERSE
movff CCPR1L_TEMP,CCPR1L
movff CCPR2L_TEMP,CCPR2L
BANKSEL CCPR3L
movff CCPR3L_TEMP,CCPR3L
; movff CCPR3L_TEMP,PWM_DUTYCYCLE
; movff PWM_DUTYCYCLE,CCPR3L
bsf PORT_LED1,LED1
return
;////////////////////////////////////////////
;/DEACTIVATE TWO DIRECTION OPERATION BY MYSELF
;////////////////////////////////////////////
ROTATE_REVERSE
movff CCPR2L_TEMP,CCPR1L
movff CCPR1L_TEMP,CCPR2L
movff CCPR3L_TEMP,PWM_DUTYCYCLE
MOVFF PWM_DUTYCYCLE,CCPR3L
bcf PORT_LED1,LED1
return
;*******************************************************************************
;This routine Updates the offset pointers to the table after every access
;*******************************************************************************
UPDATE_TABLE_OFFSET
btfss FLAGS,OFFSET1_FLAG ;If set incr. on table
bra DECREMENT_OFFSET1
movlw (SINE_TABLE_ENTRIES-1) ;Check for the last value on the table
cpfslt TABLE_OFFSET1
bra CLEAR_OFFSET1_FLAG
incf TABLE_OFFSET1,F ;Increment offset1
bra UPDATE_OFFSET2
CLEAR_OFFSET1_FLAG
bcf FLAGS,OFFSET1_FLAG
DECREMENT_OFFSET1
dcfsnz TABLE_OFFSET1,F ;Decrement offset1
bsf FLAGS,OFFSET1_FLAG
UPDATE_OFFSET2
btfss FLAGS,OFFSET2_FLAG ;If set incr. on table
bra DECREMENT_OFFSET2
movlw (SINE_TABLE_ENTRIES-1) ;Check for the last value on the table
cpfslt TABLE_OFFSET2
bra CLEAR_OFFSET2_FLAG
incf TABLE_OFFSET2,F ;Increment offset2
bra UPDATE_OFFSET3
CLEAR_OFFSET2_FLAG
bcf FLAGS,OFFSET2_FLAG
DECREMENT_OFFSET2
dcfsnz TABLE_OFFSET2,F ;Decrement offset2
bsf FLAGS,OFFSET2_FLAG
UPDATE_OFFSET3
btfss FLAGS,OFFSET3_FLAG ;If set incr. on table
bra DECREMENT_OFFSET3
movlw (SINE_TABLE_ENTRIES-1) ;Check for the last value on the table
cpfslt TABLE_OFFSET3
bra CLEAR_OFFSET3_FLAG
incf TABLE_OFFSET3,F ;Increment offset3
return
CLEAR_OFFSET3_FLAG
bcf FLAGS,OFFSET3_FLAG
DECREMENT_OFFSET3
dcfsnz TABLE_OFFSET3,F ;Decrement offset3
bsf FLAGS,OFFSET3_FLAG
return
;*******************************************************************************
;This routine calculates the Timer0 reload value based on ADC read value and the
;scaling factor calculated based on the main clock and number of Sine table entries.
;Timer0 value = FFFF - (FREQUENCY_SCALE/Frequency) Frequ = (adc result)
;*******************************************************************************
CALCULATE_FREQUENCY
movff FREQUENCY,PORTD
clrf TEMP
clrf TEMP1
movlw HIGH(FREQUENCY_SCALE) ;FREQUENCY_SCALE/Frequency
movwf TEMP_LOCATION ;16 bit by 8 bit division
movlw LOW(FREQUENCY_SCALE) ;
movwf TEMP_LOCATION+1
continue_subtraction
bsf STATUS,C
movf FREQUENCY,W
subwfb TEMP_LOCATION+1,F
clrf WREG
subwfb TEMP_LOCATION,F
btfss STATUS,C
goto keep_result_in_rpm
incf TEMP,F
btfsc STATUS,C ;Result of the division is stored in TEMP&TEMP1
incf TEMP1,F
goto continue_subtraction
keep_result_in_rpm
;Timer0 value = FFFF-Timer0
bsf STATUS,C
movlw 0xFF
subfwb TEMP,F
subfwb TEMP1,F ;The Timer0 reload value stored in
movff TEMP1,FREQ_REF_H ;FREQ_REF_H & FREQ_REF_L
movff TEMP,FREQ_REF_L ;These values will be loaded to
return ;Timer0 in Timer0 overflow interrupt
;*******************************************************************************
;This routine sets the ADC GO bit high after an aquisition time of 20uS approx.
;*******************************************************************************
SET_ADC_GO
call CALCULATE_FREQUENCY
btfss ADCON0,GO
bsf ADCON0,GO ;Set GO bit for ADC conversion start
return
;*******************************************************************************
;This routine initializes the parameters required for motor initialization.
;*******************************************************************************
Init_Motor_Parameters
clrf CCPR1L ;Initialize all duty cycles to 0
clrf CCPR2L
CLRF CCPR3L
movlw 0x09 ;Initialize the table offset to 3 registers
movwf TABLE_OFFSET1 ;to form 0-120-240 degrees
movlw 0x03
movwf TABLE_OFFSET2
movlw 0x0F
movwf TABLE_OFFSET3
bsf FLAGS,OFFSET1_FLAG ;Offset flags initialization
bcf FLAGS,OFFSET2_FLAG
bcf FLAGS,OFFSET3_FLAG
movlw 0x30 ;Initialize frequency to 12Hz
movwf FREQUENCY
movlw 0xFD ;Timer0 Initialisation
movwf FREQ_REF_H
movwf TMR0H
movlw 0x2C ;
movwf TMR0L
movwf FREQ_REF_L
bsf FLAGS,TIMER0_OV_FLAG
return
;*******************************************************************************
;Upon initialization the Sine table contents are copied to the RAM from
;Program memory
;*******************************************************************************
COPY_TABLE_TO_RAM
movlw UPPER sine_table ;Initialize Table pointer to the first
movwf TBLPTRU ;location of the table
movlw HIGH sine_table
movwf TBLPTRH
movlw LOW sine_table
movwf TBLPTRL
movlw LOW(SINE_TABLE)
movwf FSR0L
movlw HIGH(SINE_TABLE)
movwf FSR0H
movlw 0x14
movwf TEMP
COPY_AGAIN
TBLRD*+
movff TABLAT,POSTINC0
decfsz TEMP,F
bra COPY_AGAIN
movlw LOW(SINE_TABLE) ;FSR0 points to the starting of the table
movwf FSR0L
movlw HIGH(SINE_TABLE)
movwf FSR0H
return
;*******************************************************************************
;This routine checks for the keys status. 2 keys are checked, Run/Stop and
;Forward(FWD)/Reverse(REV)
;*******************************************************************************
KEY_CHECK
btfsc PORTB,RUN_STOP_KEY ;Is key pressed "RUN/STOP"?
; btfss PORTB,RUN_STOP_KEY
bra KEY_IS_RUN
bcf PORT_LED2,LED2
call STOP_MOTOR
bsf FLAGS,MOTOR_RUNNING
return
KEY_IS_RUN
bsf PORT_LED2,LED2
btfss FLAGS,MOTOR_RUNNING
bra CONT_RUN_KEY
call RUN_MOTOR_AGAIN
bcf FLAGS,MOTOR_RUNNING
return
CONT_RUN_KEY
btfsc PORTB,FWD_REV_KEY ;Fwd/Rev key pressed?
;btfss PORTA,FWD_REV_KEY ;Fwd/Rev key pressed?
bra MOTOR_SET_FWD
btfsc FLAGS,MOTOR_DIRECTION
return
call STOP_MOTOR
call DELAY
call DELAY
call DELAY
call DELAY
call DELAY
call DELAY
call RUN_MOTOR_AGAIN
bsf FLAGS,MOTOR_DIRECTION
return
MOTOR_SET_FWD
btfss FLAGS,MOTOR_DIRECTION
return
call STOP_MOTOR
call DELAY
call DELAY
call DELAY
call DELAY
call DELAY
call DELAY
call RUN_MOTOR_AGAIN
bcf FLAGS,MOTOR_DIRECTION
return
;*******************************************************************************
;This routine stops the motor by driving the PWMs to 0% duty cycle.
;*******************************************************************************
STOP_MOTOR
clrf CCPR1L
clrf CCPR2L
CLRF CCPR3L
clrf TABLE_OFFSET1
clrf TABLE_OFFSET2
clrf TABLE_OFFSET3
bcf INTCON,TMR0IE
bcf PIE1,TMR2IE
bcf PIE1,TMR1IE
bcf PIE1,ADIE
return
;*******************************************************************************
;This routine starts motor from previous stop with motor parameters initialized
;*******************************************************************************
RUN_MOTOR_AGAIN
call Init_Motor_Parameters
bsf INTCON,TMR0IE
bsf PIE1,TMR2IE
bsf PIE1,TMR1IE
bsf PIE1,ADIE
return
;*******************************************************************************
;Delay routine.
;*******************************************************************************
DELAY
movlw DELAY_COUNT1
movwf COUNTER
dec_count
movlw DELAY_COUNT2
movwf COUNTER1
dec_count1
decfsz COUNTER1,F
bra dec_count1
decfsz COUNTER,F
bra dec_count
clrf COUNTER
clrf COUNTER1
return
;*******************************************************************************
;Sine table for the inverter.
;*******************************************************************************
TABLE code 0x0100
;below table is from 270 eg. to 90 deg @ 10 deg. resolution; for 20MHz, PR2 = F9, Timer2 1:1 prescale
sine_table db 0x0,0x3,0x07,0x10,0x1B,0x28,0x38,0x4A,0x5C,0x70,0x83,0x96,0xA7,0xB7,0xC4,0xD0,0xD8,0xDD,0xDE
;*******************************************************************************
END
an additional referential file is here
PHP:
;User defined variables
;---------------------------------------------------------------------------------------
;Oscillator frequency
#define OSCILLATOR d'20000000'
;---------------------------------------------------------------------------------------
;Timer0 prescaler
#define TIMER0_PRESCALE d'16'
;---------------------------------------------------------------------------------------
;number of entries in the sine table, or the sampling frequency
#define SINE_TABLE_ENTRIES d'19'
;---------------------------------------------------------------------------------------
SAMPLES_PER_CYCLE = (SINE_TABLE_ENTRIES-1)*d'2'
INSTRUCTION_CYCLE = (OSCILLATOR)/d'4'
FREQUENCY_SCALE = (INSTRUCTION_CYCLE/SAMPLES_PER_CYCLE)/(TIMER0_PRESCALE/4)
;Timer prescale/4 is done to componsate ADC multiplication factor of 4 to the frequency)
;---------------------------------------------------------------------------------------
;PWM frequency definition
#define TIMER2_PRESCALE d'01'
#define PWM_FREQUENCY d'20000'
PR2_VALUE = (OSCILLATOR/(4*PWM_FREQUENCY*TIMER2_PRESCALE))-1
;---------------------------------------------------------------------------------------
;ADC initialization
#define Fosc_by_2 b'000'
#define Fosc_by_8 b'001'
#define Fosc_by_32 b'010'
#define FRC b'011'
#define Fosc_by_4 b'100'
#define Fosc_by_16 b'101'
#define Fosc_by_64 b'110'
#define ADC_CLOCK Fosc_by_32
#define ADC_CHANNEL d'0'
#define ADC_ON_BIT b'1'
#define LEFT_JUSTIFIED
#define ADC_PORT_CONFIG b'1110' ;Refer the table in the manual for selection
ADCON0_VALUE = ((ADC_CLOCK<<6)|(ADC_CHANNEL<<4)|(ADC_ON_BIT))
if ((ADC_CLOCK==Fosc_by_2)||(ADC_CLOCK ==Fosc_by_8)||(ADC_CLOCK ==Fosc_by_32)||(ADC_CLOCK==FRC))
ifndef LEFT_JUSTIFIED
ADCON1_VALUE = ((1<<7) | (ADC_PORT_CONFIG))
else
ADCON1_VALUE = ADC_PORT_CONFIG
endif
endif
if ((ADC_CLOCK==Fosc_by_4)||(ADC_CLOCK==Fosc_by_16)||(ADC_CLOCK==Fosc_by_64))
ifndef LEFT_JUSTIFIED
ADCON1_VALUE = ((1<<7) |(1<<6)| (ADC_PORT_CONFIG))
else
ADCON1_VALUE = ( (1<<6)| (ADC_PORT_CONFIG))
endif
endif
;---------------------------------------------------------------------------------------
;Port definitions
#define DRIVER_ENABLE_PORT PORTB
; #define DRIVER_ENABLE_BIT 3
#define FAULT_BIT 4
#define FWD_REV_KEY 1 ;It was 4
#define RUN_STOP_KEY 0 ;It was 5
#define PORT_LED1 PORTA
#define LED1 1
#define PORT_LED2 PORTA
#define LED2 2
;---------------------------------------------------------------------------------------
Please help
Last edited: