
这个是我自己diy的单片机开发板:http://www.51hei.com/bbs/dpj-42590-1.html
- //--------------------------------下面为板上运行通过的,更为简洁--------------------------------------------------------------
- //这个程序适用于天祥、淘宝上面买的开发板,我自已做的板子也是和它们的脚位一样,好处是立马可以20181031 18:05
- /*按一下灯亮再一下灯灭+松手检测*/
- #include<reg52.h>
- #define uchar unsigned char
- sbit P10=P1^0;
- void delay(int x)
- {
- int a,b;
- for(a=x;a>0;a--)
- for(b=110;b>0;b--);
- }
- void main()
- {
- uchar temp=0;
- P10=0;
- while(1)
- {
- if(T0==0)
- {
- delay(5); //延时去抖
- if(T0==0) //确实被按下了
- {
- temp=!temp;
- P10=temp; //灯的状态取反了
- while(!T0); //松手检测,灯取反后就停在这里了直到松手T0=0后退出循环
- delay(500); //灯持续亮500ms以便我们肉眼观察
- }
- }
- }
- }
-
-
复制代码
- /*此为自已写的,
- 功能:实现按一次按键灯亮,再按一次灯灭
- 欢迎仿问我的空间里头有好多单片机C语言代码
- 2018年10月24于厦门软件园二期
- */
- #include <pic.h> //此为pic的代码
- #define uint unsigned int
- #define uchar unsigned char
- void delay(uint x) //延时函数
- {
- uint a,b;
- for(a=x;a>0;a--)
- for(b=110;b>0;b--);
- }
-
- void main() //主函数
- {
- static bit temp; //定义一个静态的位变量,注意得放在第一行,否则会提示一堆的错误
- TRISB=0;PORTB=0xff;
- TRISD=0;PORTD=0x00;
- temp=RD0; //把RD0 LED灯的当前状态赋给变量temp
- while(1)
- {
- if(RB0==0) //判断按键是否有被按下,有则为0
- {
- delay(10); //去抖
- if(RB0==0) //去抖完得再判断一次
- {
- temp=!temp; //每按一次,位变量temp取反一次
- while(!RB0) //松手检测,按下时为0取反为1,一直循环直至松手为1取反后为0退出循环
- RD0=temp; //把取反后的temp值赋给RD0即LED灯
- }
- }
- }
- }
-
- //-----------------------下面为51的代码----------------------------------------------------
- /*
- #include <reg52.h> //此为51单片机的代码
- #define uint unsigned int
- #define uchar unsigned char
- void delay(uint x) //延时函数
- {
- uint a,b;
- for(a=x;a>0;a--)
- for(b=110;b>0;b--);
- }
-
- void main() //主函数
- {
- static bit temp; //定义一个静态的位变量,注意得放在第一行,否则会提示一堆的错误
- sbit RD0=P1^1; //定义LED灯为RD0
- sbit RB0=p3^2; //定义INT0为RB0(按键)
- temp=RD0; //把RD0 LED灯的当前状态赋给变量temp
- while(1)
- {
- if(RB0==0) //判断按键是否有被按下,有则为0
- {
- delay(10); //去抖
- if(RB0==0) //去抖完得再判断一次
- {
- temp=!temp; //每按一次,位变量temp取反一次
- while(!RB0) //松手检测,按下时为0取反为1,一直循环直至松手为1取反后为0退出循环
- RD0=temp; //把取反后的temp值赋给RD0即LED灯
- }
- }
- }
- }
- */
复制代码
|