PIC18F4550 HC-SR04 PWM - Interrupt problem

Thread Starter

hotarola

Joined Jul 6, 2015
1
Hey!

I'm working on a project using a PIC18F4550, I'm trying to measure distances with a HC-SR04 sensor and change frecuency of PWM , I have to trig a 10us pulse to the sensor, and I receive a pulse back from the echo. Wider the pulse, longer the distance. I'm trying to use interrupts to start counting with Timer0 the time that echo is '1'. The problem here, is that I can't measure the back pulse. I can't get into the interrupt succesfully and the timer doens't count what I need.

On the interrupt I write on PR2 register to change frecuency of PWM, but I haven't succeded yet. I already sent 10us pulses and I received an echo, but I haven't been able to use the timer properly.

I hope you can help me!

Here's my code.

Code:
    LIST P=18F4550
    #include p18f4550.inc                ; Include register definition file

    CONFIG WDT = OFF
    CONFIG MCLRE = ON
    CONFIG DEBUG = ON
    CONFIG LVP = OFF
    CONFIG FOSC = XT_XT


    CBLOCK    0x080
        D1        ;variable usada para salvar contexto
        D2
    ENDC
   
   
    org 0x0000

    goto Inicio
   
    org 0x0008
   
    goto INTERRUPCION
;Puertos
Inicio
    CLRF    PORTB
    movlw   b'00001111'
    movwf   ADCON1
    BSF    TRISB,7 ; RA0 AS INPUT (ECHOIN)
    BCF    TRISA,0 ; RA1 AS OUTPUT (TRIGGER)
    CLRF     TRISC
    movlw    b'01000111'
    movwf    T0CON
   
;Interrupciones
    bsf    INTCON, GIE
    bsf    INTCON, RBIE
    bcf    INTCON, RBIF
    
; Configuracion PWM

    MOVLW b'00000110'; BIT 2 Activar
    MOVWF T2CON
    MOVLW b'00011100'
    MOVWF CCP1CON
    MOVLW    d'30'
    MOVWF    CCPR1L
   
TRIGGER; CREADOR DE PULSO DE 10US
    BSF PORTA,0
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    nop
    BCF PORTA,0
    CALL DELAY60MS; PARA NO CREAR PULSOS EN MENOS DE 50MS
    goto TRIGGER

INTERRUPCION
    bsf T1CON, TMR0ON
INTERRUPCION_2
    btfsc PORTB,7
    goto INTERRUPCION_2
    bcf T1CON, TMR0ON
    movwf TMR0L
    movlw PR2
    bcf INTCON, RBIF
    clrf TMR0L
    retfie

   
DELAY60MS
    MOVLW D'223'
    MOVWF D1
    MOVLW D'65'
    MOVWF D2

DELAY_1
    DECFSZ D1
    GOTO DELAY_1
    DECFSZ D2
    GOTO DELAY_1
    RETURN
   
    END
 
Top