单片机C语言程序设计实训100例—基于8051+Proteus仿真(3)

2019-04-13 23:07

12 K1-K4 按键状态显示

/* 名称:K1-K4 按键状态显示 说明:K1、K2按下时LED点亮,松开时熄灭,

K3、K4按下并释放时LED点亮,再次按下并释放时熄灭;

*/

#include #define uchar unsigned char #define uint unsigned int sbit LED1=P0^0; sbit LED2=P0^1; sbit LED3=P0^2; sbit LED4=P0^3; sbit K1=P1^0; sbit K2=P1^1; sbit K3=P1^2; sbit K4=P1^3; //延时

void DelayMS(uint x) {

uchar i;

while(x--) for(i=0;i<120;i++);

} //主程序 void main() { P0=0xff; P1=0xff; }

11

while(1) { }

LED1=K1; LED2=K2; if(K3==0) { } { }

while(K3==0); LED3=~LED3;

if(K4==0)

while(K4==0); LED4=~LED4;

DelayMS(10);

13 K1-K4 分组控制LED

/* 名称:K1-K4 分组控制LED */

#include #define uchar unsigned char #define uint unsigned int //延时

void DelayMS(uint x) {

uchar i;

while(x--) for(i=0;i<120;i++);

说明:每次按下K1时递增点亮一只LED,全亮时再次按下则再次循环开始, K2按下后点亮上面4只LED,K3按下后点亮下面4只LED,K4按下后关闭所有LED

} //主程序 void main() {

uchar k,t,Key_State; P0=0xff; P1=0xff; while(1) {

t=P1; if(t!=0xff) { DelayMS(10);

if(t!=P1) continue;

//取得4位按键值,由模式XXXX1111(X中有一位为0,其他均为1) //变为模式0000XXXX(X中有一位为1,其他均为0) Key_State=~t>>4; k=0;

//检查1所在位置,累加获取按键号k while(Key_State!=0) {

k++;

Key_State>>=1;

}

//根据按键号k进行4种处理 switch(k) {

case 1:

if(P0==0x00) P0=0xff;

P0<<=1; DelayMS(200);

12

}

}

}

}

case 2: case 3: case 4:

break;

P0=0xf0;break; P0=0x0f;break; P0=0xff;

14 K1-K4 控制数码管移位显示

/* 名称:K1-K4 控制数码管移位显示

说明:按下K1时加1计数并增加显示位,

按下K2时减1计数并减少显示位, 按下K3时清零。

*/

#include #define uchar unsigned char #define uint unsigned int //段码

uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; //位码

uchar code DSY_Index[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; //待显示到各数码管的数字缓冲(开始仅在0位显示0,其他黑屏) uchar Display_Buffer[]={0,10,10,10,10,10,10,10}; //延时

void DelayMS(uint x) {

uchar i;

while(x--) for(i=0;i<120;i++); }

void Show_Count_ON_DSY() {

P0=DSY_CODE[Display_Buffer[i]]; P2=DSY_Index[i]; }

DelayMS(2); uchar i;

for(i=0;i<8;i++) {

P0=0xff;

}

//主程序 void main()

13

{ }

uchar i,Key_NO,Key_Counts=0; P0=0xff; P1=0xff; P2=0x00; while(1) { }

Show_Count_ON_DSY(); P1=0xff;

Key_NO=P1;

//P1口按键状态分别为K1-0xfe,K2-0xfd,K3-0xfb switch(Key_NO) {

case 0xfe:

Key_Counts++;

if(Key_Counts>8) Key_Counts=8;

Display_Buffer[Key_Counts-1]=Key_Counts; break;

if(Key_Counts>0)Display_Buffer[--Key_Counts]=10; break;

Display_Buffer[0]=0;

for(i=1;i<8;i++) Display_Buffer[i]=10; Key_Counts=0;

case 0xfd: case 0xfb:

}

//若键未释放则仅刷新显示,不进行键扫描 while(P1!=0xff) Show_Count_ON_DSY();

15 K1-K4 控制数码管加减演示

/* 名称:K1-K4 控制数码管加减演示 说明:按下K1后加1计数,按下K2后减1计数,按下K3后清零。 */

#include #include

#define uchar unsigned char #define uint unsigned int //段码

uchar code DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff}; //待显示的3位缓冲 uchar Num_Buffer[]={0,0,0}; //按键代码,按键计数 uchar Key_Code,Key_Counts=0; //延时

14

void DelayMS(uint x) { uchar i;

while(x--) for(i=0;i<120;i++);

}

//显示函数

void Show_Counts_ON_DSY() { uchar i,j=0x01;

Num_Buffer[2]=Key_Counts/100; Num_Buffer[1]=Key_Counts/10; Num_Buffer[0]=Key_Counts; for(i=0;i<3;i++) { j=_cror_(j,1);

P0=0xff;

P0=DSY_CODE[Num_Buffer[i]]; P2=j;

DelayMS(1);

}

}

//主程序 void main() { uchar i; P0=0xff; P1=0xff; P2=0x00; Key_Code=0xff;

while(1) { Show_Counts_ON_DSY(); P1=0xff; Key_Code=P1;

//有键按下时,数码管刷新显示30次,该行代码同时起到延时作用 if(Key_Code!=0xff)

for(i=0;i<30;i++) Show_Counts_ON_DSY(); switch(Key_Code) { case 0xfe: if(Key_Counts<255) Key_Counts++; break;

case 0xfd: if(Key_Counts>0) Key_Counts--; break;

case 0xfb:

Key_Counts=0;

}

15


单片机C语言程序设计实训100例—基于8051+Proteus仿真(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:湘教版小学六年级美术下册全册教案

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: