控制一个灯亮

#include <reg52.h>
sbit P1_0= P1^0;
void main()
{
	P1_0=0;
}

控制流水灯

#include <reg52.h>
void main()
{
	P1=0xfe;
	Delay100us();		//这里调用的函数是预先定义的延迟函数
	P1=0xfd;
	Delay100us();
	P1=0xfa;
	Delay100us();
	P1=0xf7;
	Delay100us();
	P1=0xef;
	Delay100us();
	P1=0xdf;
	Delay100us();
	P1=0xaf;
	Delay100us();
	P1=0x7f;
	Delay100us();
}

使用C语言的位运算和取反运算来循环输出

#include <reg52.h>
void main()
{
	unsigned char temp=0x1;
	while(1)
	{        
		if(temp==0)temp=0x01;
		P1=~temp;
			Delay100us();
		    Delay100us();
		temp=temp<<1;
	 
	}
}

数组

#include <reg52.h>
void main()
{
	unsigned char led[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
	unsigned char i;
	while(1)
	{
		for(i=1;i<=7;i++)
		{
			P1=led[i];                
			Delay100us();        
	    }
	}
}

输出数字

#include<reg52.h>
sbit wei0= P1^0;
sbit wei1= P1^1;
sbit wei2= P1^2;
sbit wei3= P1^3;
unsigned char i=0,a[10]={0xc0,~0x06,0xa4,0xb0,0x99,0x92,0x82,~0x07,0x80,0x90};

void main()
{
	wei0=0;
	wei1=0;
	wei2=0;
	wei3=0;
	while(i<10)
	{
		P0=a[i];
		Delay100us();
		i++;
	}
}

输出学号

#include<reg52.h>
sbit wei0= P1^0;
sbit wei1= P1^1;
sbit wei2= P1^2;
sbit wei3= P1^3;
unsigned char i=0,a[10]={0xc0,~0x06,0xa4,0xb0,0x99,0x92,0x82,~0x07,0x80,0x90};

void main()
{
	while(12)
	{
		wei0=0;
		P0=a[3];
		Delay100us();
		wei0=1;
	
		wei1=0;
		P0=a[1];
		Delay100us();
		wei1=1;
	
		wei2=0;
		P0=a[1];
		Delay100us();
		wei2=1;
	
		wei3=0;
		P0=a[0];
		Delay100us();
		wei3=1;
	}
	
	
}

输出指定的数字

#include <reg52.h>
sbit wei0= P1^0;
sbit wei1= P1^1;
sbit wei2= P1^2;
sbit wei3= P1^3;
void main()
{
	unsigned char a[10]={0xc0,~0x06,0xa4,0xb0,0x99,0x92,0x82,~0x07,0x80,0x90};
	int m=5678,g,s,b,q;
	q=m/1000;
	b=m%1000/100;
	s=m%100/10;
	g=m%10;

	//unsigned char i;
	for(;4;)
	{
		wei0=0;
		P0=a[g];
		Delay();
		wei0=1;
	
		wei1=0;
		P0=a[s];
		Delay();
		wei1=1;
	
		wei2=0;
		P0=a[b];
		Delay();
		wei2=1;
	
		wei3=0;
		P0=a[q];
		Delay();
		wei3=1;
	}


}

呼吸灯

#include <reg52.h>
#include <intrins.h>
void Delay1us()        
{
	_nop_();
	_nop_();
	_nop_();
}

unsigned char count=0;
void led_pwm(unsigned char i)			//i相当于占空比
{        
	Delay1us();
	count = (count+1)%100;
	if(count<i)P1=0xff;
	else P1=0;
} 
void main()
{
	unsigned char j=0,i;
while(1)
{   
	while(j<=99)
	{
		j++ ;
		for(i=0;i<100;i++)
		{
			led_pwm(j);  
		}
	}
	while(j>0)
	{
		j-- ;
		for(i=0;i<100;i++)
		{
			led_pwm(j); 
		}
	}
}
}

按键中断

//轮询
#include <reg52.h>
         unsigned char led[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
	unsigned char num[10]={0xc0,~0x06,0xa4,0xb0,0x99,0x92,0x82,~0x07,0x80,0x90};
	sbit KEY1=P3^3;
void main()
{
	while(1)        
	{
		if(KEY1==0)
		{
			Delay1(); 	//程序消抖,要按键稳定才可以触发
			if(KEY1==0)
			{
				P1=~P1;
			}
		}
	}
}
//中断
#include <reg52.h>
         unsigned char led[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
	unsigned char num[10]={0xc0,~0x06,0xa4,0xb0,0x99,0x92,0x82,~0x07,0x80,0x90};

void main()
{
	IT1=1;
	EX1=1;
	EA=1;                   //打开相应的按键中断开关
	P1=0;
	while(1);                //为什么不要这一行不行
}
void key()interrupt 2          //外部中断1
{
	P1=~P1;        
}

从0计数

#include<reg52.h>
#include<intrins.h>
table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98};
sbit wei0=P1^0;
sbit wei1=P1^1;
sbit wei2=P1^2;
sbit wei3=P1^3;
void Delay10us()
{
	unsigned char i;
	_nop_();
	i = 25;
	while (--i);
}
void lmj(int i)
{
	int g,s,b,q;
	g=i%10;
	s=(i/10)%10;
	b=(i/100)%10;
	q=i/1000;
	 
	P0=table[q];
	wei3=0;
	Delay10us();
	wei3=1;

	P0=table[b];
	wei2=0;
	Delay10us();  
	wei2=1;
	     
	P0=table[s];  
	wei1=0;  
	Delay10us(); 
	 wei1=1;
	     
	 P0=table[g];
	 wei0=0;  
	 Delay10us();  
	 wei0=1; 
}
void Timer0Init(void)//2毫秒@11.0592MHz
{
	TMOD |= 0x80;
	TMOD &= 0xF0;
	TL0 = 0x9A;
	TH0 = 0xA9;
	TF0 = 0;
	TR0 = 1;
	ET0 = 1;
	EA  = 1;
}
unsigned char i=0;int sec=0;
void timer0() interrupt 1
{  
i++;
TL0 = 0x9A;
TH0 = 0xA9;
if(i==120)
{
	sec++;i=0;
}
}        
void main()
{  
	Timer0Init();  
	while(1)  
	{
		lmj(sec);
	}
}

从零按分秒计数

#include<reg52.h>
#include<intrins.h>
unsigned char i=0;int sec=0,min=0,time=0;
table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98};
a[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18};
sbit wei0=P1^0;
sbit wei1=P1^1;
sbit wei2=P1^2;
sbit wei3=P1^3;
void Delay10us()
{
	unsigned char i;
	_nop_();
	i = 25;
	while (--i);
}
void lmj(int i)
{
	int g,s,b,q;
	g=i%10;
	s=(i/10)%10;
	b=(i/100)%10;
	q=i/1000;
	 
	P0=table[q];
	wei3=0;
	Delay10us();
	wei3=1;

	P0=a[b];
	wei2=0;
	Delay10us();  
	wei2=1;
	     
	P0=table[s];  
	wei1=0;  
	Delay10us(); 
	 wei1=1;
	     
	 P0=table[g];
	 wei0=0;  
	 Delay10us();  
	 wei0=1; 
}
void Timer0Init(void)//2    @11.0592MHz
{
	TMOD |= 0x80;
	TMOD &= 0xF0;
	TL0 = 0x9A;
	TH0 = 0xA9;
	TF0 = 0;
	TR0 = 1;
	ET0 = 1;
	EA  = 1;
}

void timer0() interrupt 1
{  
i++;
TL0 = 0x9A;
TH0 = 0xA9;
if(i==120)
{
	sec++;i=0;
	if(sec==60)
	{
		min++;
		sec=0;
	}
	time=min*100+sec;
}
}        
void main()
{  
	Timer0Init();  
	while(1)  
	{
		lmj(time);
	}
}

可以暂停的计数器

#include<reg52.h>
#include<intrins.h>
unsigned char i=0;int sec=0,min=0,time=0;
table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98};
a[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x18};
sbit wei0=P1^0;
sbit wei1=P1^1;
sbit wei2=P1^2;
sbit wei3=P1^3;                        
void Delay10us()
{
	unsigned char i;
	_nop_();
	i = 25;
	while (--i);
}
void lmj(int i)
{
	int g,s,b,q;
	g=i%10;
	s=(i/10)%10;
	b=(i/100)%10;
	q=i/1000;
	 
	P0=table[q];
	wei3=0;
	Delay10us();
	wei3=1;

	P0=a[b];
	wei2=0;
	Delay10us();  
	wei2=1;
	     
	P0=table[s];  
	wei1=0;  
	Delay10us(); 
	 wei1=1;
	     
	 P0=table[g];
	 wei0=0;  
	 Delay10us();  
	 wei0=1; 
}
void Timer0Init(void)               //延时函数
{
	TMOD |= 0x80;
	TMOD &= 0xF0;
	TL0 = 0x9A;
	TH0 = 0xA9;
	TF0 = 0;
	TR0 = 1;
	ET0 = 1;
	EA  = 1;                     //打开计时器中断
}

