list p=18f4550
#include <p18f4550.inc>
config PLLDIV = 3 ; (20 MHz crystal on PICDEM FS USB board)
config CPUDIV = OSC1_PLL2
config USBDIV = 2 ; Clock source from 96MHz PLL/2
config FOSC = HSPLL_HS
config FCMEN = OFF
config IESO = OFF
config PWRT = OFF
config BOR = ON
config BORV = 3
config VREGEN = ON ;SB Voltage Regulator
config WDT = OFF
config WDTPS = 32768
config MCLRE = ON
config LPT1OSC = OFF
config PBADEN = OFF
; config CCP2MX = ON
config STVREN = ON
config LVP = OFF
; config ICPRT = OFF ; Dedicated In-Circuit Debug/Programming
config XINST = OFF ; Extended Instruction Set
config CP0 = OFF
config CP1 = OFF
; config CP2 = OFF
; config CP3 = OFF
config CPB = OFF
; config CPD = OFF
config WRT0 = OFF
config WRT1 = OFF
; config WRT2 = OFF
; config WRT3 = OFF
config WRTB = OFF ; Boot Block Write Protection
config WRTC = OFF
; config WRTD = OFF
config EBTR0 = OFF
config EBTR1 = OFF
; config EBTR2 = OFF
; config EBTR3 = OFF
config EBTRB = OFF
CBLOCK 0x00
TEMP2
PWM_0_V
PWM_1_V
PWM_2_V
PWM_3_V
PWM_4_V
PWM_5_V
PWM_6_V
PWM_7_V
PWM_8_V
PWM_9_V
PWM_10_V
PWM_11_V
PWM_12_V
PWM_13_V
PWM_14_V
PWM_15_V
PWM_AUXILIAR
AUX_TMR0
AUX_RECEPCION
NUMERO_SERVO
POSICION_SERVO
TEMP
W_TEMP
STATUS_TEMP
BSR_TEMP
ENDC
#DEFINE PWM_0 PORTD,0
#DEFINE PWM_1 PORTD,1
#DEFINE PWM_2 PORTD,2
#DEFINE PWM_3 PORTD,3
#DEFINE PWM_4 PORTD,4
#DEFINE PWM_5 PORTD,5
#DEFINE PWM_6 PORTD,6
#DEFINE PWM_7 PORTD,7
#DEFINE PWM_8 PORTB,0
#DEFINE PWM_9 PORTB,1
#DEFINE PWM_10 PORTB,2
#DEFINE PWM_11 PORTB,3
#DEFINE PWM_12 PORTB,7
#DEFINE PWM_13 PORTB,4
#DEFINE PWM_14 PORTB,5
#DEFINE PWM_15 PORTB,6
ORG 0x000
GOTO INICIO
ORG 0x008
GOTO ALTA_PRIORIDAD
ORG 0x018
GOTO BAJA_PRIORIDAD
BAJA_PRIORIDAD
NOP
ALTA_PRIORIDAD
MOVWF W_TEMP
MOVFF STATUS,STATUS_TEMP
MOVFF BSR,BSR_TEMP
BTFSC INTCON,TMR0IF
GOTO INTER_TMR0
BTFSC PIR1,RCIF ;CHECAMOS SI HUBO INTERRUPCION
GOTO INTER_RECEPCION ;POR RECEPCION EN CMUNICACION SERIE
GOTO FIN_INTER
;********************************************************
INTER_RECEPCION
BCF PIR1,RCIF
BTFSC AUX_RECEPCION,3
GOTO DIFERENTE_2
BTFSC AUX_RECEPCION,2
GOTO CHECO_4
BTFSC AUX_RECEPCION,1
GOTO CHECO_3
BTFSC AUX_RECEPCION,0
GOTO CHECO_2
;0
CHECO_1
MOVF RCREG,W
SUBLW 'S'
BTFSS STATUS,Z
GOTO DIFERENTE
MOVLW .18
BSF AUX_RECEPCION,0
GOTO FIN_INTER
CHECO_2
MOVF RCREG,W
SUBLW 'V'
BTFSS STATUS,Z
GOTO DIFERENTE
MOVLW .36
BSF AUX_RECEPCION,1
CLRF NUMERO_SERVO
CLRF POSICION_SERVO
GOTO FIN_INTER
CHECO_3
MOVF RCREG,W
MOVWF TEMP
SUBLW '='
BTFSC STATUS,Z
GOTO SIGNO_IGUAL
;
MOVF NUMERO_SERVO,W
MOVWF TEMP2
BCF STATUS,C
RLCF TEMP2,F
RLCF TEMP2,F
RLCF TEMP2,F
RLCF NUMERO_SERVO,F
MOVF TEMP2,W
ADDWF NUMERO_SERVO,F
;
MOVLW .48
SUBWF TEMP,W
ADDWF NUMERO_SERVO,F
GOTO FIN_INTER
;36
SIGNO_IGUAL
BSF AUX_RECEPCION,2
GOTO FIN_INTER
CHECO_4
MOVF RCREG,W
MOVWF TEMP
SUBLW .13
BTFSC STATUS,Z
GOTO SIGNO_NULL
;
MOVF POSICION_SERVO,W
MOVWF TEMP2
BCF STATUS,C
RLCF TEMP2,F
RLCF TEMP2,F
RLCF TEMP2,F
RLCF POSICION_SERVO,F
MOVF TEMP2,W
ADDWF POSICION_SERVO,F
;
MOVLW .48
SUBWF TEMP,W
ADDWF POSICION_SERVO,F
GOTO FIN_INTER
SIGNO_NULL
MOVF NUMERO_SERVO,W
MOVWF FSR0L
MOVLW .36
ADDWF POSICION_SERVO,W
;MOVF POSICION_SERVO,W
MOVWF INDF0
BSF AUX_RECEPCION,3
GOTO FIN_INTER
DIFERENTE_2
MOVF RCREG,W
SUBLW .10
BTFSC STATUS,Z
CLRF AUX_RECEPCION
GOTO FIN_INTER
DIFERENTE
BSF AUX_RECEPCION,3
GOTO FIN_INTER
;*******************************************************
;*******************************************************
INTER_TMR0
BCF INTCON,TMR0IF
BCF T0CON,TMR0ON
BTFSC AUX_TMR0,0
GOTO CONTROL_PULSO
;*********************************************************************
;SECUENCIA DE ORDENAMIENTO DE LOS PWM´S
;*********************************************************************
MOVLW 0xF9 ;LE DA EL PULSO ACTIVO MINIMO PARA CADA SERVO
MOVWF TMR0H
MOVLW 0xA0
MOVWF TMR0L
BSF T0CON,TMR0ON
;****************************************************
BSF PWM_0 ;ACTIVA EL PULSO DA CADA SERVO
NOP
BSF PWM_1
NOP
BSF PWM_2
NOP
BSF PWM_3
NOP
BSF PWM_4
NOP
BSF PWM_5
NOP
BSF PWM_6
NOP
BSF PWM_7
NOP
BSF PWM_8
NOP
BSF PWM_9
NOP
BSF PWM_10 ;ACTIVA EL PULSO DA CADA SERVO
NOP
BSF PWM_11
NOP
BSF PWM_12
NOP
BSF PWM_13
NOP
BSF PWM_14
NOP
BSF PWM_15
CLRF PWM_AUXILIAR
BSF AUX_TMR0,0
GOTO FIN_INTER
;*******************************************
CONTROL_PULSO
;********************************************
MOVLW 0xFF
MOVWF TMR0H
MOVLW 0xEF
MOVWF TMR0L
BSF T0CON,TMR0ON
;¡¡¡¡¡¡¡¡
MOVF PWM_AUXILIAR,W ;VA DESACTIVANDO UNO A UNO CADA PWM
CPFSGT PWM_0_V
BCF PWM_0
CPFSGT PWM_1_V
BCF PWM_1
CPFSGT PWM_2_V
BCF PWM_2
CPFSGT PWM_3_V
BCF PWM_3
CPFSGT PWM_4_V
BCF PWM_4
CPFSGT PWM_5_V
BCF PWM_5
CPFSGT PWM_6_V
BCF PWM_6
CPFSGT PWM_7_V
BCF PWM_7
CPFSGT PWM_8_V
BCF PWM_8
CPFSGT PWM_9_V
BCF PWM_9
CPFSGT PWM_10_V
BCF PWM_10
CPFSGT PWM_11_V
BCF PWM_11
CPFSGT PWM_12_V
BCF PWM_12
CPFSGT PWM_13_V
BCF PWM_13
CPFSGT PWM_14_V
BCF PWM_14
CPFSGT PWM_15_V
BCF PWM_15
;***************************************************
MOVLW 0xFF ;CHECA SI LLEGO A 255
CPFSLT PWM_AUXILIAR
GOTO PERDER_TIEMPO
;************************************************
INCF PWM_AUXILIAR,F ;;SI NO A LLEGADO INCREMENTA Y REPITE
GOTO FIN_INTER
;]****************************************************
PERDER_TIEMPO
BCF T0CON,TMR0ON ; SI YA LLEGO SECUENCIA QUE PIERDE 16ms
MOVLW 0x44
MOVWF TMR0H
MOVLW 0x7F
MOVWF TMR0L
BSF T0CON,TMR0ON
CLRF AUX_TMR0
GOTO FIN_INTER
FIN_INTER
MOVFF BSR_TEMP,BSR
MOVF W_TEMP,W
MOVFF STATUS_TEMP,STATUS
RETFIE
INICIO
;CONFIGURO LOS PUERTOS DE ENTRADA Y SALIDA
MOVLW 0x0F
MOVWF ADCON1
CLRF TRISB
CLRF TRISD
;CONFIGURAMOS EL TIMER0
MOVLW B'00000001' ;SE CONFIGURA CON PREESCALA
MOVWF T0CON ;EN 4
;CONFIGURACION DE LACOMUNICACION SERIE ASINCRONA
BSF TRISC,7
BCF TRISC,6
MOVLW B'00100000'
MOVWF TXSTA
MOVLW B'10010000'
MOVWF RCSTA
CLRF SPBRGH
MOVLW .77;25
MOVWF SPBRG ;VELOCIDAD DEL PUERTO 115200
MOVLW B'00000000'
MOVWF BAUDCON
;CONFIGURO LAS INTERRUPCIONES
BSF PIE1,RCIE ;ACTIVO LA INTERRUPCION POR RECEPCION
MOVLW B'11100000' ;ACTIVO INTERRUPCION POR TMR0
MOVWF INTCON
PRINCIPAL
CLRF TMR0H
CLRF TMR0L
CLRF FSR0H
CLRF FSR0L
CLRF AUX_TMR0
CLRF AUX_RECEPCION
BSF INTCON,TMR0IF
CLRF PORTB
MOVLW .128
MOVWF PWM_0_V
MOVWF PWM_1_V
MOVWF PWM_2_V
MOVWF PWM_3_V
; MOVLW .192
MOVWF PWM_4_V
MOVWF PWM_5_V
MOVWF PWM_6_V
MOVWF PWM_7_V
; MOVLW .128
MOVWF PWM_8_V
MOVWF PWM_9_V
MOVWF PWM_10_V
MOVWF PWM_11_V
; MOVLW .64
MOVWF PWM_12_V
MOVWF PWM_13_V
MOVWF PWM_14_V
MOVWF PWM_15_V
GOTO $
END