赞
踩
#include <ioCC2530.h> #define LED1 P1_4 #define S5 P0_4 #define S6 P0_5 #define PERIOD 20 #define ON 0 #define OFF 1 #define DOWN 0 #define UP 1 unsigned char pulse_high=20; unsigned char delay_s5_flag=0; unsigned char up_s5=1; unsigned char delay_s6_flag=0; unsigned char up_s6=1; void led_init(void); void key_init(void); void poll_key(void); void delay_ms(int nms); void main(void) { led_init(); key_init(); while(1){ poll_key(); if(pulse_high !=0) { LED1=OFF; delay_ms(pulse_high); } if(PERIOD!=pulse_high) { LED1=ON; delay_ms(PERIOD-pulse_high); } } } void led_init(void) { P1SEL&=~0x10; P1DIR |=0x10; LED1=OFF;//初始状态 } void delay_ms(int nms) { unsigned int i,j; for(i=nms;i>0;i--) for(j=578;j>0;j--); } void key_init(void) { P0SEL &=~0x30; P0DIR &=~0x30; P0INP &=~0x30; P2INP &=~0x20; } void poll_key(void) { //延时消抖 if((DOWN==S5)||(DOWN==S6)) { delay_ms(20); if(DOWN==S5) { if(delay_s5_flag)//松开按键为1 { if(pulse_high&&up_s5) { up_s5=0; pulse_high-=5; } } else { delay_s5_flag=1; } } else if(DOWN==S6) { if(delay_s6_flag) { if((pulse_high<PERIOD)&&(up_s6)) { up_s6=0; pulse_high+=5; } } else { delay_s6_flag=1; } } else { delay_s5_flag=0; up_s5=1; delay_s6_flag=0; up_s6=1; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。