⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fantest.c

📁 风扇控制程序
💻 C
📖 第 1 页 / 共 2 页
字号:
      AD_TEMP2 = 1;          /* 传感器输入端口2复位 */
      Delay_us(CN_45us);    /* 延时45us */
      AD_TEMP2 = 0;
      Delay_us(CN_525us);   /* 精确延时大于480us,取525us */
      AD_TEMP2 = 1;          /* 拉高端口 */
      Delay_us(CN_30us);
      k=0; /* 滤波计数器. */
      for(j=0;j<10;j++)
      {
         if(!AD_TEMP2)       /* 如果检测结果为0则初始化成功,为1则失败. */
         {
     	    k++;             /* 检测到低电平加1. */
         }
      }
      if(k>3)
          gb_18b20init2_ok = TRUE; /* 复位成功. */
      else
          gb_18b20init2_ok = FALSE;/* 复位失败. 温感开路或温感损坏 */
      Delay_us(CN_200us);
      k=0;
      AD_TEMP2=1;                  /* 输入允许 */
      if(TRUE==gb_18b20init2_ok)
      {
      	  for(j=0;j<10;j++)
          {
             if(AD_TEMP2)          /* 如果检测结果为1则初始化成功,为0则失败. */
             {
     	        k++;                 /* 检测到高电平加1 */
             }
          }
          if(k>3)
             gb_18b20init2_ok = TRUE;    /* 复位成功 */
          else
             gb_18b20init2_ok = FALSE;   /* 复位失败, 温感对地短路 */
      }
  }
}

/***************************************************************************
函数名称: DS18B20_Readchar
函数功能: 从DS18B20读一个字节
输入参数: 温度传感器号
输出参数: 返回读取的字节型值
***************************************************************************/
uchar DS18B20_Readchar(uchar temp_number)
{
  uchar i,d_data;

  d_data=0;
  if(temp_number==CN_TEMP1)
  {
      for (i=8;i>0;i--)
      {  
      	 EA = 0;
         AD_TEMP1 = 0;  /* 给脉冲信号  */
         d_data>>=1;
         AD_TEMP1 = 1;  /* 读允许     */
         if(AD_TEMP1)
         d_data|=0x80;
         EA = 1;
         Delay_us(CN_60us);
      }
      EA = 1;
  }
  if(temp_number==CN_TEMP2)
  {
      for (i=8;i>0;i--)
      {
      	 EA = 0;
         AD_TEMP2 = 0;  /* 给脉冲信号  */
         d_data>>=1;
         AD_TEMP2 = 1;  /* 读允许     */
         if(AD_TEMP2)
         d_data|=0x80;
         EA = 1;
         Delay_us(CN_60us);
      }
      EA = 1;
  }
  Delay_us(CN_60us);
  return(d_data);
}

/***************************************************************************
函数名称: DS18B20_Writechar
函数功能: 向DS18B20写一个字节
输入参数: 要写入的字节型参数, 温度传感器号
输出参数: 无
***************************************************************************/
void DS18B20_Writechar(uchar d_data,uchar temp_number)
{
  uchar i;

  if(temp_number==CN_TEMP1)
  {
     for (i=8;i>0;i--)
     {
     	EA = 0;
        AD_TEMP1 = 0;
        AD_TEMP1 = d_data&0x01;
        Delay_us(CN_45us);
        EA = 1;
        AD_TEMP1 = 1;
        d_data>>=1;
     }
     EA = 1;
  }
  if(temp_number==CN_TEMP2)
  {
     for (i=8;i>0;i--)
     {
     	EA = 0;
        AD_TEMP2 = 0;
        AD_TEMP2 = d_data&0x01;
        Delay_us(CN_45us);
        EA = 1;
        AD_TEMP2 = 1;
        d_data>>=1;
     }
     EA = 1;
  }
  Delay_us(CN_60us);
}

