0

I am writing an assembly code for measuring the distance using ultrasonic sensor and pic18 the problem is I can't get the result to get displayed on the lcd and I don’t know if the steps to measure the distance are correct .

I tried this code

#include <p18f452.inc>
#define _XTAL_FREQ 8000000

#define LCD_DATA PORTD          ; Define the data bus port
#define LCD_RS PORTCbits.RC0    ; Define the RS control port
    
#define LCD_E PORTCbits.RC1     ; Define the EN control port
    
#define TRIG PORTBbits.RB0      ;TRIG
#define ECHO PORTBbits.CCP1      ;ECHO
    PERH EQU 0x10
    PERL EQU 0x11
    COUNT1 EQU 0x12
    COUNT2 EQU 0x13
    result equ 0x14
  
   ORG 00
   GOTO MAIN
    MAIN:
    bcf TRISB,0
    BSF TRISC,CCP1          ; configure CCP1 pin for input
   CALL delay
    call init_LCD        ; Initialize LCD
    
  MAIN_LOOP:
    ;bsf PORTB,0
   call  s_10us_signal
    bcf PORTB,0
       wait_ECHO:
  btfss PORTC,CCP1
  BRA wait_ECHO
  CALL capture_signal
  CALL distance
  call delay
  GOTO MAIN_LOOP
  
     s_10us_signal:

  ;BCF TRISB, 0           ; PB0 as an output
        BCF PORTB,0             ; PB0 = 0 
        MOVLW 0X80              ; Timer1, 16 bit, int CLK, no prescaler  0XC0
        MOVWF T1CON
        MOVLW 0XFF              ; TMR0H = FCH
        MOVWF TMR1H
        MOVLW 0XEC
        MOVWF TMR1L
        BCF PIR1, TMR1IF
        BSF PORTB, 0
        BSF T1CON, TMR1ON
        WAIT: 
        BTFSS PIR1,TMR1IF
        BRA WAIT
        BCF T1CON, TMR1ON
  ;bcf PORTB,0
        RETURN
capture_signal:
        
        CLRF   CCP1CON
  CLRF   CCP1IF
  
  MOVLW 0x80           ; use Timer1 as the time base of CCP1 capture
  MOVWF T1CON
  BCF PIE1,CCP1IE     ; disable CCP1 capture interrupt
  MOVLW 0x81           ; enable Timer1, prescaler set to 1,
  MOVWF T1CON         ; 16-bit, use internal cycle clock 
  MOVLW 0x05            ; set CCP1 to capture on every rising edge
  MOVWF CCP1CON
  BCF PIR1,CCP1IF
  Edge1: 
        BTFSS PIR1,CCP1IF   ; wait for the first edge to arrive
  BRA Edge1
  MOVFF CCPR1H,PERH   ; save the first edge
  MOVFF CCPR1L,PERL 
  BCF PIR1,CCP1IF     ; clear the CCP1IF flag
  Edge2: 
        BTFSS PIR1,CCP1IF   ; wait for the second edge to arrive bra edge2
  BRA Edge2
  CLRF CCP1CON        ; disable CCP1 capture
  MOVF PERL,W
  SUBWF CCPR1L,W    ; subtract first edge from 2nd edge
  MOVWF PERL        ; and leave the period in PERH:PERL
  MOVF PERH,W
  SUBWFB CCPR1H,W
  addwf PERL,result
  ;MOVWF PERH
  RETURN 
  
  distance:
  movlw d'34'
  mulwf result 
   movlw result 
   
  call LCD_data
  movlw 'c'
  call LCD_data
  movlw'm'
  call LCD_data
  mullw PERL
  movwf PERL
  movlw d'34'
  mullw PERH
  addwf PERL,F
  
  return
  
   
delay:
    movlw d'450'
    movwf COUNT1
    D1:
  movlw d'450'
  movwf COUNT2
    D2:
  decfsz COUNT2, F
  goto D2
  decfsz COUNT1, F
  goto D1
  return
    
  init_LCD:
    bcf TRISC, 0 ; Set RS pin as output
    bcf TRISC, 1 ; Set EN pin as output
   
    clrf TRISD ; Set data bus pins as output
    call delay
    movlw b'00000001' ; Clear display
    call LCD_cmd ; Send command
    call delay
    movlw b'00110000' ; Function set: 8-bit mode, 2-line display, 5x8 font
    call LCD_cmd ; Send command
    call delay
    movlw b'00001100' ; Display control: Display on, cursor off, blink off
    call LCD_cmd ; Send command
    call delay
    movlw b'00000110' ; Entry mode set: Increment cursor position, no display shift
    call LCD_cmd ; Send command
    call delay
    movlw 'd' ; Send character 'A' to LCD
    call LCD_data
    movlw 'i' ; Send character 'B' to LCD
    call LCD_data
     movlw 's' ; Send character 'B' to LCD
    call LCD_data
     movlw 't' ; Send character 'B' to LCD
    call LCD_data
     movlw 'a' ; Send character 'B' to LCD
    call LCD_data
     movlw 'n' ; Send character 'B' to LCD
    call LCD_data
     movlw 'c' ; Send character 'B' to LCD
    call LCD_data
     movlw 'e' ; Send character 'B' to LCD
    call LCD_data
;    movlw b'11000000' ; Set cursor to second line
;    call LCD_cmd
    movlw ':'
    
    call  LCD_data
     
    return

; Send command to LCD
LCD_cmd:
     bcf PORTC,0 ; Set RS low for command mode
     movwf PORTD ; Send command to data bus
    bsf PORTC,1 ; Pulse EN pin high
    call delay
    bcf PORTC,1 ; Pulse EN pin low
    return
; Send data to LCD
LCD_data:
    
    bsf PORTC,0 ; Set RS high for data mode
  
    movwf PORTD ; Send data to data bus
    bsf PORTC,1 ; Pulse EN pin high
    call delay
    bcf PORTC,1 ; Pulse EN pin low
    return
     ; Delay function
     
     ; Assume binary value is stored in register W
; Assume output will be stored in buffer starting at address 0x20

 END
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • 1
    Break the project up into smaller tasks. Start by controlling the LCD – display characters from the program. That will provide a feedback interface to then debug sensor communications. – jolati Mar 07 '23 at 15:44

0 Answers0