Timer + button control LED running light mode + timer clock - "51 microcontroller"

Hello fellow CSDN users, today, the content of Xiaoyalan is the timer in the 51 microcontroller and the button control of the LED running light mode & timer clock. Now, let us enter the world of the 51 microcontroller! ! !


timer

Button control LED running light mode

timer clock

source code


timer

 

 

 

 

 

 

 

 

 


Button control LED running light mode

0~65535 The count increases by one every 1 microsecond. The total timing time is 65535 microseconds.

The difference between 64535 and counter overflow is 1000, so the timing time is 1ms.

Contents of Delay.c:

void Delay(unsigned int xms)
{
	unsigned char i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

Contents of Key.c:

#include <REGX52.H>
#include "Delay.h"

/**
  * @brief  获取独立按键键码
  * @param  无
  * @retval 按下按键的键码,范围:0~4,无按键按下时返回值为0
  */
unsigned char Key()
{
	unsigned char KeyNumber=0;
	
	if(P3_1==0)
	{
		Delay(20);
		while(P3_1==0);
		Delay(20);
		KeyNumber=1;
	}
	if(P3_0==0)
	{
		Delay(20);
		while(P3_0==0);
		Delay(20);
		KeyNumber=2;
	}
	if(P3_2==0)
	{
		Delay(20);
		while(P3_2==0);
		Delay(20);
		KeyNumber=3;
	}
	if(P3_3==0)
	{
		Delay(20);
		while(P3_3==0);
		Delay(20);
		KeyNumber=4;
	}
	
	return KeyNumber;
}

Contents of Timer0.c:

#include <REGX52.H>

/**
  * @brief  定时器0初始化,1毫秒@12.000MHz
  * @param  无
  * @retval 无
  */
void Timer0Init(void)
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
	PT0=0;
}

Contents of Delay.h:

#ifndef __DELAY_H__
#define __DELAY_H__

void Delay(unsigned int xms);

#endif

Contents of Key.h:

#ifndef __KEY_H__
#define __KEY_H__

unsigned char Key();

#endif

Contents of Timer0.h:

#ifndef __TIMER0_H__
#define __TIMER0_H__

void Timer0Init(void);

#endif

Contents of main.c:

#include <REGX52.H>

#include "Timer0.h"
#include "Key.h"
#include <INTRINS.H>

unsigned char KeyNum,LEDMode;

void main()
{
	P2=0xFE;
	Timer0Init();
	while(1)
	{
		KeyNum=Key();		//获取独立按键键码
		if(KeyNum)			//如果按键按下
		{
			if(KeyNum==1)	//如果K1按键按下
			{
				LEDMode++;	//模式切换
				if(LEDMode>=2)LEDMode=0;
			}
		}
	}
}

void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	T0Count++;		//T0Count计次,对中断频率进行分频
	if(T0Count>=500)//分频500次,500ms
	{
		T0Count=0;
		if(LEDMode==0)			//模式判断
			P2=_crol_(P2,1);	//LED输出
		if(LEDMode==1)
			P2=_cror_(P2,1);
	}
}


timer clock

Contents of Delay.c:

void Delay(unsigned int xms)
{
	unsigned char i, j;
	while(xms--)
	{
		i = 2;
		j = 239;
		do
		{
			while (--j);
		} while (--i);
	}
}

Contents of Delay.h:

#ifndef __DELAY_H__
#define __DELAY_H__

void Delay(unsigned int xms);

#endif

Contents of LCD1602.c:

#include <REGX52.H>

//引脚配置:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0

//函数定义:
/**
  * @brief  LCD1602延时函数,12MHz调用可延时1ms
  * @param  无
  * @retval 无
  */
void LCD_Delay()
{
	unsigned char i, j;

	i = 2;
	j = 239;
	do
	{
		while (--j);
	} while (--i);
}

/**
  * @brief  LCD1602写命令
  * @param  Command 要写入的命令
  * @retval 无
  */
void LCD_WriteCommand(unsigned char Command)
{
	LCD_RS=0;
	LCD_RW=0;
	LCD_DataPort=Command;
	LCD_EN=1;
	LCD_Delay();
	LCD_EN=0;
	LCD_Delay();
}

/**
  * @brief  LCD1602写数据
  * @param  Data 要写入的数据
  * @retval 无
  */
void LCD_WriteData(unsigned char Data)
{
	LCD_RS=1;
	LCD_RW=0;
	LCD_DataPort=Data;
	LCD_EN=1;
	LCD_Delay();
	LCD_EN=0;
	LCD_Delay();
}

/**
  * @brief  LCD1602设置光标位置
  * @param  Line 行位置,范围:1~2
  * @param  Column 列位置,范围:1~16
  * @retval 无
  */
void LCD_SetCursor(unsigned char Line,unsigned char Column)
{
	if(Line==1)
	{
		LCD_WriteCommand(0x80|(Column-1));
	}
	else if(Line==2)
	{
		LCD_WriteCommand(0x80|(Column-1+0x40));
	}
}

/**
  * @brief  LCD1602初始化函数
  * @param  无
  * @retval 无
  */
void LCD_Init()
{
	LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
	LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
	LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
	LCD_WriteCommand(0x01);//光标复位,清屏
}

/**
  * @brief  在LCD1602指定位置上显示一个字符
  * @param  Line 行位置,范围:1~2
  * @param  Column 列位置,范围:1~16
  * @param  Char 要显示的字符
  * @retval 无
  */
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
{
	LCD_SetCursor(Line,Column);
	LCD_WriteData(Char);
}

/**
  * @brief  在LCD1602指定位置开始显示所给字符串
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  String 要显示的字符串
  * @retval 无
  */
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
{
	unsigned char i;
	LCD_SetCursor(Line,Column);
	for(i=0;String[i]!='\0';i++)
	{
		LCD_WriteData(String[i]);
	}
}

/**
  * @brief  返回值=X的Y次方
  */
int LCD_Pow(int X,int Y)
{
	unsigned char i;
	int Result=1;
	for(i=0;i<Y;i++)
	{
		Result*=X;
	}
	return Result;
}

/**
  * @brief  在LCD1602指定位置开始显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~65535
  * @param  Length 要显示数字的长度,范围:1~5
  * @retval 无
  */
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
	unsigned char i;
	LCD_SetCursor(Line,Column);
	for(i=Length;i>0;i--)
	{
		LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
	}
}

/**
  * @brief  在LCD1602指定位置开始以有符号十进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:-32768~32767
  * @param  Length 要显示数字的长度,范围:1~5
  * @retval 无
  */
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
{
	unsigned char i;
	unsigned int Number1;
	LCD_SetCursor(Line,Column);
	if(Number>=0)
	{
		LCD_WriteData('+');
		Number1=Number;
	}
	else
	{
		LCD_WriteData('-');
		Number1=-Number;
	}
	for(i=Length;i>0;i--)
	{
		LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
	}
}

/**
  * @brief  在LCD1602指定位置开始以十六进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~0xFFFF
  * @param  Length 要显示数字的长度,范围:1~4
  * @retval 无
  */
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
	unsigned char i,SingleNumber;
	LCD_SetCursor(Line,Column);
	for(i=Length;i>0;i--)
	{
		SingleNumber=Number/LCD_Pow(16,i-1)%16;
		if(SingleNumber<10)
		{
			LCD_WriteData(SingleNumber+'0');
		}
		else
		{
			LCD_WriteData(SingleNumber-10+'A');
		}
	}
}

/**
  * @brief  在LCD1602指定位置开始以二进制显示所给数字
  * @param  Line 起始行位置,范围:1~2
  * @param  Column 起始列位置,范围:1~16
  * @param  Number 要显示的数字,范围:0~1111 1111 1111 1111
  * @param  Length 要显示数字的长度,范围:1~16
  * @retval 无
  */
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
{
	unsigned char i;
	LCD_SetCursor(Line,Column);
	for(i=Length;i>0;i--)
	{
		LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
	}
}

Contents of LCD1602.h:

#ifndef __LCD1602_H__
#define __LCD1602_H__

//用户调用函数:
void LCD_Init();
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);

#endif

Contents of Timer0.c:

#include <REGX52.H>

/**
  * @brief  定时器0初始化,1毫秒@12.000MHz
  * @param  无
  * @retval 无
  */
void Timer0Init(void)
{
	TMOD &= 0xF0;		//设置定时器模式
	TMOD |= 0x01;		//设置定时器模式
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	TF0 = 0;		//清除TF0标志
	TR0 = 1;		//定时器0开始计时
	ET0=1;
	EA=1;
	PT0=0;
}

Contents of Timer0.h:

#ifndef __TIMER0_H__
#define __TIMER0_H__

void Timer0Init(void);

#endif

Contents of main.c:

#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "Timer0.h"

unsigned char Sec=55,Min=59,Hour=23;

void main()
{
	LCD_Init();
	Timer0Init();
	
	LCD_ShowString(1,1,"Clock:");	//上电显示静态字符串
	LCD_ShowString(2,1,"  :  :");
	
	while(1)
	{
		LCD_ShowNum(2,1,Hour,2);	//显示时分秒
		LCD_ShowNum(2,4,Min,2);
		LCD_ShowNum(2,7,Sec,2);
	}
}

