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>
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灯的效果
#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++);
}