基于单片机火灾报警器程序文件Word格式.docx
- 文档编号:18865149
- 上传时间:2023-01-01
- 格式:DOCX
- 页数:21
- 大小:41.24KB
基于单片机火灾报警器程序文件Word格式.docx
《基于单片机火灾报警器程序文件Word格式.docx》由会员分享,可在线阅读,更多相关《基于单片机火灾报警器程序文件Word格式.docx(21页珍藏版)》请在冰豆网上搜索。
uchary1=80;
ucharnum;
voidDelayUs2x(unsignedchart)
{
while(--t);
}
/*
mS延时函数,含有输入参数unsignedchart,无返回值
unsignedchar是定义无符号字符变量,其值的围是
0~255这里使用晶振12M,精确延时请使用汇编
*/
voidDelayMs(unsignedchart)
{while(t--)
//大致延时1mS
DelayUs2x(245);
//按键扫描函数,返回扫描键值
unsignedcharKeyScan(void)
unsignedcharkeyvalue;
if(KeyPort!
=Oxff)
DelayMs(l);
=0xff)
keyvalue=KeyPort;
while(KeyPort!
=0xff);
switch(keyvalue)
//case0xfe:
return1;
break;
//caseOxfd:
return2;
//case0xfb:
return3;
//case0xf7:
return4;
case0xef:
return5;
case0xdf:
return6;
case0xbf:
return7;
case0x7f:
return8;
default:
returnO;
return0;
voidkeyvalue()//键盘功能程序
num=KeyScan();
//循环调用按键扫描
switch(num)
case5:
w1++;
case6:
w1--;
case7:
y1++;
case8:
y1--;
default:
/*烟雾采集程序*/
初始化定时器1
voidInit_TimerO(void)
TMOD=0x01;
TH0=0xff;
/*Initvalue*/
TL0=0x00;
〃PT1=1;
/*优先级
EA=1;
/*interuptenable*/
ET0=1;
/*enabletimerlinterrupt*/
TR0=1;
启动lie总线
voidStart(void)
Sda=1;
_nop_();
Scl=1;
_nop_();
Sda=0;
Scl=0;
停止lie总线
voidStop(void)
应答lie总线
voidAck(void)
_nop_();
非应答IIC总线
voidNoAck(void)
发送一个字节
voidSend(unsignedcharData)
ucharBitCounter=8;
uchartemp;
do
{temp=Data;
Scl=0;
_nop_();
if((temp&
0x80)==0x80)Sda=1;
else
temp=Data<
<
1;
Data=temp;
BitCounter--;
while(BitCounter);
Scl=O;
读入一个字节并返回
unsignedcharRead(void)
uchartemp=0;
uchartemp1=0;
if(Sda)
temp=temp|0x01;
temp=temp&
0xfe;
if(BitCounter-1)
{temp1=temp<
temp=temp1;
BitCounter--;
return(temp);
写入DA数模转换值
voidDAC(unsignedcharData)
Start();
Send(AddWr);
//写入芯片地址
读取AD模数转换的值,有返回值
unsignedcharReadADC(unsignedcharChi){
ucharData;
Ack();
写入芯片地址
Send(0x40|Chl);
〃
写入选择的通道,本程序只用单端输入,
差分部分需要自行
添加
//Chi
的值分别为0、1、2、3,分别代表
1-4通道
Send(AddRd);
读入地址
Data=Read();
Sci=0;
NoAck();
Stop();
读数据
returnData;
返回值
voidyanwuget()//获得烟雾值
if(ADFlag)//定时采集输入模拟量
ADFlag=0;
ADtemp=ReadADC(3);
voiddelay(uintcount)//延时函数
uinti;
while(count)
i=200;
while(i>
0)
i--;
count--;
voidInit_Com(void)//串口初始化,波特率9600,方式1
TMOD=0x20;
PCON=0x00;
SCON=0x50;
TH1=0xFd;
TL1=0xFd;
TR1=1;
PT1=1;
voiddsreset(void)//发送复位和初始化命令
DS=0;
i=103;
0)i--;
DS=1;
i=4;
bittmpreadbit(void)//readabit
bitdat;
i++;
//i++fordelay
dat=DS;
i=8;
return(dat);
uchartmpread(void)//readabytedate
uchari,j,dat;
dat=O;
for(i=1;
i<
=8;
i++)
DAT里
j=tmpreadbit();
dat=(j<
7)|(dat>
1);
//读出的数据最低位在最前面,这样刚好一个字节在
return(dat);
voidtmpwritebyte(uchardat)//向DS18B20写一位
ucharj;
bittestb;
for(j=1;
j<
j++)
testb=dat&
0x01;
dat=dat>
if(testb)//写1
//写0
voidtmpchange(void)//DS18B20beginchange
dsreset();
delay
(1);
tmpwritebyte(0xcc);
//addressalldriversonbus
tmpwritebyte(0x44);
//initiatesasingletemperatureconversion
uinttmp()
//getthetemperature
floattt;
uchara,b;
tmpwritebyte(Oxcc);
tmpwritebyte(Oxbe);
a=tmpread();
b=tmpread();
temp=b;
temp<
//twobytecomposeaintvariable
temp=temp|a;
tt=temp*0.05;
temp=tt*10+0.5;
returntemp;
}voidreadrom()//readtheserial{
ucharsn1,sn2;
tmpwritebyte(0x33);
sn1=tmpread();
sn2=tmpread();
判忙函数
*/bitLCD_Check_Busy(void)
DataPort=OxFF;
RS_CLR;
RW_SET;
EN_CLR;
_nop_();
EN_SET;
return(bit)(DataPort&
0x80);
写入命令函数
---*/
voidLCD_Write_Com(unsignedchar)
while(LCD_Check_Busy());
忙则等待
RW_CLR;
DataPort=;
写入数据函数
voidLCD_Write_Data(unsignedcharData)
RS_SET;
DataPort=Data;
清屏函数
voidLCD_Clear(void)
LCD_Write_Com(0x01);
DelayMs(5);
写入字符串函数
*
voidLCD_Write_String(unsignedcharx,unsignedchary,unsignedchar*s)
if(y==0)
LCD_Write_Com(0x80+x);
//表示第一行
LCD_Write_Com(OxCO+x);
//表示第二行
while(*s)
LCD_Write_Data(*s);
s++;
写入字符函数
voidLCD_Write_Char(unsignedcharx,unsignedchary,unsignedcharData){
LCD_Write_Data(Data);
初始化函数
voidLCD_Init(void)
LCD_Write_Com(0x38);
/*显示模式设置*/
LCD_Write_Com(0x08);
/*
LCD_Write_Com(0x06);
LCD_Write_Com(0x0C);
voidwendudisplay(uinttemp)
uchara[20];
ucharA1,A2,A2t,A3,ser,A4,A5;
ser=temp/10;
SBUF=ser;
A仁temp/100;
A2t=temp%100;
A2=A2t/10;
A3=A2t%10;
A4=w1/10;
A5=w1%10;
A1=A1+'
0'
;
A2=A2+'
A3=A3+'
A4=A4+'
A5=A5+'
a[0]=A1;
a[1]=A2;
a[2]=46;
a[3]=A3;
a[4]='
'
a[5]=67;
a[6]='
a[7]='
a[8]=76;
a[9]=58;
a[10]=A4;
a[11]=A5;
a[12]='
\0'
LCD_Write_String(2,0,a);
voidyanwudisplay(uinttemp)
ucharb[20];
显示关闭*/
显示清屏*/
显示光标移动设置*/
显示开及光标设置*/
//温度显示程序
//浓度显示程序
ucharA1,A2,A2t,A3,A3t,A4,A5,A6,A6t,A7,A7t,A8;
A仁temp/1000;
A2t=temp%1000;
A2=A2t/100;
A3t=A2t%100;
A3=A3t/10;
A4=A3t%10;
A5=y1/1000;
A6t=y1%1000;
A6=A6t/100;
A7t=A6t%100;
A7=A7t/10;
A8=A7t%10;
O'
A6=A6+'
A7=A7+'
A8=A8+'
b[0]=A1;
b[1]=A2;
b[2]=A3;
b[3]=A4;
b[4]='
p'
b[5]='
b[6]='
m'
b[7]='
b[8]=76;
b[9]=58;
b[10]=A5;
b[11]=A6;
b[12]=A7;
b[13]=A8;
b[14]='
LCD_Write_String(2,1,b);
voidbeep(void)//蜂鸣器函数
FM=0;
delay(100);
FM=1;
voidbaojing(void)//报警程序
if(w>
=w1&
&
y>
=y1)
beep();
LED1=0;
LED3=1;
LED2=1;
LED4=1;
y<
y1){
LED1=1;
LED3=0;
if(w<
w1&
=y1){
LED4=0;
y1)
LED2=0;
主函数
voidmain(void)
cmg();
〃关数码管,点阵函数
LCD_Init();
LCD_Clear();
〃清屏
Init_Com();
Init_TimerO();
tmpchange();
II避免初始温度报警
tmp();
yanwuget();
delay(340);
〃避免初始烟雾报警
while
(1)
keyvalue();
w=temp/10;
LCD_Write_String(O,O,"
T:
"
);
wendudisplay(temp);
y=ADtemp;
LCD_Write_String(0,1,"
S:
yanwudisplay(ADtemp);
baojing();
定时器1执行数码管动态扫描
voidTimerO_isr(void)interrupt0using1//{
staticunsignedintj;
TH0=0xfb;
//重新赋值
j++;
if(j==200)
j=0;
ADFIag=1;
}//定时置位AD采样标志位
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 基于 单片机 火灾 报警器 程序 文件