#include <reg52.h>
#include "1602.h"
/************************************宏定義************************************/
#define VELOCITY_30C 3495 //30攝氏度時的聲速,聲速V= 331.5 + 0.6*溫度;
#define VELOCITY_23C 3453 //23攝氏度時的聲速,聲速V= 331.5 + 0.6*溫度;
/************************************位定義************************************/
sbit INPUT = P2^0; //回聲接收端口
sbit OUTPUT = P2^1; //超聲觸發端口
sbit Beep = P2^3 ; // 蜂鳴器
/********************************定義變量和數組********************************/
long int distance=0; //距離變量
uchar table[]=" Welcome to ";
uchar table0[]=" Yahboom ";
uchar table1[]="There's no echo.";
uchar table2[]=" BST-V51 ";
uchar table3[]="Distance:";
uchar count;
/***********************************函數聲明***********************************/
extern void initLCD();
extern void write_date(uchar date);
extern void write_com(uchar com);
extern void delay(uint x);
/******************************************************************************/
/* 函數名稱 : Delay_xMs */
/* 函數描述 : 延時函數 */
/* 輸入參數 : x */
/* 參數描述 : 延時時間 */
/* 返回值 : 無 */
/******************************************************************************/
void Delay_xMs(unsigned int x)
{
unsigned int i,j;
for(i = 0;i < x;i++ )
{
for(j = 0;j < 3;j++ )
{
;
}
}
}
/******************************************************************************/
/* 函數名稱 : Alarm */
/* 函數描述 : 蜂鳴器發聲函數 */
/* 輸入參數 : t */
/* 參數描述 : 發聲的次數 */
/* 返回值 : 無 */
/******************************************************************************/
void Alarm(uchar t)
{
uchar i;
for(i = 0;i < t;i++)
{
Beep = 0;
Delay_xMs(1000);
Beep = 1;
Delay_xMs(1000);
}
}
/******************************************************************************/
/* 函數名稱 : delayt */
/* 函數描述 : 延時函數 */
/* 輸入參數 : x */
/* 參數描述 : 延時時間數據 */
/* 返回值 : 無 */
/******************************************************************************/
void delayt(uint x)
{
uchar j;
while(x-- > 0)
{
for(j = 0;j < 125;j++)
{
;
}
}
}
/******************************************************************************/
/* 函數名稱 : Init_MCU */
/* 函數描述 : 初始化單片機函數 */
/* 輸入參數 : 無 */
/* 參數描述 : 無 */
/* 返回值 : 無 */
/******************************************************************************/
void Init_MCU(void)
{
TMOD = 0x01; //定時器2初始化,設置為16位自動重裝模式
TL0 = 0x66;
TH0 = 0xfc; //1ms
ET0 = 1; //開定時器2
EA = 1; //總中斷使能
}
/******************************************************************************/
/* 函數名稱 : Init_Parameter */
/* 函數描述 : 初始化參數和IO口函數 */
/* 輸入參數 : 無 */
/* 參數描述 : 無 */
/* 返回值 : 無 */
/******************************************************************************/
void Init_Parameter(void)
{
OUTPUT =1;
INPUT = 1;
count = 0;
distance = 0;
}
/******************************************************************************/
/* 函數名稱 : display_char */
/* 函數描述 : 顯示字符串函數 */
/* 輸入參數 : point,address */
/* 參數描述 : 寫入的字符串的地址指針 1602顯示對應的地址 */
/* 返回值 : 無 */
/******************************************************************************/
void display_char(uchar *point,uchar address)
{
uchar i;
write_com(0x80 + address);
for(i = 0;i < 16; i++)
{
write_date(*point);
point++;
}
}
/******************************************************************************/
/* 函數名稱 : display */
/* 函數描述 : 顯示數字 */
/* 輸入參數 : number,address */
/* 參數描述 : number寫入的數據,address地址 */
/* 返回值 : 無 */
/******************************************************************************/
void display(int number,uchar address)
{
uchar b,c,d,e;
b= (number / 1000);
c= (number / 100) % 10;
d = (number / 10) % 10;
e = number % 10;
write_com(0x80 + address);
write_date(b + 48);
write_date(c + 48);
write_date(d + 48);
write_date(46); //小數點的ASCII
write_date(e + 48);
write_date(99); //"c"的ASCII
write_date(109); //"m"的ASCII
}
/******************************************************************************/
/* 函數名稱 : Trig_SuperSonic */
/* 函數描述 : 發出聲波函數 */
/* 輸入參數 : 無 */
/* 參數描述 : 無 */
/* 返回值 : 無 */
/******************************************************************************/
void Trig_SuperSonic(void)//出發聲波
{
OUTPUT = 1;
delayt(1);
OUTPUT = 0;
}
/******************************************************************************/
/* 函數名稱 : Measure_Distance */
/* 函數描述 : 計算距離函數 */
/* 輸入參數 : 無 */
/* 參數描述 : 無 */
/* 返回值 : 無 */
/******************************************************************************/
void Measure_Distance(void)
{
uchar l;
uint h,y;
TR0 = 1;
while(INPUT)
{
;
}
TR0 = 0;
l = TL0;
h = TH0;
y = (h << 8) + l;
y = y - 0xfc66;//us部分
distance = y + 1000 * count;//計算總時間
TL0 = 0x66;
TH0 = 0xfc;
delayt(30);
distance = VELOCITY_30C * distance / 20000;
}
/******************************************************************************/
/* 函數名稱 : main */
/* 函數描述 : 主函數 */
/* 輸入參數 : 無 */
/* 參數描述 : 無 */
/* 返回值 : 無 */
/******************************************************************************/
void main(void)
{
rw = 0;
initLCD();
Init_MCU();
Init_Parameter();
Alarm(2);
display_char(table,0x00);
display_char(table0,0x40);
Delay_xMs(30000);
display_char(table2,0x00);
display_char(table1,0x40);
while(1)
{
Trig_SuperSonic(); //觸發超聲波發射
while(INPUT == 0) //等待回聲
{
;
}
Measure_Distance(); //計算脈寬并轉換為距離
display_char(table3,0x40);
display(distance,0x49); //顯示距離
Init_Parameter(); // 參數重新初始化
delayt(100); //延時,兩次發射之間要至少有10ms間隔
}
}
/******************************************************************************/
/* 函數名稱 : timer0 */
/* 函數描述 : T0中斷處理函數 */
/* 輸入參數 : 無 */
/* 參數描述 : 無 */
/* 返回值 : 無 */
/******************************************************************************/
void timer0 (void) interrupt 1
{
TF0 = 0;
TL0 = 0x66;
TH0 = 0xfc;
count++;
if(count == 18)//超聲波回聲脈寬最多18ms
{
TR0 =0;
TL0 = 0x66;
TH0 = 0xfc;
count = 0;
}
}
/******************************************************************************/
|