********
* LED闪烁的简单试验
*
*
* 连接方法:JP11(P2)和JP1用8PIN排线连接起来
*
*
*
******************************************************************************* ********/
#include
void delay(unsigned int i); //声明延时函数
main()
{
P2 = 0x00; //置P0口为低电平
delay(600); // 延时
P2 = 0xff; //置P0口为高电平
delay(600); // 延时
}
/*******延时函数*************/
void delay(unsigned int i)
{
unsigned char j;
for(i; i > 0; i--)
for(j = 255; j > 0; j--);
}
********
* LED闪烁的简单试验
*
* 延时实现p2口LED流水灯效果(用循环移位指令)
*
* 连接方法:JP11(P2)和JP1(LED灯) 用8PIN排线连接起来
*
*
*
******************************************************************************* ********/
#include
#include
void delayms(unsigned char ms)
// 延时子程序
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
main()
{
unsigned char LED;
LED = 0xfe; //0xfe = 1111 1110
while(1)
{
P2 = LED;
delayms(250);
LED = LED << 1; //循环右移1位,点亮下一个LED "<<"为左移位
if(LED == 0x00 ) {LED = 0xfe; } // 0xfe = 1111 1110
}
}
***********
*
*
* LED点阵实验(流动显示1 2 3 4 5 6 7 8 9)
*
* 说明通过P0 和P2 作为点阵接口
*
*要求学员掌握LED点阵的工作原理和各点阵脚的定义及接法,具体接线请参考接线说明*
*我们采用的LED点阵式是低功耗,在做实验时可以用单片机脚直接驱动
*
******************************************************************************* ***********/
#include
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 timecount;
unsigned char cnta;
unsigned char cntb;
void main(void)
{
TMOD=0x01;
TH0=(65536-3000)/256;
TL0=(65536-3000)%256;
TR0=1; //开启定时0
ET0=1;
EA=1; //开启中断
cntb=0;
while(1)
{ ;
}
/*************************************************
*
* 定时中断
********************************************************/ void t0(void) interrupt 1 using 0
{
TH0=(65536-3000)/256; //定时器高位装载数据
TL0=(65536-3000)%256; //定时器低位装载数据
if(cntb<18) //红色
{
P1=0xFF;
P2=tab[cnta];
P0=digittab[cntb][cnta];
}
else //绿色
{
P2=0xFF;
P1=tab[cnta];
P0=digittab[cntb-18][cnta];
}
if(++cnta>=8) cnta=0;
if(++timecount>=333)
{
timecount=0;
if(++cntb>=36)cntb=0;
}
}
/******************************************************************/
/* 按键状态显示试验*
/* *
/* 连接方法:连接JP10(P0)与JP5(按钮接口) *
/* JP11 (P2)与JP1(LED接口) *
*
/*按键则点亮LED灯,8路指示灯接p0口* /******************************************************************/
#include
#include
sbit BEEP = P1^5;
sbit RELAY = P1^4;
sbit K1 = P0^0; sbit K2 = P0^1;
sbit K3 = P0^2; sbit K4 = P0^3;
sbit K5 = P0^4; sbit K6 = P0^5;
void beep();
/*********************************************************/
main()
{
while(1)
{
P2 = 0xff;
if(K1==0)
P2 = 0xfe;
if(K2 == 0)
P2 = 0xfd;
if(!K3)
P2 = 0xfb;
if(!K4)
P2 = 0xf7;
if (!K5 )
beep(); //喇叭发声
RELAY = 1;
if (!K6 )
RELAY = 0; //通过了反相器
}
}
/**********************************************************/ void beep()
{
unsigned char i , j;
for (i=0;i<100;i++)
{
BEEP=!BEEP; //BEEP取反
for (j = 0 ; j<250 ; j++) //需要产生方波
_nop_();
}
BEEP=1; //关闭蜂鸣器
}
/****************************************************************************** *
* 按键控制程序
*
* 连接方法:JP10(P2)与JP1 (LED灯)连接,
*
* JP11(P0)与JP5(按键接口)连接* * 开始点亮P1LED
*
* 按P01 LED向右移一位*
* 按P00 LED向左移一位
*
* 连续按动按钮LED会不停的左移或右移
*
******************************************************************************* /
#include
#include
unsigned char scan_key();
void proc_key(unsigned char key_v);
void delayms(unsigned char ms);
sbit K1 = P0^0; //对应按钮K1
sbit K2 = P0^1; //对应按钮K2
main()
{
unsigned char key_s,key_v;
key_v = 0x03;
P2 = 0xfe;
while(1)
{
key_s = scan_key();
if(key_s != key_v)
{
delayms(10);
key_s = scan_key();
if(key_s != key_v)
{
key_v = key_s;
proc_key(key_v);
}
}
}
}
unsigned char scan_key()
{
unsigned char key_s;
key_s = 0x00;
key_s |= K2;
key_s <<= 1;
key_s |= K1;
return key_s;
}
void proc_key(unsigned char key_v)
{
if((key_v & 0x01) == 0)
{
P2 = _cror_(P2,1);
}
else if((key_v & 0x02) == 0)
{
P2 = _crol_(P2, 1);
}
}
void delayms(unsigned char ms) // 延时子程序{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
/****************************************************************************** **
; 二进制加法试验
*
; p2口八个灯作二进制加法。理解二进值的计算 * ; 硬件连接:p2口8路指示灯
*
;
*
;* 描述: *
; p2口八个灯作二进制加法。理解二进值的计算*
;* 连接方法:JP11(P2)和JP1(LED灯) 用8PIN排线连接起来* ******************************************************************************* **/
#include
void delay(unsigned int i); //声明延时函数
main()
{
unsigned char Num = 0xff;
while (1)
{P2 = Num;
delay(1000); //延时函数
Num--;
}
}
/*******延时函数*************/
void delay(unsigned int i)
{
unsigned char j;
for(i; i > 0; i--)
for(j = 255; j > 0; j--);
}
/************************************************************************ [文件名] C51音乐程序(八月桂花)
[功能] 通过单片机演奏音乐
/**********************************************************************/ #include
#include
//本例采用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 }
}
***************************
**
** (c) Copyright 2004-2006
** All Rights Reserved
**
**
** 版权所有:朗顿科技
** https://www.doczj.com/doc/d518588827.html,
**
******************************************************************************* *************************/
#include
sbit buzz = P3^5;
void T0_irq(void) interrupt 1 using 1
{
TH0 = -(300/256);
TL0 =- (300%256);
buzz = ~buzz;
}
main()
{
TMOD = 0x01;
TH0 = -(1000/256);
TL0 =- (1000%256);
EA = 1;
ET0 = 1;
TR0 = 1;
for(;;){
}
}
*
* 描述: 跑马灯程序*
* 连接方法:JP11(P2)和JP1(LED灯) 用8PIN排线连接起来
*
*
*
******************************************************************************* /
#include
#include
/***************************************************************************** * 延时子程序* * * ******************************************************************************/ void delayms(unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
/***************************************************************************** * 主程序* * * ******************************************************************************/ main()
{
unsigned char LED;
LED = 0xfe;
P2 = LED;
while(1)
{
delayms(250);
LED = _crol_(LED,1); //循环右移1位,点亮下一个LED 此函数位库函数
P2 = LED;
}
}
****
* 标题: 试验数码管上显示数字( 单片机直接实现位选共阴极) *
*
*
* 连接方法:P0与J12 用8PIN排线连接P1与JP16 用排线连接
*
******************************************************************************* ****
*
*
* 请学员认真消化本例程,用573锁存器控制和单片机脚直接位选控制(非译码器控制)数码管*
******************************************************************************* ****/
#include
#include
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}; //段码控制
//此表为8个数码管位选控制, 共阴数码管1-8个-
unsigned char code dispbit[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdF,0xbF,0x7F}; //位选控制查表的方法控制
/************主函数**********************/
main()
{
unsigned int i,LedNumVal=1 ; //变量定义
unsigned int LedOut[10]; //变量定义
DelayCNT=0;
while(1)
{
if(++DelayCNT>=20) //控制数字变化速度
{
DelayCNT=0; //20个扫描周期清零一次
++LedNumVal; //每隔20个扫描周期加一次
}
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++)
{
P0 = LedOut[i];
P1 = dispbit[i]; //使用查表法进行位选
/* switch(i) //使用switch 语句控制位选
{
case 0:P1 = 0x7F; break;
case 1:P1 = 0xbF; break;
case 2:P1 = 0xdF; break;
case 3:P1 = 0xeF; break;
case 4:P1 = 0xf7; break;
case 5:P1 = 0xfb; break;
case 6:P1 = 0xfd; break;
case 7:P1 = 0xfe; break;
} */
delay(150); //扫描间隔时间太长会数码管会有闪烁感}
}
}
void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--); }
/****************************************************************************** *
* 标题: 试验数码管上如何显示数字(共阳极) * *
*
* 连接方法:P0 与JP3 用8PIN排线连接
*
******************************************************************************* *
*
*
* 请学员认真消化本例程,用单片机脚直接控制数码管* ******************************************************************************* */
#include
#include
#define NOP() _nop_() /* 定义空指令*/
void delay(unsigned int i); //函数声名
// 此表为LED 的字模
unsigned char code LED7Code[] = {~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5 E,~0x79,~0x71};
main()
{
unsigned int LedNumVal=1 ,C ; //定义变量
while(1)
{
if (++C>= 300)
{ LedNumVal++ ; //每隔300个扫描周期加一次
C =0; //每隔300个扫描清零
}
// 将字模送到P0口显示
P0 = LED7Code[LedNumVal%10]&0x7f; //LED7 0x7f为小数点共阴和共阳此处也是不一样;
delay(150); //调用延时程序
}
}
/***************************************************************** * * * 延时程序* * * *****************************************************************/ void delay(unsigned int i)
{
char j;
for(i; i > 0; i--)
for(j = 200; j > 0; j--);
}