5.2.1 开关检测案例1
图5-3 开关、LED发光二极管与P1口的连接
5.2.1 开关检测案例1
参考程序如下: #include <reg51.h> #define uchar unsigned char void delay( ) {
uchar i,j; for(i=0; i<255; i++) for(j=0; j<255; j++); }
5.1.2 I/O端口的编程举例
03 用循环左、右移位函数实现
OPTION
使用C51提供的库函数,即循环左移n位函数和循环右
移n位函数,控制发光二极管点亮。参考程序:
#include <reg51.h> #include <intrins.h> 函数的头文件 #define uchar unsigned char void delay( ) {
5.1.2 I/O端口的编程举例
#include <reg51.h> #define uchar unsigned char uchar tab[ ]={ 0xfe , 0xfd , 0xfb , 0xf7 , 0xef , 0xdf , 0xbf , 0x7f , 0x7f , 0xbf , 0xdf , 0xef , 0xf7 , 0xfb , 0xfd , 0xfe }; /*前8个数据为左移点亮 数据,后8个为右移点亮数据*/ void delay( ) {
// P1口为输入 // 读入P1口的状态,送入state // 屏蔽P1口的高6位
5.2.2 开关检测案例2
switch (state) {
// 判P1口低2位开关状态