51单片机(按键在LED灯上映射的学习)基于51STC15F2K61S2系列(下)

51单片机(按键在LED灯上映射的学习)基于51STC15F2K61S2系列(下)

2.3按键松开后亮灯

#include<stc15.h>
#define tm 10
sbit S7 = P3^0;
unsigned int LED = 0;
void delay_ms(int unsigned ms);
void main()
{
	P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
	while(1)
	{
		if(S7 == 0)
		{
			delay_ms(tm);
			if(S7 == 1)
			delay_ms(tm);
			if(S7 == 1)
			{
				P0 = ~(0X01 << LED);
				LED++;
				if(LED == 8)
					LED =0;
			}
			while(S7);
			delay_ms(tm);
		}
	}
}

void delay_ms(unsigned int ms)
{
	unsigned int i , j;
	for(i = 0; i< ms; i++)
		for(j = 0; j < 853; j++);
}

//方法二:使用标志位的方法
#include<stc15.h>
//#define tm 1000
sbit S7 = P3^0;
unsigned int LED = 0;
unsigned int S7_flag = 0;
void delay_ms(int unsigned ms);
void main()
{
	P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
	while(1)
	{
		if (S7 == 0)
		{
			delay_ms(10);
			if(S7 == 0)
			{
				S7_flag = 1;
			}
			while(!S7);
		}
		if(S7_flag == 1)
		{
			S7_flag = 0;
			P0 = ~(0X01<<LED);
			LED++;
			if(LED == 8)
				LED = 0;
		}
	}
}

void delay_ms(unsigned int ms)
{
	unsigned int i , j;
	for(i = 0; i< ms; i++)
		for(j = 0; j < 853; j++);
}


2.4使用不同的按键可以控制LED使其达到流水灯效果

方法一:(if...else if...else方法)
#include<stc15.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned int S7_flag = 0;
unsigned int LED = 0;

void delay_ms(unsigned int tm);
void main()
{
	P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
	while(1)
	{
		if(S7 == 0)
		{
			delay_ms(10);
			if(S7 == 0)
			{
				S7_flag = 1;
			}
			while(!S7);
		}
		else if(S6 == 0)
		{
			delay_ms(10);
			if(S6 == 0)
			{
				S7_flag = 1;
			}
			while(!S6);
		}
		else if(S5 == 0)
		{
			delay_ms(10);
			if(S5 == 0)
			{
				S7_flag = 1;
			}
			while(!S5);
		}
		else if(S4 == 0)
		{
			delay_ms(10);
			if(S4 == 0)
			{
				S7_flag = 1;
			}
			while(!S4);
		}
		
		if(S7_flag == 1)
		{
			S7_flag = 0;
			P0 = ~(0x01<<LED);
			LED++;
			if(LED == 8)
			LED = 0;
		}
	}
}

void delay_ms(unsigned int tm)
{
	unsigned int i , j;
	for(i = 0; i < tm; i++)
		for(j = 0; j < 853; j++);
}

方法二:封装函数方法
#include<stc15.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned int S7_flag = 0;
unsigned int LED = 0;

void keypad();
void delay_ms(unsigned int tm);
void main()
{
	P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
	while(1)
	{
		keypad();
		if(S7_flag == 1)
		{
			S7_flag = 0;
			P0 = ~(0x01<<LED);
			LED++;
			if(LED == 8)
			LED = 0;
		}
	}
}

void keypad()
{
	if(S7 == 0)
		{
			delay_ms(10);
			if(S7 == 0)
			{
				S7_flag = 1;
			}
			while(!S7);
		}
		else if(S6 == 0)
		{
			delay_ms(10);
			if(S6 == 0)
			{
				S7_flag = 1;
			}
			while(!S6);
		}
		else if(S5 == 0)
		{
			delay_ms(10);
			if(S5 == 0)
			{
				S7_flag = 1;
			}
			while(!S5);
		}
		else if(S4 == 0)
		{
			delay_ms(10);
			if(S4 == 0)
			{
				S7_flag = 1;
			}
			while(!S4);
		}
}
void delay_ms(unsigned int tm)
{
	unsigned int i , j;
	for(i = 0; i < tm; i++)
		for(j = 0; j < 853; j++);
}

2.5使用不同的按键控制LED灯的亮灭

