You are on page 1of 8

AMERICAN UNIVERSITY OF SCIENCE & TECHNOLOGY

DEPARTMENT OF MECHATRONICS ENGINEERING

MTE 407 LAB

Report # 4

Ultrasonic with LCD


Seven segments
Seven segment & decoder

Prepared by
Mahmoud al abed

Presented to
Dr. Mohamad
Contents
Abstract............................................................................................................................................3
Ultrasonic with LCD and 6 LEDs...................................................................................................3
Seven segments................................................................................................................................6
Seven segment with a decoder.........................................................................................................8
Abstract
This report will talk about the ultrasonic sensor with the lcd module and 6 LEDs for the binary
representation. Pic with the seven segment display and the seven segment display with the 7448
decoder

Ultrasonic with LCD and 6 LEDs


#define _XTAL_FREQ 1000000

#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>

void main()

int a;

TRISB4 = 1;

TRISB0=0;

TRISC=0;

PORTC=0;

TRISD = 0x00; // LCD Pins as Output

Lcd_Init();

Lcd_Set_Cursor(1,1);
Lcd_Write_String("Booting");

Lcd_Set_Cursor(2,1);

Lcd_Write_String("Up");

__delay_ms(500);

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

//while(!RB4); //Waiting for Echo

TMR1ON = 1;

Lcd_Write_String("Distance = ");//Timer Starts

while(RB4); //Waiting for Echo goes LOW

TMR1ON = 0; //Timer Stops

a = (TMR1L | (TMR1H<<8)); //Reads Timer Value

a = a/58.82; //Converts Time to Distance

if(a>=2 && a<=400) //Check whether the result is valid or not

Lcd_Clear();

Lcd_Set_Cursor(1,1);
if(a>=2 && a<=20)

Lcd_Write_String("Too Close!");

PORTC=0b00111111;

if(a>=21 && a<=80)

Lcd_Write_String("Close");

PORTC=0b00001111;

if(a>81)

Lcd_Write_String("Safe Distance");

PORTC=0b00000011;

else

Lcd_Clear();

Lcd_Set_Cursor(1,1);

Lcd_Write_String("Out of Range");

__delay_ms(1000);

}
}

Seven segments
#include <stdio.h>

#include <stdlib.h>
#include <pic16f877a.h>

#include <xc.h>

#define _XTAL_FREQ 4000000

void main()

TRISD=0;

PORTD=0;

int
seg[]={0b0111111,0b0000110,0b1011011,0b1001111,0b1100110,0b1101101,0b1111100,0b00
00111,0b1111111,0b1101111};

//seg[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7C,0x07,0x7F,0x6F}

while(1)

for(int i=0;i<10;i++)

PORTD=seg[i];

__delay_ms(1000);

}
}

Seven segment with a decoder

You might also like