单片机——6个小例子

目录

1.步进电机控制电路设计

 2.车灯控制

 3. 0-59倒计时

4.定时器,计数器--弥红灯

 5.矩形键盘

6.模数转换


1.步进电机控制电路设计

所需元件:

74ls04,at89c51,cap,cap-elec,crystal,motor-stepper,res,uln2003a,74ls04。

例图:

例图代码:

#include<AT89X52.H>
void mdelay(unsigned int delay)
{
	unsigned int i;
	for(;delay>0;delay--)
		for(i=0;i<124;i++);
}
void main()
{
 	while(1)
	{
	 	P1=0x03;   //0运行,0011,取反1100,a,b
		mdelay(50);
		P1=0x06;
		mdelay(50);
		P1=0x0C;
		mdelay(50);
		P1=0x09;
		mdelay(50);
	}
}

 2.车灯控制

所需元件:

at89c51,cap,cap-elec,27n5771,crysal,led-red,res,respack-8,sw-rot-3

例图:

 例图代码:

#include<REG52.H>
sbit LEDL1=P2^0;
sbit LEDL2=P2^1;
sbit LEDR1=P2^2;
sbit LEDR2=P2^3;
sbit S1=P1^0;
sbit S2=P1^1;
void delay(unsigned int i)
{
while(i--);
}
void main()
{
 while(1)
 {
 	P1=0xff;
	LEDL1=S1;
  	LEDL2=S1;
	LEDR1=S2;
	LEDR2=S2;
	delay(20000);
	LEDL1=1;
  	LEDL2=1;
	LEDR1=1;
	LEDR2=1;
	delay(20000);
  }
}

 3. 0-59倒计时

所需元件:

7SEG-MPX2-CC,74LS245,AT89C51-CAP,CAP-ELEC,CRYSTAL,RES,RESPACK-7

例图:

 例图代码:

  #include<AT89X52.h>
  #include<absacc.h>
  unsigned char code
  Tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6D,0x7D,0x07,0x7f,0x6f,0x77,0x7c};
  unsigned char Dat[]={0,0,0,0,0,0};
  unsigned char Second,t;
  void Delay()
  {
  unsigned char i;
  for(i=0;i<250;i++);
  }
  void main()
  {
  EA=1;
  ET0=1;
  TMOD=0x01;
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%256;
  TR0=1;
  while(1)
  {
  Dat[0]=Second/10;
  Dat[1]=Second%10;
  P0=Tab[Dat[0]];
  P1=0xfe;
  Delay();
  P1=0xff;
  P0=Tab[Dat[1]];
  P1=0xfd;
  Delay();
  P1=0xff;
  }
  }
  void intserv1 (void) interrupt 1 using 1
  {
  TH0=(65536-50000)/256;
  TL0=(65536-50000)%256;
  t++;
  if(t==20)
  {
  t=0;
  Second++;
  if(Second>=60) Second=0;
  }
  }

4.定时器,计数器--弥红灯

所需元件:

AT89C51,CAP,CAP-ELEC,CRYSTAL,LED-BARGRAPH-RED,LED-BIBY,RES

例图:

例图代码:

#include<reg52.h>
unsigned char i10,i8,i6;
unsigned char mod1,mod2;
bit F;
void main(void)
{
i10=10;
i8=8;
i6=6;
mod1=0x01;
mod2=0xAA;
F0=0;
TMOD=0x10;
TH1=0x3C;
TL1=0xB0;
IE=0x88;
TR1=1;
while(1);
}
void timer0(void) interrupt 3 using 1
{
TH1=0x3C;
TL1=0xB0;
i10--;
if(i10==0)
{
i10=10;
if(F==0)
{
P1=~mod1;
mod1=mod1<<1;
i8--;
if(i8==0)
{
i8=8;
F=1;
mod1=0x01;
}
}

else
{
P1=mod2;
mod2=~mod2;
i6--;
if(i6==0)
{
i6=6;
F=0;
}
}
}
}

 5.矩形键盘

所需元件:

7SEG-COM-CATHODE,74LS21,AT89C51,BUTTIN,CAP,

CAP-ELEC,CRYSTAL,RES,74LS21

例图:

例图代码:

/*#include<AT89X52.H>
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay10ms(void)
{
 	unsigned char i,j;
	for(i=20;i>0;i--)
		for(j=248;j>0;j--);
}
unsigned char scan_key(void)
 {
 	unsigned char scan,col,rol,tmp;
	bit flag=0;
	scan=0xef;
	for(col=0;col<4;col++)
		{
		 P0=scan;
		 tmp=~P0;
		 tmp=tmp&0x0f;
		for(rol=0;rol<4;rol++)
			 {
		 	if(tmp==(0x01<<rol)	)
			{
			flag=1;
			break;
			
		 }

		}
		if(flag==1)
		{
		while(tmp!=0x0f)
		{
		 P0=0x0f;
		 tmp=P0;
		}
		break;
		}	
		 scan=(scan<<1)+1;
		}
		
		return(rol*4+col);
 }
 /*void  key_led(void) interrupt 0
 {
   	unsigned char tmp,key;
	   	delay10ms();
			P0=0x0f;
			tmp=P0;	
			if(tmp!=0x0f)
			{
			 key=scan_key();  
			 P1=table[key];
			}
 } */