eg;
S7按键按下松开后第一个LED灯亮
S6按键按下松开后第一个LED灯灭
S5按键按下松开后第六个LED灯亮
S4按键按下松开后第六个LED灯灭
#include<stc15.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned int S7_flag = 0 , S6_flag = 0 , S5_flag = 0 , S4_flag = 0;
unsigned int LED = 0;

void keypad();
void delay_ms(unsigned int tm);
void main()
{
	P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
	while(1)
	{
		keypad();
		if(S7_flag == 1)
		{
			P00 = 0;
			S7_flag = 0;
		}
		else if(S6_flag == 1)
		{
			P00 = 1;
			S6_flag = 0;
		}
		else if(S5_flag == 1)
		{
			P05 = 0;
			S5_flag = 0;
		}
		else if(S4_flag == 1)
		{
			P05 = 1;
			S4_flag = 0;
		}
	}
}

void keypad()
{
	if(S7 == 0)
		{
			delay_ms(10);
			if(S7 == 0)
			{
				S7_flag = 1;
			}
			while(!S7);
		}
		else if(S6 == 0)
		{
			delay_ms(10);
			if(S6 == 0)
			{
				S6_flag = 1;
			}
			while(!S6);
		}
		else if(S5 == 0)
		{
			delay_ms(10);
			if(S5 == 0)
			{
				S5_flag = 1;
			}
			while(!S5);
		}
		else if(S4 == 0)
		{
			delay_ms(10);
			if(S4 == 0)
			{
				S4_flag = 1;
			}
			while(!S4);
		}
}
void delay_ms(unsigned int tm)
{
	unsigned int i , j;
	for(i = 0; i < tm; i++)
		for(j = 0; j < 853; j++);
}

2.6使用不同的按键控制不同的LED灯的效果

//使用S7按键控制流水灯的正方向流水灯
//使用S6按键控制流水灯的负方向流水灯
//使用S5按键控制流水灯暂停
//使用S4按键控制流水灯关闭
#include<stc15.h>
sbit S7 = P3^0;
sbit S6 = P3^1;
sbit S5 = P3^2;
sbit S4 = P3^3;
unsigned int S7_flag = 0 , S6_flag = 0 , S5_flag = 0 , S4_flag = 0;
unsigned int LED = 0;
unsigned int flag = 0;

void keypad();
void delay_ms(unsigned int tm);
void main()
{
	P2 = 0XA0; P0 = 0X00; P2 = 0X80; P0 = 0XFF;
	while(1)
	{
		keypad();
		if(S7_flag == 1)
		{
			S7_flag = 0;
			flag = 1;
		}
		if(S6_flag == 1)
		{
			S6_flag = 0;
			flag = 2;
		}
		if(S5_flag == 1)
		{
			S5_flag = 0;
			flag = 0;
		}
		if(S4_flag == 1)
		{
			S4_flag = 0;
			flag = -1;
			P0 = 0XFF;
		}	
		if(flag == 1)
		{
			for(LED = 0 ; LED < 8 ; LED++)
			{
				P0 = ~(0X01<<LED);
				delay_ms(200);
				if(LED == 8)
					LED = 0;
			}
		}
		if(flag == 2)
		{
			for(LED = 0 ; LED < 8 ; LED++)
			{
				P0 = ~(0X80>>LED);
				delay_ms(200);
				if(LED == 8)
					LED = 0;
			}
		}	
	}
	
}

void keypad()
{
	if(S7 == 0)
		{
			delay_ms(10);
			if(S7 == 0)
			{
				S7_flag = 1;
			}
			while(!S7);
		}
		else if(S6 == 0)
		{
			delay_ms(10);
			if(S6 == 0)
			{
				S6_flag = 1;
			}
			while(!S6);
		}
		else if(S5 == 0)
		{
			delay_ms(10);
			if(S5 == 0)
			{
				S5_flag = 1;
			}
			while(!S5);
		}
		else if(S4 == 0)
		{
			delay_ms(10);
			if(S4 == 0)
			{
				S4_flag = 1;
			}
			while(!S4);
		}
}
void delay_ms(unsigned int tm)
{
	unsigned int i , j;
	for(i = 0; i < tm; i++)
		for(j = 0; j < 853; j++);
}
	
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值