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));
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
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- LCD Double Buffering Implementation on S3C2410 Bare Metal
- EEWORLD University Hall----Electronic Measurement and Intelligent Instruments (Zhejiang University)
- Controllable non-motor vehicle direction indicator based on LED dot matrix screen
- EDMA3--LINK item in PARAM
- If you don’t understand millimeter wave, ask: the latest application of millimeter wave radar in automobiles
- NXP Rapid IoT Review] +⑥NXP Rapid IoT self-exploration program, review summary and impressions
- RF Circuit PCB Design Processing Techniques
- RSL10-002GEVB Plant Manager Node Design
- Development environment preparation
- Random notes small total small picture signal reflection picture