;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;PROGRAM : ESC (Electronic Speed Control) Forward Only ;PROCESSOR : PIC16F684 ;DETAILS : This program takes an input PWM signal from a radio ; frequency reciever and decodes and maps it to a direction and ; duty cycle for the PWM output for one directional ; DC Motor Control. ;HARDWARE : PIC16F684 ; PORTA<5> ;This is the PWM input from the radio reciever. ; PORTC<2> ;This is the port for the PWM output signal. ; ; ; V0.2B Improvements include the remapping of the throttle closer to where ; it should be, as opposed to going from stop to full twice when pulling the ; trigger from rest to fully extended. ; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #include __config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOD_OFF & _IESO_OFF & _FCMEN_OFF) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Initialization ;DETAILS : Initializes processor modules. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ORG H'0000' MOVLW B'00000001' MOVWF OSCCON CALL SETUP_PWM ;Start PWM module. CALL SETUP_PORTA ;Make PORTA<5> a digital input. CALL SETUP_TIMER ;Setup the TIM1 module. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;Main Program Loop ;DETAILS : This is the main program loop. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; MAINLOOP: ;Capture the length of the input ;pulse and store in ;TMR1H:TMR1L. CALL DO_CAPTURE ;Calculate and map the input to ;PWM duty cycle ;and direction. CALL CALC_PULSE ;Do MAINLOOP again. GOTO MAINLOOP ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;SUBROUTINE : SETUP_PWM ;DETAILS : Sets up Port C for PWM use ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SETUP_PWM: ;Initialize PORTC for PWM use. ;This means PORTC <5:2> will be inputs. BCF STATUS,RP0 ;Bank 0 CLRF PORTC ;Init PORTC MOVLW 07h ;Set RC<4,1:0> to MOVWF CMCON0 ;digital I/O BSF STATUS,RP0 ;Bank 1 CLRF ANSEL ;digital I/O MOVLW B'00111100' ;Set RC<5:2> as inputs MOVWF TRISC BCF STATUS,RP0 ;Bank 0 ;Set the PWM period by loading the PR2 register. MOVLW H'FF' MOVWF PR2 ;Configure the ECCP module for the desired ;PWM mode and configuration by loading the ;CCP1CON register with the appropriate values. ;The first bit of CCP1CON will control DIRECTION. MOVLW B'01001100' MOVWF CCP1CON ;Set the PWM duty cycle by loading the CCPR1L ;register and CCP1CON<5:4> bits. MOVLW B'00000000' MOVWF CCPR1L BCF PIR1,TMR2IF MOVLW B'01111000' MOVWF T2CON BSF T2CON,TMR2ON WAITFORSET: BTFSC PIR1,TMR2IF ;Wait until timer2 overflows GOTO TMR2OVERFLOW ;and then continue. GOTO WAITFORSET TMR2OVERFLOW: BSF STATUS,RP0 CLRF TRISC ;Enable the outputs by clearing the BCF STATUS,RP0 ;respective TRISC bits. BCF ECCPAS,ECCPASE ;Clear the ECCPASE bit. RETURN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;SUBROUTINE : SETUP_PORTA ;DETAILS : Sets up Port A for input capture ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SETUP_PORTA: ;Initialize PORTA for PWM use. ;This means PORTA <5> will be an input. BCF STATUS,RP0 ;Bank 0 CLRF PORTA ;Init PORTC MOVLW 07h ;Set RC<4,1:0> to MOVWF CMCON0 ;digital I/O BSF STATUS,RP0 ;Bank 1 CLRF ANSEL ;digital I/O MOVLW B'00100000' ;Set RA<5> as an input MOVWF TRISA BCF STATUS,RP0 ;Bank 0 RETURN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;SUBROUTINE : DO_CAPTURE ;DETAILS : Captures pulse length from Reciever Signal and stores ; it to data word TIME_RECORDED ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; DO_CAPTURE: WAITFORLOW: BTFSS PORTA,5 ;Wait until a low signal is found GOTO GOHERE ;and then continue. GOTO WAITFORLOW GOHERE: WAITFORHIGH: BTFSC PORTA,5 ;Wait until a high signal is started GOTO STARTTIMING ;and then continue. GOTO WAITFORHIGH STARTTIMING: MOVLW B'00000000' MOVWF TMR1H MOVWF TMR1L WAITFORLOWAGAIN: BTFSS PORTA,5 ;Wait until it goes low again GOTO STOPTIMING ;and then continue. GOTO WAITFORLOWAGAIN STOPTIMING: MOVFW TMR1L MOVWF TIMER_COUNT_LOW MOVFW TMR1H MOVWF TIMER_COUNT_HIGH RETURN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;SUBROUTINE : SETUP_TIMER ;DETAILS : Sets up the TIMER1 Module for use in the ; DO_CAPTURE subroutine. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; SETUP_TIMER: MOVLW B'00010101' MOVWF T1CON RETURN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;SUBROUTINE : CALC_PULSE ;DETAILS : Sets up the TIMER1 Module for use in the ; DO_CAPTURE subroutine. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CALC_PULSE: MOVFW TIMER_COUNT_LOW MOVWF CCPR1L RETURN ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; DATA VARIABLES ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; TIME_RECORDED DW B'0000000000000000' TIME_MIN DB B'01111000' TIME_MID DB B'10111000' TIME_MAX DB B'11110000' COUNTER DW B'0000000000000000' TEMP_DATA DW B'0000000000000000' TIMER_COUNT_LOW DB B'00000000' TIMER_COUNT_HIGH DB B'00000000' END