主程序
#include
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define BUSY 0x80 //常量界说
#define DATAPORT P0
//ADC0832的引脚
sbit ADCS =P3^5; //ADC0832 chip seclect
sbit ADDI =P3^7; //ADC0832 k in
sbit ADDO =P3^7; //ADC0832 k out
sbit ADCLK =P3^6; //ADC0832 clock signal
sbit LCM_RS=P2^0;
sbit LCM_RW=P2^1;
sbit LCM_EN=P2^2;
uchar ad_data; //采样值存储
sbit Alarm_led_red =P1^5; //超越压力表量程最大值赤色led报警界说
sbit Alarm_led_green=P1^6; //低于压力表量程最小值绿色led报警界说
//adc采样值存储单元
char press_data; //标度改换存储单元
unsigned char ad_alarm; //报警值存储单元
unsigned char press_bai=0; //显现值百位
unsigned char press_shi=0; //显现值十位
unsigned char press_ge=0; //显现值个位
unsigned char press_dot=0; //显现值非常位
uchar code str0[]={“Press: . kpa. “};
uchar code str1[]={” Check BY Jack “};
void delay(uint);
void lcd_wait(void);
void delay_LCM(uint); //LCD延时子程序
void initLCM( void); //LCD初始化子程序
void lcd_wait(void); //LCD检测忙子程序
void WriteCommandLCM(uchar WCLCM,uchar BusyC); //写指令到ICM子函数
void WriteDataLCM(uchar WDLCM); //写数据到LCM子函数
void DisplayOneChar(uchar X,uchar Y,uchar DData); //显现指定坐标的一个字符子函数
void DisplayListChar(uchar X,uchar Y,uchar code *DData); //显现指定坐标的一串字符子函数
void display(void); //体系显现子函数
uchar Adc0832(unsigned char channel);
void alarm(void);
void data_pro(void);
/**********main funcation************/
void main(void)
{
delay(500); //体系延时500ms发动
//ad_data=0; //采样值存储单元初始化为0
initLCM( );
WriteCommandLCM(0x01,1); //清显现屏
DisplayListChar(0,0,str0);
DisplayListChar(0,1,str1);
while(1)
{
ad_data =Adc0832(0); //采样值存储单元初始化为0
alarm();
data_pro();
display();
}
}
/*********延时K*1ms,12.000mhz**********/
void delay(uint k)
{
uint i,j;
for(i=0;i
for(j=0;j<60;j++)
{;}
}
}
/**********写指令到%&&&&&%M子函数************/
void WriteCommandLCM(uchar WCLCM,uchar BusyC)
{
if(BusyC)lcd_wait();
DATAPORT=WCLCM;
LCM_RS=0; // 选中指令寄存器
LCM_RW=0; // 写形式
LCM_EN=1;
_nop_();
_nop_();
_nop_();
LCM_EN=0;
}
/**********写数据到LCM子函数************/
void WriteDataLCM(uchar WDLCM)
{
lcd_wait( ); //检测忙信号
DATAPORT=WDLCM;
LCM_RS=1; // 选中数据寄存器
LCM_RW=0; // 写形式
LCM_EN=1;
_nop_();
_nop_();
_nop_();
LCM_EN=0;
}
/***********lcm内部等候函数*************/
void lcd_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;
}
/**********LCM初始化子函数***********/
void initLCM( )
{
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); //显现屏翻开,光标不显现,不闪耀,检测忙信号
}
/****显现指定坐标的一个字符子函数****/
void DisplayOneChar(uchar X,uchar Y,uchar DData)
{
Y&=1;
X&=15;
if(Y)X|=0x40; //若y为1(显现第二行),地址码+0X40
X|=0x80; //指令码为地址码+0X80
WriteCommandLCM(X,0);
WriteDataLCM(DData);
}
/*******显现指定坐标的一串字符子函数*****/
void DisplayListChar(uchar X,uchar Y,uchar code *DData)
{
uchar ListLength=0;
Y&=0x01;
X&=0x0f;
while(X<16)
{
DisplayOneChar(X,Y,DData[ListLength]);
ListLength++;
X++;
}
}
/*****************体系显现子函数*****************/
void display(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); //安稳显现
}
/************
读ADC0832函数
************/
//收集并回来
uchar Adc0832(unsigned char channel) //AD转化,回来成果
{
uchar i=0;
uchar j;
uint dat=0;
uchar ndat=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=(channel>>1)&0x1;
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,构成下降沿3
ADDI=1;//操控指令完毕
_nop_();
_nop_();
dat=0;
for(i=0;i<8;i++)
{
dat|=ADDO;//收数据
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//构成一次时钟脉冲
_nop_();
_nop_();
dat<<=1;
if(i==7)dat|=ADDO;
}
for(i=0;i<8;i++)
{
j=0;
j=j|ADDO;//收数据
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//构成一次时钟脉冲
_nop_();
_nop_();
j=j<<7;
ndat=ndat|j;
if(i<7)ndat>>=1;
}
ADCS=1;//拉低CS端
ADCLK=0;//拉低CLK端
ADDO=1;//拉高数据端,回到初始状况
dat<<=8;
dat|=ndat;
return(dat); //return ad k
}
void data_pro(void)
{
unsigned int temp;
float press;
if(14 {
int vary=ad_data;//y=(115-15)/(243-13)*X+15kpa
press=((10.0/23.0)*vary)+9.3;//测验时补偿值为9.3
temp=(int)(press*10); //扩大10倍,便于后边的核算
press_bai=temp/1000; //取压力值百位
press_shi=(temp%1000)/100; //取压力值十位
press_ge=((temp%1000)%100)/10; //取压力值个位
press_dot=((temp%1000)%100)%10;//取压力值非常位
}
}
/*****************报警子函数*******************/
unsigned int a;
void alarm(void)
{
if(ad_data>=243) //假如当时压力值大于115kpa,
{ Alarm_led_red=0;
a=10000;
while(a–);
{
}
Alarm_led_red=1;
a=10000;
while(a–);
{
}
} //则发动red led报警
else
{ Alarm_led_red=1; } //封闭red led 报警
if(ad_data<=14)//假如当时压力值小于16kpa
{ Alarm_led_green=0;
a=10000;
while(a–);
{
}
Alarm_led_green=1;
a=10000;
while(a–);
{
}
} //则发动green led报警
else
{ Alarm_led_green=1; } //封闭green led 报警
}
reg51.h子程序
#ifndef __REG51_H__
#define __REG51_H__
/* BYTE Register */
sfr P0 = 0x80;
sfr P1 = 0x90;
sfr P2 = 0xA0;
sfr P3 = 0xB0;
sfr PSW = 0xD0;
sfr ACC = 0xE0;
sfr B = 0xF0;
sfr SP = 0x81;
sfr DPL = 0x82;
sfr DPH = 0x83;
sfr PCON = 0x87;
sfr TCON = 0x88;
sfr TMOD = 0x89;
sfr TL0 = 0x8A;
sfr TL1 = 0x8B;
sfr TH0 = 0x8C;
sfr TH1 = 0x8D;
sfr IE = 0xA8;
sfr IP = 0xB8;
sfr SCON = 0x98;
sfr SBUF = 0x99;
/* BIT Register */
/* PSW */
sbit CY = 0xD7;
sbit AC = 0xD6;
sbit F0 = 0xD5;
sbit RS1 = 0xD4;
sbit RS0 = 0xD3;
sbit OV = 0xD2;
sbit P = 0xD0;
/* TCON */
sbit TF1 = 0x8F;
sbit TR1 = 0x8E;
sbit TF0 = 0x8D;
sbit TR0 = 0x8C;
sbit IE1 = 0x8B;
sbit IT1 = 0x8A;
sbit IE0 = 0x89;
sbit IT0 = 0x88;
/* IE */
sbit EA = 0xAF;
sbit ES = 0xAC;
sbit ET1 = 0xAB;
sbit EX1 = 0xAA;
sbit ET0 = 0xA9;
sbit EX0 = 0xA8;
/* IP */
sbit PS = 0xBC;
sbit PT1 = 0xBB;
sbit PX1 = 0xBA;
sbit PT0 = 0xB9;
sbit PX0 = 0xB8;
/* P3 */
sbit RD = 0xB7;
sbit WR = 0xB6;
sbit T1 = 0xB5;
sbit T0 = 0xB4;
sbit INT1 = 0xB3;
sbit INT0 = 0xB2;
sbit TXD = 0xB1;
sbit RXD = 0xB0;
/* SCON */
sbit SM0 = 0x9F;
sbit SM1 = 0x9E;
sbit SM2 = 0x9D;
sbit REN = 0x9C;
sbit TB8 = 0x9B;
sbit RB8 = 0x9A;
sbit TI = 0x99;
sbit RI = 0x98;
#endif
INTRINS.H子程序
#ifndef __INTRINS_H__
#define __INTRINS_H__
extern void _nop_ (void);
extern bit _testbit_ (bit);
extern unsigned char _cror_ (unsigned char, unsigned char);
extern unsigned int _iror_ (unsigned int, unsigned char);
extern unsigned long _lror_ (unsigned long, unsigned char);
extern unsigned char _crol_ (unsigned char, unsigned char);
extern unsigned int _irol_ (unsigned int, unsigned char);
extern unsigned long _lrol_ (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
#endif
ABSACC.H子程序
#ifndef __ABSACC_H__
#define __ABSACC_H__
#define CBYTE ((unsigned char volatile code *) 0)
#define DBYTE ((unsigned char volatile data *) 0)
#define PBYTE ((unsigned char volatile pdata *) 0)
#define XBYTE ((unsigned char volatile xdata *) 0)
#define CWORD ((unsigned int volatile code *) 0)
#define DWORD ((unsigned int volatile data *) 0)
#define PWORD ((unsigned int volatile pdata *) 0)
#define XWORD ((unsigned int volatile xdata *) 0)
#ifdef __CX51__
#define FVAR(object, addr) (*((object volatile far *) (addr)))
#define FARRAY(object, base) ((object volatile far *) (base))
#define FCVAR(object, addr) (*((object const far *) (addr)))
#define FCARRAY(object, base) ((object const far *) (base))
#else
#define FVAR(object, addr) (*((object volatile far *) ((addr)+0x10000L)))
#define FCVAR(object, addr) (*((object const far *) ((addr)+0x810000L)))
#define FARRAY(object, base) ((object volatile far *) ((base)+0x10000L))
#define FCARRAY(object, base) ((object const far *) ((base)+0x810000L))
#endif
#endif
MATH.h子程序
#ifndef __MATH_H__
#define __MATH_H__
#pragma SAVE
#pragma REGPARMS
extern char cabs (char val);
extern int abs (int val);
extern long labs (long val);
extern float fabs (float val);
extern float sqrt (float val);
extern float exp (float val);
extern float log (float val);
extern float log10 (float val);
extern float sin (float val);
extern float cos (float val);
extern float tan (float val);
extern float asin (float val);
extern float acos (float val);
extern float atan (float val);
extern float sinh (float val);
extern float cosh (float val);
extern float tanh (float val);
extern float atan2 (float y, float x);
extern float ceil (float val);
extern float floor (float val);
extern float modf (float val, float *n);
extern float fmod (float x, float y);
extern float pow (float x, float y);
#pragma RESTORE
#endif