DIY Timer Controller for a Fan Using 51 Microcontroller

DIY Timer Controller for a Fan Using 51 Microcontroller

Q: How can I receive such great articles for free every day?

A: Just click on the above “51 Microcontroller Learning Network” to follow for free!

My bedside fan’s mechanical timer is broken, so I designed a simple timer controller using the 51 microcontroller. The program has been initially debugged on the experimental board, and I’m waiting for the components to arrive to make the board, although it seems a bit wasteful of the microcontroller’s resources.

1. Design Idea:

Initially, power is supplied to the microcontroller manually via a button. Once the power indicator LED4 lights up, the microcontroller runs, and Relay 2 works to supply power to the microcontroller. The working time of Relay 2 can be set via buttons to achieve automatic power-off, and the working time of Relay 1 can be set to achieve intermittent operation of the fan for energy saving.

1. Display Section: Using buttons, the display can switch between the fan’s running time, pause time, and the remaining time until the system shuts down. The digital display shows two digits for “minutes”, while the remaining time until the system shuts down is displayed in “hours”. The LEDs are red, yellow, and blue, respectively indicating the states of the fan running, paused, and the remaining time until the system shuts down.

2. Button Section:

l K1 is the time adjustment shift key. When pressed, the display digits flash, and the corresponding light flashes. It switches between the fan running time, pause time, and system.

l K2 is the increment and state switch display key. When K1 is pressed, it adds 1 to the time. Otherwise, it switches the display between the fan running time, pause time, and system.

l K3 is the decrement and standby key. When K1 is pressed, it adds 1 to the time; otherwise, it has no effect.

3. Relay Section:

l Relay 1 controls the power line of the fan, enabling its operation and pause.

l Relay 2 controls the power of the microcontroller system, enabling the system to shut down on a timer.

2. Schematic Diagram

DIY Timer Controller for a Fan Using 51 Microcontroller

3. Program Listing

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

System Name: Simple Timer Controller for Fan

Creator: w418781840 Date: 2008.7.6

System Functions:

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

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

Function Name: Declaration Area

Function Description:

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

#include

#define uchar unsigned char

#define uint unsigned int

#define SEG P0 // Segment selection for digital tube.

#define DIG P2 // Digit selection for digital tube

uchar dis[2]; // Digital drive group

uchar code TAB[]={ 0xC0,0xF9,0xA4,0xB0,0x99,//0-4 common anode.

0x92,0x82,0xF8,0x80,0x90,0xff};//5-9, off symbol

uchar start,stop,close,sum,time; // Basic variables

uchar fliflag,cflag,zflag,x,y;// Flag bits

uint count_T0,count_T1,c_count; // Counting variables

sbit K1=P3^7; // Function shift key

sbit K2=P3^2; // Increment key and switch shift key.

sbit K3=P3^3; // Decrement.

sbit RLED=P2^2; // Red light, indicates fan running status.

sbit YLED=P2^3; // Yellow light, indicates fan paused status

sbit BLED=P2^4; // Blue light, indicates system status

sbit CLED=P2^6; // Fan control bit

sbit ZLED=P2^7; // System operation control bit

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

Function Name: Delay 1ms Function

Function Description: Display call.

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

void delay1ms(uchar x)

