好文档 - 专业文书写作范文服务资料分享网站

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

天下 分享 时间: 加入收藏 我要投稿 点赞

*/

#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; 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 说明:每次按下K1时递增点亮一只LED,全亮时再次按下则再次循环开始, K2按下后点亮上面4只LED,K3按下后点亮下面4只LED,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++); } //主程序 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); break; case 2: P0=0xf0;break; case 3: P0=0x0f;break; case 4: 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() { uchar i; for(i=0;i<8;i++) { P0=0xff; P0=DSY_CODE[Display_Buffer[i]]; P2=DSY_Index[i]; DelayMS(2); } }

//主程序 void main() { 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; case 0xfd: if(Key_Counts>0)Display_Buffer[--Key_Counts]=10; break; case 0xfb: Display_Buffer[0]=0; for(i=1;i<8;i++) Display_Buffer[i]=10; Key_Counts=0; } //若键未释放则仅刷新显示,不进行键扫描 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; //延时

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; } Key_Code=0xff; } }

16 4X4矩阵键盘控制条形LED显示

/* 名称:4X4矩阵键盘控制条形LED显示 说明:运行本例时,按

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

*/#include#defineucharunsignedchar#defineuintunsignedintsbitLED1=P0^0;sbitLED2=P0^1;sbitLED3=P0^2;sbitLED4=P0^3;sbitK1=P1^0;sbitK2=P1^1;sbitK3=P1^2;sb
推荐度:
点击下载文档文档为doc格式
31jsa094bd1cf865breu5a66i6tmb7010us
领取福利

微信扫码领取福利

微信扫码分享