您好,欢迎访问三七文档
当前位置:首页 > 行业资料 > 酒店餐饮 > GP2Y0A21例程
/*用法:LCD_init();LCD_write_string(列,行,字符串);LCD_write_char(列,行,'字符');---------------------------------------------------------------下面是STC与LCD连接信息LCDdataP0.4-P0.7RSP2.4ENP2.6RWP2.5(RW可不用端口,直接接地)要使用本驱动,改变下面配置信息即可-----------------------------------------------------------------*/#includeSTC12C5A.h#includeintrins.h#defineADC_POWER0x80#defineADC_FLAG0x10#defineADC_START0x08#defineADC_SPEEDLL0x00#defineADC_SPEEDL0x20#defineADC_SPEEDH0x40#defineADC_SPEEDHH0x60#defineucharunsignedchar#defineuintunsignedint#defineLCD_DATA_PORTP0#defineLCD_RSP24//out#defineLCD_ENP26//out#defineLCD_RWP25//out可以直接接地uchardis1[]={CCFROBOTIR:0A21};uchardis2[]={Range:cm};ucharch=0;uintADC_HS;ucharbai,shi,ge;ucharbuf[6];uintA2D;/*--------------------------------------------------------------------------------------------------函数说明--------------------------------------------------------------------------------------------------*/voidLCD_init(void);voidLCD_en_write(void);voidLCD_write_command(ucharcommand);voidLCD_write_data(uchardata1);//(uchardata);不能定义为data,data在kiel51中是关键词。voidLCD_set_xy(ucharx,uchary);voidLCD_write_string(ucharX,ucharY,uchar*s);voidLCD_write_char(ucharX,ucharY,uchardata1);//-----------------------------------------------------------------------------------------voiddelay_1us(void)//1us延时函数{_nop_();//asm(nop);}voiddelay_nus(uintn)//Nus延时函数{uinti=0;for(i=0;in;i++)delay_1us();}voiddelay_1ms(void)//误差0us可以用单片机小精灵计算已包含调用时间。{uchara,b;for(b=222;b0;b--)for(a=12;a0;a--);}voiddelay_nms(uintn)//Nms延时函数{uinti=0;for(i=0;in;i++)delay_1ms();}voidLCD_init(void)//液晶初始化{LCD_RW=0;//RW=0LCD_write_command(0x38);LCD_en_write();delay_nus(40);LCD_write_command(0x38);//0x38为8位显示0x28为4位显示LCD_write_command(0x0c);//显示开LCD_write_command(0x01);//清屏delay_nms(2);}voidLCD_en_write(void)//液晶使能{LCD_EN=1;delay_nus(1);LCD_EN=0;}voidLCD_write_command(ucharcommand)//写指令{/*//连线为高4位的写法delay_nus(16);LCD_RS=0;//RS=0LCD_DATA_PORT&=0X0f;//清高四位LCD_DATA_PORT|=command&0xf0;//写高四位LCD_en_write();command=command4;//低四位移到高四位LCD_DATA_PORT&=0x0f;//清高四位LCD_DATA_PORT|=command&0xf0;//写低四位LCD_en_write();*///数据线8线写法delay_nus(16);LCD_RS=0;//RS=0LCD_DATA_PORT=command;LCD_en_write();/*//连线为低四位的写法delay_nus(16);LCD_RS=0;//RS=0LCD_DATA_PORT&=0xf0;//清高四位LCD_DATA_PORT|=(command4)&0x0f;//写高四位LCD_en_write();LCD_DATA_PORT&=0xf0;//清高四位LCD_DATA_PORT|=command&0x0f;//写低四位LCD_en_write();*/}voidLCD_write_data(uchardata1)//写数据{/*//连线为高4位的写法delay_nus(16);LCD_RS=1;//RS=1LCD_DATA_PORT&=0X0f;//清高四位LCD_DATA_PORT|=data1&0xf0;//写高四位LCD_en_write();data1=data14;//低四位移到高四位LCD_DATA_PORT&=0X0f;//清高四位LCD_DATA_PORT|=data1&0xf0;//写低四位LCD_en_write();*///数据线8线写法delay_nus(16);LCD_RS=1;//RS=1LCD_DATA_PORT=data1;LCD_en_write();/*//连线为低四位的写法delay_nus(16);LCD_RS=1;//RS=1LCD_DATA_PORT&=0Xf0;//清高四位LCD_DATA_PORT|=(data4)&0x0f;//写高四位LCD_en_write();LCD_DATA_PORT&=0Xf0;//清高四位LCD_DATA_PORT|=data&0x0f;//写低四位LCD_en_write();*/}voidLCD_set_xy(ucharx,uchary)//写地址函数{ucharaddress;if(y==0)address=0x80+x;elseaddress=0xc0+x;LCD_write_command(address);}voidLCD_write_string(ucharX,ucharY,uchar*s)//列x=0~15,行y=0,1{LCD_set_xy(X,Y);//写地址while(*s!='\0')//写显示字符{LCD_write_data(*s);s++;}}voidLCD_write_char(ucharX,ucharY,uchardata1)//列x=0~15,行y=0,1{LCD_set_xy(X,Y);//写地址LCD_write_data(data1);}voidAD_Init(){P1ASF=0x01;ADC_RES=0;ADC_CONTR=ADC_POWER|ADC_SPEEDLL|ADC_START|ch;delay_nms(2);}voidGetADCResult(){ADC_CONTR=ADC_POWER|ADC_SPEEDLL|0|ADC_START;_nop_();_nop_();_nop_();_nop_();while(!(ADC_CONTR&ADC_FLAG));ADC_CONTR&=~ADC_FLAG;A2D=ADC_RES*256+ADC_RESL;ADC_HS=(7605/(A2D+12))-4;//returnADC_HS;}voidCount(){bai=(ADC_HS/100+48);shi=(ADC_HS%100/10+48);ge=(ADC_HS%100%10+48);buf[0]=bai;buf[1]=shi;buf[2]=ge;buf[3]=0x0d;buf[4]=0x0a;buf[5]=0x00;}voidmain(){AUXR1=0x04;LCD_init();AD_Init();LCD_write_string(0,0,dis1);LCD_write_string(0,1,dis2);while(1){GetADCResult();Count();LCD_write_char(7,1,bai);LCD_write_char(8,1,shi);LCD_write_char(9,1,ge);}}
本文标题:GP2Y0A21例程
链接地址:https://www.777doc.com/doc-4493576 .html