/***************************************************************************
函数名称: DS18B20_Gettemp
函数功能: 从DS18B20读取温度值
输入参数: 温度传感器号
输出参数: 字节型温度值(全局变量)
***************************************************************************/
void DS18B20_Gettemp(uchar temp_number)
{
   uchar temp_low;
   uchar temp_high;
   uchar i,j;
   uint  k;
   uchar temp_buff[5];

   for(i=0;i<5;i++)  /* 进行5次温度检测,以避免检测数据错误. */
   {
      DS18B20_Init(temp_number);
      DS18B20_Writechar(0xCC,temp_number); /* 跳过读序列号操作 */
      DS18B20_Writechar(0xBE,temp_number); /* 读取温度寄存器操作,共可读9个,前两个是温度 */
      temp_low  = DS18B20_Readchar(temp_number);
      temp_high = DS18B20_Readchar(temp_number);
      DS18B20_Init(temp_number);
      k = temp_high;
      k<<=8;
      k = k|temp_low;
      k>>=4;       /* 将精度变为1度 */
      k=k&0x00FF;  /* 消去高8位 */
      temp_buff[i]=k;
   }
   for(i=0;i<5;i++)
   {
      temp_low  = temp_buff[i];
      temp_high = 0;
      for(j=0;j<5;j++)
      {
         if(temp_buff[i]==temp_buff[j])
              temp_high++;       /* 如果发现一次两个温度值相等,则计数器加1 */
      }
      if(temp_high>=4)
      {
          if(temp_buff[i]>=128)  /* 符号位为负, 表示为负温度 */
          {
              temp_buff[i] = temp_buff[i]-255;
              temp_buff[i] = temp_buff[i]-1;
          }
          else
          {
              temp_buff[i] = temp_buff[i];

            
          }   
          if(temp_number==CN_TEMP1 && gb_18b20init1_ok==TRUE)
          {
      	      g_temp_value1 = temp_buff[i];  /* 将温度值写入温度寄存器1 */
          }
      	  if(temp_number==CN_TEMP2 && gb_18b20init2_ok==TRUE)
      	  {
      	      g_temp_value2 = temp_buff[i];  /* 将温度值写入温度寄存器2 */
      	  }
      	  break;
      }
  
   DS18B20_Writechar(0xCC,temp_number); /* 跳过读序列号操作 */
   DS18B20_Writechar(0x44,temp_number); /* 启动温度转换    */
   WDG_CONTR = 0x35;                    /* 喂狗,2秒看门狗定时 */ 
}

 }


 /***************************************************************************
函数名称: TEMP_handle
函数功能: 根据获取的两路温度值进行处理,变为所需的温度值
输入参数: 无
输出参数: 无
***************************************************************************/ 
void TEMP_handle()
{
   Delay_Set(CN_10ms);
   g_temp_getcnt++;
   DS18B20_Gettemp(CN_TEMP1);
   DS18B20_Gettemp(CN_TEMP2);
  if(g_temp_getcnt>=70)
  	{
  	 WDG_CONTR = 0x35;                                   /*喂狗,2秒看门狗定时 */ 
  if ((TRUE == gb_18b20init1_ok) || (TRUE == gb_18b20init1_ok) )
     g_temp_value = (g_temp_value1 + g_temp_value2)/2;   /* 两路温感均正常,取其平均值*/
  if (FALSE == gb_18b20init1_ok)                         /* 如果温感1坏*/
     g_temp_value = g_temp_value2;                       /* 温度取温感2的值*/
  if (FALSE == gb_18b20init2_ok)                         /* 如果温感2坏*/
     g_temp_value = g_temp_value1;                       /* 温度取温感1的值*/
  else g_temp_value = 0;                                 /* 两路温感均坏,取0*/
  if (g_temp_value >=99)
     g_temp_value = 99;                                  /* 如果大于99度,取99*/
  if (g_temp_value <=-9)
     g_temp_value = -9;                                  /* 如果小于-9度,取-9*/
   else 
     g_temp_value = g_temp_value;	
  if (g_temp_value > SUP_temp || g_temp_value < LOW_temp)
      AD_OVTEMP_ALM = 0;  
	  AD_TEMP_LED=0;                               /* 高低温告警输出*/
	}
} 

  /***************************************************************************
函数名称: Display_handle
函数功能: 根据温度值进行处理,并在数码管显示
输入参数: 无
输出参数: 无
***************************************************************************/
void Display_handle()
{
	uchar i,j;
    uchar code data_arry [11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};   /*  定义"0~9,-"*/
    i=g_temp_value%10;
	j=g_temp_value/10;
	 LE1 = 0; LE2=1;                    /* 开个位显示*/
	 P2 = data_arry[i];           /* 显示个位*/
	 Delay_Set(CN_5ms);
	 LE1 = 1;	   LE2 = 0;         /* 开十位显示*/
	 P2 = data_arry[j];           /* 显示十位*/
	 Delay_Set(CN_5ms);
	 LE2 = 1;
}

 /***************************************************************************
函数名称: System_Init
函数功能: 系统初始化
输入参数: 无
输出参数: 无
***************************************************************************/
void System_Init()
{	uchar x;
    P1M0 = 0x01;             /*  P1.0为开漏,其余I/O为传统口*/       
    P1M1 = 0x01;
	g_ADC_fan=0;
	Delay_Set(CN_1000ms);
	AD_FAN_ALM      = 1;            /* 关闭风机告警输出  */
    AD_TEMP_ALM     = 1;            /* 设温感正常;关闭告警输出 */
	AD_TEMP_LED     = 1;            /* 设温感正常;告警灯灭 */
	g_temp1_errcnt  = 0;            /* 温感1故障计数器清0  */
    g_temp2_errcnt  = 0;            /* 温感2故障计数器清0  */
	AD_FAN_CONT1    = CN_FAN_ON;    /* 开启第一组风机*/
 	AD_FAN_CONT2    = CN_FAN_OFF;    /* 开启第二组风机*/	
    	
   for(x=0;x<10;x++)                /* 做10次温度转换,稳定参数.   */
   {
      DS18B20_Gettemp(CN_TEMP1);
      DS18B20_Gettemp(CN_TEMP2);
      Delay_Set(CN_200ms);
   }	
}


void main()
  {
  System_Init();
  while(1)
  {
   	TEMP_handle();
	Display_handle();
   }
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -