蓝桥杯单片机组——细节注意——模快代码

1.数码管,数码管静态显示。

//ÊýÂë¹Ü£¬µÍµçƽµãÁÁ
//0	--	0xC0
//1	--	0xF9
//2	--	0xA4
//3	--	0xB0
//4	--	0x99
//5	--	0x92
//6	--	0x82
//7	--	0xF8
//8	--	0x80
//9	--	0x90
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
#include <STC15F2K60S2.H>
void Delay(unsigned int t)
{
	while(t--);
	while(t--);

}
void  suocun(unsigned char n)
{
		switch(n)
		{
			case 4 : P2 = (P2 & 0x1f) | 0x80;break;
			case 5 : P2 = (P2 & 0x1f) | 0xa0;break;
			case 6 : P2 = (P2 & 0x1f) | 0xc0;break;
			case 7 : P2 = (P2 & 0x1f) | 0xe0;break;
		}
}
void smg(unsigned char shu,unsigned char wei)
{
		suocun(6);
	  P0=0x01<<wei;
		suocun(7);
	  P0=shu;
}

void smg_st()
{
	unsigned char i;
	for (i=0;i<8;i++)
	{
		smg(Duan[i],i);
		Delay(65535);
	}
}
void main()
{
	while(1)
	{
		smg_st();
	}
}

 其中的细节是:

switch(n)
		{
			case 4 : P2 = (P2 & 0x1f) | 0x80;break;
			case 5 : P2 = (P2 & 0x1f) | 0xa0;break;
			case 6 : P2 = (P2 & 0x1f) | 0xc0;break;
			case 7 : P2 = (P2 & 0x1f) | 0xe0;break;
		}

case 后面的数字前要加空格!!!,注意!!!



数码管动态显示:

#include <STC15F2K60S2.H>
unsigned char num=0;
void dongtai();
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void suocun(unsigned char n)
{
	switch(n){
		case 4:P2 = (P2 & 0x1f)|0x80;break;
		case 5:P2 = (P2 & 0x1f)|0xa0;break;
		case 6:P2 = (P2 & 0x1f)|0xc0;break;
		case 7:P2 = (P2 & 0x1f)|0xe0;break;
	}
}
void shuma(unsigned char wei,unsigned char shu)
{
	suocun(6);
	P0=0x01<<wei;
	suocun(7);
	P0=Duan[shu];	
}

void Delay_shuma(unsigned int t)
{
	while(t--);
}
void Delay_dong(unsigned int t)
{
	while(t--)
	{
	dongtai();
	}

}
void dongtai()
{
	shuma(1,2);
	Delay_shuma(500);
	shuma(2,3);
	Delay_shuma(500);
	shuma(3,4);
	Delay_shuma(500);
	shuma(4,num);
	Delay_shuma(500);
}
void main()
{

	while(1)
	{
	 dongtai();
		num++;
		if(num>9)
		{
			num=0;
		}
	Delay_dong(100);
	}
}

里面有两个 ,延时操作。

一个延时操作,是Delay_shu(unsigned int t)——这个是延迟当数码管静态显示的时候

Delay_dong(unsigned int t)——这个是动态显示的时候,在while里面显示静态。给他一个演示==延时。



独立键盘:

#include <STC15F2K60S2.H>
sbit s1 = P3^0;
sbit s2 = P3^1;
sbit s3 = P3^2;
sbit s4 = P3^3;

sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
sbit L5 = P0^4;
sbit L6 = P0^5;
sbit L7 = P0^6;


void suocun(unsigned char n)
{
	switch(n)
	{
	case 4:P2=(P2 & 0x1f)|0x80;break;
	case 5:P2=(P2 & 0x1f)|0xa0;break;
	case 6:P2=(P2 & 0x1f)|0xc0;break;
	case 7:P2=(P2 & 0x1f)|0xe0;break;
	}
}
void Delay_Key(unsigned char t)
{
	while(t--);
}
void saomiao()
{
	if(s1==0)
		{
	Delay_Key(100);
	if(s1==0)
		{
			L1=0;while(s1==0);L1=1;
	  }
	}
	if(s2==0)
		{
	Delay_Key(100);
	if(s1==0)
		{
			L2=0;while(s2==0);L2=1;
	  }
	}
		if(s3==0)
		{
	Delay_Key(100);
	if(s3==0)
		{
			L3=0;while(s3==0);L3=1;
	  }
	}
		if(s4==0)
		{
	Delay_Key(100);
	if(s4==0)
		{
			L4=0;while(s4==0);L4=1;
	  }
	}
}
void main()
{
	suocun(4);
	while(1)
	{
	saomiao();
	}	
}

 独立键盘,我们delay时间会少一点,前面用到,一个数码管的Delay,最主要的就是在数码管delay,要扫描数码管。

独立键盘就是按下是否为0 ,if为0 则表示按下。



矩阵键盘:

#include <STC15F2K60S2.H>
sbit R1 = P3^0;
sbit R2 = P3^1;
sbit R3 = P3^2;
sbit R4 = P3^3;

sbit C1 = P3^4;
sbit C2 = P3^5;
sbit C3 = P4^2;
sbit C4 = P4^4;
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void suocun(unsigned char n)
{
	switch(n)
	{
		case 4:P2=(P2&0x1f)|0x80;break;
		case 5:P2=(P2&0x1f)|0xa0;break;
		case 6:P2=(P2&0x1f)|0xc0;break;
		case 7:P2=(P2&0x1f)|0xe0;break;
	}
}
void shuma(unsigned char num)
{
	suocun(6);
	P0=0x01;
	suocun(7);
	P0=Duan[num];
}
unsigned char Key;
void jvbzhen()
{
	R1 = 0;
	R2 = R3 = R4=1;
	C1=C2=C3=C4=1;
	if (C1==0)
	{
		while(C1==0);
		Key=0;
		shuma(Key);
	}
	else if(C2==0)
	{
		while(C2==0);
		Key=1;
		shuma(Key);
	}
	else if(C3==0)
	{
		while(C3==0);
		Key=2;
		shuma(Key);
	}
	else if(C4==0)
	{
		while(C4==0);
		Key=4;
		shuma(Key);
	}
}

