logo资料库

基于51单片机温度补偿的超声波测距系统.docx

第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
资料共11页,剩余部分请下载后查看
系统流程图:
程序: #include "ds18b20.h" void Delay1ms(unsigned int y) { unsigned int x; for(y;y>0;y--) for(x=110;x>0;x--); } unsigned char Ds18b20Init() { unsigned int i; DSPORT=0; i=70; while(i--); DSPORT=1; i=0; while(DSPORT) { i++; if(i>5000) return 0; } return 1; } void Ds18b20WriteByte(unsigned char dat) { unsigned int i,j; for(j=0;j<8;j++) { DSPORT=0; i++; DSPORT=dat&0x01; i=6; while(i--); DSPORT=1; dat>>=1; } } unsigned char Ds18b20ReadByte() { unsigned char byte,bi; unsigned int i,j; for(j=8;j>0;j--) {
DSPORT=0; i++; DSPORT=1; i++; i++; bi=DSPORT; byte=(byte>>1)|(bi<<7); i=4; while(i--); } return byte; } void Ds18b20ChangTemp() { Ds18b20Init(); Delay1ms(1); Ds18b20WriteByte(0xcc); Ds18b20WriteByte(0x44); } void Ds18b20ReadTempCom() { Ds18b20Init(); Delay1ms(1); Ds18b20WriteByte(0xcc); Ds18b20WriteByte(0xbe); } int Ds18b20ReadTemp() { int temp=0; unsigned char tmh,tml; Ds18b20ChangTemp(); Ds18b20ReadTempCom(); tml=Ds18b20ReadByte(); tmh=Ds18b20ReadByte(); temp=tmh; temp<<=8; temp|=tml; return temp; } float Get_Temp() { int temp=0; float tp=0.0; temp=Ds18b20ReadTemp(); //跳过 ROM 操作命令 //温度转换命令 //跳过 ROM 操作命令 //发送读取温度命令 //先写入转换命令 //然后等待转换完后发送读取温度命令 //读取温度值共 16 位,先读低字节 //再读高字节
tp=temp*0.0625; return tp; } #include "echo.h" #include "usart.h" #include "ds18b20.h" sbit Trig=P2^0; sbit Echo=P2^1; void delayus(int time) { while(time--); } void send_pulse() { Trig=1; delayus(1); Trig=0; } void Echo_Init() { TMOD|=0X09; //定时器 0 位 16 位定时模式 TH0=0; TL0=0; TR0=0; //清零当前计数值 //停止计数 } long Count_Pulse_Time() { char i=0; long time=0; while(Echo==0); TR0=1;//开始计数 while(Echo==1) { //防止距离太远,导致计数溢出 if(TF0) { TF0=0; i++; } //停止计数 } TR0=0; time=((TH0<<8)|TL0)+65535*i; //获得总的计数时间 TH0=0;//计数值清零
TL0=0; i=0; return time; } int get_distance() { int S; long timeus=0; float v=0; send_pulse(); v=331.45+0.61*Get_Temp(); timeus=1500; S=timeus*v*0.00005; return S; } #include "lcd.h" #include "charcode.h" void LcdSt7565_WriteCmd(cmd) { //计算当前温度下的速度值 LCD12864_RE = 0; _nop_(); _nop_(); LCD12864_RS = 0; LCD12864_RW = 0; _nop_(); _nop_(); LCD12864_RE = 1; DATA_PORT = cmd; //put command,放置命令 _nop_(); _nop_(); LCD12864_RE = 0; _nop_(); _nop_(); LCD12864_RW = 1; LCD12864_RE = 0; //command writing ,写入命令 } void show_back(void) { show_Chinese_16X16(1,0,0); show_Chinese_16X16(1,16,1); show_Chinese_16X16(1,32,2); show_Chinese_16X16(1,48,3); show_char_8X16(1,64,0); show_Chinese_16X16(5,0,0);
show_Chinese_16X16(5,16,1); show_Chinese_16X16(5,32,4); show_Chinese_16X16(5,48,5); show_char_8X16(5,64,0); show_char_8X16(5,108,3); show_char_8X16(5,116,3); } void show_data(uchar x,uchar y,int _data) { char g,s,b,q; g=_data%10; s=_data%100/10; b=_data%1000/100; q=_data/1000; show_num_8X16(x,y,q); show_num_8X16(x,y+8,b); show_num_8X16(x,y+16,s); show_num_8X16(x,y+24,g); } void show_num_8X16(uchar x, uchar y, uchar _num) { uchar i; if(y < 64) { LCD12864_CS1 = 0; LCD12864_CS2 = 1; //chip select,打开片选 } else { LCD12864_CS1 = 1; LCD12864_CS2 = 0; y -= 63; //chip select,打开片选 } LcdSt7565_WriteCmd(0XB8 + x); LcdSt7565_WriteCmd(0X40 + y); for(i = 0; i < 16; i++) { LcdSt7565_WriteData(num[_num][i]); if(i == 7) { LcdSt7565_WriteCmd(0XB8 + x + 1); LcdSt7565_WriteCmd(0X40 + y); } }
} void show_char_8X16(uchar x, uchar y, uchar _index) { uchar i; if(y < 64) { LCD12864_CS1 = 0; LCD12864_CS2 = 1; //chip select,打开片选 } else { LCD12864_CS1 = 1; LCD12864_CS2 = 0; y -= 63; //chip select,打开片选 } LcdSt7565_WriteCmd(0XB8 + x); LcdSt7565_WriteCmd(0X40 + y); for(i = 0; i < 16; i++) { LcdSt7565_WriteData(my_char[_index][i]); if(i == 7) { LcdSt7565_WriteCmd(0XB8 + x + 1); LcdSt7565_WriteCmd(0X40 + y); } } } void show_Chinese_16X16(uchar x, uchar y, uchar _index) { uchar i; if(y < 64) { LCD12864_CS1 = 0; LCD12864_CS2 = 1; //chip select,打开片选 } else { LCD12864_CS1 = 1; LCD12864_CS2 = 0; y -= 63; //chip select,打开片选 } LcdSt7565_WriteCmd(0XB8 + x); LcdSt7565_WriteCmd(0X40 + y); for(i = 0; i < 16; i++)
{ LcdSt7565_WriteData(Chinese[_index * 2][i]); } LcdSt7565_WriteCmd(0XB8 + x + 1); LcdSt7565_WriteCmd(0X40 + y); for(i = 0; i < 16; i++) { LcdSt7565_WriteData(Chinese[_index * 2 + 1][i]); } } void LcdSt7565_WriteData(dat) { LCD12864_RE = 0; _nop_(); _nop_(); LCD12864_RS = 1; LCD12864_RW = 0; _nop_(); _nop_(); LCD12864_RE = 1; DATA_PORT = dat; //put command,放置命令 _nop_(); _nop_(); LCD12864_RE = 0; _nop_(); _nop_(); LCD12864_RW = 1; LCD12864_RE = 0; //command writing ,写入命令 } void Lcd12864_Init() { uchar i; LCD12864_RSET = 0; for (i = 0; i < 100; i++); LCD12864_RSET = 1; LCD12864_CS1 = 1; LCD12864_CS2 = 0; LcdSt7565_WriteCmd(0x3f); LCD12864_CS1 = 0; LCD12864_CS2 = 1; LcdSt7565_WriteCmd(0x3f); Lcd12864_ClearScreen(); //chip select,打开片选 //chip select,打开片选 } void Lcd12864_ClearScreen(void)
分享到:
收藏