新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 單片機——壓力傳感器LCD

單片機——壓力傳感器LCD

作者: 時間:2016-11-18 來源:網(wǎng)絡(luò) 收藏

本文引用地址:http://m.butianyuan.cn/article/201611/315861.htm

主程序
#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子函數(shù)
void WriteDataLCM(uchar WDLCM); //寫數(shù)據(jù)到LCM子函數(shù)
void DisplayOneChar(uchar X,uchar Y,uchar DData); //顯示指定坐標的一個字符子函數(shù)
void DisplayListChar(uchar X,uchar Y,uchar code *DData); //顯示指定坐標的一串字符子函數(shù)
void display(void); //系統(tǒng)顯示子函數(shù)
uchar Adc0832(unsigned char channel);
void alarm(void);
void data_pro(void);


/**********main funcation************/

void main(void)
{
delay(500); //系統(tǒng)延時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++)
{;}
}
}
/**********寫指令到ICM子函數(shù)************/

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;

}

/**********寫數(shù)據(jù)到LCM子函數(shù)************/

void WriteDataLCM(uchar WDLCM)
{
lcd_wait( ); //檢測忙信號
DATAPORT=WDLCM;
LCM_RS=1; // 選中數(shù)據(jù)寄存器
LCM_RW=0; // 寫模式
LCM_EN=1;
_nop_();
_nop_();
_nop_();
LCM_EN=0;
}

/***********lcm內(nèi)部等待函數(shù)*************/

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初始化子函數(shù)***********/

void initLCM( )
{
DATAPORT=0;
delay(15);
WriteCommandLCM(0x38,0); //三次顯示模式設(shè)置,不檢測忙信號
delay(5);
WriteCommandLCM(0x38,0);
delay(5);
WriteCommandLCM(0x38,0);
delay(5);

WriteCommandLCM(0x38,1); //8bit數(shù)據(jù)傳送,2行顯示,5*7字型,檢測忙信號
WriteCommandLCM(0x08,1); //關(guān)閉顯示,檢測忙信號
WriteCommandLCM(0x01,1); //清屏,檢測忙信號
WriteCommandLCM(0x06,1); //顯示光標右移設(shè)置,檢測忙信號
WriteCommandLCM(0x0c,1); //顯示屏打開,光標不顯示,不閃爍,檢測忙信號
}

/****顯示指定坐標的一個字符子函數(shù)****/

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);
}

/*******顯示指定坐標的一串字符子函數(shù)*****/

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++;
}
}

/*****************系統(tǒng)顯示子函數(shù)*****************/

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); //穩(wěn)定顯示
}
/************
讀ADC0832函數(shù)
************/

//采集并返回
uchar Adc0832(unsigned char channel) //AD轉(zhuǎn)換,返回結(jié)果
{
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;//控制命令結(jié)束
_nop_();
_nop_();
dat=0;
for(i=0;i<8;i++)
{
dat|=ADDO;//收數(shù)據(jù)
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;//收數(shù)據(jù)
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;//拉高數(shù)據(jù)端,回到初始狀態(tài)
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;//取壓力值十分位

}

}
/*****************報警子函數(shù)*******************/
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; } //關(guān)閉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; } //關(guān)閉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



關(guān)鍵詞: 單片機壓力傳感器LC

評論


技術(shù)專區(qū)

關(guān)閉