void Timer0_Routine() interrupt 1
{
	static unsigned int T0Count;
	TL0 = 0x18;		//设置定时初值
	TH0 = 0xFC;		//设置定时初值
	T0Count++;
	if(T0Count>=1000)	//定时器分频,1s
	{
		T0Count=0;
		Sec++;			//1秒到,Sec自增
		if(Sec>=60)
		{
			Sec=0;		//60秒到,Sec清0,Min自增
			Min++;
			if(Min>=60)
			{
				Min=0;	//60分钟到,Min清0,Hour自增
				Hour++;
				if(Hour>=24)
				{
					Hour=0;	//24小时到,Hour清0
				}
			}
		}
	}
}


 source code

timer clock

Contents of main.c:

#include <REGX52.H>
#include "Delay.h"
#include "LCD1602.h"
#include "Timer0.h"

unsigned char Sec=55,Min=59,Hour=23;

void main()
{     LCD_Init();     Timer0Init();     LCD_ShowString(1,1,"Clock:"); //Display static string when power on     LCD_ShowString(2,1," : :");     while(1)     {         LCD_ShowNum(2,1,Hour,2); //Display hours, minutes and seconds         LCD_ShowNum(2,4,Min,2);         LCD_ShowNum(2,7,Sec,2);     } }


    


    






void Timer0_Routine() interrupt 1
{     static unsigned int T0Count;     TL0 = 0x18; //Set the initial timing value     TH0 = 0xFC; //Set the initial timing value     T0Count++;     if(T0Count>=1000) //Timer frequency division, 1s     {         T0Count=0;         Sec++; //When 1 second comes, Sec will increase automatically         if(Sec>=60)         {             Sec=0; //When 60 seconds arrive, Sec will clear to 0, and Min will increase automatically             Min++;             if(Min>=60)             {                 Min=0; //60 minutes is up, Min is cleared to 0, Hour is incremented by                 Hour++;                 if(Hour>=24)                 {                     Hour=0; //24 hours is up, Hour is cleared to 0



















                }
            }
        }
    }
}

 

Contents of Delay.c:

void Delay(unsigned int xms)
{
    unsigned char i, j;
    while(xms--)
    {
        i = 2;
        j = 239;
        do
        {
            while (--j);
        } while (--i);
    }
}

Contents of Timer0.c:

#include <REGX52.H>

/**
  * @brief Timer 0 initialization, 1 millisecond @12.000MHz
  * @param None
  * @retval None
  */
