C51源代码--用按键控制led走马灯实例

  • Post author:
  • Post category:其他


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

*

* Description : 4 key control led flow

*     key1 — start

*    key2 — stop

*    key3 — up->down

*    key4 — down->up

*

* Author : liyangth@gmail.com

*

* Date : 2006-12-3

*

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

#include “reg51.h”

#include “INTRINS.H”

typedef unsigned int uint_t;

typedef unsigned char uchar_t;

uchar_t key(void);

void mydelay(uint_t delay_time);

void main()

{

uchar_t key_v;

bit start = 0;

bit updown = 0;

uchar_t out_data = 0xfe;

for(;;){

key_v = key();

switch(key_v){

case 0xfb:{

start = 1;

break;

}

case 0xf7:{

start = 0;    /*stop*/

break;

}

case 0xef:{

updown = 1;

break;

}

case 0xdf:{

updown = 0;

break;

}

}

if(start){

//P1 = 0xf0;

if(updown)

out_data = _crol_(out_data,1);

else

out_data = _cror_(out_data,1);

P1 = out_data;

}

else

P1 = 0xff;    /* all down */

mydelay(1000);

}

}

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

*

* Function: uchar_t key(void)

*

* Description: key event

*

* Returns: key_v

*

* Note: respond key value

*

* Add: liy 2006-12-3

*

* Modify:

*

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

uchar_t key(void)

{

uchar_t key_v;

uchar_t tmp;

P3 = P3|0x3c;

key_v = P3;

if(0xff == (key_v|0xc3))

return (0);

mydelay(10);

key_v = P3;

if(0xff == (key_v|0xc3))

return (0);

else{

for(;;){

tmp = P3;

if(0xff == (tmp|0xc3))

break;

}

return (key_v);

}

}

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

*

* Function: void mydelay(uint_t delay_time)

*

* Description: delay delay_time ms

*

* Param:

*

* Returns: void

*

* Note: delay;

*

* add: liyangth@mail.com 12-3

*

* modify:

*

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

void mydelay(uint_t delay_time)

{

uint_t i;

while(delay_time–){

for(i = 0; i < 125; i++)

;

}

}



版权声明:本文为freedom1013原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。