51单片机-超声波测距程序
0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
/******************************************************************************/
void Lcd_init(void) //初始化LCD
{
Delaynms(10); //启动等待,等LCM讲入工作状态
CS=1;
Write_char(0,0x30); //8 位介面,基本指令集
Write_char(0,0x0c); //显示打开,光标关,反白关
Write_char(0,0x01); //清屏,将DDRAM的地址计数器归零
}
/******************************************************************************/
void Write_char(bit start, unsigned char ddata) //写指令或数据
{
unsigned char start_data,Hdata,Ldata;
if(start==0)
start_data=0xf8; //写指令
else
start_data=0xfa; //写数据
Hdata=ddata&0xf0; //取高四位
Ldata=(ddata<4)&0xf0; //取低四位
Send_byte(start_data); //发送起始信号
Delaynms(5); //延时是必须的
Send_byte(Hdata); //发送高四位
Delaynms(1); //延时是必须的
Send_byte(Ldata); //发送低四位
Delaynms(1); //延时是必须的
}
/******************************************************************************/
void Send_byte(unsigned char bbyte) //发送一个字节
{
unsigned char i;
for(i=0;i<8;i++)
{
SID=bbyte&0x80; //取出最高位
SCLK=1;
SCLK=0;
bbyte<=1; //左移
}
}
/******************************************************************************/
void Delaynms(unsigned int di) //延时
{
unsigned int da,db;
for(da=0;da for(db=0;db<10;db++); } /******************************************************************************/ void Disp_img(unsigned char *img) //图形方式12864显示字模221 横向取膜 { unsigned char i,j; unsigned int k = 0; Write_char(0,0x36); //图形方式 for(i=0;i<32;i++) { Write_char(0,0x80+i); Write_char(0,0x80); for(j=0;j<16;j++) { Write_char(1,img[k++]); } } for(i=0;i<32;i++) { Write_char(0,0x80+i); Write_char(0,0x88); for(j=0;j<16;j++) { Write_char(1,img[k++]); } } } /******************************************************************************/ void Clr_Scr(void)//清屏函数 { Write_char(0,0x01); } /******************************************************************************/ void LCD_set_xy( unsigned char x, unsigned char y ) { //设置LCD显示的起始位置,X为行,Y为列 unsigned char address; switch(x) { case 0: address = 0x80 + y; break; case 1: address = 0x80 + y; break; case 2: address = 0x90 + y; break; case 3: address = 0x88 + y; break; case 4: address = 0x98 + y; break; default:address = 0x80 + y; break; } Write_char(0, address); } /******************************************************************************/ void LCD_Write_string(unsigned char X,unsigned char Y,unsigned char *s) { // 中英文字符串显示函数 LCD_set_xy( X, Y ); while (*s) { Write_char( 1, *s ); s ++; Delaynms(1); } } /******************************************************************************/ void LCD_Write_number(unsigned char s)// 数字显示函数 { Write_char(1,num[s]); Delaynms(1); } /******************************************************************************/ void Lcd_Mark2(void) { Clr_Scr();//清屏 LCD_Write_string(1,0,znwk);// LCD_Write_string(2,0,waves);// LCD_Write_string(3,7,CM);// } /********************************************************/ void Conut(void) { time=TH0*256+TL0; TH0=0; TL0=0; S=time*1.87/100; //算出来是CM 11。0592M晶振 if(flag==1) //超出测量 { flag=0; LCD_set_xy( 3, 4 ); LCD_Write_number(13); LCD_Write_number(12); LCD_set_xy( 3, 5 ); LCD_Write_number(13); LCD_Write_number(13); } else { disbuff[1]=S%1000/100; disbuff[2]=S%1000%100/10; disbuff[3]=S%1000%10 %10; LCD_set_xy( 3, 4 ); LCD_Write_number(disbuff[1]); LCD_Write_number(12); LCD_set_xy( 3, 5 ); LCD_Write_number(disbuff[2]); LCD_Write_number(disbuff[3]); } } /********************************************************/ void delayms(unsigned int ms) { unsigned char i=100,j; for(;ms;ms--) { while(--i) { j=10; while(--j); } } } /************
51单片机超声波测 相关文章:
- 51单片机驱动超声波测距模块C51程序(11-30)
- AT89C2051单片机超声波测距系统硬件原理(11-30)
- 基于C8051单片机超声波测距电平触发编写(11-29)
- 51单片机超声波测距C程序(11-27)
- 51单片机超声波测距程序C调汇编(11-19)
- Windows CE 进程、线程和内存管理(11-09)