/*****************************************************************************
* Distance calc
* pic 16f887a .
* Portrait orientation
*****************************************************************************
* FileName: magvitron.mcppi
* Dependencies: timer 1
* Processor: PIC16F87A
* Compiler: MIKRO C PRO FOR PIC V 1.65 (2009)
* Company: Edaboard
*
* Software License Agreement
*
* Copyright © 2011 xxxxxx All rights reserved.
*
* Author Date Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Manukrishnan 18/08/12 ...
*****************************************************************************/
char txt[14];
#define TRIG PORTB.F6
#define ECHO PORTB.F7
// Keypad module connections
// End Keypad module connections
// LCD module connections
sbit LCD_RS at RD2_bit;
sbit LCD_EN at RD3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// End LCD module connections
int distance;
int test;
//main function
void main() {
Lcd_Init(); //Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,2,"((--Distance--))");
TRISB.F6=0; //port b.6 as out port
TRISB.F7=1; //portb.7 as input from the module
T1CON = 0b00000000; // config timer refer page 79 of datasheet
INTCON =0b11000000;
PIE1 =0x01;
/*
>>here we use prescalor of 8, and the internal clock ,precisly fosc/4
so one increment will be (let the crstal you use is 8mhz)
8m/(4*8) ie 250000hz or 4us
>>distance traveled by sound is about 343 m/s (in air at 20degrees)
for 30 uS the sound will travel about 1 c.m.
(we have a precision of 1c.m if you need to change by calculating the number
of increment for the patricular length and then changing the calculation
accordingly
>>the sound is reflected back, so we have to divide the value from timer
with 2
*/
while(1)
{
if(test ==1)
{
lcd_out(2,15,"*Error : Ovflw*") ;
delay_ms(1000);
test =0 ;
}
/**********************************
trigger pulse to the module
**********************************/
TRIG = 0;
delay_us(10);
TRIG = 1;
/**********************************
end trigger, Start Timer 1
**********************************/
T1CON.TMR1ON =1;
/**********************************
End timer,
**********************************/
TRIG = 0;
/**********************************
Listen for echo
**********************************/
while(ECHO==0);
/**********************************
Gottcha!, turn timer off
**********************************/
T1CON.TMR1ON =0;
/**********************************
whats the distance?
**********************************/
distance = (4*TMR1H)+(TMR1L/60);
/* distance calculation, the timer 1 is
used here, as the lower nibble of it overflows,
it will increment the higher nibble.
the incrementing will be @246 or at 150us.
that corresponds to 4 cm, so we have to multiply the
higher nibble with 4 and add it will the current
value if TMR1L/60 for distance calculation.
*/
TMR1H=TMR1L=0;
/**********************************
distance to LCD.
**********************************/
inttostr(distance,txt);
lcd_out(2,3,txt) ;
lcd_out(2,11,"cm") ;
Delay_ms(500);
/**********************************
End routine
**********************************/
}
}
/**********************************
check for overflow of timer1
**********************************/
void interrupt()
{
if (PIR1.TMR1IF)
{
test =1;
distance = distance +1;
PIR1.TMR1IF = 0;
}
}