无线传感网——zigbee基础实验-按键控制灯光开关

#include "ioCC2530.h"
#define D3  P1_0
#define D4  P1_1
#define D5  P1_3
#define D6  P1_4
#define SW1 P1_2
#define SW2 P0_1
/*=======================简单的延时函数========================*/
void Delay(unsigned int t)
{
  while(t--);
}
/*======================端口初始化函数========================*/
void Init_Port()
{
  P1SEL &= ~0x1b;     //P1_0、P1_1、P1_3和P1_4作为通用I/O端口
  P1DIR |= 0x1b;      //P1_0、P1_1、P1_3和P1_4端口输出
 
  P1SEL &= ~0x04;     //P1_2作为通用I/O端口
  P1DIR &= ~0x04;     //P1_2端口输入
  P1INP &= ~0x04;     //P1_2设置为上拉/下拉模式
  P2INP &= ~0x40;     //P1_2设置为上拉
 
  P0SEL &= ~0x02;     //P0_1作为通用I/O端口
  P0DIR &= ~0x02;     //P0_1端口输入
  P0INP &= ~0x02;     //P0_1设置为上拉/下拉模式
  P2INP &= ~0x20;     //P0_1设置为上拉
 
  D3 = 0;         
  D4 = 0;         
  D5 = 0;          
  D6 = 0;
}
/*=======================按键扫描函数=========================*/
void Scan_Keys()
{
  if(SW1 == 0)            //发现有SW1按键信号
  {
    Delay(100);           //延时片刻,去抖动处理
    if(SW1 == 0)          //确认为SW1按键信号
    {
      while(SW1 == 0);    //等待按键松开
      D3 = ~D3;           //切换D3灯的开关状态
    }
  }   
 
  if(SW2 == 0)  
  {
    Delay(100);
    if(SW2 == 0)
    {
      while(SW2 == 0);
      D4 = ~D4;
    }
  }   
}
/*=========================主函数============================*/
void main()
{
  Init_Port();         //端口初始化
  while(1)
  {
    Scan_Keys();        //反复扫描按键状态
  }
}
原文地址:https://www.cnblogs.com/breads/p/12727526.html