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

📄 ds1821_1.c

📁 PIC HiTech_C_Mike_Pearces_DS1821_thermostatwith Dallas DS1821 Programmer and schematic. Tested on P
💻 C
📖 第 1 页 / 共 2 页
字号:
//*********** END OF ReadCurrent

//************************************************************************
//                          WriteCurrent
//
// Write Current Temperature settings from the thermo device
//************************************************************************
void WriteCurrent(void)
{

 if(TempH >120) TempH=120;
 if(TempL < (-55) ) TempL= (-55);

 D_Reset();          //-- Reset the Bus
 D_Write(0x01);      //-- Issue Write Temp High Command
 D_Write(TempH);     //-- Write the "High Temp"

 D_Reset();          //-- Reset the Bus
 D_Write(0x02);      //-- Issue Write Temp Low Command
 D_Write(TempL);     //-- Write the "Low Temp"

 D_Reset();          //-- Reset the bus
 D_Write(0x0C);      //-- Write Write Status Command
 D_Write(TempS);     //-- read the status Byte

}
//*********** END OF ReadCurrent


//************************************************************************
//                        DisplayCurrent
//Display current settings of the Buffer
//************************************************************************
void DisplayCurrent(void)
{

 DisplayBuff[0]=0;      //-- Command Byte

 switch(Edit.Area.Hundreds)   //-- Select '0','1' or '-'
 {
  case 0:
  case 1:
   DisplayBuff[2]=Digits[Edit.Area.Hundreds];
   break;
  case T_NEGATIVE:
   DisplayBuff[2]=MINUS;
   break;
  default:
   DisplayBuff[2]=ERROR;
   break;
 }

 DisplayBuff[3]=Digits[Edit.Area.Tens];   //-- The tens digit
 DisplayBuff[4]=Digits[Edit.Area.Ones];   //-- The Ones Digit

 switch(Edit.Area.Type) //-- Current Edit Mode
 {
  case 0:
   DisplayBuff[1]=THIGH;
   break;
  case 1:
   DisplayBuff[1]=TLOW;
   break;
  case 2:
   DisplayBuff[1]=PROGP;
   DisplayBuff[2]=PROGR;
   DisplayBuff[3]=PROGG;
   if(Programmed)
   {
    DisplayBuff[4]=PRGDONE;
   }
   else
   {
    DisplayBuff[4]=MINUS;
   }
   break;
  default:
   DisplayBuff[1]=ERROR;
   break;
 }

 //-- Write the buffer to the LCD module for displaying
 DisplayBuff[0]=0;
 I2C_Send(0x74,DisplayBuff,5);
}
//*********** END OF DisplayCurrent

//************************************************************************
//                          DoUserIO
// read keys and do what is required
//************************************************************************
void DoUserIO(void)
{
 ConvertForDisplay();
 Current=Edit.Data[EditPosition];        //-- Ensure current data loaded
 switch(GetKeyPressed())
 {
  //*********** Minus Pressed **************
  case KEY_MINUS:
   if(Edit.Area.Type==T_PROG)
   {
    //-- Program the DS1821 --
    WriteCurrent();
    Programmed=1;
    break;
   }
   Current--;
   if(Current > 9) Current =9;    //-- Rolls around if drops below zero
   break;

  //*********** Plus Pressed **************
  case KEY_PLUS:
   Current++;
   if(Current > 9) Current =0;   //-- Roll around if Above 9
   Programmed=0;                 //-- reset the programed Flag

   break;

  //*********** Select Pressed **************
  case KEY_SELECT:
   if(Edit.Area.Type==T_PROG)break;        //-- Do Not shift position if Prog
   EditPosition++;
   if(EditPosition > 3) EditPosition=0;
   Current=Edit.Data[EditPosition];        //-- Ensure current data loaded
   break;
 }

 if(EditPosition > 1)
 {
  if(Current > 3) Current=2;    //-- Only 3 selections for positions 2 & 3
  if(Current == 3) Current=0;   //--  '0','1' and '-' or 'L','H','P'
 }
 Edit.Data[EditPosition]=Current;
 ConvertForUpload();
}
//*********** END OF DoUserIO

//************************************************************************
//GetKeyPressed
//************************************************************************
char GetKeyPressed(void)
{
 char pos,done=0;
 pos=4-EditPosition;
 temp=DisplayBuff[pos];
 while(!done)
 {
  DelayMs(250);
  if(Flash)
  {
   DisplayBuff[pos]=LBLANK;
   I2C_Send(0x74,DisplayBuff,5);  //-- Blank the Digit
  }
  if(!SW1)
  {
   done+=1;
  }
  if(!SW2)
  {
   done+=2;
  }
  DelayMs(250);
  if(Flash)
  {
   DisplayBuff[pos]=temp;
   I2C_Send(0x74,DisplayBuff,5); //-- Reshow the digit
  }
  switch(done)
  {
   case 1:
    if(SW1) done=0;            //-- Check if key still Held Down
    break;
   case 2:
    if(SW2) done=0;            //-- Check if key still Held Down
    break;
   case 3:
    if(SW1 | SW2) done=0;      //-- Check if both keys still Held Down
    break;
   default:
    done=0;
    break;
  }
 }
 return(done);
}
//*********** END OF GetKeyPressed

//************************************************************************
//                       ConvertForDisplay
//************************************************************************
void ConvertForDisplay(void)
{
 signed char temp=0;
 //---- Convert values to Editable Digits ----
 switch(Edit.Area.Type)
 {
  case T_HIGH:
   temp=TempH;
   break;

  case T_LOW:
   temp=TempL;
   break;

  default:
   return;
 }
 if(temp <0)
 {
  temp=0-temp;    //-- Convert all values to +ve Digits
  if(temp > 99) temp=99;
  Edit.Area.Hundreds=T_NEGATIVE;
 }
 else
 {
  if(temp >99)
  {
   Edit.Area.Hundreds=(temp/100);
   temp-=(Edit.Area.Hundreds*100);
  }
 }
 if(temp >9)
 {
  Edit.Area.Tens=(temp/10);
  temp-=(Edit.Area.Tens*10);
 }
 Edit.Area.Ones=temp;
 if(Edit.Area.Hundreds > 2)Edit.Area.Hundreds=0;
 if(Edit.Area.Tens > 9)Edit.Area.Tens=9;
 if(Edit.Area.Ones > 9)Edit.Area.Tens=9;
}
//*********** END OF ConvertForDisplay

//************************************************************************
//                      ConvertForUpload
//************************************************************************
void ConvertForUpload(void)
{
 signed char temp;
 //***** Build The actual Value *****
 temp=Edit.Area.Ones + (Edit.Area.Tens*10);

 if(Edit.Area.Hundreds == T_NEGATIVE)
 {
  //-- a -ve Number
  temp=0-temp;   //-- Generate the negative Number
 }
 else
 {
  temp+=(Edit.Area.Hundreds*100);
 }

 switch(Edit.Area.Type)
 {
  case T_HIGH:
   //-- High Value
   TempH=temp;
   break;

  case T_LOW:
   //-- Low Value
   TempL=temp;
   break;
 }
}
//*********** END OF ConvertForUpload

//************************************************************************
//
//************************************************************************


//*********** END OF

//************************************************************************
//
//************************************************************************










⌨️ 快捷键说明

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