单片机C程序。一个按键控制3个led点亮30秒。现在想要让led2和led3由长亮变为交替亮。该怎么改 50
#include<reg52.h>#defineucharunsignedchar#defineuintunsignedintbitsign_a=0,sign_b=0,s...
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
bit sign_a=0,sign_b=0,sign_c=0,sign_d=0;//标志
uint count1=0,count2=0;
sbit LED1 = P2^0;
sbit LED2 = P2^1;
sbit LED3 = P2^2;
sbit key = P3^1;
void keyscan()
{
static uchar count=0;
if(key==0)
{
count++;
if(count>=100)
{
count=100;
if(sign_c==0)
{
sign_a=1;
sign_c=1;
EA = 0;
count2+=600;
EA = 1;
}
}
}
else
{
if(sign_c==1)
{
sign_c=0;
sign_d=1;
}
count=0;
}
}
void main(void) //@12T、12MHz、定时50ms
{
TMOD = 0x01;
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
TR0=1;
EA = 1;
ET0 = 1;
while(1)
{
keyscan();
if(sign_b==1)
LED1 = 0,LED2 = 0,LED3 = 0;
else
LED1 = 1,LED2 = 1,LED3 = 1;
}
}
void time0() interrupt 1
{
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
if(sign_a==1)
{
count1++;
if(count1>=20)
{
count1=0;
sign_a=0;
sign_b=1;
}
}
if((sign_b==1)&(sign_d==1))
{
count2--;
if(count2==0)
{
sign_b=0;
sign_d=0;
}
}
} 展开
#define uchar unsigned char
#define uint unsigned int
bit sign_a=0,sign_b=0,sign_c=0,sign_d=0;//标志
uint count1=0,count2=0;
sbit LED1 = P2^0;
sbit LED2 = P2^1;
sbit LED3 = P2^2;
sbit key = P3^1;
void keyscan()
{
static uchar count=0;
if(key==0)
{
count++;
if(count>=100)
{
count=100;
if(sign_c==0)
{
sign_a=1;
sign_c=1;
EA = 0;
count2+=600;
EA = 1;
}
}
}
else
{
if(sign_c==1)
{
sign_c=0;
sign_d=1;
}
count=0;
}
}
void main(void) //@12T、12MHz、定时50ms
{
TMOD = 0x01;
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
TR0=1;
EA = 1;
ET0 = 1;
while(1)
{
keyscan();
if(sign_b==1)
LED1 = 0,LED2 = 0,LED3 = 0;
else
LED1 = 1,LED2 = 1,LED3 = 1;
}
}
void time0() interrupt 1
{
TH0 =(65536-50000)/256;
TL0 =(65536-50000)%256;
if(sign_a==1)
{
count1++;
if(count1>=20)
{
count1=0;
sign_a=0;
sign_b=1;
}
}
if((sign_b==1)&(sign_d==1))
{
count2--;
if(count2==0)
{
sign_b=0;
sign_d=0;
}
}
} 展开
2个回答
展开全部
#include<reg51.h>
unsigned char k;
sbit K1=P0^0;
sbit K2=P0^1;
sbit LED=P1^1;
unsigned char SZ[]={3,5,7,9,11,13,15}
delay1s();
void main(){
while(1){
if(K1==0){LED=0,TR1=1};
delay1s();
LED=1;
}
}
delay1s(){
while(LED=0){
for(i=0;i<k;i++){
TMOD=0x10;
TH1=0x3c;
TL1=0xb0;
while(!TF1);
TF1=0;}
}
}
思路仅供参考。
unsigned char k;
sbit K1=P0^0;
sbit K2=P0^1;
sbit LED=P1^1;
unsigned char SZ[]={3,5,7,9,11,13,15}
delay1s();
void main(){
while(1){
if(K1==0){LED=0,TR1=1};
delay1s();
LED=1;
}
}
delay1s(){
while(LED=0){
for(i=0;i<k;i++){
TMOD=0x10;
TH1=0x3c;
TL1=0xb0;
while(!TF1);
TF1=0;}
}
}
思路仅供参考。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include #define uchar unsigned char #define uint unsigned int bit sign_a=0,sign_b=0,sign_c=0,sign_d=0;//标志 uint count1=0,count2=0; sbit LED1 = P2^0; sbit LED2 = P2^1; sbit LED3 = P2^2; sbit key = P3^1; void keyscan() { static uchar count=0; if(key==0) { count++; if(count>=100) { count=100; if(sign_c==0) { sign_a=1; sign_c=1; EA = 0; count2+=600; EA = 1; } } } else { if(sign_c==1) { sign_c=0; sign_d=1; } count=0; } } void main(void) //@12T、12MHz、定时50ms { TMOD = 0x01; TH0 =(65536-50000)/256; TL0 =(65536-50000)%256; TR0=1; EA = 1; ET0 = 1; while(1) { keyscan(); if(sign_b==1) LED1 = 0,LED2 = 0,LED3 = 0; else LED1 = 1,LED2 = 1,LED3 = 1; } } void time0() interrupt 1 { TH0 =(65536-50000)/256; TL0 =(65536-50000)%256; if(sign_a==1) { count1++; if(count1>=20) { count1=0; sign_a=0; sign_b=1; } } if((sign_b==1)&(sign_d==1)) { count2--; if(count2==0) { sign_b=0; sign_d=0; } } }
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询