The single chip computer program that controls the motor speed in three ways: button, temperature, and infrared

Publisher:梦想启航Latest update time:2019-12-17 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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

}

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

[1] [2]
Reference address:The single chip computer program that controls the motor speed in three ways: button, temperature, and infrared

Previous article:Traffic light course design based on 51 single chip microcomputer
Next article:51 MCU Assembly Learning Routine (3) - Timer Interrupt

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号