void timer0() interrupt 1
{  
i++;
TL0 = 0x9A;
TH0 = 0xA9;
if(i==120)
{
	sec++;i=0;
	if(sec==60)
	{
		min++;
		sec=0;
	}
	time=min*100+sec;
}
}
void KEY1() interrupt 2
{  
	TR0=~TR0;
}        
void main()
{  
	IT1=1;
	EX1=1;     
	EA=1;              //打开外部中断(为了使KEY1中断函数生效)
	Timer0Init();  
	while(1)  
	{
		lmj(time);
	}

串口和串口中断,蓝牙遥控小车

#include <reg52.h>
#include <intrins.h>
#include <string.h>


sbit La = P1^0;
sbit Lb = P1^1;
sbit Ra = P1^2;
sbit Rb = P1^3;                                                  //几个位定义用来控制电机

void Delay1us()                                                  //@11.0592MHz 延时函数,控制调速时使用
{
	_nop_();
	_nop_();
	_nop_();
}

unsigned char count=0;                                                                   //定义调速函数用到的变量
void run_pwm (unsigned char L,unsigned char R)                //左右轮调速函数
{
	Delay1us();
	count = (count+1)%10;
	La = 1;Ra = 1;
	if(count<L) Lb=0;
	else Lb = 1;
	if(count<R) Rb = 0;
	else Rb = 1;

}

//几个预置run函数

unsigned char s = 5;
void speed_up()
{
	s++;
	if(s<=10)s = 10;

}

void speed_down()
{
	 s--;
	 if(s<=0)s = 0;
}

void run_towards()
{
	run_pwm(s,s);
}

void turn_left()
{
	La = 1;Lb = 1;
	Ra = 1;Rb = 0;
}

void turn_right()
{
	La = 1;Lb = 0;
	Ra = 1;Rb = 1;
}

void run_backwards()
{
	La = 0;Lb = 1;
	Ra = 0;Rb = 1;
}

void stop()
{
	La = 0;Lb = 0;
	Ra = 0;Rb = 0;
}







void UartInit(void)                //9600bps@11.0592MHz
{
	PCON &= 0x7F;                //波特率不倍速
	SCON = 0x50;                //8位数据,可变波特率
//        AUXR &= 0xBF;                //定时器1时钟为Fosc/12,即12T
//        AUXR &= 0xFE;                //串口1选择定时器1为波特率发生器
	TMOD &= 0x0F;                //清除定时器1模式位
	TMOD |= 0x20;                //设定定时器1为8位自动重装方式
	TL1 = 0xFD;                //设定定时初值
	TH1 = 0xFD;                //设定定时器重装值
	ET1 = 0;                //禁止定时器1中断
	TR1 = 1;                //启动定时器1
	
	
	EA = 1;                         //         打开总中断
	ES = 1;                         //  打开串口中断
}



unsigned char a = 8,b = 9;                  //在这个地方输入初始速度(调速好后可以刚好直走)
void main()
{
	UartInit();
	while(1);
	
}




unsigned char temp;
unsigned char cmd[50]={0};
unsigned char i = 0;

void usb_1()interrupt 4                         //串口中断程序
{
	cmd[i] =SBUF;
	RI = 0;
	SBUF = cmd[i]; 
	while(TI==0);                                                //等待发送完成
	TI = 0;
	if(cmd[i]=='\n')
	{
		if(strncmp("towa",cmd,4)==0) run_towards();                                                  
		else if(strncmp("left",cmd,4)==0)turn_left();                         
		else if(strncmp("righ",cmd,4)==0)turn_right();
		else if(strncmp("back",cmd,4)==0)run_backwards();                        
		else if(strncmp("++++",cmd,4)==0)speed_up();                        
		else if(strncmp("----",cmd,4)==0)speed_down();                         
		else if(strncmp("stop",cmd,4)==0)stop();                         
		else if(strncmp("led_on",cmd,6)==0)P1 = 0;        
		i = 0;
	}
	else i++;
}
讲故事的人
最后更新于 2023-03-20