欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
超聲波旋轉(zhuǎn)測(cè)距LCD1602顯示(親自測(cè)試)
[打印本頁(yè)]
作者:
269683987
時(shí)間:
2018-4-26 18:42
標(biāo)題:
超聲波旋轉(zhuǎn)測(cè)距LCD1602顯示(親自測(cè)試)
/******************SL-51B實(shí)驗(yàn)開(kāi)發(fā)板例程(超聲波測(cè)距1602顯示)******************
注意:用杜邦線將超聲波模塊的VCC接開(kāi)發(fā)板5V對(duì)外供電接口
用杜邦線將超聲波模塊的GND接開(kāi)發(fā)板GND
用杜邦線將超聲波模塊的Trig接單片機(jī)的P1.5
用杜邦線將超聲波模塊的Echo接單片機(jī)的P1.6
舵機(jī)黃線 PWM信號(hào)輸出P2^2 (舵機(jī)程序)
/**********************************包含頭文件**********************************/
#include <reg51.h>
#define LCD_Data P0
#define Busy 0x80
/************************************宏定義************************************/
#define SPEED_30C 3495 //30攝氏度時(shí)的聲速,聲速V= 331.5 + 0.6*溫度;
#define SPEED_23C 3453 //23攝氏度時(shí)的聲速,聲速V= 331.5 + 0.6*溫度;
/************************************位定義************************************/
sbit ECHO = P1^6; //回聲接收端口
sbit TRIG = P1^5; //超聲觸發(fā)端口
sbit BEEP = P2^3; //蜂鳴器
sbit LCD_RS = P1^0;
sbit LCD_RW = P1^1;
sbit LCD_E = P2^5;
sbit DU = P2^0;
sbit WE = P2^1;
sbit pwm =P2^2 ; //黃線 PWM信號(hào)輸出P0^1 (舵機(jī)程序)
sbit jia =P3^0; //角度增加按鍵檢測(cè)IO口 上:k1 (舵機(jī)程序)
sbit jan =P3^1; //角度減少按鍵檢測(cè)IO口 下:k2 (舵機(jī)程序)
sbit go =P3^7; // 開(kāi)始:k8 (舵機(jī)程序)
sbit sp =P3^6; // 停止:k7 (舵機(jī)程序)
/********************************定義變量和數(shù)組********************************/
long int distance = 0; //距離變量
unsigned char count; //0.5ms次數(shù)標(biāo)識(shí)(舵機(jī)程序)
unsigned char jd; //角度標(biāo)識(shí)(舵機(jī)程序)
unsigned char code table0[] = {" SL-51A "};
unsigned char code table1[] = {" NO ECHO "};
unsigned char code table2[] = {"Distance:xxx.xcm"};
unsigned char count; //同上
unsigned int xh; //(舵機(jī)程序)
void Delay5Ms(void);
void delay(int In,int Out);
void WriteDataLCD(unsigned char WDLCD);
void WriteCommandLCD(unsigned char WCLCD,BuysC);
unsigned char ReadDataLCD(void);
unsigned char ReadStatusLCD(void);
void LCDInit(void);
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData);
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData);
/******************************************************************************/
/* 函數(shù)名稱 : Delay_xMs */
/* 函數(shù)描述 : 延時(shí)函數(shù) */
/* 輸入?yún)?shù) : x */
/* 參數(shù)描述 : 延時(shí)時(shí)間 */
/* 返回值 : 無(wú) */
/******************************************************************************/
void Delay_xMs(unsigned int x)
{
unsigned int i,j;
for(i = 0;i < x;i++ )
{
for(j = 0;j < 3;j++ )
{;}
}
}
/******************************************************************************/
/* 函數(shù)名稱 : delayt */
/* 函數(shù)描述 : 延時(shí)函數(shù) */
/* 輸入?yún)?shù) : x */
/* 參數(shù)描述 : 延時(shí)時(shí)間數(shù)據(jù) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void delayt(unsigned int x)
{
unsigned char j;
while(x-- > 0)
{
for(j = 0;j < 125;j++)
{;}
}
}
/*************************5ms延時(shí)函數(shù)***************************/
void Delay5Ms(void)
{
unsigned int TempCyc = 3552;
while(TempCyc--);
}
/**************************延遲函數(shù)*****************************/
void delay(int In,int Out)
{
int i,j;
for( i = 0;i < In;i++ )
{
for( j = 0;j < Out;j++ )
{;}
}
}
/******************************************************************************/
/* 函數(shù)名稱 : Alarm */
/* 函數(shù)描述 : 蜂鳴器發(fā)聲函數(shù) */
/* 輸入?yún)?shù) : t */
/* 參數(shù)描述 : 發(fā)聲的次數(shù) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void Alarm(unsigned char t)
{
unsigned char i;
for(i = 0;i < t;i++)
{
BEEP = 0;
delay(10,1000);
BEEP = 1;
delay(10,1000);
}
}
/******************************************************************************/
/* 函數(shù)名稱 : Init_timer */
/* 函數(shù)描述 : 初始化單片機(jī)函數(shù) */
/* 輸入?yún)?shù) : 無(wú) */
/* 參數(shù)描述 : 無(wú) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void Init_timer(void)
{
TMOD = 0x01; //定時(shí)器2初始化,設(shè)置為16位自動(dòng)重裝模式
TL0 = 0x66;
TH0 = 0xfc; //1ms
ET0 = 1; //開(kāi)定時(shí)器2
EA = 1; //總中斷使能
}
/******************************************************************************/
/* 函數(shù)名稱 : Init_Parameter */
/* 函數(shù)描述 : 初始化參數(shù)和IO口函數(shù) */
/* 輸入?yún)?shù) : 無(wú) */
/* 參數(shù)描述 : 無(wú) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void Init_Parameter(void)
{
TRIG = 1;
ECHO = 1;
count = 0;
distance = 0;
}
/******************************************************************************/
/* 函數(shù)名稱 : display */
/* 函數(shù)描述 : 顯示數(shù)字 */
/* 輸入?yún)?shù) : number,address */
/* 參數(shù)描述 : number寫(xiě)入的數(shù)據(jù) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void display(int number)
{
unsigned char b,c,d,e;
b = (number/1000);
c = (number/100)%10;
d = (number/10)%10;
e = number%10;
DisplayOneChar(9,1,(0x30+b));
DisplayOneChar(10,1,(0x30+c));
DisplayOneChar(11,1,(0x30+d));
DisplayOneChar(13,1,(0x30+e));
}
/******************************************************************************/
/* 函數(shù)名稱 : Trig_SuperSonic */
/* 函數(shù)描述 : 發(fā)出聲波函數(shù) */
/* 輸入?yún)?shù) : 無(wú) */
/* 參數(shù)描述 : 無(wú) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void Trig_SuperSonic(void)//出發(fā)聲波
{
TRIG = 1;
delayt(1);
TRIG = 0;
}
/******************************************************************************/
/* 函數(shù)名稱 : Measure_Distance */
/* 函數(shù)描述 : 計(jì)算距離函數(shù) */
/* 輸入?yún)?shù) : 無(wú) */
/* 參數(shù)描述 : 無(wú) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void Measure_Distance(void)
{
unsigned char l;
unsigned int h,y;
TR0 = 1;
while(ECHO)
{
;
}
TR0 = 0;
l = TL0;
h = TH0;
y = (h << 8) + l;
y = y - 0xfc66; //us部分
distance = y + 1000 * count; //計(jì)算總時(shí)間
TL0 = 0x66;
TH0 = 0xfc;
delayt(30);
distance = SPEED_30C * distance / 20000;
}
/**************************寫(xiě)數(shù)據(jù)函數(shù)***************************/
void WriteDataLCD(unsigned char WDLCD)
{
ReadStatusLCD(); //檢測(cè)忙
LCD_Data = WDLCD;
LCD_RS = 1;
LCD_RW = 0;
LCD_E = 0; //若晶振速度太高可以在這后加小的延時(shí)
LCD_E = 0; //延時(shí)
LCD_E = 1;
}
/*************************寫(xiě)指令函數(shù)****************************/
void WriteCommandLCD(unsigned char WCLCD,BuysC) //BuysC為0時(shí)忽略忙檢測(cè)
{
if (BuysC) ReadStatusLCD(); //根據(jù)需要檢測(cè)忙
LCD_Data = WCLCD;
LCD_RS = 0;
LCD_RW = 0;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
}
/**************************讀數(shù)據(jù)函數(shù)***************************/
unsigned char ReadDataLCD(void)
{
LCD_RS = 1;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
return(LCD_Data);
}
/**************************讀狀態(tài)函數(shù)***************************/
unsigned char ReadStatusLCD(void)
{
LCD_Data = 0xFF;
LCD_RS = 0;
LCD_RW = 1;
LCD_E = 0;
LCD_E = 0;
LCD_E = 1;
while (LCD_Data & Busy); //檢測(cè)忙信號(hào)
return(LCD_Data);
}
/***************************LCD初始化***************************/
void LCDInit(void) //LCD初始化
{
LCD_Data = 0;
WriteCommandLCD(0x38,0); //三次顯示模式設(shè)置,不檢測(cè)忙信號(hào)
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();
WriteCommandLCD(0x38,0);
Delay5Ms();
WriteCommandLCD(0x38,1); //顯示模式設(shè)置, 開(kāi)始要求每次檢測(cè)忙信號(hào)
WriteCommandLCD(0x08,1); //關(guān)閉顯示
WriteCommandLCD(0x01,1); //顯示清屏
WriteCommandLCD(0x06,1); //顯示光標(biāo)移動(dòng)設(shè)置
WriteCommandLCD(0x0C,1); //顯示開(kāi)及光標(biāo)設(shè)置
}
/****************************清屏 函數(shù)********************************/
void LCD_Clear(void)
{
WriteCommandLCD(0x01,1);
Delay5Ms();
}
/**********************按指定位置顯示一個(gè)字符*********************/
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //當(dāng)要顯示第二行時(shí)地址碼+0x40;
X |= 0x80; //算出指令碼
WriteCommandLCD(X, 0); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
WriteDataLCD(DData);
}
/***********************按指定位置顯示一串字符********************/
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char code *DData)
{
unsigned char ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF; //限制X不能大于15,Y不能大于1
while (DData[ListLength]>=0x20) //若到達(dá)字串尾則退出
{
if (X <= 0xF) //X坐標(biāo)應(yīng)小于0xF
{
DisplayOneChar(X, Y, DData[ListLength]); //顯示單個(gè)字符
ListLength++;
X++;
}
}
}
/***********************舵機(jī)函數(shù)*******************************************************/
void Time0_Init() //定時(shí)器初始化
{
TMOD = 0x01; //定時(shí)器0工作在方式1
IE = 0x82;
TH0 = 0xfe;
TL0 = 0x33; //11.0592MZ晶振,0.5ms
TR0=1; //定時(shí)器開(kāi)始
}
void Time0_Int() interrupt 1 //中斷程序
{
TH0 = 0xfe; //重新賦值
TL0 = 0x33;
if(count< jd) //判斷0.5ms次數(shù)是否小于角度標(biāo)識(shí)
pwm=1; //確實(shí)小于,PWM輸出高電平
else
pwm=0; //大于則輸出低電平
count=(count+1); //0.5ms次數(shù)加1
count=count%40; //次數(shù)始終保持為40 即保持周期為20ms
}
void keyscan() //按鍵掃描
{
if(jia==0) //角度增加按鍵是否按下
{
delay(10); //按下延時(shí),消抖
if(jia==0) //確實(shí)按下
{
jd++; //角度標(biāo)識(shí)加1
count=0; //按鍵按下 則20ms周期從新開(kāi)始
if(jd==6)
jd=5; //已經(jīng)是180度,則保持
while(jia==0); //等待按鍵放開(kāi)
}
}
if(jan==0) //角度減小按鍵是否按下
{
delay(10);
if(jan==0)
{
jd--; //角度標(biāo)識(shí)減1
count=0;
if(jd==0)
jd=1; //已經(jīng)是0度,則保持
while(jan==0);
}
}
}
void xhcan()
{
if(go==0) // 按鍵是否按下
{
delay(10); //按下延時(shí),消抖
if(go==0) //確實(shí)按下
{
xh=1; //
while(go==0); //等待按鍵放開(kāi)
}
}
if(sp==0) // 按鍵是否按下
{
delay(10); //按下延時(shí),消抖
if(sp==0) //確實(shí)按下
{
xh=0; //
while(sp==0); //等待按鍵放開(kāi)
}
}
}
/******************************************************************************/
/* 函數(shù)名稱 : main */
/* 函數(shù)描述 : 主函數(shù) */
/* 輸入?yún)?shù) : 無(wú) */
/* 參數(shù)描述 : 無(wú) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void main(void)
{
DU=1;
P0=0x00;
DU=0;
BEEP=0;
delay(50,100);
BEEP=1;
delay(50,100);
BEEP=0;
delay(50,100);
BEEP=1;
LCDInit();
Init_timer();
Init_Parameter();
Alarm(5);
DisplayListChar(0,0,table0);
DisplayListChar(0,1,table1);
jd=0; //(舵機(jī)程序)
count=0; //(舵機(jī)程序)
Time0_Init(); //(舵機(jī)程序)
while(1)
{
Trig_SuperSonic(); //觸發(fā)超聲波發(fā)射
while(ECHO == 0) //等待回聲
{
;
}
Measure_Distance(); //計(jì)算脈寬并轉(zhuǎn)換為距離
DisplayListChar(0,1,table2);
display(distance); //顯示距離
Init_Parameter(); // 參數(shù)重新初始化
delayt(100); //延時(shí),兩次發(fā)射之間要至少有10ms間隔
keyscan(); //按鍵掃描
xhcan(); //(舵機(jī)程序)
while(xh) //(舵機(jī)程序)
{
jd=4;
delay(100,100);
jd=3;
delay(100,100);
jd=2;
delay(100,100);
jd=1;
delay(100,100);
jd=2;
delay(100,100);
jd=3;
delay(100,100);
jd=4;
delay(100,100);
jd=5;
delay(100,100);
jd=6;
xhcan();
}
}
}
/******************************************************************************/
/* 函數(shù)名稱 : timer0 */
/* 函數(shù)描述 : T0中斷處理函數(shù) */
/* 輸入?yún)?shù) : 無(wú) */
/* 參數(shù)描述 : 無(wú) */
/* 返回值 : 無(wú) */
/******************************************************************************/
void timer0 (void) interrupt 1
{
TF0 = 0;
TL0 = 0x66;
TH0 = 0xfc;
count++;
if(count == 18)//超聲波回聲脈寬最多18ms
{
TR0 =0;
TL0 = 0x66;
TH0 = 0xfc;
count = 0;
}
}
/******************************************************************************/
作者:
xjx
時(shí)間:
2019-2-4 14:54
別吧,樓主直接粘貼復(fù)制各模塊資料的吧。。還親測(cè)有效,寫(xiě)兩個(gè)time0中斷處理是啥意思。。
歡迎光臨 (http://m.raoushi.com/bbs/)
Powered by Discuz! X3.1