{

uchar i,j;

for(i=0;i for(j=0;j<120;j++);

}

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

Function Name: Display Function

Function Description: Display minutes, two digits.

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

void display(void)

{

uchar i,scan=1;

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

{ SEG=0xff;

DIG=~scan;

SEG=TAB[dis[i]];

delay1ms(5);

scan<<=1;

}

}

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

Function Name: Delay 5ms Function

Function Description: Key debounce call.

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

void delay5ms(uchar x)

{

uchar j;

for(j=0;j display();

}

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

Function Name: Initialization Function

Function Description: Initialize each variable.

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

void init()

{ fliflag=0;// Flashing flag.

cflag=1; // Fan state flag.

zflag=0; // Running flag.

x=30; // Temporary storage

y=10; // Temporary storage

CLED=1;// System operation.

sum=0; // Shift

start=30; // Fan running time

stop=10; // Pause time.

close=5; // Remaining time until system shuts down

count_T0=0; // Related to T0,T1

count_T1=0;

c_count=0;

TMOD=0x11;

TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

TH1=(65536-50000)/256;

TL1=(65536-50000)%256;

EA=1;

ET0=1;

ET1=1;

TR0=1;

}

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

Function Name: Split Function

Function Description: Split display into tens and units

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

void disnner(void)

{ if(cflag==1)//1

{ RLED=0;YLED=1;BLED=1;// Red light flashes.

time=start; // Display running time.

}

if(cflag==2)

{ RLED=1;YLED=0;BLED=1;

time=stop;

}

if(cflag==3)

{ RLED=1;YLED=1;BLED=0;

time=close;

}

dis[0]=time/10;

dis[1]=time%10;

}

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

Function Name: T0 Interrupt Function

Function Description: Generate 1 minute.

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

void timer0(void)interrupt 1

{ TH0=(65536-50000)/256;

TL0=(65536-50000)%256;

if(++count_T0==1200)// one minute.

{ count_T0=0;

if(zflag==0) // is 0

{ ZLED=1; // then rotate.

if(start!=99)// If running time is 99, display 99, do not switch flag. Keep rotating.

{

start–; // If not, countdown.

if(start==0)

{ zflag=1;// Countdown time is up. Switch flag.

cflag=2;

start=x;// Reassign

}

}

}

else

{ ZLED=0; // Otherwise pause.

stop–; // Countdown.

if(stop==0)

{ cflag=1; // Time is up.

zflag=0;

stop=y;

}

}

if(close!=99)// If system time is 99, display 99, system always works..

{ if(++c_count==60) // Count to 60 for one minute, that is, after one hour.

{ c_count=0;

close–; // Do a one-hour countdown.

if(close==0) // Time is up, system powers off.

CLED=0; // Low level is effective.

}

}

}

disnner();// Separate. Send display/

}

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

Function Name: T1 Interrupt Service Function

Function Description: Adjust time for flashing.

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

void timer1(void)interrupt 3

{

TH1=(65536-50000)/256;

TL1=(65536-50000)%256;

if(++count_T1==6)// Flashing period 30MS

{

count_T1=0;

fliflag=~fliflag; // Toggle

if(fliflag==0) // Flag is effective.

{

if(sum==1)cflag=1;// Use state flashing.

if(sum==2)cflag=2;

if(sum==3)cflag=3;

disnner(); // For flashing.

dis[0]=10; // Turn off symbol.

dis[1]=10;

}

else // Otherwise, normal display.

{

RLED=1;YLED=1;BLED=1;

dis[0]=time/10;

dis[1]=time%10;

}

}

}

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

Function Name: Key Scanning Function

Function Description: Adjust time

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

void scanner(void)

{ if(K1==0) // If 0, it indicates a key is pressed.

{

delay5ms(100);// Delay 500MS.

if(K1==0) // Still pressed. Just released.

{

while(K1==0)display();// Wait for release.

delay5ms(2); // Debounce.

cflag=1; // Exit display state 1.

count_T0=0;

TR0=1; // Start T0

TR1=0; // Turn off flashing

sum=0; // Reset.

x=start; // Temporary storage.

y=stop; // Temporary storage.

}

else

{ // Otherwise, a shift key is pressed.

TR0=0; // Stop time movement.

TR1=1; // Turn on flashing.

sum++; // Shift.

if(sum==4)

sum=1;

}

}

if(K2==0)

{

delay5ms(2);

if(K2==0)

{

while(K2==0)display();

delay5ms(2);

if(sum) // If shift

{ if(sum==1)

{

start++;

if(start==61)

start=99;

if(start==100)

start=30;

}

if(sum==2)

{

stop++;

if(stop==61)

stop=5;

}

if(sum==3)

{

close++;

if(close==9)

close=99;

if(close==100)

close=1;

}

}

else // No shift.

{

cflag++; // Then proceed to display state shift.

if(cflag==4)

cflag=1;

}

}

}

if(K3==0)

{

delay5ms(2);

if(K3==0)

{

while(K3==0)display();

delay5ms(2);

if(sum==1)

{ start–;

if(start==29)

start=99;

if(start==98)

start=60;

}

if(sum==2)

{ stop–;

if(stop==4)

stop=60;

}

if(sum==3)

{ close–;

if(close==0)

close=99;

if(close==98)

close=8;

}

}

}

}

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

Function Name: Main Function

Function Description:

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

main()

{

init();

while(1)

{

display();// Display

scanner(); // Detect keys..

}

}

This article is reprinted from the Internet, copyright belongs to the original author. If you find it unsatisfactory, please contact us for deletion!

DIY Timer Controller for a Fan Using 51 Microcontroller
If you think this is a good article, feel free to share it!
Read for free every day
Long press the QR code to follow us!
DIY Timer Controller for a Fan Using 51 Microcontroller

Leave a Comment