أهلا ،،
لو عدنا إلى الكود السابق ،،
حسنا ،، لنأخذه حبة حبة ،،،
كود:
// LCD module connections
sbit LCD_RS at RB2_bit;
sbit LCD_EN at RB3_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISB2_bit;
sbit LCD_EN_Direction at TRISB3_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections
char txt[14];
#define TRIG PORTA.F5
#define ECHO PORTA.F4
int distance;
int test;
//main function
void main() {
delay_ms(500);
Lcd_Init();
delay_ms(500); //Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,2,"( Distance )");
TRISA.F5=0; //port b.6 as out port
TRISA.F4=1; //portb.7 as input from the module
إلى هنا ،، لا يوجد ما هو مهم جدا ،، مجرد أعدادات الشاشة ،، وتوضيح انه سيكون التريقر عند الطرف A5 ،، والإيكو ، عند الطرف A4 ،، حسنا ،،
نأتي للجزء الحساس قليلا :
كود:
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.
each 1cm ( soundwave ) = 30us
(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
*/
هنا في هذا الجزء ،، بغض النظر عن ما كتبه الشخص الذي كتب البرنامج ،، اريد ان افهم امرا ما ،،
الآن فهمنا ان سرعة الصوت 343 متر في الثانية ،، وتساوي 34300 سم في الثانية ،، وبمعرفة ان السرعة = المسافة / الزمن ،، فكل 1 سم تحتاج إلى 1/ 34300 , وهي تساوي تقريبا 30 مايكرو ثانية ،،
1سم = 30 مايكرو ثانية ! ،،
أيضا بما اننا سنستخدم المذبذب الداخلي للمايكركنترولر ( 4 ميغا هرتز ) ،، وبالتالي ،، فالديوتي سايكل ستكون 1/ 4 ميغا هرتز ،، وتساوي 250 نانو ثانية ! ،،
الآن من هاتين المعلومتين ،، كيف يمكن حساب الزمن الحقيقي ! ،، ما هو المعامل الذي يجب ان يضرب في الوقت الذي سيحتسبه التايمر ،، لإيجاد الوقت الحقيقة " مدة ارسال الموجة الصوتية وارتدادها من اقرب جسم " !! ؟