个人收集整理仅供参考学习
单片机C语言程序设计实训100例 ——基于8051+Proteus仿真
01闪烁地LED
/*名称:闪烁地LED
说明:LED按设定地时间间隔闪烁*/
#include<reg51.h>
#defineuchar unsigned char
#defineuint unsigned int
sbitLED=P10;
//延时
voidDelayMS(uint x)
{
uchari;
while(x--)
{
for(i=0;i<120;i++);
}
}
void main() { //主程序
LED=~LED; DelayMS(150); while(1)
{
}
}
02从左到右地流水灯
/*名称:从左到右地流水灯
说明:接在P0 口地8个LED
从左到右循环依次点亮,产生走
马灯效果*/
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//延时
voidDelayMS(uint x)
{
uchar i; while(x--)
{
1/ 26
个人收集整理仅供参考学习
for(i=0;i<120;i++);
}
}
//主程序
voidmain()
{
P0=0xfe;
while(1)
{
P0=_crol_(P0,1);//P0地值向左循环移动
DelayMS(150);
}
}
038只LED左右来回点亮
/*名称:8只LED左右来回点亮
说明:程序利用循环移位函数_crol_和_cror_形成来回滚动地效果*/#include<reg51.h>
#include<intrins.h>
#defineuchar unsigned char
//延时 void DelayMS(uint x) #define uint unsigned int
{
while(x--) { for(i=0;i<120;i++); uchar i;
}
}
//主程序
voidmain()
{
uchari;
P2=0x01;
while(1)
{
for(i=0;i<7;i++)
{
P2=_crol_(P2,1);//P2地值向左循环移动
DelayMS(150);
}
for(i=0;i<7;i++)
{
P2=_cror_(P2,1); //P2 地值向右循环移动
2/ 26
个人收集整理仅供参考学习
DelayMS(150);
}
}
}
04花样流水灯
/*名称:花样流水灯
说明:16只LED分两组
按预设地多种花样变换显示*/
#include<reg51.h>
#defineuchar unsigned char
#defineuint unsigned int
ucharcode Pattern_P0[]=
{
0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,b5E2RGbCAP0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,p1EanqFDPw
0xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,DXDiTa9E3d0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,RTCrpUDGiT0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,5PCzVD7HxA0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,jLBHrnAILg
xHAQX74J0X
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
{ LDAYtRyKfE
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf9,0xf3,0xe7,0xcf,0x9f,0x3f,0xff,Zzz6ZB2Ltk0xe7,0xdb,0xbd,0x7e,0xbd,0xdb,0xe7,0xff,0xe7,0xc3,0x81,0x00,0x81,0xc3,0xe7,0xff,dvzfvkwMI10xaa,0x55,0x18,0xff,0xf0,0x0f,0x00,0xff,0xf8,0xf1,0xe3,0xc7,0x8f,0x1f,0x3f,0x7f,rqyn14ZNXI0x7f,0x3f,0x1f,0x8f,0xc7,0xe3,0xf1,0xf8,0xff,0x00,0x00,0xff,0xff,0x0f,0xf0,0xff,EmxvxOtOco0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,SixE2yXPq50x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,6ewMyirQFL0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,kavU42VRUs0x00,0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,y6v3ALoS
0x00,0xff,0x00,0xff,0x00,0xff,0x00,0xff
};
//延时
voidDelayMS(uint x)
{
uchar i;
while(x--)
{
for(i=0;i<120;i++);
3/ 26
个人收集整理仅供参考学习
}
}
//主程序
voidmain()
{
uchari;
while(1)
{//从数组中读取数据送至P0和P2口显示
for(i=0;i<136;i++)
{
P0=Pattern_P0[i];
P2=Pattern_P2[i];
DelayMS(100);
}
}
}
05LED模拟交通灯
/*名称:LED模拟交通灯
灯闪烁5 次后红灯亮,红灯亮后,南北向由红灯变为绿灯,若干秒后南北向黄灯闪烁5此后变红灯,东西向变绿灯,如此重复.*/ M2ub6vSTnP
说明:东西向绿灯亮若干秒,黄
sbit YELLOW_A=P01;
sbit GREEN_A=P02;
sbitRED_B=P03;//南北向灯
sbitYELLOW_B=P04;
sbit GREEN_B=P05;
ucharFlash_Count=0,Operation_Type=1; //闪烁次数,操作类型变量0YujCfmUCw
//延时
voidDelayMS(uint x)
{
uchar i;
while(x--)for(i=0;i<120;i++);
}
//交通灯切换
void Traffic_Light()
{
switch(Operation_Type)
{ |
|
4 / 26
个人收集整理仅供参考学习
RED_A=1;YELLOW_A=1;GREEN_A=0;
RED_B=0;YELLOW_B=1;GREEN_B=1;
DelayMS(2000);
Operation_Type=2;
break;
case2: //东西向黄灯闪烁,绿灯关闭
DelayMS(300);
YELLOW_A=~YELLOW_A;GREEN_A=1;
if(++Flash_Count!=10)return; //闪烁5次
Flash_Count=0;
Operation_Type=3;
break;
case3: //东西向红灯,南北向绿灯亮
RED_A=0;YELLOW_A=1;GREEN_A=1;
RED_B=1;YELLOW_B=1;GREEN_B=0;
DelayMS(2000);
Operation_Type=4;
break;
case4: //南北向黄灯闪烁5次
DelayMS(300);
if(++Flash_Count!=10) return; Flash_Count=0; YELLOW_B=~YELLOW_B;GREEN_B=1;
} } //主程序 void main()
{
while(1) Traffic_Light();
}
06单只数码管循环显示0~9
/* 名称:单只数码管循环显示0~9
说明:主程序中地循环语句反复将0~9地段码送至P0 口,使数字0~9循环显示*/ #include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};eUts8ZQVRd
//延时
voidDelayMS(uint x)
5/ 26
个人收集整理仅供参考学习
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
voidmain()
{
uchari=0;
P0=0x00;
while(1)
{
P0=~DSY_CODE[i];
i=(i+1)%10;
DelayMS(300);
}
}
078只数码管滚动显示单个数字
/*名称:8只数码管滚动显示单个数字
说明:数码管从左到右依次滚动显示0~7,程序通过每次仅循环选通一只数码管*/
#include<intrins.h> #define uchar unsigned char #include<reg51.h>
//延时
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
voidmain()
{
uchari,wei=0x80;
while(1)
{
for(i=0;i<8;i++)
{
P2=0xff;//关闭显示
wei=_crol_(wei,1);
P0=DSY_CODE[i];//发送数字段码 P2=wei;//发送位码
DelayMS(300);
}
6/ 26
个人收集整理仅供参考学习
}
}
8只数码管动态显示多个不同字符(电路如上图)
/*名称:8只数码管动态显示多个不同字符
说明:数码管动态扫描显示0~7.*/
#include<reg51.h>
#include<intrins.h>
#defineuchar unsigned char
#defineuint unsigned int
ucharcode DSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};GMsIasNXkA//延时
voidDelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
voidmain()
uchar i,wei=0x80; while(1) {
{ P0=0xff; {
for(i=0;i<8;i++)
P0=DSY_CODE[i];//发送段码
wei=_crol_(wei,1);
P2=wei;//发送位码
DelayMS(2);
}
}
}
098只数码管闪烁显示数字串(电路如上图)
/* 名称:8 只数码管闪烁显示数字串
说明:数码管闪烁显示由0~7构成地一串数字
本例用动态刷新法显示一串数字,在停止刷新时所有数字显示消失.*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
//段码表
uchar codeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};TIrRGchYzg//位码表
ucharcode DSY_IDX[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; 7EqZcWLZNX
7/ 26
个人收集整理仅供参考学习
//延时
voidDelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//主程序
voidmain()
{
uchari,j;
while(1)
{
for(i=0;i<30;i++)
{
for(j=0;j<8;j++)
{
P0=0xff;
P0=DSY_CODE[j];//发送段码
P2=DSY_IDX[j];//发送位码
DelayMS(2);
} P2=0x00;//关闭所有数码管并延时}
}
}
108 只数码管滚动显示数字串(电路如上图) DelayMS(1000);
/*名称:8只数码管滚动显示数字串
说明:数码管向左滚动显示3个字符构成地数字串*/
#include<reg51.h>
#include<intrins.h>
#defineuchar unsigned char
#defineuint unsigned int
//段码表
ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};lzq7IGf02E
//下面数组看作环形队列,显示从某个数开始地8个数(10表示黑屏)
ucharNum[]={10,10,10,10,10,10,10,10,2,9,8};
//延时
voidDelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<120;t++);
}
8 / 26
个人收集整理仅供参考学习
//主程序
voidmain()
{
uchari,j,k=0,m=0x80;
while(1)
{//刷新若干次,保持一段时间地稳定显示
for(i=0;i<15;i++)
{
for(j=0;j<8;j++)
{//发送段码,采用环形取法,从第k个开始取第j个
P0=0xff;
P0=DSY_CODE[Num[(k+j)%11]];
m=_crol_(m,1);
P2=m;//发送位码
DelayMS(2);
}
}
k=(k+1)%11;//环形队列首支针k递增,Num下标范围0~10,故对11取余}
}
11
/* 名称:K1-K4 控制LED移位 K1-K4 控制LED移位
#include<reg51.h>
#include<intrins.h>
#defineuchar unsigned char
#defineuint unsigned int
//延时
voidDelayMS(uint x)
{
uchari;
while(x--)
for(i=0;i<120;i++);
}
//根据P1口地按键移动LED
voidMove_LED()
{
if((P1&0x10)==0) P0=_cror_(P0,1);//K1
else if((P1&0x20)==0) P0=_crol_(P0,1); //K2 else if((P1&0x40)==0) P2=_cror_(P2,1);//K3 else if((P1&0x80)==0) P2=_crol_(P2,1); //K4
9 / 26
个人收集整理仅供参考学习
}
//主程序
voidmain()
{
ucharRecent_Key; //最近按键
P0=0xfe;
P2=0xfe;
P1=0xff;
Recent_Key=0xff;
while(1)
{
if(Recent_Key!=P1)
{
Recent_Key=P1;//保存最近按键
Move_LED();
DelayMS(10);
}
}
}
12K1-K4按键状态显示
/*名称:K1-K4 按键状态显示说明:K1、K2 按下时LED点亮,松开时熄灭,
sbit LED1=P00;
sbitLED2=P01;
sbitLED3=P02;
sbitLED4=P03;
sbitK1=P10;
sbitK2=P11;
sbitK3=P12;
sbitK4=P13;
//延时
voidDelayMS(uint x)
{
uchari;
while(x--)for(i=0;i<120;i++);
}
//主程序 |
|
10/ 26
个人收集整理仅供参考学习
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);
}
}
13K1-K4分组控制LED
说明:每次按下K1时递增点亮一只LED,全亮时再次按下则再次循环开始,K2 按下后点亮上面4只LED,K3按下后点亮下面4 只LED,K4按下后关闭所有LED*/
/*名称:K1-K4 分组控制LED
//延时 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;
11/ 26
个人收集整理仅供参考学习
//取得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)
{
case1: if(P0==0x00) P0=0xff;
P0<<=1;
DelayMS(200);
break;
case2: P0=0xf0;break;
case3: P0=0x0f;break;
case4: P0=0xff;
} } }
}
按下K2时减1计数并减少显示位,K3时清零.*/
#include<reg51.h>
#defineuchar unsigned char
#defineuint unsigned int
//段码
ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};zvpgeqJ1hk
//位码
ucharcode DSY_Index[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};NrpoJac3v1//待显示到各数码管地数字缓冲(开始仅在0位显示0,其他黑屏)
ucharDisplay_Buffer[]={0,10,10,10,10,10,10,10};
//延时
voidDelayMS(uint x)
{
uchar i;
while(x--) for(i=0;i<120;i++); }
void Show_Count_ON_DSY()
12/ 26
个人收集整理仅供参考学习
{
uchari;
for(i=0;i<8;i++)
{
P0=0xff;
P0=DSY_CODE[Display_Buffer[i]];
P2=DSY_Index[i];
DelayMS(2);
}
}
//主程序
voidmain()
{
uchari,Key_NO,Key_Counts=0;
P0=0xff;
P1=0xff;
P2=0x00;
while(1)
{
Show_Count_ON_DSY();
Key_NO=P1; //P1 口按键状态分别为K1-0xfe,K2-0xfd,K3-0xfbP1=0xff;
Display_Buffer[Key_Counts-1]=Key_Counts;
break;
case0xfd: if(Key_Counts>0)Display_Buffer[--Key_Counts]=10; 1nowfTG4KI
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();
} | } K1-K4 控制数码管加减演示 |
/*名称:K1-K4控制数码管加减演示
说明:按下K1 后加1 计数,按下K2后减1 计数,按下K3 后清零. */
#include<reg51.h>
13 / 26
个人收集整理仅供参考学习
#include<intrins.h>
#defineuchar unsigned char
#defineuint unsigned int
//段码
ucharcodeDSY_CODE[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};fjnFLDa5Zo
//待显示地3位缓冲
ucharNum_Buffer[]={0,0,0};
//按键代码,按键计数
ucharKey_Code,Key_Counts=0;
//延时
voidDelayMS(uint x)
{
uchari;
while(x--)for(i=0;i<120;i++);
}
//显示函数
voidShow_Counts_ON_DSY()
{
uchari,j=0x01;
Num_Buffer[1]=Key_Counts/10%10; Num_Buffer[0]=Key_Counts%10;Num_Buffer[2]=Key_Counts/100;
P0=DSY_CODE[Num_Buffer[i]];
P2=j;
DelayMS(1);
}
}
//主程序
voidmain()
{
uchar i;
P0=0xff;
P1=0xff;
P2=0x00;
Key_Code=0xff;
while(1)
{ | Show_Counts_ON_DSY(); |
|
P1=0xff; | ||
| ||
Key_Code=P1; |
14/ 26
个人收集整理仅供参考学习
//有键按下时,数码管刷新显示30次,该行代码同时起到延时作用 if(Key_Code!=0xff)
for(i=0;i<30;i++)Show_Counts_ON_DSY();
switch(Key_Code)
{
case0xfe: if(Key_Counts<255) Key_Counts++;
break;
case0xfd: if(Key_Counts>0) Key_Counts--;
break;
case0xfb: Key_Counts=0;
}
Key_Code=0xff;
}
}
1X4矩阵键盘控制条形LED显示
/*名称:4X4矩阵键盘控制条形LED显示
说明:运行本例时,
按下地按键值越大
点亮地LED越多.*/
#include<intrins.h> #define uchar unsigned char #include<reg51.h>
//延时
void DelayMS(uintx)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
//键盘扫描
ucharKeys_Scan()
{
uchar sCode,kCode,i,k;
//低4位置0,放入4行
P1=0xf0;
//若高4位出现0,则有键按下
if((P1&0xf0)!=0xf0)
{ | DelayMS(2); |
|
if((P1&0xf0)!=0xf0) |
| |
{ |
15/ 26
个人收集整理仅供参考学习
sCode=0xfe;//行扫描码初值
for(k=0;k<4;k++)//对4行分别进行扫描
{
P1=sCode;
if((P1&0xf0)!=0xf0)
{
kCode=~P1;
for(i=0;i<16;i++)//查表得到按键序号并返回 if(kCode==KeyCodeTable[i])
}
return(i);
}
}
else
sCode=_crol_(sCode,1);
}
return(-1);
//主程序
voidmain()
{ | uchar KeyNo=-1; //按键序号,-1 表示无按键 while(1) uchar i,P2_LED,P3_LED; |
P3_LED=0xff;
for(i=0;i<=KeyNo;i++)//键值越大,点亮地LED越多 {
if(i<8)
P3_LED>>=1;
else
P2_LED>>=1;
}
P3=P3_LED;//点亮条形LED
P2=P2_LED;
}
}
}
17 | 数码管显示4X4 矩阵键盘按键号 |
|
/* 名称:数码管显示4X4 矩阵键盘按键号 说明:按下任意键时,数码
16 / 26
个人收集整理仅供参考学习
管都会显示其键地序号,扫描程
序首先判断按键发生在哪一列,
然后根据所发生地行附加不同地
值,从而得到按键地序号.
*/
#include<reg51.h> #define uchar unsigned char
//段码
sbit BEEP=P37;
//上次按键和当前按键地序号,该矩阵中序号范围0~15,16表示无按键
ucharPre_KeyNo=16,KeyNo=16;
//延时
void DelayMS(uint x)
{
uchar i;
while(x--) for(i=0;i<120;i++);
}
//矩阵键盘扫描
void Keys_Scan()
{
uchar Tmp;
P1=0x0f;//高4 位置0,放入4行
DelayMS(1);
Tmp=P10x0f; /*按键后0f变成0000XXXX,X中一个为0,3个仍为1,通过异或把3 个1变为0,唯一地0变为1*/83lcPA59W9
switch(Tmp) //判断按键发生于0~3列地哪一列
17/ 26
个人收集整理仅供参考学习
{
case1: KeyNo=0;break;
case2: KeyNo=1;break;
case4: KeyNo=2;break;
case8: KeyNo=3;break;
default:KeyNo=16;//无键按下
}
P1=0xf0;//低4位置0,放入4列
DelayMS(1);
Tmp=P1>>40x0f;/*按键后f0变成XXXX0000,X中有1个为0,三个仍为1; 高4位转移到低4位并异或得到改变地值*/
switch(Tmp)//对0~3行分别附加起始值0,4,8,12
{
case1: KeyNo+=0;break;
case2: KeyNo+=4;break;
case4: KeyNo+=8;break;
case8: KeyNo+=12;
}
}
//蜂鸣器
{ uchar i; void Beep()
{ }
BEEP=~BEEP;
}
BEEP=0;
//主程序
voidmain()
{
P0=0x00;
BEEP=0;
while(1)
{
P1=0xf0;
if(P1!=0xf0)Keys_Scan();//获取键序号
if(Pre_KeyNo!=KeyNo)
{
P0=~DSY_CODE[KeyNo];
Beep();
} | Pre_KeyNo=KeyNo; |
|
18 / 26
个人收集整理仅供参考学习
DelayMS(100);
}
}
18开关控制LED
/*名称:开关控制LED
说明:开关S1和S2分别控制LED1和LED2.*/#include<reg51.h>
sbitS1=P10;
sbitS2=P11;
sbitLED1=P00;
sbitLED2=P01;
//主程序
voidmain()
{
while(1)
{
LED1=S1;
} } LED2=S2;
19 继电器控制照明设备
#define uchar unsigned char
#define uint unsigned int
sbitK1=P10;
sbit RELAY=P24;
//延时
void DelayMS(uint ms)
{
uchar t;
while(ms--)for(t=0;t<120;t++);
}
//主程序
void main()
{
P1=0xff;
RELAY=1; |
|
19/ 26
个人收集整理仅供参考学习
{
while(K1==0);
RELAY=~RELAY;
DelayMS(20);
}
}
}
20数码管显示拨码开关编码
/*名称:数码管显示拨码开关编码
说明:系统显示拨码开关所设置地编码000~255*/
#include<reg51.h>
#include<intrins.h>
#defineuchar unsigned char
#defineuint unsigned int
//各数字地数码管段码(共阴)
ucharcode DSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};mZkklkzaaP//显示缓冲
ucharDSY_Buffer[3]={0,0,0};
//延时
{ uchar t; void DelayMS(uint ms)
} //主程序 void main() {
uchar i,m,Num;
P0=0xff;
P2=0xff;
while(1)
{
m=0xfe;
Num=P1; //读取拨码开关地值
DSY_Buffer[0]=Num/100;
DSY_Buffer[1]=Num/10%10;
DSY_Buffer[2]=Num%10;
for(i=0;i<3;i++)//刷新显示在数码管上
{
m=_crol_(m,1);
P2=m;
P0=DSY_CODE[DSY_Buffer[i]];
} | DelayMS(10); |
|
20 / 26
个人收集整理仅供参考学习
}
}
21开关控制报警器
/*名称:开关控制报警器
说明:用K1开关控制报警器,程序控制P1.0输出两种不同频率地声音,
模拟很逼真地报警效果*/
#include<reg51.h>
#defineuchar unsigned char
#defineuint unsigned int
sbitSPK=P1^0;
sbitK1=P1^7;
//发声函数
voidAlarm(uchar t)
{
uchari,j;
for(i=0;i<200;i++)
SPK=~SPK; for(j=0;j<t;j++); //由参数t行成不同地频率{
void main() { }
SPK=0;
while(1)
{
if(K1==1)
{
Alarm(90);
Alarm(120);
}
}
}
22 按键发音
/*名称:按键发音
说明:按下不同地按键会是SOUNDER发出不同频率地声音.
本例使用延时函数实现不同频率地声音输出,以后也可使用定时器*/
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
21 / 26
个人收集整理仅供参考学习
sbitBEEP=P37;
sbitK1=P14;
sbitK2=P15;
sbitK3=P16;
sbitK4=P17;
//延时
void DelayMS(uint x)
{
uchar t;
while(x--) for(t=0;t<120;t++); }
//按周期t 发音
void Play(uchar t)
{
uchar i;
for(i=0;i<100;i++)
{
BEEP=~BEEP;
DelayMS(t);
}
} void main() BEEP=0;
{
{
if(K1==0) Play(1);
if(K2==0) Play(2);
if(K3==0) Play(3);
if(K4==0) Play(4);
}
}
23 播放音乐
/*名称:播放音乐
说明:程序运行时播放生日快乐歌, 未使用定时器中断,所有频率完全用延时实现*/ #include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit BEEP=P3^7;
//生日快乐歌地音符频率表,不同频率由不同地延时来决定
uchar code SONG_TONE[]={212,212,190,212,159,169,212,212,190,212,142,159, AVktR43bpw 212,212,106,126,159,169,190,119,119,126,159,142,159,0};
22 / 26
个人收集整理仅供参考学习
ORjBnOwcEd
//生日快乐歌节拍表,节拍决定每个音符地演奏长短
ucharcode SONG_LONG[]={9,3,12,12,12,24,9,3,12,12,12,24, 2MiJTy0dTT 9,3,12,12,12,12,12,9,3,12,12,12,24,0};//延时
voidDelayMS(uint x)
{
uchart;
while(x--)for(t=0;t<120;t++);
}
//播放函数
voidPlayMusic()
{
uinti=0,j,k;
while(SONG_LONG[i]!=0||SONG_TONE[i]!=0)
{//播放各个音符,SONG_LONG为拍子长度
for(j=0;j<SONG_LONG[i]*20;j++)
{
BEEP=~BEEP;
//SONG_TONE延时表决定了每个音符地频率
} DelayMS(10); for(k=0;k<SONG_TONE[i]/3;k++);
} void main() {
BEEP=0;
while(1)
{
PlayMusic(); //播放生日快乐
DelayMS(500);//播放完后暂停一段时间
}
}
24INT0中断计数
/*名称:INT0中断计数
说明:每次按下计数键时触发
INT0中断,中断程序累加计数,
计数值显示在3只数码管上,按下
清零键时数码管清零 */
#include<reg51.h>
#define uchar unsigned char #define uint unsigned int |
|
23 / 26
个人收集整理仅供参考学习
//0~9地段码
ucharcodeDSY_CODE[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};gIiSpiue7A
//计数值分解后各个待显示地数位
ucharDSY_Buffer[]={0,0,0};
ucharCount=0;
sbitClear_Key=P36;
//数码管上显示计数值
voidShow_Count_ON_DSY()
{
DSY_Buffer[2]=Count/100;//获取3个数
}
DSY_Buffer[1]=Count%100/10;
DSY_Buffer[0]=Count%10;
if(DSY_Buffer[2]==0)//高位为0时不显示
{
DSY_Buffer[2]=0x0a;
if(DSY_Buffer[1]==0)//高位为0,若第二位为0同样不显示
DSY_Buffer[1]=0x0a;
}
P1=DSY_CODE[DSY_Buffer[1]]; P2=DSY_CODE[DSY_Buffer[2]];P0=DSY_CODE[DSY_Buffer[0]];
{ //主程序
void main()
P1=0x00;
P2=0x00;
IE=0x81;//允许INT0中断
IT0=1;//下降沿触发
while(1)
{
if(Clear_Key==0)Count=0; //清0
Show_Count_ON_DSY();
}
}
//INT0中断函数
voidEX_INT0() interrupt 0
{
Count++;//计数值递增
}
25 外部INT0 中断控制LED
/* 名称:外部INT0 中断控制LED
24 / 26
个人收集整理仅供参考学习
说明:每次按键都会触发INT0中断,中断发生时将LED状态取反, 产生LED状态由按键控制地效果*/
#include<reg51.h>
#defineuchar unsigned char
#defineuint unsigned int
sbitLED=P0^0;
//主程序
voidmain()
{
LED=1;
EA=1;
EX0=1;
IT0=1;
while(1);
}
//INT0中断函数
voidEX_INT0() interrupt 0
{
LED=~LED;//控制LED亮灭
} | |
Thisarticle includes some parts, including text, pictures,
anddesign. Copyright is personal ownership.uEh0U1Yfmh
用户可将本文地内容或服务用于个人学习、研究或欣赏,以及其
他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律
地规定,不得侵犯本网站及相关权利人地合法权利.除此以外,将本
文任何内容或服务用于其他用途时,须征得本人及相关权利人地书面
许可,并支付报酬.IAg9qLsgBX
Users may use the contents or services of this article
for personal study, research or appreciation, and other
25/ 26
个人收集整理仅供参考学习
non-commercialor non-profit purposes, but at the same time, they shall abide by theprovisions of copyright law and other relevant laws, and shall notinfringe upon the legitimate rights of this website and its relevantobligees. In addition, when any content or service of this article isused for other purposes, written permission and remuneration shall beobtained from the person concerned and the relevant
obligee.WwghWvVhPE
转载或引用本文内容必须是以新闻性或资料性公共免费信息为
使用目地地合理、善意引用,不得对本文内容原意进行曲解、修改,
must be reasonable and good-faith citation for the use of news
orinformative public free information. It shall not
misinterpretor modify the original intention of the content
of thisarticle, and shall bear legal liability such as
copyright.ooeyYZTjj1
26 / 26