#include#include#include#include#defineucharunsignedchar#defineuintunsignedint#defineBUSY0x80//常量定义#defineDATAPORTP0//ADC0832的引脚sbitADCS=P3^5;//ADC0832chipseclectsbitADDI=P3^7;//ADC0832kinsbitADDO=P3^7;//ADC0832koutsbitADCLK=P3^6;//ADC0832clocksignalsbitLCM_RS=P2^0;sbitLCM_RW=P2^1;sbitLCM_EN=P2^2;ucharad_data;//采样值存储sbitAlarm_led_red=P1^5;//超过压力表量程最大值红色led报警定义sbitAlarm_led_green=P1^6;//低于压力表量程最小值绿色led报警定义//adc采样值存储单元charpress_data;//标度变换存储单元unsignedcharad_alarm;//报警值存储单元unsignedcharpress_bai=0;//显示值百位unsignedcharpress_shi=0;//显示值十位unsignedcharpress_ge=0;//显示值个位unsignedcharpress_dot=0;//显示值十分位ucharcodestr0[]={Press:.kpa};ucharcodestr1[]={CheckBYJack};voiddelay(uint);voidlcd_wait(void);voiddelay_LCM(uint);//LCD延时子程序voidinitLCM(void);//LCD初始化子程序voidlcd_wait(void);//LCD检测忙子程序voidWriteCommandLCM(ucharWCLCM,ucharBusyC);//写指令到ICM子函数voidWriteDataLCM(ucharWDLCM);//写数据到LCM子函数voidDisplayOneChar(ucharX,ucharY,ucharDData);//显示指定坐标的一个字符子函数voidDisplayListChar(ucharX,ucharY,ucharcode*DData);//显示指定坐标的一串字符子函数voiddisplay(void);//系统显示子函数ucharAdc0832(unsignedcharchannel);voidalarm(void);voiddata_pro(void);voidmain(void){delay(500);//系统延时500ms启动initLCM();WriteCommandLCM(0x01,1);//清显示屏DisplayListChar(0,0,str0);DisplayListChar(0,1,str1);while(1){ad_data=Adc0832(0);//采样值存储单元初始化为0alarm();data_pro();display();}}voiddelay(uintk){uinti,j;for(i=0;i{for(j=0;j60;j++){;}}}voidWriteCommandLCM(ucharWCLCM,ucharBusyC){if(BusyC)lcd_wait();DATAPORT=WCLCM;LCM_RS=0;//选中指令寄存器LCM_RW=0;//写模式LCM_EN=1;_nop_();_nop_();_nop_();LCM_EN=0;}voidWriteDataLCM(ucharWDLCM){lcd_wait();//检测忙信号DATAPORT=WDLCM;LCM_RS=1;//选中数据寄存器LCM_RW=0;//写模式LCM_EN=1;_nop_();_nop_();_nop_();LCM_EN=0;}voidlcd_wait(void){DATAPORT=0xff;//读LCD前若单片机输出低电平,而读出LCD为高电平,则冲突,Proteus仿真会有显示逻辑黄色LCM_EN=1;LCM_RS=0;LCM_RW=1;_nop_();_nop_();_nop_();while(DATAPORT&BUSY){LCM_EN=0;_nop_();_nop_();LCM_EN=1;_nop_();_nop_();}LCM_EN=0;}voidinitLCM(){DATAPORT=0;delay(15);WriteCommandLCM(0x38,0);//三次显示模式设置,不检测忙信号delay(5);WriteCommandLCM(0x38,0);delay(5);WriteCommandLCM(0x38,0);delay(5);WriteCommandLCM(0x38,1);//8bit数据传送,2行显示,5*7字型,检测忙信号WriteCommandLCM(0x08,1);//关闭显示,检测忙信号WriteCommandLCM(0x01,1);//清屏,检测忙信号WriteCommandLCM(0x06,1);//显示光标右移设置,检测忙信号WriteCommandLCM(0x0c,1);//显示屏打开,光标不显示,不闪烁,检测忙信号}voidDisplayOneChar(ucharX,ucharY,ucharDData){Y&=1;X&=15;if(Y)X|=0x40;//若y为1(显示第二行),地址码+0X40X|=0x80;//指令码为地址码+0X80WriteCommandLCM(X,0);WriteDataLCM(DData);}voidDisplayListChar(ucharX,ucharY,ucharcode*DData){ucharListLength=0;Y&=0x01;X&=0x0f;while(X16){DisplayOneChar(X,Y,DData[ListLength]);ListLength++;X++;}}voiddisplay(void){WriteCommandLCM(0x0c,1);//显示屏打开,光标不显示,不闪烁,检测忙信号DisplayListChar(0,0,str0);DisplayListChar(0,1,str1);DisplayOneChar(7,0,press_bai+0x30);DisplayOneChar(8,0,press_shi+0x30);DisplayOneChar(9,0,press_ge+0x30);DisplayOneChar(11,0,press_dot+0x30);delay(1000);//稳定显示}//采集并返回ucharAdc0832(unsignedcharchannel)//AD转换,返回结果{uchari=0;ucharj;uintdat=0;ucharndat=0;if(channel==0)channel=2;if(channel==1)channel=3;ADDI=1;_nop_();_nop_();ADCS=0;//拉低CS端_nop_();_nop_();ADCLK=1;//拉高CLK端_nop_();_nop_();ADCLK=0;//拉低CLK端,形成下降沿1_nop_();_nop_();ADCLK=1;//拉高CLK端ADDI=channel&0x1;_nop_();_nop_();ADCLK=0;//拉低CLK端,形成下降沿2_nop_();_nop_();ADCLK=1;//拉高CLK端ADDI=(channel1)&0x1;_nop_();_nop_();ADCLK=0;//拉低CLK端,形成下降沿3ADDI=1;//控制命令结束_nop_();_nop_();dat=0;for(i=0;i8;i++){dat|=ADDO;//收数据ADCLK=1;_nop_();_nop_();ADCLK=0;//形成一次时钟脉冲_nop_();_nop_();dat=1;if(i==7)dat|=ADDO;}for(i=0;i8;i++){j=0;j=j|ADDO;//收数据ADCLK=1;_nop_();_nop_();ADCLK=0;//形成一次时钟脉冲_nop_();_nop_();j=j7;ndat=ndat|j;if(i7)ndat=1;}ADCS=1;//拉低CS端ADCLK=0;//拉低CLK端ADDO=1;//拉高数据端,回到初始状态dat=8;dat|=ndat;return(dat);//returnadk}voiddata_pro(void){unsignedinttemp;floatpress;if(14{intvary=ad_data;//y=(115-15)/(243-13)*X+15kpapress=((10.0/23.0)*vary)+9.3;//测试时补偿值为9.3temp=(int)(press*10);//放大10倍,便于后面的计算press_bai=temp/1000;//取压力值百位press_shi=(temp00)/100;//取压力值十位press_ge=((temp00)0)/10;//取压力值个位press_dot=((temp00)0);//取压力值十分位}}voidalarm(void){if(ad_data=243)//如果当前压力值大于115kpa,{Alarm_led_red=0;}//则启动redled报警else{Alarm_led_red=1;}//关闭redled报警if(ad_data=14)//如果当前压力值小于16kpa{Alarm_led_green=0;}//则启动greenled报警else{Alarm_led_green=1;}//关闭greenled报警}