其他分享
首页 > 其他分享> > 按键控制不同频率灯的闪烁

按键控制不同频率灯的闪烁

作者:互联网

题目要求:三个按键:一个menu键,一个加按键,一个减按键,控制LED的闪烁频率。

主函数处理(main.c)

#include"reg52.h"
#include"TIMERINIT.h"
#include"TIMEchange.h"

/*****led的配置*****/
sbit led1=P1^3;
sbit led2=P1^7;

void main(void)
{
  T0Init(10);
  while(1)
  { 
	   ;  
  }
}

void T0Int(void) interrupt 1
{ 
    static unsigned char cnt=0;
	static unsigned char ant=0;
    TH0=VTH0;
    TL0=VTL0;
	cnt++;
	ant++;
	keypros1();
	keypros2();
	keypros3();
    if(cnt>=TIMEX1 && led_change==0)
	{
	  cnt=0;
	  led1=~led1;
	  led2=1;
	}
	if(ant>=TIMEX1 && led_change==1)
	{
	  ant=0;
	  led2=~led2;
		led1=1;
	}	
 } 

定时器配置(time.c)

#include"reg52.h"

#define Fosc (11059200L) 
unsigned char VTH0, VTL0;

void T0Init(unsigned char VT0)
{
	TMOD=(TMOD&0xf0)|0x01;
	VTH0=(65536-VT0*Fosc/12000)/256;
	VTL0=(65536-VT0*Fosc/12000)%256;
	TH0=VTH0;
	TL0=VTL0;
	EA=1;
	ET0=1;
	TR0=1;	 
}
#ifndef __TIMERINIT_H__
#define __TIMERINIT_H__

#define Fosc (11059200L)
extern unsigned char VTH0, VTL0;
extern void T0Init(unsigned char VT0);

#endif

按键配置(keyprosl.c)

#include"reg52.h"
#include"TIMEchange.h"
#define uint  unsigned int
#define uchar unsigned char	

/*****key的配置*****/
sbit MENUKEY=P3^2;
sbit INCKEY=P3^3;
sbit DECKEY=P3^4;

/*****频率参数的配置*****/
uint TIMEX1=20,TIMEX2=40;
/*****按键功能模式的配置*****/
uint mode=0;
bit led_change=0;


void keypros1()
{ 
	if(MENUKEY==0)
	{
		delay(15);
		if(MENUKEY==0)
		{
			mode++;		
			if(mode==1)
			{
				led_change=0;
			}
			if(mode==2)
			{
				led_change=1;
				mode=0;
			}	
		} 
		while(!MENUKEY);	
	}
}


void delay(uint xms)
{
	 uint i,j;
	 for(i=xms;i>0;i--)
	 for(j=110;j>0;j--);	
}



void keypros2()
{
	if(INCKEY==0)
	{
		 delay(15);
		 if(INCKEY==0)
			{
			  TIMEX1+=20;
        TIMEX2+=20;
		    while(!INCKEY);
	    }
  }
}

void keypros3()
{
	if(DECKEY==0)
	{
		delay(15);
		if(DECKEY==0)
		{
		  	TIMEX1-=20;
        TIMEX2-=20;
		while(!DECKEY);
		}
	}
}
#ifndef __TIMEchange_H__
#define __TIMEchange_H__

#define uint unsigned int
#define uchar unsigned char
	
//sbit INCKEY=P3^3;
//sbit DECKEY=P3^4;
extern uint TIMEX1,TIMEX2;
extern bit led_change;
extern uint c;
void delay(uint xms);
void keypros1();
void keypros2();
void keypros3();
 
#endif

****转载请注明出处****

****请大佬指点****

****希望给个三连****

标签:__,void,unsigned,char,uint,频率,按键,闪烁,define
来源: https://blog.csdn.net/qq_46136319/article/details/120687275