• AVR Freaks

Hot!PIC18f45K20 and HC SR04 Problem

Author
simski24
New Member
  • Total Posts : 5
  • Reward points : 0
  • Joined: 2019/04/18 02:31:38
  • Location: 0
  • Status: offline
2020/11/29 09:29:33 (permalink)
0

PIC18f45K20 and HC SR04 Problem

hello everyone,
currently im working on a project where i have to interface hc-sr04 sensor with pic18f45k20 and  currently im having difficulties in displaying the distance. can anyone suggest why timer1 will not stop when echo goes high?
the coding is bellow
thanks
mplabx IDE v5.40
xc8 v2.30
#1

3 Replies Related Threads

    Mysil
    Super Member
    • Total Posts : 4130
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: PIC18f45K20 and HC SR04 Problem 2020/11/30 12:33:09 (permalink)
    0
    Hi,
    I have not looked into your timer1 problem yet.
     
    There is the same code for LCD character display both in main.c file and in LCD.c
    This may make the Linker confused and complaining, if both files are part of the same MPLAB X project.
    For tidiness, LCD code should be removed from the main file,
    and existence and properties of available functions
    explained to the compiler by prototype declarations like:
    // Here are prototype declations for functions provided in file LCD.c
    void Lcd_Start(void);
     
    void Lcd_Clear(void);
    ...      

    These might be typed directly in main.c, but is better placed in a header file,  LCD.h
    which may then be #included both in main.c   and in LCD.c, and in any other file where it may be needed.
    See attached file, remove the extra .txt, it is there because of limitations in forum or firewall.
     
    About using PIC18F45K20 with a character LCD display:
    There may be a limitation that this microcontroller type is not allowed to be powered by voltage exceeding 3.6 V 
    while most character LCD display need at least 4.5 V for the LCD panel to show any text.
    The Display Controller in the module, Hitahci HD44780 or compatible will work with supply voltage 3.3 V or even lower.
    But display panel will need  VEE pin to be VDD - 4.5 V, which vill require a negative voltage if VDD supply is 3.3 V.
    Some display modules have provisions for this, or a charge pump may be arranged.
    Or a 1.5 V battery may be used for VEE, aka. Contrast pin.
     
    Powering the Display module with +5V, and the microcontroller with 3.3 V may Not be a good solution.
    The display controller have internal pull-up circuits on most pins, so may feed current into the microcontroller,
    unless voltage translators are inserted.
     
    Regards,
        Mysil
     
     
    post edited by Mysil - 2020/12/01 11:48:17
    #2
    dan1138
    Super Member
    • Total Posts : 4321
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC18f45K20 and HC SR04 Problem 2020/12/01 09:55:23 (permalink)
    0
    @simski24,
     
    The code you attached to post#1 seems to have a lot of issues.
     
    As Mysil has mentioned the PIC18F45K20 will not work with VDD more than 3.3 volts.
     
    The HCSR04 and most LCD character modules only work with VDD at 5.0 volts.
     
    The PIC18F4520 does run at 5.0 volts and is almost identical to the PIC18F45K20.
     
    Please reply if you have any controllers available that will operate at 5.0 volts.
     
     
    On a final note:
     
    The timers in the PIC18F4520 and PIC18F4K520 lack a gate input. Timers with gate inputs are really good at measuring pulse widths. If you decide to select another controller it could make your task easier if it has at least one 16-bit timer with a gate input.
    #3
    dan1138
    Super Member
    • Total Posts : 4321
    • Reward points : 0
    • Joined: 2007/02/21 23:04:16
    • Location: 0
    • Status: offline
    Re: PIC18f45K20 and HC SR04 Problem 2020/12/06 20:00:38 (permalink)
    0
    @simski24,
     
    I have worked out a way to interface the PIC18F45K20 with an LCD character module and the HC-SR04 ultrasonic range module.
     
    This code was verified to work in a PICDEM2+ demo board using a PIC18F4520.
     
    As I do not have a PIC18F45K20 available this project has be tested using only the MPLABX simulator.
     
    I believe it will work in a PIC18F45K20 but it will not run in your circuit until you change the GPIO pins defined for the LCD character module to work with your hardware.
     
    #4
    Jump to:
    © 2021 APG vNext Commercial Version 4.5