毕业设计自行车测速仪(共18页)_第1页
毕业设计自行车测速仪(共18页)_第2页
毕业设计自行车测速仪(共18页)_第3页
毕业设计自行车测速仪(共18页)_第4页
毕业设计自行车测速仪(共18页)_第5页
已阅读5页,还剩13页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上#include <reg52.h>#include <stdio.h>#include <intrins.h>#define GDM12864ADataPort P1 /LCD数据线#define uchar unsigned charsbit DI=P22; / 数据指令 选择sbit RW=P21; / 读写 选择sbit EN=P20; / 读写使能sbit cs1=P24; / 片选1sbit cs2=P23; / 片选2sbit sclk=P25;sbit i_o=P26;sbit rstb=P27;sbit ds18s

2、20_dq=P36;sbit I2C_SCK=P33;sbit I2C_SDA=P35;uchar qq=0,pp=0;/开始/暂停按键,计算机传输过来的数据为8则上传uchar code a=/16*16 0x10,0x22,0x64,0x0C,0x80,0x00,0xFE,0x92,0x92,0x92,0x92,0x92,0xFF,0x02,0x00,0x00, 0x04,0x04,0xFE,0x01,0x40,0x7E,0x42,0x42,0x7E,0x42,0x7E,0x42,0x42,0x7E,0x40,0x00, 0x00,0x00,0xFC,0x24,0x24,0x24,0xFC

3、,0xA5,0xA6,0xA4,0xFC,0x24,0x34,0x26,0x04,0x00, 0x40,0x20,0x9F,0x80,0x42,0x42,0x26,0x2A,0x12,0x2A,0x26,0x42,0x40,0xC0,0x40,0x00, 0x00,0xFC,0x84,0x84,0x84,0xFE,0x14,0x10,0x90,0x10,0x10,0x10,0xFF,0x10,0x10,0x00, 0x00,0x3F,0x10,0x10,0x10,0x3F,0x00,0x00,0x00,0x23,0x40,0x80,0x7F,0x00,0x00,0x00, 0x00,0xF8,

4、0x01,0x02,0xF6,0x10,0x12,0x12,0x12,0x12,0xFA,0x12,0x02,0xFF,0x02,0x00, 0x00,0xFF,0x00,0x00,0x3F,0x11,0x11,0x11,0x11,0x11,0x3F,0x40,0x80,0x7F,0x00,0x00, 0x40,0x42,0x44,0xCC,0x00,0xF4,0x94,0x94,0x94,0xFF,0x94,0x94,0x94,0xF6,0x04,0x00, 0x00,0x40,0x20,0x1F,0x20,0x51,0x48,0x44,0x42,0x7F,0x42,0x44,0x4C,0x

5、61,0x20,0x00, 0x00,0x00,0xFE,0x12,0x12,0x12,0x12,0xFE,0x12,0x12,0x12,0x12,0xFF,0x02,0x00,0x00, 0x40,0x48,0x49,0x49,0x49,0x49,0x49,0x7F,0x49,0x49,0x49,0x49,0x4D,0x68,0x40,0x00, 0x24,0x24,0xA4,0xFE,0xA3,0x22,0x20,0x7E,0x42,0x42,0x42,0x42,0x42,0x7F,0x02,0x00, 0x08,0x06,0x01,0xFF,0x00,0x43,0x41,0x49,0x4

6、9,0x49,0x7F,0x49,0x4D,0x69,0x41,0x00, 0x00,0x00,0xFE,0x82,0x92,0x92,0x92,0xFE,0x92,0x9A,0xD2,0x82,0xFF,0x02,0x00,0x00, 0x40,0x20,0x1F,0x00,0x00,0x7E,0x22,0x22,0x22,0x3F,0x42,0x80,0x7F,0x00,0x00,0x00, 0x80,0x80,0x80,0x80,0xFF,0x80,0x80,0xA0,0x90,0x88,0x84,0x86,0x80,0xC0,0x80,0x00, 0x00,0x00,0x00,0x00

7、,0xFF,0x40,0x40,0x23,0x04,0x08,0x10,0x20,0x60,0x20,0x00,0x00, 0x00,0x06,0x09,0x09,0xE6,0xF0,0x18,0x08,0x08,0x08,0x18,0x30,0x78,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x07,0x0F,0x18,0x30,0x20,0x20,0x20,0x10,0x08,0x00,0x00,0x00, 0x80,0x40,0x20,0xF8,0x47,0x48,0x48,0x48,0xC8,0x7F,0x48,0x48,0x4C,0x68,0x40,0

