博客
关于我
试题T-1-16 按键开关控制LED 灯从右至左闪烁
阅读量:689 次
发布时间:2019-03-17

本文共 1454 字,大约阅读时间需要 4 分钟。

#include <ioCC2530.h>

#define uint unsigned int

#define uchar unsigned char

//定义控制灯的端口

#define LED1 P1_0    //定义LED1为P10口控制
#define LED2 P1_5    //定义LED2为P15口控制
#define LED3 P1_3    //定义LED3为P13口控制
#define LED4 P1_4    //定义LED3为P14口控制

void Initial(void);

/****************************

//延时
*****************************/
void Delay(uint n)
{
    uint tt;
    for(tt = 0;tt<n;tt++);
    for(tt = 0;tt<n;tt++);
    for(tt = 0;tt<n;tt++);
    for(tt = 0;tt<n;tt++);
    for(tt = 0;tt<n;tt++);
}

/*****************************************

//读键值
*****************************************/
uchar KeyScan(void)
{
  if(P1_2 == 0)     //低电平有效
  {
    if(P1_2 == 0)
    {
        while(!P1_2); //直到松开按键
        return(1);
    }
  }
  return(0);
}
/****************************
//初始化程序
***************************/
void Initial(void)
  P1SEL &= ~0x3B; //定义GPIO设备
  P1DIR |= 0x3B;  //LED定义为输出
  LED1 = 0;
  LED2 = 0;
  LED3 = 0;
  LED4 = 0;   
}

/***************************

//主函数
***************************/
void main()
{
  uchar LEDFlag,i;
  
  Initial();
  LEDFlag = 0;
  i=0;
  while(1)
  {
      if(KeyScan() == 1)
      {
        if(LEDFlag == 0)
          LEDFlag = 1;
        else
        {
          LEDFlag = 0;
          LED1 = 0;
          LED2 = 0;
          LED3 = 0;
          LED4 = 0; 
        }
      }
      if(LEDFlag)
      {
       switch(i)
       {
         case 0:
            LED1 = 0;
            LED1 = 0;
            LED4 = 0;
            LED3 = 1;
            break;
         case 1:
            LED2 = 0;
            LED1 = 0;
            LED4 = 1;
            LED3 = 0;
            break;
         case 2:
            LED2 = 0;
            LED1 = 1;
            LED4 = 0;
            LED3 = 0;
            break;
         case 3:
            LED2 = 1;
            LED1 = 0;
            LED4 = 0;
            LED3 = 0;
            break;            
        default:
            break;        
       }
      Delay(10000);
      LED1 = 0;
      LED2 = 0;
      LED3 = 0;
      LED4 = 0;
      Delay(10000);       
      if(i<3)
       i++;
      else
       i=0;
    }
  }
}

转载地址:http://omjhz.baihongyu.com/

你可能感兴趣的文章
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>
no such file or directory AndroidManifest.xml
查看>>