首页 > 代码库 > 「ZigBee模块」基础实验(2)按键

「ZigBee模块」基础实验(2)按键

1.实现功能

按键KEY1按下LED1依次亮灭

按键KEY2按下LED2依次亮灭

 

2.元件连接方式

P1_0连接LED1

P1_1连接LED2

P0_0连接KEY1

P0_1连接KEY2

 

3.部分代码分析

按键需要消抖(因为按键选择之后的代码较少,直接写这里面啦)

void keyScan()

{

  if(KEY1 == 0)

  {

    delayms(10);

    if(KEY1 == 0)

    {

      while(!KEY1);

      LED1 = !LED1;

    }

  }

  if(KEY2 == 0)

  {

    delayms(10);

    if(KEY2 == 0)

    {

      while(!KEY2);

      LED2 = !LED2;

    }

  }

}

 

4.完整代码

 

#include <ioCC2530.h>#define uchar unsigned char#define uint  unsigned int#define LED1 P1_0#define LED2 P1_1#define KEY1 P0_0#define KEY2 P0_1void ledInit();void keyInit();void keyScan();void delayms(uint ms);void ledInit(){  P1SEL &= ~0x03;  P1DIR |= 0x03;  P1INP &= ~0x03;    LED1 = 0;  LED2 = 0;}void keyInit(){  P0SEL &= ~0x03;  P0DIR &= ~0x03;  P0INP &= ~0x03;}void keyScan(){  if(KEY1 == 0)  {    delayms(10);    if(KEY1 == 0)    {      while(!KEY1);      LED1 = !LED1;    }  }  if(KEY2 == 0)  {    delayms(10);    if(KEY2 == 0)    {      while(!KEY2);      LED2 = !LED2;    }  }}void delayms(uint ms){  uint i, j;  for(i=ms; i>0; i--)    for(j=578; j>0; j--);}void main(){  ledInit();  keyInit();  while(1)  {    keyScan();  }}

 

 

 

「ZigBee模块」基础实验(2)按键