8、x00, 0x00,0x00,0x00,0xFF,0x00,0x00,0x02,0x0B,0x12,0x62,0xD2,0x0A,0x06,0x02,0x00,0x00;uchar code b=/8*160x00,0xF8,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x3F,0x20,0x20,0x20,0x20,0x3F,0x00,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0xF8,0x

9、00,0x00,0x3F,0x21,0x21,0x21,0x21,0x21,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x21,0x21,0x21,0x21,0x21,0x3F,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0xF8,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x3F,0x00,0x00,0xF8,0x08,0x08,0x08,0x08,0x00,0x00,0x00,0x21,0x21,0x21,0x21,0x21,0x3F,0x00,0x00,0xF8,0x08,0x

10、08,0x08,0x08,0x08,0x00,0x00,0x3F,0x21,0x21,0x21,0x21,0x3F,0x00,0x00,0x08,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0xF8,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x3F,0x21,0x21,0x21,0x21,0x3F,0x00,0x00,0xF8,0x08,0x08,0x08,0x08,0xF8,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x3F,0x

11、00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3E,0x01,0x01,0x3E,0x01,0x01,0x3E,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x13,0x24,0x24,0x19,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x

12、0C,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x80,0x40,0x00,0x

13、00,0x00,0x3F,0x06,0x09,0x10,0x20,0x00,0x00,/17,k0x00,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x02,0x02,0x02,0x3E,0x00,0x00/18,h;/延时程序,延时10usvoid Delay_10_uS(void)char i=10;while(i-);/n mS delayvoid Delay_N_mS( unsigned int n_milisecond) /* n mS delay */unsigned char i;while(n_milisecond-)i=37;w

14、hile(i-);/启动传送bit I2C_Start(void)Delay_10_uS();I2C_SDA =1;Delay_10_uS();I2C_SCK =1;Delay_10_uS();if ( I2C_SDA = 0) return 0;if ( I2C_SCK = 0) return 0;I2C_SDA = 0;Delay_10_uS();I2C_SCK = 0;Delay_10_uS();return 1;/停止信号void I2C_Stop(void)Delay_10_uS();I2C_SDA = 0;Delay_10_uS();I2C_SCK = 1;Delay_10_uS(

15、);I2C_SDA = 1;Delay_10_uS();/无应答的时序,有应答时需要在第9位输出0,没有应答时,第9位为高void I2C_Nack(void)Delay_10_uS();I2C_SDA=1;Delay_10_uS();I2C_SCK=1;Delay_10_uS();I2C_SCK=0;Delay_10_uS();/如果发送完成并且在第9个脉冲处得到ack,那么返回0,表示成功bit I2C_Send_Byte( unsigned char d)unsigned char i = 8;bit bit_ack;while( i- )Delay_10_uS();if ( d &am

16、p;0x80 ) I2C_SDA =1;else I2C_SDA =0;Delay_10_uS();I2C_SCK = 1;Delay_10_uS();I2C_SCK = 0;d = d << 1;Delay_10_uS();I2C_SDA = 1;Delay_10_uS();I2C_SCK = 1;Delay_10_uS();bit_ack = I2C_SDA;I2C_SCK =0;Delay_10_uS();return bit_ack;/接收一个8位数据unsigned char I2C_Receive_Byte(void)unsigned char i = 8, d;Del

17、ay_10_uS();I2C_SDA = 1;while ( i-)d = d << 1;Delay_10_uS();I2C_SCK =1;if ( I2C_SDA ) d+;Delay_10_uS();I2C_SCK =0;return d;/写EEPROM操作void AT24C64_W(void *mcu_address, unsigned int AT24C64_address, unsigned int count )while(count-)I2C_Start();I2C_Send_Byte( 0xa0 );I2C_Send_Byte( AT24C64_address/

18、256 );I2C_Send_Byte( AT24C64_address %256 );I2C_Send_Byte( *(unsigned char*)mcu_address );I2C_Stop();Delay_N_mS(10);(unsigned char*)mcu_address)+;AT24C64_address+;/读EEPROM函数void AT24C64_R(void *mcu_address,unsigned int AT24C64_address,unsigned int count)while(count-)I2C_Start();I2C_Send_Byte( 0xa0 )

19、;I2C_Send_Byte( AT24C64_address/256 );I2C_Send_Byte( AT24C64_address % 256 );I2C_Start();I2C_Send_Byte( 0xa1 );*(unsigned char*)mcu_address = I2C_Receive_Byte();I2C_Nack();I2C_Stop();(unsigned char*)mcu_address)+;AT24C64_address+;/单总线延时void ds18s20delay(int useconds)int s;for(s=0;s<useconds;s+);/