void Timer0Init(void)
{     TMOD &= 0xF0; //Set the timer mode     TMOD |= 0x01; // Set the timer mode     TL0 = 0x18; //Set the initial value of the timer     TH0 = 0xFC; //Set the initial value of the timer     TF0 = 0; //Clear the TF0 flag     TR0 = 1; //Start timer 0     ET0=1;     EA= 1;     PT0=0; }









 

Contents of LCD1602.c:

#include <REGX52.H>

//Pin configuration:
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_EN=P2^7;
#define LCD_DataPort P0

//Function definition:
/**
  * @brief LCD1602 delay function, 12MHz call can delay 1ms
  * @param None
  * @retval None
  */
void LCD_Delay()
{     unsigned char i, j;

    i = 2;
    j = 239;
    do
    {
        while (--j);
    } while (--i);
}

/**
  * @brief LCD1602 write command
  * @param Command Command to be written
  * @retval None
  */
void LCD_WriteCommand(unsigned char Command)
{     LCD_RS=0;     LCD_RW=0;     LCD_DataPort=Command;     LCD_EN=1;     LCD_Delay( );     LCD_EN=0;     LCD_Delay(); }







/**
  * @brief LCD1602 write data
  * @param Data Data to be written
  * @retval None
  */
void LCD_WriteData(unsigned char Data)
{     LCD_RS=1;     LCD_RW=0;     LCD_DataPort=Data;     LCD_EN=1;     LCD_Delay( );     LCD_EN=0;     LCD_Delay(); }







/**
  * @brief LCD1602 sets the cursor position
  * @param Line line position, range: 1~2
  * @param Column column position, range: 1~16
  * @retval None
  */
void LCD_SetCursor(unsigned char Line, unsigned char Column )
{     if(Line==1)     {         LCD_WriteCommand(0x80|(Column-1));     }     else if(Line==2)     {         LCD_WriteCommand(0x80|(Column-1+0x40));     } }








/**
  * @brief LCD1602 initialization function
  * @param None
  * @retval None
  */
void LCD_Init()
{     LCD_WriteCommand(0x38);//Eight-bit data interface, two lines of display, 5*7 dot matrix     LCD_WriteCommand(0x0c); //Display on, cursor off, flashing off     LCD_WriteCommand(0x06);//After data reading and writing operations, the cursor automatically increases by one, and the screen does not move     LCD_WriteCommand(0x01);//The cursor is reset, clear the screen }




/**
  * @brief Display a character at the specified position of LCD1602
  * @param Line row position, range: 1~2
  * @param Column column position, range: 1~16
  * @param Char Character to be displayed
  * @retval None
  */
void LCD_ShowChar(unsigned char Line, unsigned char Column, char Char)
{     LCD_SetCursor(Line,Column);     LCD_WriteData(Char); }


/**
  * @brief Start displaying the given string at the specified position on LCD1602
  * @param Line starting line position, range: 1~2
  * @param Column starting column position, range: 1~16
  * @param String to display String
  * @retval None
  */
void LCD_ShowString(unsigned char Line, unsigned char Column, char *String)
{     unsigned char i;     LCD_SetCursor(Line,Column);     for(i=0;String[i]!='\ 0';i++)     {         LCD_WriteData(String[i]);     } }






    / ** * @brief     return
  value=X to the power of Y         *     /     int
  LCD_Pow
(
int =X;     }     return Result; }







/**
  * @brief Start displaying the given number at the specified position on LCD1602
  * @param Line starting line position, range: 1~2
  * @param Column starting column position, range: 1~16
  * @param Number to be displayed Number, range: 0~65535
  * @param Length The length of the number to be displayed, range: 1~5
  * @retval None
  */
void LCD_ShowNum(unsigned char Line, unsigned char Column, unsigned int Number, unsigned char Length)
{     unsigned char i;     LCD_SetCursor(Line,Column);     for(i=Length;i>0;i--)     {         LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');     } }






/**
  * @brief Display the given number in signed decimal starting from the specified position on LCD1602
  * @param Line starting line position, range: 1~2
  * @param Column starting column position, range: 1~16
  * @param Number The number to be displayed, range: -32768~32767
  * @param Length The length of the number to be displayed, range: 1~5
  * @retval None
  */
void LCD_ShowSignedNum(unsigned char Line, unsigned char Column, int Number, unsigned char Length )
{     unsigned char i;     unsigned int Number1;     LCD_SetCursor(Line,Column);     if(Number>=0)     {         LCD_WriteData('+');         Number1=Number;     }     else     {         LCD_WriteData('-');         Number1=-Number ;     }













    for(i=Length;i>0;i--)
    {
        LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
    }
}

/**
  * @brief Display the given number in hexadecimal starting from the specified position of LCD1602
  * @param Line starting line position, range: 1~2
  * @param Column starting column position, range: 1~16
  * @ param Number The number to be displayed, range: 0~0xFFFF
  * @param Length The length of the number to be displayed, range: 1~4
  * @retval None
  */
void LCD_ShowHexNum(unsigned char Line, unsigned char Column, unsigned int Number, unsigned char Length)
{     unsigned char i,SingleNumber;     LCD_SetCursor(Line,Column);     for(i=Length;i>0;i--)     {         SingleNumber=Number/LCD_Pow(16,i-1)%16;         if(SingleNumber< 10)         {             LCD_WriteData(SingleNumber+'0');         }         else         {











            LCD_WriteData(SingleNumber-10+'A');
        }
    }
}

/**
  * @brief Display the given number in binary starting from the specified position on LCD1602
  * @param Line starting line position, range: 1~2
  * @param Column starting column position, range: 1~16
  * @param Number to be Displayed number, range: 0~1111 1111 1111 1111
  * @param Length The length of the number to be displayed, range: 1~16
  * @retval None
  */
void LCD_ShowBinNum(unsigned char Line, unsigned char Column, unsigned int Number, unsigned char Length)
{     unsigned char i;     LCD_SetCursor(Line,Column);     for(i=Length;i>0;i--)     {         LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');     } }







 

 

Contents of LCD1602.h:

#ifndef __LCD1602_H__
#define __LCD1602_H__

//用户调用函数:
void LCD_Init();
void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);
void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);

#endif

 

Contents of Delay.h:

#ifndef __DELAY_H__
#define __DELAY_H__

void Delay(unsigned int xms);

#endif
 

Contents of Timer0.h:

#ifndef __TIMER0_H__
#define __TIMER0_H__

void Timer0Init(void);

#endif


Okay, that’s all Xiao Yalan’s content for today, keep up the good work! ! !

 

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/133546055
Recommended