====================================================================

LED闪烁的简单试验

====================================================================
#include <reg51.h> //此文件中定义了51的一些特殊功能寄存器

void delay(unsigned int i); //声明延时函数

main()
{

P2 = 0x00; //置P0口为低电平 点亮P2口8个LED灯

delay(600); // 调用延时程序 等待一段时间后熄灭

P2 = 0xff; //置P0口为高电平 熄灭P2口8个LED灯

delay(600); // 调用延时程序


}

/*******延时函数*************/
void delay(unsigned int i)
{
unsigned char j;
for(i; i > 0; i--) //循环600*255次 机器在这里执行需要一段时间 也就达到了延时效果
for(j = 255; j > 0; j--);
}

 ====================================================================

跑马灯实验 

 延时实现LED流水灯效果   p2口八个灯作跑马灯。采用了寄存器存中间数

====================================================================


#include <reg52.h>
void delay(unsigned int i); //声明延时函数
char LED;


main()
{
unsigned char i;

while (1)
{ LED = 0xfe;

for (i = 0 ;i < 8 ; i++)
{
P2 = LED;
delay(500);
LED = LED << 1; // 左移
LED = LED | 0x01; // 移位后,后面的位为高电平
if (LED == 0x7f) break; //提前退出 FOR 循环
}

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

P2 = LED;
delay(500);
LED = LED >> 1; // 右移
LED = LED | 0x80; // 移位后,后面的位为高电平

}

}

}


/*******延时函数*************/
void delay(unsigned int i)
{
unsigned char j;
for(i; i > 0; i--)
for(j = 255; j > 0; j--);

}

====================================================================

蜂鸣器

====================================================================

#include <reg51.h> //51的端口和各特殊寄存器定义在此文件中 此包含比不可少
sbit BEEP = P1^5; //定义蜂鸣器接、在P1.5脚上
sbit K1 = P0^0; //定义按钮在P0.0脚上


/*********************************************************/
main()
{

if(!K1) BEEP = 0; //当按键按下对应单片机脚变低电平 BEEP发声
else BEEP = 1; //当松开键按对应单片机脚变高电平 BEEP关闭
}

 

====================================================================

无源蜂鸣器,八月桂花音乐

====================================================================

 

#include <REG52.H>
#include <INTRINS.H>
//本例采用89C52, 晶振为11.0592MHZ
//关于如何编制音乐代码, 其实十分简单,各位可以看以下代码.
//频率常数即音乐术语中的音调,而节拍常数即音乐术语中的多少拍;
//所以拿出谱子, 试探编吧!

sbit Beep = P1^5 ;

unsigned char n=0; //n为节拍常数变量
unsigned char code music_tab[] ={
0x18, 0x30, 0x1C , 0x10, //格式为: 频率常数, 节拍常数, 频率常数, 节拍常数,
0x20, 0x40, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x10,
0x1C, 0x10, 0x18 , 0x40,
0x1C, 0x20, 0x20 , 0x20,
0x1C, 0x20, 0x18 , 0x20,
0x20, 0x80, 0xFF , 0x20,
0x30, 0x1C, 0x10 , 0x18,
0x20, 0x15, 0x20 , 0x1C,
0x20, 0x20, 0x20 , 0x26,
0x40, 0x20, 0x20 , 0x2B,
0x20, 0x26, 0x20 , 0x20,
0x20, 0x30, 0x80 , 0xFF,
0x20, 0x20, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x20, 0x1C , 0x10,
0x18, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x20, 0x15 , 0x20,
0x1C, 0x20, 0x20 , 0x20,
0x26, 0x40, 0x20 , 0x20,
0x2B, 0x20, 0x26 , 0x20,
0x20, 0x20, 0x30 , 0x80,
0x20, 0x30, 0x1C , 0x10,
0x20, 0x10, 0x1C , 0x10,
0x20, 0x20, 0x26 , 0x20,
0x2B, 0x20, 0x30 , 0x20,
0x2B, 0x40, 0x20 , 0x15,
0x1F, 0x05, 0x20 , 0x10,
0x1C, 0x10, 0x20 , 0x20,
0x26, 0x20, 0x2B , 0x20,
0x30, 0x20, 0x2B , 0x40,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x20, 0x15 , 0x20,
0x1C, 0x20, 0x20 , 0x20,
0x26, 0x40, 0x20 , 0x20,
0x2B, 0x20, 0x26 , 0x20,
0x20, 0x20, 0x30 , 0x30,
0x20, 0x30, 0x1C , 0x10,
0x18, 0x40, 0x1C , 0x20,
0x20, 0x20, 0x26 , 0x40,
0x13, 0x60, 0x18 , 0x20,
0x15, 0x40, 0x13 , 0x40,
0x18, 0x80, 0x00
};

void int0() interrupt 1 //采用中断0 控制节拍
{ TH0=0xd8;
TL0=0xef;
n--;
}

void delay (unsigned char m) //控制频率延时
{
unsigned i=3*m;
while(--i);
}

void delayms(unsigned char a) //豪秒延时子程序
{
while(--a); //采用while(--a) 不要采用while(a--); 各位可编译一下看看汇编结果就知道了!
}

