仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
eeab20b6699cccd5544261a2ec8beb9.png (69.3 KB, 下載次數: 49)
下載附件
2020-6-12 13:11 上傳
單片機源程序如下:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char //宏定義
#define uint unsigned int //宏定義
#define High 1
#define Low 0
#define _nop {}
#define PAGEADD 0xb8
#define TIERADD 0x40
#define DIS_STARADD 0xc0
sbit DI=P3^7;
sbit E=P3^5;
sbit CS1=P3^1;
sbit CS2=P3^4;
sbit RW=P3^6;
sbit ST=P3^0; //位定義
sbit OE=P1^1; //位定義
sbit EOC=P1^0; //位定義
sbit CLK=P3^3; //位定義
sbit DQ = P1^7;//溫度傳感器
sbit BEEP = P1^2;//蜂鳴器
void init1820();
void write1820(uchar a);
unsigned char read1820(void);
uchar gettemp();
void ShowTem();
unsigned char idata flag;
unsigned char temp;
uint Tem;
uchar show[4] = {1,2,3,4};
uint key_value; //按鍵數值
/*****************************
字符表
******************************/
uchar code table1[]={
0x00,0x3e,0x51,0x49,0x45,0x3e,0x00,0x00,//0(0)
0x00,0x00,0x42,0x7f,0x40,0x00,0x00,0x00,//1
0x00,0x42,0x61,0x51,0x49,0x46,0x00,0x00,//2
0x00,0x21,0x41,0x45,0x4b,0x31,0x00,0x00,//3
0x00,0x18,0x14,0x12,0x7f,0x10,0x00,0x00,//4
0x00,0x27,0x45,0x45,0x45,0x39,0x00,0x00,//5
0x00,0x3c,0x4a,0x49,0x49,0x30,0x00,0x00,//6
0x00,0x01,0x01,0x79,0x05,0x03,0x00,0x00,//7
0x00,0x36,0x49,0x49,0x49,0x36,0x00,0x00,//8
0x00,0x06,0x49,0x49,0x29,0x1e,0x00,0x00,//9
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// (10)
0x00,0x00,0x00,0x47,0x00,0x00,0x00,0x00,//!
0x00,0x23,0x13,0x08,0x64,0x62,0x00,0x00,//%
0x00,0x36,0x49,0x55,0x22,0x50,0x00,0x00,//&
0x00,0x14,0x08,0x3e,0x08,0x14,0x00,0x00,//*
0x00,0x08,0x08,0x3e,0x08,0x08,0x00,0x00,//+
0x00,0x08,0x08,0x08,0x08,0x08,0x00,0x00,//-
0x00,0x00,0x60,0x60,0x00,0x00,0x00,0x00,//.
0x00,0x20,0x10,0x08,0x04,0x02,0x00,0x00,///
0x00,0x00,0x36,0x36,0x00,0x00,0x00,0x00,//:(19)
0x00,0x14,0x14,0x14,0x14,0x14,0x00,0x00,//=
0x00,0x02,0x01,0x51,0x09,0x06,0x00,0x00,//?
0x00,0x32,0x49,0x79,0x41,0x3E,0x00,0x00,//@(22)
0x00,0x7e,0x11,0x11,0x11,0x7f,0x00,0x00,//A
0x00,0x41,0x7f,0x49,0x49,0x36,0x00,0x00,//B
0x00,0x3e,0x41,0x41,0x41,0x22,0x00,0x00,//C
0x00,0x41,0x7f,0x41,0x41,0x3e,0x00,0x00,//D
0x00,0x7f,0x49,0x49,0x49,0x49,0x00,0x00,//E
0x00,0x7f,0x09,0x09,0x09,0x01,0x00,0x00,//F
0x00,0x3e,0x41,0x41,0x49,0x7a,0x00,0x00,//G(29)
0x00,0x7f,0x08,0x08,0x08,0x7f,0x00,0x00,//H
0x00,0x00,0x41,0x7f,0x41,0x00,0x00,0x00,//I
0x20,0x40,0x41,0x3f,0x01,0x01,0x00,0x00,//J
0x00,0x7f,0x08,0x14,0x22,0x41,0x00,0x00,//K
0x00,0x7f,0x40,0x40,0x40,0x40,0x00,0x00,//L
0x00,0x7f,0x02,0x0c,0x02,0x7f,0x00,0x00,//M
0x00,0x7f,0x06,0x08,0x30,0x7f,0x00,0x00,//N
0x00,0x3e,0x41,0x41,0x41,0x3e,0x00,0x00,//O
0x00,0x7f,0x09,0x09,0x09,0x06,0x00,0x00,//P
0x00,0x3e,0x41,0x51,0x21,0x5e,0x00,0x00,//Q(39)
0x00,0x7f,0x09,0x19,0x29,0x46,0x00,0x00,//R
0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00,//S
0x00,0x01,0x01,0x7f,0x01,0x01,0x00,0x00,//T
0x00,0x3f,0x40,0x40,0x40,0x3f,0x00,0x00,//U
0x00,0x1f,0x20,0x41,0x20,0x1f,0x00,0x00,//V
0x00,0x7f,0x20,0x80,0x20,0x7f,0x00,0x00,//W
0x00,0x63,0x14,0x08,0x14,0x63,0x00,0x00,//X
0x00,0x07,0x08,0x70,0x08,0x07,0x00,0x00,//Y
0x00,0x61,0x51,0x49,0x45,0x43,0x00,0x00,//Z
0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00,//a(49)
0x00,0x20,0x54,0x54,0x54,0x78,0x00,0x00,//b
0x00,0x38,0x44,0x44,0x44,0x28,0x00,0x00,//c
0x00,0x38,0x44,0x44,0x48,0x7f,0x00,0x00,//d
0x00,0x38,0x54,0x54,0x54,0x18,0x00,0x00,//e
0x00,0x00,0x08,0x7e,0x09,0x02,0x00,0x00,//f
0x00,0x0c,0x52,0x52,0x4c,0x3e,0x00,0x00,//g
0x00,0x7f,0x08,0x04,0x04,0x78,0x00,0x00,//h
0x00,0x00,0x44,0x7d,0x40,0x00,0x00,0x00,//i
0x00,0x20,0x40,0x44,0x3d,0x00,0x00,0x00,//j
0x00,0x00,0x7f,0x10,0x28,0x44,0x00,0x00,//k(59)
0x00,0x00,0x41,0x7f,0x40,0x00,0x00,0x00,//l
0x00,0x7c,0x04,0x78,0x04,0x78,0x00,0x00,//m
0x00,0x7c,0x08,0x04,0x04,0x78,0x00,0x00,//n
0x00,0x38,0x44,0x44,0x44,0x38,0x00,0x00,//o
0x00,0x7e,0x0c,0x12,0x12,0x0c,0x00,0x00,//p
0x00,0x0c,0x12,0x12,0x0c,0x7e,0x00,0x00,//q
0x00,0x7C,0x08,0x04,0x04,0x08,0x00,0x00,//r
0x00,0x58,0x54,0x54,0x54,0x64,0x00,0x00,//s
0x00,0x04,0x3f,0x44,0x40,0x20,0x00,0x00,//t
0x00,0x3c,0x40,0x40,0x3c,0x40,0x00,0x00,//u(69)
0x00,0x1c,0x20,0x40,0x20,0x1c,0x00,0x00,//v
0x00,0x3c,0x40,0x30,0x40,0x3c,0x00,0x00,//w
0x00,0x44,0x28,0x10,0x28,0x44,0x00,0x00,//x
0x00,0x1c,0xa0,0xa0,0x90,0x7c,0x00,0x00,//y
0x00,0x44,0x64,0x54,0x4c,0x44,0x00,0x00,//z(74)
};
/****************************
中文漢字表
*****************************/
uchar code table2[]={
0x10,0x10,0x14,0xD4,0x54,0x54,0x54,0xFC,
0x52,0x52,0x52,0xD3,0x12,0x10,0x10,0x00,
0x40,0x40,0x50,0x57,0x55,0x55,0x55,0x7F,
0x55,0x55,0x55,0x57,0x50,0x40,0x40,0x00,//重(0)
};
/********************************
延時 ms 大小由n決定
*********************************/
void Delay_nms(uchar n)
{ uchar a;
for(;n>0;n--)
{
for(a=0;a<100;a++)
{
_nop;
_nop;
_nop;
_nop;
};
};
}
/*****************************************
DEM12864B狀態檢測
******************************************/
void LCD_Busy()
{
uchar busy;
E=Low;
DI=Low;
RW=High;
while(1)
{
E=High;
_nop;
_nop;
busy=P2;//都狀態標志寄存器
_nop;
E=Low;
if((busy&0x90)==0)//讀BF和RES 都為0可寫入
break;
};
}
/*********************************
寫指令
**********************************/
void WRCommand_L(uchar command)
{
CS1=High;
CS2=Low;
LCD_Busy();
DI=Low;
_nop;
RW=Low;
_nop;
E=High;
_nop;
P2=command;
_nop;
E=Low;
_nop;
}
void WRCommand_R(uchar command)
{
CS1=Low;
CS2=High;
LCD_Busy();
DI=Low;
_nop;
RW=Low;
_nop;
E=High;
_nop;
P2=command;
_nop;
E=Low;
_nop;
}
/***********************************
寫一個字節的顯示數據
************************************/
void WRdata_L(uchar ucdata)
{
CS1=High;
CS2=Low;
LCD_Busy();
DI=High;
_nop;
RW=Low;
_nop;
E=High;
_nop;
P2=ucdata;
_nop;
E=Low;
_nop;
}
void WRdata_R(uchar ucdata)
{
CS1=Low;
CS2=High;
LCD_Busy();
DI=High;
_nop;
RW=Low;
_nop;
E=High;
_nop;
P2=ucdata;
_nop;
E=Low;
_nop;
}
/****************************************************
字符為16*16顯示 例如漢字
*****************************************************/
C_display_L(uchar C_Pagenum,uchar C_Tiernum,uchar C_Temp)
{
uchar k;
C_Pagenum=PAGEADD|C_Pagenum;
C_Tiernum=TIERADD|C_Tiernum;
WRCommand_L(C_Pagenum);
WRCommand_L(C_Tiernum);
for(k=0;k<16;k++)
{
WRdata_L(table2[C_Temp*32+k]);
};
C_Pagenum=C_Pagenum+1;
WRCommand_L(C_Pagenum);
WRCommand_L(C_Tiernum);
for(k=0;k<16;k++)
{
WRdata_L(table2[C_Temp*32+k+16]);
};
}
C_display_R(uchar C_Pagenum,uchar C_Tiernum,uchar C_Temp)
{
uchar k;
C_Pagenum=PAGEADD|C_Pagenum;
C_Tiernum=TIERADD|C_Tiernum;
WRCommand_R(C_Pagenum);
WRCommand_R(C_Tiernum);
for(k=0;k<16;k++)
{
WRdata_R(table2[C_Temp*32+k]);
};
C_Pagenum=C_Pagenum+1;
WRCommand_R(C_Pagenum);
WRCommand_R(C_Tiernum);
for(k=0;k<16;k++)
{
WRdata_R(table2[C_Temp*32+k+16]);
};
}
/****************************************
寫入西文字符
*****************************************/
E_Display_L(uchar E_Pagenum,uchar E_Tiernum,uchar E_Temp)
{
uchar k;
WRCommand_L(PAGEADD|E_Pagenum);
WRCommand_L(TIERADD|E_Tiernum);
for(k=0;k<8;k++)
{
WRdata_L(table1[E_Temp*8+k]);
};
}
E_Display_R(uchar E_Pagenum,uchar E_Tiernum,uchar E_Temp)
{
uchar k;
WRCommand_R(PAGEADD|E_Pagenum);
WRCommand_R(TIERADD|E_Tiernum);
for(k=0;k<8;k++)
{
WRdata_R(table1[E_Temp*8+k]);
};
}
/*********************************
清空顯示RAM
**********************************/
CLR_DisplayRAM()
{
uchar C_page,i,k;
for(i=0;i<8;i++)
{
C_page=PAGEADD|i;//起始頁為0
WRCommand_L(C_page);//清除起始頁寫入
WRCommand_L(TIERADD);//清除起始行地址寫入
WRCommand_R(C_page);
WRCommand_R(TIERADD);
for(k=0;k<64;k++)
{
WRdata_L(0x00);
WRdata_R(0x00);//lcm?ram自+1 只潤許循環64次
};
};
}
/*********************************
初始化LCD
**********************************/
void Init_LCD(void)
{
CLR_DisplayRAM();//清除所有顯示寄存器
WRCommand_L(DIS_STARADD);
WRCommand_R(DIS_STARADD);//設置顯示起始地址
WRCommand_L(0x3f);
WRCommand_R(0x3f);//開顯示
}
uchar k; //按鍵值
void Getch (void) //取鍵值函數
{
unsigned char X,Y,Z;
P1=0x0f; //先對P3 置數 行掃描
if(P1!=0x0f) //判斷是否有鍵按下
{
Delay_nms(10); //延時,軟件去干擾
if(P1!=0x0f) //確認按鍵按下
{
X=P1; //保存行掃描時有鍵按下時狀態
P1=0xf0; //列掃描
Y=P1; //保存列掃描時有鍵按下時狀態
Z=X|Y; //取出鍵值
/*********************************************************************/
switch ( Z ) //判斷鍵值(那一個鍵按下)
{
case 0xee: k=0; break; //對鍵值賦值
case 0xed: k=1; break;
case 0xeb: k=2; break;
case 0xe7: k=3; break;
case 0xde: k=4; break;
case 0xdd: k=5; break;
case 0xdb: k=6; break;
case 0xd7: k=7; break;
case 0xbe: k=8; break;
case 0xbd: k=9; break;
case 0xbb: k=10;break;
case 0xb7: k=11;break;
case 0x7e: k=12;break;
case 0x7d: k=13;break;
case 0x7b: k=14;break;
case 0x77: k=15;break;
}
while(P1!=0xf0); //等待按鍵放開
}
}
}
unsigned char TempBuffer[6]; // 顯示數組
unsigned char getdata; //ADC數值
unsigned char kflag;
unsigned int count=0; //計數器
unsigned int xintiao =0; //心跳
void main(void)
{
long int temp2; //臨時變量
uchar a=0,temp1=0,b=0,number=0,pagenum=0;
ST=0;
OE=0;
ET0=1; //打開定時器0中斷允許
ET1=1; //打開定時器1中斷允許
EA=1; //打開總中斷
TMOD=0x12; //選擇定時器工作方式
TH0=216; //裝載初值
TL0=216; //裝載初值
TH1=(65536-5000)/256; //裝載初值
TL1=(65536-5000)%256; //裝載初值
TR1=1; //打開定時器1
TR0=1; //打開定時器0
ST=1;
ST=0;
Init_LCD();
IT0=1;//跳變沿出發方式(下降沿)
EX0=1;//打開INT0的中斷允許
EA=1;//打開總中斷
pagenum=1;//第1頁
E_Display_L(pagenum,0*8,47); //Y
E_Display_L(pagenum,1*8,34); //L
E_Display_L(pagenum,2*8,19); //:
pagenum=3;//第5頁
E_Display_L(pagenum,0*8,46); //X
E_Display_L(pagenum,1*8,42); //T
E_Display_L(pagenum,2*8,19); //:
pagenum=5;//第3頁
E_Display_L(pagenum,0*8,45); //W
E_Display_L(pagenum,1*8,26); //D
E_Display_L(pagenum,2*8,19); //:
BEEP=0;
while(1)
{
if(EOC==1)
{
OE=1;
getdata=P0;
OE=0;
temp2=getdata*196; //ADC數值獲取
if((temp2>=30000)||(xintiao>=150)||(Tem>=30) ) //檢查是否報警
{
BEEP=1;
}
else
{
BEEP=0;
}
TempBuffer[0]=temp2/10000; //轉換成字符
TempBuffer[1]=17; //轉換成字符 小數點
temp2=temp2%10000;
TempBuffer[2]=temp2/1000; //轉換成字符
temp2=temp2%10000;
TempBuffer[3]=temp/1000; //轉換成字符
temp2=temp2%1000;
TempBuffer[4]=temp2/100; //轉換成字符
pagenum=1;//第1頁
for(a=4;a<8;a++)
{
E_Display_L(pagenum,a*8,TempBuffer[a-4]);
}
ST=1;
ST=0;
}
pagenum=3;//第3頁
TempBuffer[0]=xintiao/1000; //轉換成字符
TempBuffer[1]=xintiao%1000/100; //轉換成字符
TempBuffer[2]=xintiao%100/10; //轉換成字符
TempBuffer[3]=xintiao%10; //轉換成字符
for(a=4;a<8;a++)
{
E_Display_L(pagenum,a*8,TempBuffer[a-4]);
}
ShowTem();
pagenum=5;//第5頁
TempBuffer[0]=Tem%1000/100; //轉換成字符
TempBuffer[1]=Tem%100/10; //轉換成字符
TempBuffer[2]=Tem%10; //轉換成字符
for(a=4;a<7;a++)
{
E_Display_L(pagenum,a*8,TempBuffer[a-4]);
}
Delay_nms(50);
}
}
void t0(void) interrupt 1 using 0 //定時器0 中斷服務
{
CLK=~CLK;
}
void t1(void) interrupt 3 using 0 //定時器1 中斷服務
{
static unsigned int timer=0;
TH1=(65536-5000)/256; //重新裝載
TL1=(65536-5000)%256;
timer++ ;
if(timer >=200)
{
xintiao= count;
timer=0;
count=0;
}
}
/*******************************************************************************
* 函 數 名 : Int0() interrupt 0
* 函數功能 : 外部中斷0的中斷函數
* 輸 入 : 無
* 輸 出 : 無
*******************************************************************************/
void Int0() interrupt 0 //外部中斷0的中斷函數
{
count++;
}
/***************
* 函 數:顯示溫度函數
* 參 數:無
* 返 回: 無
****************/
void ShowTem()
{
Tem =gettemp(); /* 讀取18b20溫度*/
}
//**************************延時程序,一個是1us的延時。一個是1ms的延時 **************
void delay_us(uchar a)//when crystal is 12M ,a*2+5 us ,子程序調用要5us,while 就等于DJNZ指令
{
while(--a);
}
//********************1820初始化,讀和寫的子程序,延時
//*******initial**********
void init1820()
{
DQ = 1; _nop_();
DQ = 0; //拉低數據線,準備Reset OneWire Bus;
delay_us(125); //延時510us,Reset One-Wire Bus.
delay_us(125);
DQ = 1; //提升數據線;
delay_us(15); //延時35us;
while(DQ) //等待Slave 器件Ack 信號;
{ _nop_(); }
delay_us(60); //延時125us;
DQ = 1; //提升數據線,準備數據傳輸;
}
//******write********
void write1820(uchar a)
{
uchar i;
for(i=0;i<8;i++)
{if(a & 0x01) //低位在前;
{DQ = 0; //結束Recovery time;
_nop_();_nop_();_nop_();
DQ = 1; } //發送數據;
else
DQ = 0; //結束Rec time;
_nop_();_nop_();_nop_();
//DQ = 0; } //發送數據;
delay_us(30); //等待Slave Device采樣;
DQ = 1; //Recovery;
_nop_(); //Recovery Time Start;
a >>= 1;
}
}
//*******read************
unsigned char read1820(void)
{
unsigned char i;
unsigned char tmp=0;
DQ = 1; _nop_(); //準備讀;
for(i=0;i<8;i++)
{
tmp >>= 1; //低位先發;
DQ = 0; //Read init;
_nop_(); //2ms;
DQ = 1; //必須寫1,否則讀出來的將是不預期的數據;
delay_us(2); //延時9us;
_nop_();
if(DQ) //在12us處讀取數據;
tmp |= 0x80;
delay_us(30); //延時55us;
DQ = 1; _nop_(); //恢復One Wire Bus;
}
return tmp; }
//**********************************************************
uchar gettemp()
{ unsigned int tp;
init1820();
write1820(0xcc);
// delay_ms(2);
write1820(0x44);
// _nop_();
// DQ=1;
// delay_ms(250); //多個1820時要延時,單個就不用,數據手冊里看
// delay_ms(250);
// delay_ms(250);
init1820();
write1820(0xcc);
write1820(0xbe);
show[0]=read1820();
show[1]=read1820();
init1820();
tp=show[1]*256+show[0];
flag = show[1] >> 7; //判斷溫度正負,正時flag = 0;負時flag = 1
if(flag == 0)
{
temp=(tp&0x0f)*10/16;
tp = tp >> 4;
}
if(flag == 1)
{
tp=~(tp-1);
temp=(tp&0x0f)*10/16 + 1;
tp = tp >> 4;
// tp = 256 - tp;
}
return tp;
}
51hei.png (7.38 KB, 下載次數: 56)
下載附件
2020-6-12 15:41 上傳
全部資料51hei下載地址:
壓力心跳溫度報警.rar
(114.81 KB, 下載次數: 36)
2020-6-12 13:12 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|