/*void main()
{
	P1=0x00;
	EA=1;
	EX0=1;
	IT0=1;
	P0=0x0f; 
	 while(1){
	 	scan_key();
	 }
 }
	void key_led(void) interrupt 0			
	{
	unsigned char key,tmp;
		delay10ms();
		P0=0x0f;
		tmp=P0;	
		if(tmp!=0x0f)
		{
		key=scan_key();
		P1=table[key];
	}
} */ 
/*#include<AT89X52.H>
unsigned char table[]={0x3f,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
void delay10ms(void)
{
unsigned char i,j;
for(i=20;i>0;i--)
for(j=248;j>0;j--);
}
void main()
{
unsigned char tmp,key;
P1=0x00;
while(1)
{
while(tmp==0x0f)
{
P0=0x0f;
tmp=P0;
}
delay10ms();
P0=0x0f;
tmp=P0;
if(tmp==0x0f) continue;
key=scan_key();
P1=table[key];
}
}
unsigned char scan_key(void)
{
unsigned char scan,col,rol,tmp;
bit flag=0;
scan=0xef;
for(col=0;col<4;col++)
{
P0=scan;
tmp=P0;
switch(tmp&0x0f)
{
case 0x0e:rol=0;flag=1;break;
case 0x0e:rol=1;flag=1;break;
case 0x0e:rol=2;flag=1;break;
case 0x0e:rol=3;flag=1;break;
}
if(flag==1)break;
scan=(scan<<1)+1;
}
while(tmp!=0x0f)
{
P0=0x0f;
tmp=P0;
}
return(rol*4+col);
}
 
 #include<AT89X52.H>
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay10ms(void)
{
 	unsigned char i,j;
	for(i=20;i>0;i--)
		for(j=248;j>0;j--);
}
unsigned char 	scan_key(void)
 {
 	unsigned char scan,col,rol,tmp;
	bit flag=0;
	scan=0xef;
	for(col=0;col<4;col++)
		{
		 P0=scan;
		 tmp=P0;
		 switch(tmp&0x0f)
		 {
		  case 0x0e: rol=0;flag=1;break;
		  case 0x0d: rol=1;flag=1;break;
		  case 0x0b: rol=2;flag=1;break;
		  case 0x07: rol=3;flag=1;break;
		 }
		 if( flag==1)break;
		 scan=(scan<<1)+1;

		}
		while(tmp!=0x0f)
		{
		 P0=0x0f;
		 tmp=P0;
		}	
		return(rol*4+col);
 }
void main()
{
 	unsigned char tmp,key;
	P1=0x00;
	while(1)
	{
	 	while(tmp==0x0f)
			{
				P0=0x0f;
				tmp=P0;
				}
		delay10ms();
		P0=0x0f;
		tmp=P0;	
		if(tmp==0x0f)continue;
		key=scan_key();
		P1=table[key];
	}	
}
 	*/
	#include<AT89X52.H>
unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,
0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void delay10ms(void)
{
 	unsigned char i,j;
	for(i=20;i>0;i--)
		for(j=248;j>0;j--);
}
unsigned char 	scan_key(void)
 {
 	unsigned char scan,col,rol,tmp;
	bit flag=0;
	scan=0xef;
	for(col=0;col<4;col++)
		{
		 P0=scan;
		 tmp=P0;
		 switch(tmp&0x0f)
		 {
		  case 0x0e: rol=0;flag=1;break;
		  case 0x0d: rol=1;flag=1;break;
		  case 0x0b: rol=2;flag=1;break;
		  case 0x07: rol=3;flag=1;break;
		 }
		 if( flag==1)break;
		 scan=(scan<<1)+1;

		}
		while(tmp!=0x0f)
		{
		 P0=0x0f;
		 tmp=P0;
		}	
		return(rol*4+col);
 }
void main()
{
 	unsigned char tmp,key;
	P1=0x00;
	while(1)
	{
	 	while(tmp==0x0f)
			{
				P0=0x0f;
				tmp=P0;
				}
		delay10ms();
		P0=0x0f;
		tmp=P0;	
		if(tmp==0x0f)continue;
		key=scan_key();
		P1=table[key];
	}	
} 
 
 

6.模数转换

所需元件:

ADC0809,AT89C51,CAP,CAP-ELEC,CRYSTAL,LED-BARGRAPH-RED,LED-BIBY,

POT-HG,RES

例图:

例图代码:

#include<reg52.h>
#include<INTRINS.h>
sbit EOC=P2^4;
sbit START=P2^5;
sbit CLOCK=P2^6;
sbit OE=P2^7;
unsigned char temp;
void main(void)
{
 TMOD=0x02;
 TH0=206;
 TL0=206;
 EA=1;
 ET0=1;
 TR0=1;
 while(1)
 {
	START=0;
	START=1;
	START=0;
	while(EOC==0);
	OE=1;
	temp=P0;
	P1=temp;
	_nop_();
	_nop_();
 }
 }
 void t0(void) interrupt 1 using 0
 {
  CLOCK=~CLOCK;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值