The circuit diagram is as follows:
The microcontroller source program is as follows:
#include #include unsigned int times; unsigned int controlway; unsigned int flag = 0; unsigned int flaghongwai=0; //motor control sbit out3=P2^2; sbit out1=P2^0; sbit out2=P2^1; //Button control sbit button=P1^0; sbit buttonup=P1^6; sbit buttondown=P1^7; //temperature control sbit DQ=P3^7; sbit temp=P1^1; //Infrared control sbit hongwai=P1^2; sbit IR = P3^2; unsigned char N[4]; //Identification code, operation code storage unsigned char X1,X2,X3,X4; void delay(unsigned char n){ do{ _nop_(); _nop_(); _nop_(); n--; }while(n); } //temperature control void init_ds18b20(){ unsigned char x=0; DQ=0; delay(120); DQ=1; delay(16); delay(80); } unsigned char readbyte(){ unsigned char i=0; unsigned char date=0; for(i=8;i>0;i--){ DQ=0; delay(1); DQ=1; date>>=1; if(DQ) date|=0x80; delay(11); } return (date); } void writebyte(unsigned char dat){ unsigned char i=0; for(i=8;i>0;i--){ DQ=0; DQ=dat&0x01; delay(5); DQ=1; dat>>=1; delay(5); } } unsigned char retemp(){ unsigned char a,b,tt; unsigned int t; init_ds18b20(); writebyte(0xcc); writebyte(0x44); init_ds18b20(); writebyte(0xcc); writebyte(0xbe); a=readbyte(); b=readbyte(); t=b; //t=t<<8; t<<=8; t=t|a; tt=t*0.0625; return (tt); } //Button detection, motor speed control timer0 () interrupt 1 { unsigned char tempread=0; if(button==0){ controlway=1; }else if(temp==0){ controlway=2; }else if(hongwai==0){ controlway=3; }else{ controlway=1; } switch(controlway){ case 1: if(buttonup==0){ if(times<10000){ times+=1000; } }else if(buttondown==0){ if(times>0){ times-=1000; } } delay(100); break; case 2: flag++; if(flag==20) { tempread=retemp(); if(tempread>24&&tempread<34){ times=1000*(tempread-24); }else{ times=10000; } flag=0; } break; case 3: //Convert to external interrupt IT0 = 0; EX0 = 1; break; } TH0=0x3c; TL0=0xb0; } //Infrared receiving signal /********************** External interrupt function****************************/ void exint0() interrupt 0 { unsigned int cnt; unsigned char i; EX0 = 0; cnt = 0; while(!IR) cnt++; //Record boot code time if(cnt < 1000){EX0=1;return;} //9ms count value (12MHz: 1000< cnt <1500) cnt = 0; while(IR) if(cnt++ > 400){EX0=1;return;} // Anti-stuck, timeout protection (12MHz: > 300) if(cnt < 200){EX0=1;return;} //(12MHz without frequency division: <260) for(i=0; i<32; i++) //Read 32-bit code { cnt = 0; while(!IR); while(IR) if(cnt++ > 200){EX0=1;return;} //Timeout protection (12MHz:>=200) N[i/8] >>= 1; if(cnt>60) N[i/8] |= 0x80; //Counting boundary between 0 and 1 (12MHz: < 109) } if(N[0] == ~N[1] && N[2] == ~N[3]) //Verify identification code, operation code { /* X1 = N[0]/16; X2 = N[0]%16; X3 = N[2]/16; */ X4 = N[2]%16; flaghongwai=1; } EX0 = 1; } main(){ TMOD = 0x01; TH0=0x3c; TL0=0xb0; //TH0 = (65536-50000)/256; //TL0 = (65536-50000)%256; EA=1; ET0=1; //TR0=1; X1=X2=X3=X4 = 0; times=0; delay(1000); while(1){ TR0=1; //Convert to timer if(flaghongwai==1) { TR0=0; if(X4==1&×<10000){ times=times+1000; }else if(X4==2&×>100){ times=times-1000; } X4 = 0; flaghongwai=0; } out2=1; out1=0; out3=0; delay(times); out1=1; out3=1; delay(10000-times); } } #include #include #define uchar unsigned char #define uint unsigned int sbit sendup=P1^0; sbit senddown=P1^1; #define SBM 0x80 //Identification code #define m9 (65536-9000) //about 9mS #define m4_5 (65536-4500) //about 4.5mS #define m1_6 (65536-1630) //about 1.65mS #define m_65 (65536-580) //about 0.65mS #define m_56 (65536-560) //about 0.56mS #define m40 (65536-40000) //about 40mS #define m56 (65536-56000) //56mS #define m2_25 (65536-2250) //about 2.25mS sbit IR = P3^6; //Define the emission pin (connected to the base of the PNP transistor) uchar KEY(void); void SanZhuan(void); void ZZ(uchar x); void Z0(uchar temp); void TT0(bit BT,uint x); void YS(uchar time); void delay(unsigned char n){ do{ _nop_(); _nop_(); _nop_(); n--; }while(n); } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ Function: Main function ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ void main(void) { TMOD = 0x01; //T0 16-bit working mode IR=1; //The transmitting port is normally high level while(1) { SanZhuan(); } } uchar KEY(void) { P1 = 0xff; //Set the keyboard port row value to 1 and the column value to 0 if(sendup==0){ return 1; }else if(senddown==0){ return 2; } return 0; } /******************************************************************* Function: Scatter program Entry parameter: v // // 13 14 15 16 // 9 10 11 12 // 5 6 7 8 // 1 2 3 4 *******************************************************************/ void SanZhuan(void) { uchar v; v = KEY(); switch(v) { case 1:ZZ(0x01);v=0;break; //" " case 2:ZZ(0x02);v=0;break; //" " default:v=0; } } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ Function: Send the main program ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ void ZZ(uchar x) { TT0(1,m9); //High level 9mS TT0(0,m4_5); //Low level 4.5mS /*┈ Send 4 frames of data┈*/ Z0(SBM); Z0(~SBM); Z0(x); Z0(~x); /*┈┈ End code┈┈*/ TT0(1,m_65); TT0(0,m40); /*┈┈ Repeat code┈┈*/ while(KEY()) { TT0(1,m9); TT0(0,m2_25); TT0(1,m_56); TT0(0,m40); TT0(0,m56); } } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ Function: Single frame sending program Entry parameters: 1 frame of data ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ void Z0(uchar temp) { uchar v; for (v=0;v<8;v++) //Loop 8 times shift { TT0(1,m_65); //High level 0.65mS if(temp&0x01) TT0(0,m1_6); //Send the lowest bit else TT0(0,m_56); temp >>= 1; //Shift right one bit } } /*┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈ Function: 38KHz pulse transmission + delay program Entry parameters: (whether to emit pulses, delay of about x (uS)) ┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈┈*/ void TT0(bit BT,uint x) { TH0 = x>>8; // Input T0 initial value TL0 = x; TF0=0; //clear to 0 TR0=1; //Start timer 0 if(BT == 0) while(!TF0); //When BT=0, 38KHz pulse is not emitted and only delay is performed; when BT=1, 38KHz pulse is emitted and delayed; else while(1) //38KHz pulse, duty cycle 5:26 { IR = 0; if(TF0)break; if(TF0)break; IR = 1; if(TF0)break; if(TF0)break; if(TF0)break; if(TF0)break; if(TF0)break; if(TF0)break; if(TF0)break; if(TF0)break; if(TF0)break; if(TF0)break; } TR0=0; //turn off timer 0 TF0=0; // flag overflow is cleared to 0 IR =1; //After the pulse stops, the transmitting port is normally at a high level } /*******************************************************************
Previous article:Traffic light course design based on 51 single chip microcomputer
Next article:51 MCU Assembly Learning Routine (3) - Timer Interrupt
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Predictions for the development trend of the Internet of Things in 2021
- cc2530 zigbee-zha modification supports serial port 1 position 1 P0_4 P0_5
- 【AT-START-F425 Review】 4. External interrupts and code analysis
- Steps for inner layer processing of printed circuit boards
- Who has the metal dome PCB package?
- Circuit design based on DCDC chip voltage doubling to achieve high voltage and low current
- Read the good book "Electronic Engineer Self-study Manual" + experience one
- EEWORLD University ---- Learn myRIO with me
- Can 868.3MHZ, used as a remote control, go through walls?
- How are diodes classified?