20、单总线复位uchar ds18s20reset()uchar presencesignal;ds18s20_dq=0;ds18s20delay(30);ds18s20_dq=1;ds18s20delay(3);presencesignal=ds18s20_dq;ds18s20delay(30);return presencesignal;/单总线位写入void ds18s20writebit(char val)ds18s20_dq=0;if(val=1)ds18s20_dq=1;elseds18s20_dq=0;ds18s20delay(5);ds18s20_dq=1;/单总线字节写入void

21、 ds18s20writebyte(char val)uchar i;uchar temp;for(i=0;i<8;i+)temp=val>>i;temp&=0x01;ds18s20writebit(temp);ds18s20delay(5);/单总线位读取uchar ds18s20readbit(void)uchar i;ds18s20_dq=0;ds18s20_dq=1;for(i=0;i<3;i+)return ds18s20_dq;/单总线字节读取uchar ds18s20readbyte(void)uchar i;uchar value=0;for(i

22、=0;i<8;i+)if(ds18s20readbit()value|=0x01<<i;ds18s20delay(7);return(value);/实时时钟复位函数void resetds1302()ds18s20delay(1);sclk=0;ds18s20delay(1);rstb=0;ds18s20delay(1);rstb=1;ds18s20delay(1);/时钟字节读取函数uchar readbyteds1302()uchar i;uchar rbyte;uchar tempbyte; rbyte=0x00; i_o=1; ds18s20delay(1); fo

23、r(i=0;i<8;+i) sclk=1; ds18s20delay(1); sclk=0; ds18s20delay(1); tempbyte=(uchar)i_o; tempbyte<<=7; rbyte>>=1; rbyte|=tempbyte; return rbyte; /时钟字节写入函数void writebyteds1302(uchar w_byte)uchar i;ds18s20delay(1); for(i=0;i<8;+i) i_o=0; if(w_byte&0x01)i_o=1; sclk=0; ds18s20delay(1);

24、 sclk=1; ds18s20delay(1); w_byte>>=1; /时钟初始化函数void initds1302()resetds1302();writebyteds1302(0x8e);writebyteds1302(0);resetds1302();writebyteds1302(0x90);writebyteds1302(0xab);resetds1302();writebyteds1302(0xbe);writebyteds1302(0);writebyteds1302(0);writebyteds1302(0);writebyteds1302(0);writeb

25、yteds1302(0);writebyteds1302(0);writebyteds1302(0);writebyteds1302(0);writebyteds1302(0);resetds1302();/时钟字节写入函数void writeclkbyte(uchar adress,uchar date)adress=(adress*2)|0x80);resetds1302();writebyteds1302(adress);writebyteds1302(date);resetds1302(); /状态检查 ,void CheckState() unsigned char dat; DI=