void main()
{
	while(1)
	{
	jvbzhen();
	}
}

扫描矩阵,就是,一行为0 其他行全为1。列为1.

如果在列检测到0,那就是按下。



外部中断:

#include <STC15F2K60S2.H>

sbit L1 = P0^0;
sbit L8 = P0^7;

void suocun(unsigned char n)
{
	switch(n)
	{
		case 4:P2 = (P2 & 0x1f) | 0x80;break;
		case 5:P2 = (P2 & 0x1f) | 0xa0;break;
		case 6:P2 = (P2 & 0x1f) | 0xc0;break;
		case 7:P2 = (P2 & 0x1f) | 0xe0;break;
	}
}
void Delay(unsigned int t)
{
	while(t--);
		while(t--);
}
//====================
void Init_init()
{
	IT0=1;//ϽµÑØ´¥·¢
	EX0=1;//ÖжÏʹÄÜ
	EA=1;

}
void Ser0() interrupt 0
{
	L8=0;
	Delay(60000);
	Delay(60000);
	Delay(60000);
	Delay(60000);
	Delay(60000);
	Delay(60000);
	Delay(60000);
	Delay(60000);
	Delay(60000);
	L8=1;
}
void working()
{
	suocun(4);
	L1=0;
	Delay(60000);
	L1=1;
	Delay(60000);
}


void main()
{
	Init_init();
	suocun(4);
	
	while(1)
	{
		working();
	}
	
}

外部中段, 

    IT0=1;———下降沿触发
    EX0=1;——使能中断
    EA=1;——中段总开关

要在main函数里,引用init函数。我在这里卡了半天



定时器:

#include <STC15F2K60S2.H>
sbit L8=P0^7;
//65535΢Ãë
void suocun(unsigned char n)
{
	switch(n)
	{
		case 4:P2=(P2 & 0x1f) |0x80;break;
		case 5:P2=(P2 & 0x1f) |0xa0;break;
		case 6:P2=(P2 & 0x1f) |0xc0;break;
		case 7:P2=(P2 & 0x1f) |0xe0;break;	
	}
}
void time0()
{
	TMOD = 0x01;
	TH0 = (65535-50000)/256;
	TH0 = (65535-50000)%256;
	
	ET0=1;
	EA=1;
	TR0=1;
}
unsigned char count=0;
void init() interrupt 1
{
	TH0 = (65535-50000)/256;
	TH0 = (65535-50000)%256;
	
	count++;
	if(count==10)
	{
		L8 = ~L8;
		count=0;
	} 
	
	
}



void main()
{
	time0();
	suocun(4);
	while(1)
	{
		
	}
}




    ET0=1;
    EA=1;
    TR0=1;这个是真记不住。!!!


PWM脉宽调制:

#include <STC15F2K60S2.H>

sbit L1 = P0^0;
sbit S7 = P3^0;

unsigned char count=0;
unsigned char pwm=60;
unsigned char stat = 0;
void suocun(unsigned char n)
{
	switch(n)
	{
		case 4:P2 = (P2 & 0x1f)| 0x80;break;
		case 5:P2 = (P2 & 0x1f)| 0xa0;break;
		case 6:P2 = (P2 & 0x1f)| 0xc0;break;
		case 7:P2 = (P2 & 0x1f)| 0xe0;break;
	}
}

void Delay(unsigned int t)
{
	while(t--);
}
void  danmei()
{
	if(S7 == 0)
	{
		Delay(100);
		if(S7 == 0)
		{
			switch(stat)
			{
				case 0 :L1=0;TR0=1;pwm = 10;stat=1;break;
				case 1 :pwm = 50;stat=2;break;
				case 2 :pwm = 90;stat=3;break;
				case 3 :L1=1;TR0=0;stat=0;break;
			}
			while(S7==0);
		}
	}
}

//===========¶¨Ê±Æ÷=============
void InitTime0()
{
	TMOD = 0x01;
	TH0 = (65535-100)/256;
	TL0 = (65535-100)%256;
	
	EA = 1;
	ET0=1;
	//TR0=1;
}
void servoTimer0() interrupt 1
{
	TH0 = (65535-100)/256;
	TL0 = (65535-100)%256;
	count++;
	if(count== pwm)
	{
		L1=1;
	}
	else if (count == 100)
	{
		L1=0;
		count = 0;
	}
}

void main()
{
	InitTime0();
	suocun(4);
	L1=1;
	while(1)
	{
		danmei();
	}
}

这个代码的思路真的很好;

switch(n)
	{
		case 4:P2 = (P2 & 0x1f)| 0x80;break;
		case 5:P2 = (P2 & 0x1f)| 0xa0;break;
		case 6:P2 = (P2 & 0x1f)| 0xc0;break;
		case 7:P2 = (P2 & 0x1f)| 0xe0;break;
	}

让L1低于PWM设定值时,让他亮

再用到定时器的时候,给他开启。——TR0=0

还有在初始化函数里别忘了。EA=1;和ET0=1;

中断是EX0=1;EA=1;IT0=1;

  • 19
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值