PIC18F4550 HC-SR04 PWM - Interrupt problem

Discussion in 'Embedded Systems and Microcontrollers' started by hotarola, Jul 6, 2015.

  1. hotarola

    Thread Starter New Member

    Jul 6, 2015
    1
    0
    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 (Text):
    1.     LIST P=18F4550
    2.     #include p18f4550.inc                ; Include register definition file
    3.  
    4.     CONFIG WDT = OFF
    5.     CONFIG MCLRE = ON
    6.     CONFIG DEBUG = ON
    7.     CONFIG LVP = OFF
    8.     CONFIG FOSC = XT_XT
    9.  
    10.  
    11.     CBLOCK    0x080
    12.         D1        ;variable usada para salvar contexto
    13.         D2
    14.     ENDC
    15.    
    16.    
    17.     org 0x0000
    18.  
    19.     goto Inicio
    20.    
    21.     org 0x0008
    22.    
    23.     goto INTERRUPCION
    24. ;Puertos
    25. Inicio
    26.     CLRF    PORTB
    27.     movlw   b'00001111'
    28.     movwf   ADCON1
    29.     BSF    TRISB,7 ; RA0 AS INPUT (ECHOIN)
    30.     BCF    TRISA,0 ; RA1 AS OUTPUT (TRIGGER)
    31.     CLRF     TRISC
    32.     movlw    b'01000111'
    33.     movwf    T0CON
    34.    
    35. ;Interrupciones
    36.     bsf    INTCON, GIE
    37.     bsf    INTCON, RBIE
    38.     bcf    INTCON, RBIF
    39.    
    40. ; Configuracion PWM
    41.  
    42.     MOVLW b'00000110'; BIT 2 Activar
    43.     MOVWF T2CON
    44.     MOVLW b'00011100'
    45.     MOVWF CCP1CON
    46.     MOVLW    d'30'
    47.     MOVWF    CCPR1L
    48.    
    49. TRIGGER; CREADOR DE PULSO DE 10US
    50.     BSF PORTA,0
    51.     nop
    52.     nop
    53.     nop
    54.     nop
    55.     nop
    56.     nop
    57.     nop
    58.     nop
    59.     nop
    60.     BCF PORTA,0
    61.     CALL DELAY60MS; PARA NO CREAR PULSOS EN MENOS DE 50MS
    62.     goto TRIGGER
    63.  
    64. INTERRUPCION
    65.     bsf T1CON, TMR0ON
    66. INTERRUPCION_2
    67.     btfsc PORTB,7
    68.     goto INTERRUPCION_2
    69.     bcf T1CON, TMR0ON
    70.     movwf TMR0L
    71.     movlw PR2
    72.     bcf INTCON, RBIF
    73.     clrf TMR0L
    74.     retfie
    75.  
    76.    
    77. DELAY60MS
    78.     MOVLW D'223'
    79.     MOVWF D1
    80.     MOVLW D'65'
    81.     MOVWF D2
    82.  
    83. DELAY_1
    84.     DECFSZ D1
    85.     GOTO DELAY_1
    86.     DECFSZ D2
    87.     GOTO DELAY_1
    88.     RETURN
    89.    
    90.     END
    91.  
    92.    
     
Loading...