26、0; RW=1; do GDM12864ADataPort=0x00; EN=1; dat=GDM12864ADataPort; EN=0; dat=0x80 & dat; /仅当第7位为0时才可操作(判别busy信号) while(!(dat=0x00); /向LCD发送命令/command :命令void SendCommandToLCD(unsigned char command) CheckState(); RW=0; GDM12864ADataPort=command; EN=1; EN=0;/写显示数据/dat:显示数据void WriteByte(unsigned cha

27、r dat) CheckState(); DI=1; RW=0; GDM12864ADataPort=dat; EN=1; EN=0; /设定行地址(页)-X 0-7void SetLine(unsigned char line) line=line & 0x07; / 0<=line<=7 line=line|0xb8; /1011 1xxx SendCommandToLCD(line);/设定列地址-Y 0-63void SetColumn(unsigned char column) column=column &0x3f; / 0=<column<

28、=63 感觉多余的,column已近小于64了。 column=column | 0x40; /01xx xxxx SendCommandToLCD(column);/设定显示开始行-XXvoid SetStartLine(unsigned char startline) /0-63 startline=startline & 0x07; startline=startline|0xc0; /1100 0000 SendCommandToLCD(startline); /开关显示void SetOnOff(unsigned char onoff)onoff=0x3e | onoff;

29、/0011 111xSendCommandToLCD(onoff);/选择屏幕/screen: 0-全屏,1-左屏,2-右屏void SelectScreen(unsigned char screen) /cs1: 1-左; cs2: 1-右switch(screen) case 0: cs1=0;/全屏_nop_(); _nop_(); _nop_(); cs2=0; _nop_(); _nop_(); _nop_(); break; case 1: cs1=0;/左屏_nop_(); _nop_(); _nop_(); cs2=1;_nop_(); _nop_(); _nop_(); br

30、eak;case 2: cs1=1;/右屏_nop_(); _nop_(); _nop_(); cs2=0; _nop_(); _nop_(); _nop_(); break;/延时delay(unsigned int t )unsigned int i=t,m,n;for(m=0;m<i;m+)for(n=0;n<100;n+);/清屏/screen: 0-全屏,1-左屏,2-右 ?分屏干嘛,直接全屏不好吗void ClearScreen(unsigned char screen) unsigned char i,j;SelectScreen(screen);for(i=0;i&

31、lt;8;i+)SetLine(i);for(j=0;j<128;j+)WriteByte(0x00);/初始化LCDvoid InitLCD() unsigned char i=250; /延时while(i-);SelectScreen(0);SetOnOff(0); /关显示/ClearScreen(0);/清屏SelectScreen(0);SetOnOff(1); /开显示SelectScreen(0);SetStartLine(0); /开始行:0/8*8汉字显示函数void z88(unsigned char lin,unsigned char column,unsigne

32、d char *w, unsigned int address) unsigned char i;if(column>16) return;if(column<8) SelectScreen(1); /如果列数<8(0,1,2,3,4,5,6,7)则写在第一屏上 ,每列一个字节else SelectScreen(2); /否则 (8,9,10,11,12,13,14,15)写在第二屏上column=column & 0x07; /防止越界 SetLine(lin);SetColumn(column<<3);/colum×8 0-64for(i=0

33、;i<8;i+) WriteByte(waddress+i);/8*16汉字显示函数void z816(unsigned char lin,unsigned char column,unsigned char *p,unsigned int m) lin=lin<<1;z88(lin,column,p,m*16);z88(lin+1,column,p,m*16+8);/16×16汉字显示函数void z1616(unsigned char lin,unsigned char column,unsigned char *p,unsigned int m) /lin(页

34、)=16,8格=column(列,不是y),m为第几个汉字,所有汉字的字模都写在一个数组中。lin=lin<<1; /lin*2,一个汉字占两页z88(lin,column,p,m*32);z88(lin,column+1,p,m*32+8);z88(lin+1,column,p,m*32+16);z88(lin+1,column+1,p,m*32+24);/单总线温度读取void ds18s20readtemperature(void)char temp_lsb,temp_msb;char ctemperature;z1616(1,0,a,0);z1616(1,2,a,1);z1

35、616(1,8,a,9);z816(1,4,b,13);ds18s20reset();ds18s20writebyte(0xcc);ds18s20writebyte(0x44);ds18s20delay(5);ds18s20reset();ds18s20writebyte(0xcc);ds18s20writebyte(0xbe);ds18s20delay(5);temp_lsb=ds18s20readbyte();temp_msb=ds18s20readbyte();if(temp_msb=0)temp_lsb>>=1;z816(1,5,b,16);else z816(1,5,b,

36、15);ctemperature=temp_lsb=(-1)*temp_lsb);temp_lsb>>=1;ctemperature=temp_lsb;ctemperature/=10;z816(1,6,b,ctemperature);temp_lsb%=10;z816(1,7,b,temp_lsb);/时钟读秒寄存器uchar dumiaojicunqqi()uchar d;writebyteds1302(0xbf);d=readbyteds1302();resetds1302();return(d); /时钟寄存器内容显示函数void ds1302display()uchar

37、sec,min,hrs,c;z1616(0,0,a,2);z1616(0,2,a,3);z816(0,4,b,13);z816(0,7,b,13);z816(0,10,b,13);resetds1302();writebyteds1302(0xbf);sec=readbyteds1302();c=sec&0x0f;z816(0,12,b,c);c=(sec&0x70);c>>=4;z816(0,11,b,c);min=readbyteds1302();c=min&0x0f;z816(0,9,b,c);c=(min&0x70);c>>=4;

38、z816(0,8,b,c);hrs=readbyteds1302();c=hrs&0x0f;z816(0,6,b,c);c=(hrs&0x30);c>>=4;z816(0,5,b,c);resetds1302();/读秒的低4位uchar shizhongdumiao()uchar sec;resetds1302();writebyteds1302(0xbf);sec=readbyteds1302();resetds1302();sec&=0x0f;return(sec);/速度float sudu()float a;TR0=0;a=TL0;TL0=0;TR0=1;a*=1.1304;/0.314*3.6km/hreturn(a);/显示速度void xianshisudu(float e)unsigned int c,d,f;z1616(2,0,a,4);z1616(2,2,a,1);z816(2,4,b,13);z816(2,7,b,14);z816(2,9,b,17);z816(2,10,b,10);z816(2,11,b,12);z816(2,12,b,18);e*=10;f=d=c=e;c/=100;/十位z816(2,5,b,c);d%=100;d/=10

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论