;**************** motor speed control start routine ********************
start_up_setting: clr on_time ;initial power must be 0
ldi temp,hys_hi ;avoid jitter of control input data
sts RAM_HYS,temp ;by setting the slow speed hysterisis
ret
;-----------------------------------
output_on_timer:
set_off_check: cpi on_time,0 ;Control off?
breq free_run_tim
set_on_timer: mov duty_time,on_time
com duty_time ;on_time=$FF-(on_time) until overflow
out TCNT0,duty_time
st_pulse_out: rcall P_FET_on_fast
out PORTB,temp1 ;Turn on/off the U,V,W Output transistor
clr timset ;Clear timer0 flag
watch_tim_over: sbrs timset,0 ;Skip here if timer0 overflow is occurred
rjmp watch_tim_over
free_run_tim:
set_off_timer: rcall P_FET_off_fast
mov duty_time,on_time
out TCNT0,duty_time ;Do not confuse. off-timing is on_time data, until over flow!
tim_set: clr timset ;Clear timer0 timeover flag
off_phase: sbrs timset,0 ;Skip if timer0 overflow is occurred
rjmp off_phase
get_sense: in r_sense,PIND
andi r_sense,0b00000111 ;get rotate data from sensor circuit
ret
;---------------------------------
P_FET_off_fast: in temp,PORTB ;Turn of all of output FET with photo cuppler delay compasation
andi temp,0b01010111 ;Turn off setting for the UP,VP,WP P-site Output FETs
Pch_deley: out PORTB,temp ;'02/01/11 Photo cupller delay compensation, 2uS for Toshiba TLP521
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
clr temp
out PORTB,temp ;
ret
P_FET_on_fast: mov temp,temp1 ;Turn of all of output FET with photo cuppler delay compasation
andi temp,0b10101011 ;Turn off setting for the UP,VP,WP P-site Output FETs
out PORTB,temp ;'02/01/17 Photo cupller delay compensation, 2uS for Toshiba TLP521
out PORTB,temp ;Turn on the UP,VP,WP P-site Output FETs
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp
out PORTB,temp1 ;Output FETs all off for brake point test
ret
;---------------------------------
;*** Reset handler ***
;*** to provide initial port, timer and interrupt setting up
Reset_handler: clr temp
out PORTB,temp ;Output FETs all off
ser temp ;set register temp=R16 to "11111111"
out DDRB,temp ;initialize port B as all Outputs
ldi temp,0x00 ;clear temp
out DDRD,temp ;initialize portD as all Inputs
;Timer0 internal clock divide for output motor switching duty resolution
ldi temp,(1<<CS00) ;CLK/1
;ldi temp,(1<<CS01) ;CLK/8
;ldi temp,(1<<CS01)+(1<<CS00) ;CLK/64
;ldi temp,(1<<CS02) ;CLK/256
out TCCR0,temp
;Disable Timer0 overflow interrupt for output duty power control initialize
ldi temp,(0<<TOIE0)
out TIMSK,temp
;Timer1 Timer/Counter1 Control ResisterA(TCCR1A)
clr temp ;no output on pin15(OC1), Off the PWM operation
out TCCR1A,temp
;Timer1 internal clock divide for input power control digital prop. pulse width measuring
;Also, Input Timer1 counts data capture Edge select is "Falling Edge" by ICES1="0"
;ldi temp,(1<<CS10) ;CLK/1
;ldi temp,(1<<CS11) ;CLK/8
;ldi temp,(1<<CS11)+(1<<CS10)+(1<<ICNC1)+(0<<ICES1) ;CLK/64, 6.4uS resolution + Noise canceller for pin11 ICP
ldi temp,(1<<CS11)+(1<<CS10)+(0<<ICNC1)+(0<<ICES1) ;CLK/64, 6.4uS resolution
;ldi temp,(1<<CS12) ;CLK/256
;ldi temp,(1<<CS10)+(1<<CS12) ;CLK/1024
;ldi temp,(1<<CS11)+(1<<CS12) ;EXT pin T1, falling edge
;ldi temp,(1<<CS11)+(1<<CS10)+(1<<CS12) ;EXT pin T1, rising edge
out TCCR1B,temp
;Interrupt input setting for digital prop. pulse input
;ldi temp,(1<<ISC11) ;the falling edge of INT1 generates IRQ1
ldi temp,(1<<ISC11)+(1<<ISC10) ;the rising edge of INT1 generates IRQ1
out MCUCR,temp
ldi temp,(1<<INT1)
out GIMSK,temp ;Enable INT1 interrupt for Digital Prop. input line
ret
;---------------------------------
get_control_pulse:
rcall single_beep ;initial powered sound
rcall pulse_in_test ;wait the one sample of control pulse. Possibly noise when TX powered.
rcall pulse_in_test ;wait the one sample of control pulse
rcall pulse_in_test ;wait the one sample of control pulse
rcall pulse_width_check
lds temp,correct_pulse_width ;test control input pulse width by this flag. 0:incorrect, 1:correct
cpi temp,1 ;correct?
brne get_control_pulse ;No. incorrect
ret
;---------------------------------
get_shortest_pulse: rcall wait_2sec
rcall get_T1_data
store_short_pulse:
lds temp,RAM_TCNT1H ;get T1 hi byte data
cpi temp,0
brne get_shortest_pulse ;short not enough
lds temp,RAM_TCNT1L ;Get motor off control pulse width
clc
cpi temp,uS1100 ;Shorter than 1100uS?
brsh get_shortest_pulse ;short not enough
cpi temp,uS800 ;Shorter than 800uS?
brmi get_shortest_pulse ;Too short
inc temp ;get digital 1 digit margin
inc temp ;get trim 1 digit margin
inc temp ;get 1 digit margin
inc temp ;get 1 digit margin
cpi temp,0x81 ;RAM_SHORT must be more than $80 to avoid more than 8 bit subtract calculate
brmi get_shortest_pulse
sts RAM_SHORT,temp ;Store motor off control pulse width data
rcall po_pi
ret
;---------------------------------
get_longest_pulse:rcall wait_2sec
rcall wait_2sec
;rcall pulse_width_check
rcall get_T1_data
store_long_pulse:
lds temp,RAM_TCNT1H ;get T1 hi byte data
cpi temp,1
brne get_longest_pulse ;long not enough
lds temp,RAM_TCNT1L ;Get motor full power control pulse width
cpi temp,uS1900L ;longer than 1900uS?
brmi get_longest_pulse ;long not enough
clc
cpi temp,uS2200L ;longer than 2200uS?
brsh get_longest_pulse ;Too long
;brpl get_longest_pulse ;Too long
dec temp ;get digital 1 digit margin
dec temp ;get trim 1 digit margin
dec temp ;get trim 1 digit margin
sts RAM_LONG,temp ;Store motor full power control pulse width data
rcall po_pi
rcall po_pi
ret
;---------------------------------
get_motor_off_pulse: rcall wait_2sec
rcall pulse_width_check
lds temp,correct_pulse_width ;test control input pulse width by this flag. 0:incorrect, 1:correct
cpi temp,1 ;correct?
brne get_motor_off_pulse ;No. incorrect
lds temp,RAM_TCNT1H ;get T1 hi byte data
cpi temp,0
brne get_motor_off_pulse ;Is the TX stick position motor_off?
lds temp,RAM_TCNT1L ;Get motor off control pulse width
lds ZH,RAM_SHORT ;ZH is used as just temporary register
clc
cp temp,ZH ;Is the TX stick motor off position?
;brpl get_motor_off_pulse ;short not enough
brsh get_motor_off_pulse ;short not enough
rcall po_pi
rcall po_pi
rcall po_pi
ret
;-----------------------------------
no_signal_check:in r_sense,PIND ;
andi r_sense,0b01000000 ;get digital prop. input pulse on pin11(PD6)
mov XH,r_sense ;HX is just scratch
;brne sense_lo_batt ;pulse is "H", then should not read T1 Timer/Counter
in temp,TCNT1L ;get T1 law lo-byte timer data from T1 timer
in temp,TCNT1H ;get T1 law hi-byte timer data from T1 timer
in r_sense,PIND ;
andi r_sense,0b01000000 ;get digital prop. input pulse on pin11(PD6)
cp XH,r_sense
breq mS400_check
brpl continue_run ;ignore the data at "H" to "L" falling edge of T1 capture timing as spec. says
mS400_check: clc
cpi temp,0xfe ;no-signal over 6.4uS * 255 * 255 = 400mS?
brsh braking ;stop the motor
continue_run: ldi YH,0xa
ret
braking: ldi YH,0xb
ret
;------------------------------------------------
brake_out: ldi temp,0b01010100 ;motor Brake out
out PORTB,temp
rcall pulse_in_test ;wait the one sample of control pulse. Possibly noise when TX powered.
rcall pulse_in_test ;wait the one sample of control pulse
rcall pulse_in_test ;wait the one sample of control pulse
ret
;--------------------------------- |