void main()
{ unsigned char p,m; //m为频率常数变量
unsigned char i=0;
TMOD&=0x0f;
TMOD|=0x01;
TH0=0xd8;TL0=0xef;
IE=0x82;
play:
while(1)
{
a: p=music_tab[i];
if(p==0x00) { i=0, delayms(1000); goto play;} //如果碰到结束符,延时1秒,回到开始再来一遍
else if(p==0xff) { i=i+1;delayms(100),TR0=0; goto a;} //若碰到休止符,延时100ms,继续取下一音符
else {m=music_tab[i++], n=music_tab[i++];} //取频率常数 和 节拍常数
TR0=1; //开定时器1
while(n!=0) Beep=~Beep,delay(m); //等待节拍完成, 通过P1口输出音频(可多声道哦!)
TR0=0; //关定时器1
}
}

====================================================================

试验数码管上显示数字(译码器位选 共阴极)

====================================================================

用573锁存器和译码器控制和数码管 

====================================================================


#include <reg51.h>
#include <intrins.h>

sbit LS138A = P2^2; //定义138译码器的输入A脚由P2.2控制
sbit LS138B = P2^3; //定义138译码器的输入脚B由P2.3控制
sbit LS138C = P2^4; //定义138译码器的输入脚C由P2.4控制

void delay(unsigned int i); //函数声名

char DelayCNT;

//此表为 LED 的字模, 共阴数码管 0-9 -
unsigned char code Disp_Tab[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40};

/************主函数**********************/
main()
{
unsigned int i,LedNumVal=1 ;
unsigned int LedOut[10];

DelayCNT=0;

while(1) //进入循环状态
{
if(++DelayCNT>=50)
{
DelayCNT=0; //延时计数 每扫描一次加一次
++LedNumVal; //每隔50个扫描周期加一次
}

LedOut[0]=Disp_Tab[LedNumVal%10000/1000];
LedOut[1]=Disp_Tab[LedNumVal%1000/100]|0x80;
LedOut[2]=Disp_Tab[LedNumVal%100/10];
LedOut[3]=Disp_Tab[LedNumVal%10];

LedOut[4]=Disp_Tab[LedNumVal%10000/1000]; //千位
LedOut[5]=Disp_Tab[LedNumVal%1000/100]|0x80; //百位带小数点
LedOut[6]=Disp_Tab[LedNumVal%100/10]; //十位
LedOut[7]=Disp_Tab[LedNumVal%10]; //个位


for( i=0; i<9; i++) //实现8位动态扫描循环
{
P0 = LedOut[i]; //将字模送到P0口显示

switch(i) //使用switch 语句控制位选 也可以是用查表的方式 学员可以试着自己修改
{
case 0:LS138A=0; LS138B=0; LS138C=0; break;
case 1:LS138A=1; LS138B=0; LS138C=0; break;
case 2:LS138A=0; LS138B=1; LS138C=0; break;
case 3:LS138A=1; LS138B=1; LS138C=0; break;
case 4:LS138A=0; LS138B=0; LS138C=1; break;
case 5:LS138A=1; LS138B=0; LS138C=1; break;
case 6:LS138A=0; LS138B=1; LS138C=1; break;
case 7:LS138A=1; LS138B=1; LS138C=1; break;

}

delay(150);
}

}
}

/***************************************************************************
* *
* 延时程序 *
****************************************************************************/
void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}

 

====================================================================

LED点阵实验(流动显示1 2 3 4 5 6 7 8 9)

说明 通过P0 和 P2 作为点阵接口  做实验时用单片机脚直接驱动

====================================================================


#include<reg51.h>

unsigned char code tab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};


unsigned char code digittab[18][8]={

{0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00}, //0

{0x00,0x00,0x00,0x00,0x21,0x7f,0x01,0x00}, //1

{0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00}, //2

{0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00}, //3

{0x00,0x00,0x0c,0x14,0x24,0x7f,0x04,0x00}, //4

{0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00}, //5

{0x00,0x00,0x3e,0x49,0x49,0x49,0x26,0x00}, //6

{0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00}, //7

{0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00}, //8

{0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00}, //9

{0x00,0x00,0x7F,0x48,0x48,0x30,0x00,0x00}, //P

{0x00,0x00,0x7F,0x48,0x4C,0x73,0x00,0x00}, //R

{0x00,0x00,0x7F,0x49,0x49,0x49,0x00,0x00}, //E

{0x00,0x00,0x3E,0x41,0x41,0x62,0x00,0x00}, //C

{0x00,0x00,0x7F,0x08,0x08,0x7F,0x00,0x00}, //H

{0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00}, //I

{0x00,0x7F,0x10,0x08,0x04,0x7F,0x00,0x00}, //N

{0x7C,0x48,0x48,0xFF,0x48,0x48,0x7C,0x00} //中

};

unsigned int timecount1 , timecount2;

unsigned char cntx , cnty ;

void main(void)
{
cnty=0;

while(1)

{
if(cnty<18) //红色
{
P1=0xFF;
P2=tab[cntx]; // 列线
P0=digittab[cnty][cntx]; // 行线
}
else //绿色
{
P2=0xFF;
P1=tab[cntx]; // 列线
P0=digittab[cnty-18][cntx]; // 行线
}


//用于控制动态扫描的速度
if(++timecount1>=50)
{
timecount1=0;
if(++cntx>=8) cntx=0;
}


//用于控制动字符间的切换速度
if(++timecount2>=20000)
{
timecount2=0;
if(++cnty>=36)cnty=0;
}

}

}