
我给你写个参考程序,看看程序符不符合你的要求,程序如下:
#include "REG52H"
// LED灯端口定义
sbit GPIO_Exio1 = P0^0;
sbit GPIO_Exio2 = P0^1;
sbit GPIO_Exio3 = P0^2;
sbit GPIO_Exio4 = P0^3;
// 按键输入端口
sbit KEY1_Exitm = P1^0;
sbit KEY2_Exitm = P1^1;
sbit KEY3_Exitm = P1^2;
sbit KEY4_Exitm = P1^3;
/
延时1ms
/
void Delay_1ms(unsigned int Cnt)
{
unsigned int x, y;
for(x = Cnt; x > 0; x--)
for(y = 120; y > 0; y--);
}
/
按键扫描子函数
/
void KEY_ScanExtio(void)
{
// 判断K1 、K2其中一个按键按下
if((KEY1_Exitm == 0) || (KEY2_Exitm == 0))
{
Delay_1ms(5);
if((KEY1_Exitm == 0) || (KEY2_Exitm == 0))
{
// 按键按下点亮LED
GPIO_Exio1 = 0;
GPIO_Exio2 = 0;
GPIO_Exio3 = 0;
GPIO_Exio4 = 0;
}
while((KEY1_Exitm == 0) || (KEY2_Exitm == 0)); // 按键松手检测
// 按键松手后LED灭
GPIO_Exio1 = 1;
GPIO_Exio2 = 1;
GPIO_Exio3 = 1;
GPIO_Exio4 = 1;
}
// 判断K3 、K4其中一个按键按下
if((KEY3_Exitm == 0) || (KEY4_Exitm == 0))
{
Delay_1ms(5);
if((KEY3_Exitm == 0) || (KEY4_Exitm == 0))
{
}
while((KEY3_Exitm == 0) || (KEY4_Exitm == 0)); // 按键松手检测
// 按键松手后取反LED
GPIO_Exio1 = ~GPIO_Exio1;
GPIO_Exio2 = ~GPIO_Exio2;
GPIO_Exio3 = ~GPIO_Exio3;
GPIO_Exio4 = ~GPIO_Exio4;
}
}
/
主函数
/
int main(void)
{
// 初始化IO
P0 = 0xFF;
P1 = 0xFF;
P2 = 0xFF;
P3 = 0xFF;
while(1)
{
KEY_ScanExtio();
}
}
led应该是一个变量,是代表led灯 亮或者灭的一个变量。 当你按下开关后,s1==0,就执行 led=!led 这条语句。 led=!led 的意思就是取反的意思,即原来led灯亮的话 执行该条语句后就变成灭;原来灭的话 执行该条语句后 就变成亮。按键按下就打控制灯的io取反就可以了。如:if(key1==1){while(key1==1); // 等待按键放开led1=~led1;}
使用四个独立按键控制四个LED灯亮灭的汇编程序怎么弄?
单片机的P10-P13接四个发光二极管L1-L4的阴极,P14-P17接四个开关K1-K4
程序:
ORG 00H
START: MOV A,P1
ANL A,#0F0H
RR A
RR A
ORl A,#0F0H
MOV P1,A
SJMP START
可以检测单个按键,也可以同时检测多个按键
过程:开始--读P1口数据到A--A中的数据右移4次--A中的内容和F0H相或--A中的数据送到P1口--回到开始
#include<reg51h>
#include<INTRINSh>
#define u16 unsigned int
#define u8 unsigned char
#define Pin0 0x01
#define Pin1 0x02
#define Pin2 0x04
#define Pin3 0x08
#define Pin4 0x10
#define Pin5 0x20
#define Pin6 0x40
#define Pin7 0x80
sbit led1=P1^1;
sbit led2=P1^2;
sbit led3=P1^3;
sbit led4=P1^4;
sbit led5=P1^5;
sbit kg1=P3^1;
sbit key2=P3^2;
sbit key3=P3^3;
//
void init() //初始化函数
{ TMOD=0x01;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
}
/////////////////////////
u8 s(){//扫描按键
static u8 ms;
u8 t=0;
// if(key1==0)t|=Pin1;
if(key2==0)t|=Pin2;
if(key3==0)t|=Pin3;
if(t!=0x00){
if(ms<100)ms++;
if(ms==5){
return t;
}
}else ms=0;
return 0x00;
}
///////////////////////
typedef struct{
u16 time;//n毫秒后
u8 l; //亮的灯
u8 m; //灭的灯
}ledcmd;
#define LED_CMD_SIZE 10
code ledcmd lcmd[]={
{0,Pin2,0},//0s后 led2亮
{500,Pin4,0,},//05s后 led4亮
{1000,0,Pin4},//1s后 led4灭
{300,Pin3,0,},
{300,Pin4,0,},
{1000,0,Pin4,},
{300,0,Pin2,},
{200,Pin5,0,},
{40,0,Pin5|Pin3},
{500,0,Pin1},
};
void led_l(u8 v){//亮灯程序
P1=P1&(~v);
}
void led_m(u8 v){//灭灯程序
P1=P1|(v);
}
u8 ledzt=0xff;//led任务zt 为0xff表示待机
u16 ledjs;//倒计时
void led_start(){ //启动程序
ledzt=0;
ledjs=lcmd[ledzt]time;
}
void ledcx(){ //led流程程序
if(ledzt<LED_CMD_SIZE){
if((ledjs==0)&&(ledzt<LED_CMD_SIZE)){//计时到0
ledzt++;
if(ledzt!=LED_CMD_SIZE){
ledjs=lcmd[ledzt]time;
}
}
if(ledzt>1){//
led_l(lcmd[ledzt-1]l);
led_m(lcmd[ledzt-1]m);
}
}
}
//
void main()//主函数
{
u8 k;
init();//系统初始化
TR0=1; //定时器开始计时
while(1){
if(ledzt==0xff){led1=(kg1!=0);};//led不在运行状态时根据开关改变led1
k=s();//读取按键值
if((k&Pin2)!=0){//key2
if(led1==0&&ledzt==0xff){
led_start();
}
}
if((k&Pin3)!=0){//key3
ledzt=0xff;
led_m(Pin2|Pin4);
}
ledcx();
}
}
//
void timer0() interrupt 1//1ms定时器
{
TH0=(65536-1000)/256;//重载初值
TL0=(65536-1000)%256;
if(ledjs>0)ledjs--;
}
#include<reg51h> 包含单片机专用头文件
#include<intrinsh> 这个是什么俺也不知道,但这个程序不需要这个头文件
void Delay10ms(unsigned int c); 自定义的延时函数声明
main() 主函数
{
unsigned int LED; 建立一个数值型变量,变量名 LED
LED=0xfe; 给这个变量赋值,0xfe 换成二进制数为 1111 1110
while(1) 主循环
{
P2=LED; 点亮发光管,即先点亮右边的第一个
Delay10ms(50); 延时一段时间
LED=LED<<1; 点亮的发光管,向左移动一位
if (P2==0x00) 判断是否8个全部点亮了
{
LED=0xff; 如果全部点亮,则将全部发光管熄灭
}
}
}
void Delay10ms(unsigned int c)延时函数主程序
{
unsigned char a, b;建立两个变量 a b
for(;c>0;c--)循环语句,不断改变变量数值,已达到延时目的
{
for(b=38;b>0;b--)
{
for(a=130;a>0;a--);
}
}
}
给你翻译了,希望能帮到你
没学过51就想搞飞思卡尔单片机
,你至少要懂单片机
思路
1
你用蓝牙
你肯定要有蓝牙模块和你的手机配对
2
蓝牙模块通过串口和单片机通信
蓝牙模块的波特率等于你单片机的波特率
3
先确定蓝牙能够发送到pc机
4自己定个编码
5编写飞思卡尔串口程序
6根据编码控制端口
你这个程序死循环了,我一点一点给你纠正:
1 你有个不好的习惯,希望你改正:
定义变量的时候,请明确的指明变量的类型:unsigned char i,或者unsigned int i
而不要是unsigned i,这是不好的习惯。
2 你的这个地方是个死循环:
for(j=7;j>=0;j--)
{
led=(temp>>=1);
yanshi(50);
}
因为你的j定义的是unsigned 类型,所以,j永远都会是非负数,所以,你的for循环会一直进行,修改如下:
for(j=7;j>0;j--)
{
led=(temp>>=1);
yanshi(50);
}
3 看你的while(1)循环里:
while(1) {
if(kaiguan==0) yanshi(20);
if(kaiguan==0)
{
kaideng();
}
if(kaiguan!=1)
{led=0xff;}
}
你这样写,我问你 if(kaiguan==0)和if(kaiguan!=1)有什么区别吗?
以上就是关于求写一个单片机控制LED灯程序全部的内容,包括:求写一个单片机控制LED灯程序、单片机按键控制LED灯亮灭程序怎么做到的、单片机编程,控制LED延时亮灭的程序,等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)