51单片机开关简单使用
实验环境
软件: Keil5+Proteus7
元件(主要)
:
AT89C51 * 1,
RESPACK-8 * 1,
7S3G-COM-CATHODE * 1
仿真图
代码实现
/*****程序*****/
#include "reg51.h" //头文件
#include "intrins.h"
#define uint unsigned int //预处理宏定义
#define uchar unsigned char
sbit key = P1^0;
uchar count=0;
//定义表格存放共阴极LED的点阵码
unsigned char code ledTab[]={0x3F,0x06,0x5B,0x4F,0x66,
0x6D,0x7D,0x07,0x7F,0x6F};
/***********************************
函数名称:Delay_Ms
函数功能:延时ms级别
输入参数:要延时的毫秒数
输出参数:无
***********************************/
void Delay_Ms(unsigned int time)
{
unsigned char i;
while(time--)
{
for(i=0;i<120;i++);
}
}
/********************
函数名称:main
函数功能: 主函数
输入参数:无
输出参数:无
*********************/
void main()
{
uchar i=0;
while(1)
{
P0=ledTab[count%10];
if(key==0){
Delay_Ms(150); //延时消抖
if(key==0){
count++;
}
}
}
}