补充一个知识点 这个像不像一位八段数码管会点灯就等等价于会静态数码管显示了。1.流水灯方法一#include REGX52.H //也可以使用REG52.H 缺点是只能对P2整体操作不能操作单独操作一位如P2_0 #include INTRINS.H //这个库里面有_nop_() void Delay1ms(unsigned int time) //11.0592MHz { unsigned char i, j; while(time--){ _nop_(); _nop_(); _nop_(); i 11; j 190; do { while (--j); } while (--i); } } int main(){ while(1) { /*1111 1110是点亮P27~P20中的P20*/ P20xFE;//1111 1110 P20对应原理图的D1第一个灯 Delay1ms(100); P20xFD;//1111 1101 Delay1ms(100); P20xFB;//1111 1011 Delay1ms(100); P20xF7;//1111 0111 Delay1ms(100); P20xEF;//1110 1111 Delay1ms(100); P20xDF;//1101 1111 Delay1ms(100); P20xBF;//1011 1111 Delay1ms(100); P20x7F;//0111 1111 Delay1ms(100); } }方法二使用位运算的方法非逻辑运算增| axxxx xxxx把D0位变成1 a | 10删~ axxxx xx1x把D1位变成0 a ~(11)查 axxxx x1xx查D2位是否为1 a (12) 如果a不为零说明D2位是1#include REGX52.H #include INTRINS.H void Delay1ms(unsigned int time) //11.0592MHz { unsigned char i, j; while(time--){ _nop_(); _nop_(); _nop_(); i 11; j 190; do { while (--j); } while (--i); } } int main(){ unsigned char LedFlg0; while(1) { char count0; for(count0;count8;count){ P2~(LedFlg|(1count)); Delay1ms(100); } } }2.按键控制led灯1.K1长按亮灯松手不亮#include REGX52.H //不可以使用REG52.H 只能对P2整体操作不能操作单独操作一位如P2_0 void main(){ while(1) { if(P3_10) //如果K1按键 P2_00; else P2_01; } }2.K1按下切换亮灭#include REGX52.H #include INTRINS.H void Delay1ms(unsigned int time) //11.0592MHz { unsigned char i, j; while(time--){ _nop_(); _nop_(); _nop_(); i 11; j 190; do { while (--j); } while (--i); } } void main(){ while(1) { if(P3_10){ //如果K1按键 Delay1ms(10);//按下消抖 while(P3_10); Delay1ms(10);//抬起消抖 P2_0~P2_0; } } }3.K1按下led灯右移3.控制八个LED灯实现二进制加法#include REGX52.H #include INTRINS.H void Delay1ms(unsigned int time) //11.0592MHz { unsigned char i, j; while(time--){ _nop_(); _nop_(); _nop_(); i 11; j 190; do { while (--j); } while (--i); } } void main(){ unsigned char num0; while(1) { if(P3_10){ //如果K1按键 Delay1ms(10);//按下消抖 while(P3_10); Delay1ms(10);//抬起消抖 num; P2~num; } } }要按(256)次才能实验按完太累了如果按着不动就可以自动加只需要去掉抬手的代码24和25行。4.思考1.#include REGX52.H #include INTRINS.H void Delay1ms(unsigned int time) //11.0592MHz { unsigned char i, j; while(time--){ _nop_(); _nop_(); _nop_(); i 11; j 190; do { while (--j); } while (--i); } } void main(){ unsigned char num0; while(1) { if(P3_10){ //如果K1按键 Delay1ms(10);//按下消抖 num; }else if(P3_00){ num--; Delay1ms(10); } P2~num; } }上述代码按键同时按下的话是只能加不能减。如何实现同时按下不加也不减。判断的先后顺序很重要驱动库分享整理1——用于单片机中的小巧多功能按键支持库_单片机 state machine 库-CSDN博客2.如果led灯不进行加法而是进行移位需要改哪些。#include REGX52.H #include INTRINS.H void Delay1ms(unsigned int time) //11.0592MHz { unsigned char i, j; while(time--){ _nop_(); _nop_(); _nop_(); i 11; j 190; do { while (--j); } while (--i); } } void main(){ unsigned char count0; P20xFE; while(1) { if(P3_10){ //如果K1按键 Delay1ms(20);//按下消抖 while(P3_10); Delay1ms(20); if(count7) count0;//加之前一定要先检查count的值快超了吗 else count; P2~(0x01count); }else if(P3_00){ Delay1ms(20); while(P3_00); Delay1ms(20); if(count0) count7; else count--; P2~(0x01count); } } }