附录B 单片机烧录程序
/*************************** 火灾模块主程序
*****************************/ #include \ #include \
extern uchar data Dis_play[5]; extern uint temp; uchar yanwu = 50; sbit baojing_CS=P1^0; void main() {
uchar temp_yanwu; do {
temp_yanwu = AD_Convert(); if(yanwu tmpchange(); //温度转换 tem_deal(temp); //显示温度值 display(); } while(1); } /******************************************** A/D,烟雾传感器模块 ****************************************/ #include \#include \#include \ #define Wait1us _nop_(); #define Wait2us {_nop_();_nop_();} #define Wait4us {Wait2us;Wait2us;} #define Wait8us {Wait4us;Wait4us;} #define Wait10us {Wait8us;Wait2us;} #define Wait30us {Wait10us;Wait8us;Wait4us;Wait2us;} #define N 100 /************定义接口总线**************/ sbit Clock = P1 ^ 2; //时钟口线 sbit DataOut = P1 ^ 3; //数据输出口线 sbit ChipSelect = P1 ^ 4; //片选口线 /************接口总线定义结束**********/ /*********函数名称:ADCSelChannel******** *功能:转换指定通道的模拟量为数字量 ****************************************/ unsigned char ADCSelChannel(void) { unsigned char ConvertValue = 0; unsigned char i; ChipSelect = 1; //芯片复位 ChipSelect = 0; ChipSelect = 1; Clock = 0; Wait4us; ChipSelect = 0; //芯片起始 Wait4us; //等待延时 for (i = 0; i < 8; i ++) //输入采样转换时钟 { Clock = 1; Clock = 0; } ChipSelect = 1; //开始转换 Wait10us; //等待转换结束 ChipSelect = 0; //读取转换结果 Wait4us; for (i = 0; i < 8; i ++) // { Clock = 1; ConvertValue <<= 1; if (DataOut) { ConvertValue |= 0x1; } Clock = 0; } ChipSelect = 1; return (ConvertValue); //返回转换结果 } /************ADCSelChannel函数结束**********/ unsigned char TLC549_GetValue(void) { unsigned char ConvertValue; ChipSelect=0; //打开片选 ConvertValue=ADCSelChannel(); //读取转换后的8位AD值 ChipSelect=1; //关闭片选 Wait30us; //等待转换结束 最长17us return ConvertValue; //返回转换结果 } unsigned char TLC549_Filter() //滤波函数 { char count,i,j; unsigned char value_buf[N]; unsigned char temp; int q; int sum=0; for (count=0;count sum += value_buf[count]; return (unsigned char)(sum/(N-2)); }}}} unsigned char AD_Convert(void) { unsigned char AD_Value; AD_Value=TLC549_Filter(); return AD_Value; } /*********************************************** 温度传感器模块 **********************************************/ #include \ #include \ sbit DQ=P3^7; // 温度输入口 extern uint temp; // variable of temperature 定义一个变量 extern uchar flag1; // 定义一个标志,标志温度是负还是正,1为负,0 为正 sbit DIN=P0^7; // 小数点控制 uchar h; // 定义变量 uint temp; // variable of temperature 定义一个变量 uchar flag1; // 定义一个标志,标志温度是负还是正,1为负,0为正 uchar data Dis_play[5]={0x00,0x00,0x00,0x00,0x00};//显示单元数据,共4个数据和一 个运算暂用 uchar data temp_data[2]={0x00,0x00};//读出温度暂放 unsigned char code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0xbf}; //共阳数码管段码表 \ \ \ \ \ \ \ \ \ \灭\ \ unsigned char code table1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};// 带小数点的编码 //共阳数码管带小数点段码表 \ \ \ \ \ \ \ \ \ \ uchar code ditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08, 0x09,0x09}; //小数部分对应十进制 \ \ \ \ \ \ \ \ //\ \ \ \ \ \ \ \ uchar code scan_con[4]={0x01,0x02,0x04,0x08}; //列扫描控制字 /***************************************/ /* 延时子程序 */ /***************************************/ void delay(uint i) //delay { while(i--); } /************************************************/ /* 初始化ds18b2子函数* */ /************************************************/ Init_DS18B20(void) { uchar x=0; DQ = 1; //DQ复位 delay(8); //稍做延时 DQ = 0; //单片机将DQ拉低 delay(80); //精确延时 大于 480us DQ = 1; //拉高总线 delay(14);
相关推荐: