Fan based on STC51 microcontroller

Publisher:SereneSerenityLatest update time:2024-04-15 Source: elecfansKeywords:STC51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Design requirements:#

Using DC motor as fan


The keyboard can adjust the fan speed


Design Overview:

According to the design requirements, the fan needs to be turned on and off with an independent keyboard, and the speed control needs to use PWM technology. The required single-chip microcomputer chip is STC89C52, and the hardware tool used is the smart car based on STC89C52 developed by Huaqing Yuanjian. The car is equipped with the required independent key module and DC motor module. The independent key module is controlled by the P3 port, and the DC motor module is controlled by the P1 port. STC89C52 is a low-power, high-performance 8-bit microcontroller, which is an enhanced version of the 80C51 single-chip microcomputer, but like the 80C51 single-chip microcomputer, it does not have a PWM hardware module, so we need to write a program to simulate the PWM square wave by software.


PWM is a square wave that can realize digital signal control of analog circuits. It has two important parameters: period or frequency and duty cycle. Duty cycle = high level time/period, with a minimum of 0% and a maximum of 100%. By adjusting the duty cycle, the proportion of high level and low level can be controlled, thereby controlling the speed of the DC motor.


DC motor drive: The motor enable terminal P1.4 is valid at high level. The motor enable terminal must be set to 1 before using the DC motor; the forward and reverse rotation of the motor are determined by the level status of P1.2 and P1.3. When the motor rotates forward, P1.2 is set to 1 and P1.3 is set to 0. When the motor rotates reversely, P1.2 is set to 0 and P1.3 is set to 1.

Independent keyboard driver: The independent keyboard has four keys, s2, s3, s4, and s5, which are controlled by P3.0, P3.1, P3.2, and P3.3 respectively. When the key is pressed, P3 is turned on to a low level. When P3.0 is set to 0, it means the key is pressed, and when it is set to 1, it means the key is released. The same is true for the other three keys.

Fan speed: There are two settings for the fan speed.

source code:#

#include




sbit key_s2 = P3^0; //Fan first speed button


sbit key_s3 = P3^1; //Fan second speed button


sbit key_s4 = P3^2; // Turn off the fan button




sbit EN1 = P1^4; //1, motor enable


sbit IN1 = P1^2; //1 means the motor rotates forward


sbit IN2 = P1^3; //1 means the motor will reverse






/*Fan speed 1*/


void fan_motor1()


{


//Define a variable pwm, and adjust the duty cycle of PWM by accumulating pwm


unsigned int pwm;


while(1)


{


for(pwm = 0;pwm <= 1000;pwm++)


{


if(pwm == 700)


{


EN1 = 1;


IN1 = 1;


IN2 = 0;


}


else if(pwm == 1000)


{


EN1 = 0;


}


}


// Press one of the keys to exit the loop and execute the corresponding code section


if(key_s3 == 0 || key_s4 == 0)


break;


}




}




/*Fan second gear speed function*/


void fan_motor2()


{


EN1 = 1;


IN1 = 1;


IN2 = 0;


}




/*Fan shutdown function*/


void fan_motor_stop()


{


EN1 = 0;


}




/*Delay function*/


void delays(unsigned int ms)


{


//If volatile is not added, the compiler will automatically ignore the for loop without loop body


volatile unsigned int i,j;


for(i=ms;i>0;i--)


{


for(j=110;j>0;j--)


;


}


}




/*Timer interrupt service function*/


void timer0 () interrupt 1


{


if(key_s2 == 0) // Check if the key is pressed


{


delays(10); //delay debounce


if(key_s2 == 0) //Confirm that the key has been pressed


{


fan_motor1(); //Open the first gear


}


}


else if(key_s3 == 0)


{


delays(10);


if(key_s3 == 0)


{


fan_motor2(); //Open second gear


}


while(!key_s3);//Wait for the key to be released


}


else if(key_s4 == 0)


{


delays(10);


if(key_s4 == 0)


{


fan_motor_stop(); //Turn off the fan


}


while(!key_s4);


}


}




void main()


{


TMOD |= 1<<1; //Change the state of the bit through the shift operator "<<"


TMOD &= ~(1<<0); //Set the timer/counter to work in mode 2




TMOD &= ~(1<<2); //Select the timing working mode


TMOD &= ~(1<<3); //Gate bit: start the timer by the run control bit TR




TL0 = 156;


TH0 = 156; //100us enters an interrupt, 0.1 milliseconds




ET0 = 1; //Timer 0 interrupt


EA = 1; //CPU interrupt


TR0 = 1; //Start


while(1) //Prevent the program from running away


;


}

Schematic diagram of some modules of the car: #

image
image


Keywords:STC51 Reference address:Fan based on STC51 microcontroller

Previous article:Counter based on STC51 microcontroller
Next article:C51_MCU Development_Use of XBYTE

Recommended ReadingLatest update time:2024-11-16 09:29

STC51 MCU serial port sending program
#include reg52.h    #define uchar unsigned char    unsigned char rtemp,sflag; unsigned char code Buffer = "Welcome To The MCU World.";//Data to be sent unsigned char *p;   unsigned char TestBuff ; unsigned char mode=0; unsigned char ArrayIndex=0; void SerialInit() //11.0592M crystal, baud rate 19200    {       TMO
[Microcontroller]
How to use STC51 microcontroller EEPROM
STC51 microcontroller has a flash-like function EEPROM, which can save data when power is turned off. Different models can save data of different sizes. Taking 12C5A60S2 as an example, the size of the EEPROM is 2K and is divided into two sectors. It needs to be saved in many places when power is turned off. . First
[Microcontroller]
How to use STC51 microcontroller EEPROM
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号