EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / PIC18F4550 HC-SR04 and PWM on Assembler

PIC18F4550 HC-SR04 and PWM on Assembler

|

Microcontroller › PIC › PIC18F4550 HC-SR04 and PWM on Assembler

  • This topic has 1 reply, 2 voices, and was last updated 9 years, 9 months ago by Prabakaran P M.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • July 6, 2015 at 4:39 am #3743
    Hector Otarola Garces
    Participant

    Hey!

    I'm working on a project, I'm using a PIC18F4550, I'm trying to measure distances with a HC-SR04 sensor, 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.

    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 ; RB7 AS INPUT (ECHOIN)
    BCF TRISA,0 ; RA0 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'220'
    MOVWF PR2
     
    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 CCPR1L
    bcf INTCON, RBIF
    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
     
    August 3, 2015 at 12:59 pm #13133
    Prabakaran P M
    Participant

    #define _XTAL_FREQ 8000000

    #define RS RD2
    #define EN RD3
    #define D4 RD4
    #define D5 RD5
    #define D6 RD6
    #define D7 RD7

    #include <xc.h>
    #include "lcd.h";
    #include <pic16f877a.h>

    // BEGIN CONFIG
    #pragma config FOSC = HS
    #pragma config WDTE = OFF
    #pragma config PWRTE = OFF
    #pragma config BOREN = ON
    #pragma config LVP = OFF
    #pragma config CPD = OFF
    #pragma config WRT = OFF
    #pragma config CP = OFF
    //END CONFIG

    int a;

    void interrupt echo()
    {
    if(RBIF == 1) //Makes sure that it is PORTB On-Change Interrupt
    {
    RBIE = 0; //Disable On-Change Interrupt
    if(RB4 == 1) //If ECHO is HIGH
    TMR1ON = 1; //Start Timer
    if(RB4 == 0) //If ECHO is LOW
    {
    TMR1ON = 0; //Stop Timer
    a = (TMR1L | (TMR1H<<8))/58.82; //Calculate Distance
    }
    }
    RBIF = 0; //Clear PORTB On-Change Interrupt flag
    RBIE = 1; //Enable PORTB On-Change Interrupt
    }
    void main()
    {
    TRISB = 0b00010000; //RB4 as Input PIN (ECHO)
    TRISD = 0x00; // LCD Pins as Output
    GIE = 1; //Global Interrupt Enable
    RBIF = 0; //Clear PORTB On-Change Interrupt Flag
    RBIE = 1; //Enable PORTB On-Change Interrupt

    Lcd_Init();

    Lcd_Set_Cursor(1,1);
    Lcd_Write_String("Developed By");
    Lcd_Set_Cursor(2,1);
    Lcd_Write_String("electroSome");

    __delay_ms(3000);
    Lcd_Clear();

    T1CON = 0x10; //Initialize Timer Module

    while(1)
    {
    TMR1H = 0; //Sets the Initial Value of Timer
    TMR1L = 0; //Sets the Initial Value of Timer

    RB0 = 1; //TRIGGER HIGH
    __delay_us(10); //10uS Delay
    RB0 = 0; //TRIGGER LOW

    __delay_ms(100); //Waiting for ECHO
    a = a + 1; //Error Correction Constant

    if(a>=2 && a<=400) //Check whether the result is valid or not
    {
    Lcd_Clear();
    Lcd_Set_Cursor(1,1);
    Lcd_Write_String("Distance = ");

    Lcd_Set_Cursor(1,14);
    Lcd_Write_Char(a%10 + 48);

    a = a/10;
    Lcd_Set_Cursor(1,13);
    Lcd_Write_Char(a%10 + 48);

    a = a/10;
    Lcd_Set_Cursor(1,12);
    Lcd_Write_Char(a%10 + 48);

    Lcd_Set_Cursor(1,15);
    Lcd_Write_String("cm");
    }
    else
    {
    Lcd_Clear();
    Lcd_Set_Cursor(1,1);
    Lcd_Write_String("Out of Range");
    }
    __delay_ms(400);
    }
    }

     


    LCD.H


     

    //LCD Functions Developed by electroSome
     
     
    void Lcd_Port(char a)
    {
    if(a & 1)
    D4 = 1;
    else
    D4 = 0;
     
    if(a & 2)
    D5 = 1;
    else
    D5 = 0;
     
    if(a & 4)
    D6 = 1;
    else
    D6 = 0;
     
    if(a & 8)
    D7 = 1;
    else
    D7 = 0;
    }
    void Lcd_Cmd(char a)
    {
    RS = 0;             // => RS = 0
    Lcd_Port(a);
    EN  = 1;             // => E = 1
            __delay_ms(4);
            EN  = 0;             // => E = 0
    }
     
    Lcd_Clear()
    {
    Lcd_Cmd(0);
    Lcd_Cmd(1);
    }
     
    void Lcd_Set_Cursor(char a, char b)
    {
    char temp,z,y;
    if(a == 1)
    {
     temp = 0x80 + b – 1;
    z = temp>>4;
    y = temp & 0x0F;
    Lcd_Cmd(z);
    Lcd_Cmd(y);
    }
    else if(a == 2)
    {
    temp = 0xC0 + b – 1;
    z = temp>>4;
    y = temp & 0x0F;
    Lcd_Cmd(z);
    Lcd_Cmd(y);
    }
    }
     
    void Lcd_Init()
    {
      Lcd_Port(0x00);
       __delay_ms(20);
      Lcd_Cmd(0x03);
    __delay_ms(5);
      Lcd_Cmd(0x03);
    __delay_ms(11);
      Lcd_Cmd(0x03);
      /////////////////////////////////////////////////////
      Lcd_Cmd(0x02);
      Lcd_Cmd(0x02);
      Lcd_Cmd(0x08);
      Lcd_Cmd(0x00);
      Lcd_Cmd(0x0C);
      Lcd_Cmd(0x00);
      Lcd_Cmd(0x06);
    }
     
    void Lcd_Write_Char(char a)
    {
       char temp,y;
       temp = a&0x0F;
       y = a&0xF0;
       RS = 1;             // => RS = 1
       Lcd_Port(y>>4);             //Data transfer
       EN = 1;
       __delay_us(40);
       EN = 0;
       Lcd_Port(temp);
       EN = 1;
       __delay_us(40);
       EN = 0;
    }
     
    void Lcd_Write_String(char *a)
    {
    int i;
    for(i=0;a!='';i++)
      Lcd_Write_Char(a);
    }
     
    void Lcd_Shift_Right()
    {
    Lcd_Cmd(0x01);
    Lcd_Cmd(0x0C);
    }
     
    void Lcd_Shift_Left()
    {
    Lcd_Cmd(0x01);
    Lcd_Cmd(0x08);
    }
     
  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • Electronic board faulty?!? May 13, 2025
  • Can I use this charger in every country? May 13, 2025
  • using a RTC in SF basic May 13, 2025
  • An Update On Tarrifs May 13, 2025
  • Wish to buy Battery, Charger and Buck converter for 12V , 2A router May 13, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise