Realizing temperature sensor and key-press clock adjustment based on 51 single-chip microcomputer

Publisher:知识智慧Latest update time:2020-08-20 Source: 51hei Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This summer, I made a 12864 based 51 single-chip computer to realize DHT11 temperature and humidity sensor and DS1302 to realize alarm clock and temperature and humidity display on the same interface. The clock can be paused and the position of hours, minutes, seconds, years, months and days can be switched through four independent buttons. The clock addition and subtraction can be realized. There are many small bugs in it, which is very difficult to adjust. If you are interested, you can take a look at how to optimize the code. In short, the basic functions can be realized. Because my single-chip computer has multiple matrix buttons damaged, the positions of the matrix buttons are different. Just pay attention to it.
I don’t know so much about the appendix code, you can just download it and take a look.

// Some small bugs are revealed in advance. Some are affected by the K3 button being pressed, but the interrupt is not turned on and the clock is not paused. I don’t know
where the reason is. In short, if you are interested, just take a look. It’s a bit rotten. I hope the big guys can point out the following
 
single-chip source program as follows:

#include "all.h"

#include "DHT11.h"


sbit K1=P3^4;

sbit K2=P3^5;

sbit K3=P3^2; //The upper right corner is this button light, which triggers external interrupt 0

sbit K4=P3^3; //Pin wiring

sbit Beep = P3^1;

unsigned char Temp[8];

char val_flag = 0;

static unsigned char SetState,SetPlace,Place;

static int k = 1, q = 1, e = 1, r = 1, t = 1;

static int w = 1;


void tiaojie();

char Key_Scan();

void hanshuming();

void Int0Configuration();

void Delay10ms();

char Key_Scan();

void SCLKTemp();

void BeepHear();


/*8421 code conversion*/

unsigned char ChangeTime(unsigned char tim)

{

           //tim = Time_buf[7]=0x4A,0x0A,10

unsigned char Time;

if((tim&0x0F)>9) //If the ones digit exceeds 9, then add 1 to the tens digit and clear the ones digit

{

  tim=((tim+0x10)&0xF0); //0x4A+0x10=0x5A&0xF0=0x50

           

//If not, skip this part


           //Timebuf = 0x50

Time = (tim>>4)*10+(tim&0x0f); //Convert 8421 code into decimal number

           //0x50>>4=0x05*10=50,0x50&0x0f=0x00=0; 50 DS1302_Write_Time(void)


return Time;      


}


void DS1302()

{


uchar tishi1[] = "Set alarm";


unsigned char sec_buf = 0x00; //Used to save the last seconds

unsigned char i,j;

  

Init_12684(); //12864 initialization

DS1302_Init(); //DS1302 initialization

DS1302_Write_Time(); //Initialize data


   for(i = 0; i < 8; i++)

  {

     Temp[i] = Time_buf[i];

  }

    while(1)

  {

     static char k ;

     Rec_DHTI0();

   if(0 == val_flag)

   {

    DS1302_Read_Time();

   }

        

       if(SetState==0)

     {

      DS1302_Read_Time();

     }

     

     else

     {

     

       if(K1==0) //Check if button K1 is pressed

     {

      

      Delay10ms(); //eliminate jitter

      if(K1==0)

      {

       SetPlace++;

      

        for(j = 0; j < SetPlace; j++) //1 2 3 4 5 6 7 //According to this result, the address is successfully added to 7

        {

         if(SetPlace>=8) //The button can control the switching of minutes, seconds, year, month, and day. When the button is pressed to 6, the year can be switched. After that, the button is pressed to 7 without any display. Press it again to switch back to the second.

          SetPlace=0;   

         

         if(SetPlace == 6)

         {

           SetPlace = 7;

         }

         

         xianshi();

        }

        j = 0;

      }

      

      while((i<50)&&(K1==0)) //Check if the button is released

      {

       Delay10ms();

       i++;

      }

      i=0;

     }

     

     if(K2==0) //Check if button K2 is pressed

     {

      Delay10ms(); //eliminate jitter

      if(K2==0)

      {

      

        Time_buf[SetPlace]++;

       if((Time_buf[SetPlace]&0x0f)>9) //Convert to BCD code.

       {

        xianshi();

       Time_buf[SetPlace]=Time_buf[SetPlace]+6;

       }

       if((Time_buf[SetPlace]>=0x60)&&(SetPlace<2)) // seconds

       {

        Time_buf[SetPlace]=0;

       }

       if((Time_buf[SetPlace]>=0x60)&&(SetPlace==2)) //minutes

       {

        Time_buf[SetPlace]=0;

       }

       if((Time_buf[SetPlace]>=0x24)&&(SetPlace==3)) // Hours

       {

        Time_buf[SetPlace]=0;

       }

       if((Time_buf[SetPlace]>=0x31)&&(SetPlace==4)) //Day //There is a problem with the date display

       {

        Time_buf[SetPlace]=0;

       }

       if((Time_buf[SetPlace]>=0x13)&&(SetPlace==5)) //Month

       {

        Time_buf[SetPlace]=1;

       }  

  // if(SetPlace==5) //The month can only go to December

  // {

  //TIME[SetPlace]=;

  // }

      }

      

      while((i<50)&&(K2==0)) //Check if the button is released

      {

       Delay10ms();

       i++;

      }

      i=0;

      

     }  

           if(K4==0) //Check if button K2 is pressed

     {

       //xianshi();

      Delay10ms(); //eliminate jitter

      if(K4==0)

      {

      

        Time_buf[SetPlace]--; // Subtraction needs to set the value to 0x59 or something like that when the value reaches 0

       if((Time_buf[SetPlace]&0x0f)>9) //Convert to BCD code.

       {

       // xianshi();

       Time_buf[SetPlace]=Time_buf[SetPlace]-6;

       }

       if((Time_buf[SetPlace]<= 0x00)&&(SetPlace < 2)) //minutes

       {

        Time_buf[SetPlace]=0x59;

       }

      

       if((Time_buf[SetPlace]<= 0x00)&&(SetPlace==2)) //minutes

       {

        Time_buf[SetPlace]=0x59;

       }

       if((Time_buf[SetPlace]<= 0x00)&&(SetPlace==3)) // Hours

       {

        Time_buf[SetPlace] = 0x23;

       }

      

      

       if((Time_buf[SetPlace] <= 0x00)&&(SetPlace==4)) //Day

       {

        Time_buf[SetPlace]=0x30;

       }

       if((Time_buf[SetPlace] <= 0x00)&&(SetPlace==5)) //Month

       {

        Time_buf[SetPlace]=0x12;

       }  

      

  // if(SetPlace==5) //The month can only go to December

  // {

  //TIME[SetPlace]=;

  // }

      }

      

      while((i<50)&&(K4==0)) //Check if the button is released

      {

       Delay10ms();

       i++;

      }

      i=0;

     

     }   

         

         

      

              

   }

   

   

   

  if(Key_Scan() == 8)

{

      

   write_command(0x01);

  

   print(1,0,tishi1);

   SCLKTemp();

    while(Key_Scan() != 9)

   {

      tiaojie();

      SCLKTemp();

      

   }

}


else

{   

  Init_12684();

   LCD_Display();

}

  if(Time_buf[1] == Temp[1] && Time_buf[2] == Temp[2] && Time_buf[3] == Temp[3] ) // alarm buzzer

{

   BeepHear();

   

}


  }

}

void BeepHear()

{

   int i = 0;

for(i = 0; i < 200; i++)

  {

   Beep = 0;

  Delay10ms();

}

Beep = 1;


}

void start()

{

static unsigned char Place = 0;

  uchar i,j;

     if(K1==0) //Check if button K1 is pressed

     {

      

      Delay10ms(); //eliminate jitter

      if(K1==0)

      {

       Place++;

      

        for(j = 0; j < Place; j++) //1 2 3 4 5 6 7 //The result is that the address is successfully added to 7

        {

         if(Place>=4) //The button can control the switching of minutes, seconds, year, month and day. When the button is pressed to 6, the year can be switched. After that, the button is pressed to 7 without any display. Press it again to switch back to the second.

          Place=1;   

         

   

         

         xianshi();

        }

        j = 0;

      }

      

      while((i<50)&&(K1==0)) //Check if the button is released

      {

       Delay10ms();

       i++;

      }

      i=0;

     }

     

     if(K2==0) //Check if button K2 is pressed

     {

      Delay10ms(); //eliminate jitter

      if(K2==0)

      {

      

        Temp[Place]++;

       if((Temp[Place]&0x0f)>9) //Convert to BCD code.

       {

        xianshi();

       Temp[Place]=Temp[Place]+6;

       }

       if((Temp[Place]>=0x60)&&(Place<2)) // seconds

       {

        Temp[Place]=0;

       }

       if((Temp[Place]>=0x60)&&(Place==2)) //minutes

       {

        Temp[Place]=0;

       }

       if((Temp[Place]>=0x24)&&(Place==3)) // Hours

       {

        Temp[Place]=0;

       }

      

  // if(SetPlace==5) //The month can only go to December

  // {

  //TIME[SetPlace]=;

  // }

      }

      

      while((i<50)&&(K2==0)) //Check if the button is released

      {

       Delay10ms();

       i++;

      }

      i=0;

     while(!K2);

     }  


           if(K4==0) //Check if button K2 is pressed

     {

       //xianshi();

      Delay10ms(); //eliminate jitter

      if(K4==0)

      {

      

        Temp[Place]--; // Subtraction needs to set the value to 0x59 or something like that when the value is reduced to 0

       if((Temp[Place]&0x0f)>9) //Convert to BCD code.

       {

       // xianshi();

       Temp[Place]=Temp[Place]-6;

       }

       if((Temp[Place]<= 0x00)&&(Place < 2)) //minutes

       {

        Temp[Place]=0x59;

       }

      

       if((Temp[Place]<= 0x00)&&(Place==2)) //minutes

       {

        Temp[Place]=0x59;

       }

       if((Temp[Place]<= 0x00)&&(Place==3)) // Hours

       {

        Temp[Place] = 0x23;

       }

      

  // if(SetPlace==5) //The month can only go to December

  // {

  //TIME[SetPlace]=;

  // }

      }

      

      while((i<50)&&(K4==0)) //Check if the button is released

      {

       Delay10ms();

       i++;

      }

      i=0;

      while(!K4);

     

     }   

         

         

}

void main()

{




Beep = 1;

Int0Configuration();   

DS1302();


}


void Int0Configuration()

{

//Set INT0 //Set external interrupt 1, mode is falling edge trigger

IT0=1; // Jump edge start mode (falling edge)

EX0=1; //Turn on the interrupt enable of INT0.

EA=1; //Open the general interrupt

}

void Int0() interrupt 0   

{

static val = 0;

  int i = 2;

if(val == 0)

{

  val = 1;

  DS1302_Data_Input(DS1302_SEC_ADD,0x80);

}

else

{

    DS1302_Write_Time();

     DS1302_Data_Input(DS1302_SEC_ADD,0x00);

  val = 0;

}

val_flag = val;


}

void Delay10ms(void) //Error 0us

{

    unsigned char a,b,c;

    for(c=1;c>0;c--)

        for(b=38;b>0;b--)

            for(a=130;a>0;a--);

}

void hanshuming()

{

write_command(0x80);

write_data('2');

write_data('0');

write_data('0'+Time_buf[7]/16); //year

write_data('0'+(Time_buf[7]&0x0f));

write_data('-');

write_data('0'+Time_buf[5]/16); //month

write_data('0'+(Time_buf[5]&0x0f));

[1] [2] [3]
Reference address:Realizing temperature sensor and key-press clock adjustment based on 51 single-chip microcomputer

Previous article:Use MCU PWM to enable L298N to drive TT motor and McRae wheel car drive
Next article:Single chip infrared transmitting and receiving circuit

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号