单片机—西北工业大学电子实习

一天的单片机电子实习挺有趣的,特别是老师拿出的机器狗,俄罗斯方块单片机,太强了。上午的电子实习主要是写流水灯,有七个样例只需要写出来五个就能拿到四分,下午是做电子表,实现基础功能拿到四分,在增加一些功能如闹钟,日历等可以加分。此外在写交通表也可以加分。

#1.流水灯

#include
#include

#define uchar unsigned char
#define uint unsigned int
	
sbit  k1 = P3 ^ 2;
sbit  k2 = P3 ^ 3;
sbit  k3 = P3 ^ 4;
sbit  k4 = P3 ^ 5;
sbit  led = P4 ^ 1; //流水灯开关,只有P4.0 = 0 流水灯才能工作

//延时子程序
void delay_t(uint t)
{
	 uint a; uchar b;
   for(b=0;b<4;b++)
		for(a=0;a<t;a++);
}
// 1
void start_1()
{
	uchar a, b;
	b = 0xfe;
	for(a=0;a<7;a++)
	{
		P0 = b;
		delay_t(50000);
		b = _crol_(b , 1);
	}
	for(a=0;a<7;a++)
	{
		P0 = b;
		delay_t(50000);
		b = _cror_(b , 1);
	}
}
// 2
void start_2()
{
	uchar a, b;
	b = 0xfe;
	for(a=0;a<7;a++)
	{
		P0 = 0xe7 & b;
		delay_t(500000);
		b = _crol_(b, 1);
	}
	for(a=0;a<7;a++)
	{
		P0 = 0xe7 & b;
		delay_t(500000);
		b = _cror_(b, 1);
	}	
}
//3
void start_3()
{
	uchar a, b, c;
	b = 0xfe; // 11111110
	c = 0x7f; // 01111111
	for(a=0;a<4;a++)
	{
		P0 = b & c;
		delay_t(500000);
		b = _crol_(b, 1);
		c = _cror_(c, 1);
	}
	for(a=0;a<3;a++)
	{
		P0 = b & c;
		delay_t(500000);
		b = _crol_(b, 1);
		c = _cror_(c, 1);
	}
}
//4
void start_4()
{
	uchar a, b, c;
	b = 0x7f;// 01111111
	c = 0x7f;// 01111111
	for(a=0;a<8;a++)
	{
		P0 = b;
		delay_t(500000);
		b = _cror_(b, 1);//10111111
		c = b & c;			 //00111111
		b = c;
	}
	b = 0x01;
	c = 0x01;
	for(a=0;a<8;a++)
	{
		P0 = b;
		delay_t(500000);
		b = _crol_(b, 1);
		c = b | c;
		b = c;
	}
}
//5
void start_5()
{
	uchar a, b, c, d, e, t;
	b = 0xfe;
	c = b;
	d = 0xff;
  for(e=8;e>0;e--)
	{
	for(t=0;t<e;t++)
	{
		P0 = b & d;
		delay_t(500000);
		b = _crol_(b, 1);
	}
	d = P0;
	b = c;
	}
	
}

void main()
{
    led =0; //点亮流水灯
	while(1)
	{
		start_1();
		start_2();
		start_3();
		start_4();
		start_5();
	}
}

#2.电子表
想着增加一个闹钟的功能但做不出来,我增加了一个时间内置好的闹钟,按f5的时候可以关闭闹钟。

#include
#define uchar unsigned char
#define uint unsigned int

//显示的数字数组,依次为0 1 2 3 4 5 6 7 8 9 -
uchar table[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xbf};
//显示数码管数组,依次选择1 2 3 4 5 6 7 8
uchar select[] = {0xfe, 0xfd, 0xfb, 0xf7, 0xef, 0xdf, 0xbf, 0x7f};

//按键定义
sbit k1 = P3 ^ 2;  //小时加
sbit k2 = P3 ^ 3;  //小时减
sbit k3 = P3 ^ 4;  //分钟加
sbit k4 = P3 ^ 5;  //分钟减
sbit k5 = P4 ^ 0;
sbit beep = P4 ^ 2; //蜂鸣器

//标志位定义
bit flag1, flag2, flag3, flag4, flag5;
//时间变量定义
char hour, minute, second;

//延时
void delay()
{
	uint i = 100;
	while(i--);
}

//初始化
void init()
{
	P0M1 = 0x00;  
	P0M0 = 0xff;
	P1M1 = 0x00;
	P1M0 = 0xff;
	P2M1 = 0x00;
	P2M0 = 0xff;
	//定时器T0初始化产生20毫秒
	AUXR &= 0x7F; //定时器时钟12T模式
	TMOD &= 0xF0; //设置定时器模式
	TL0 = 0xC0;   //设置定时初值
	TH0 = 0x63;   //设置定时初值
	TR0 = 1;      //定时器0开始计时
	ET0 = 1;      //允许定时器T0中断
	EA = 1;       //打开总中断
}
//数码管显示子程序
void display()
{
	P1 = select[0];        //选择第1个数码管
	P0 = table[second%10]; //显示秒个数位
	delay();
	P1 = select[1];        //选择第2个数码管
	P0 = table[second/10]; //显示秒十位数
	delay();
	
	P1 = select[2];        //选择第3个数码管
	P0 = table[10];        //显示 ' - '
	delay();
	
	P1 = select[3];        //选择第1个数码管
	P0 = table[minute%10]; //显示分个位数
	delay();
	P1 = select[4];        //选择第2个数码管
	P0 = table[minute/10]; //显示分十位数
	delay();
	
	P1 = select[5];        //选择第3个数码管
	P0 = table[10];        //显示 ' - '
	delay();
	
	P1 = select[6];        //选择第1个数码管
	P0 = table[hour%10]; //显示时个位数
	delay();
	P1 = select[7];        //选择第2个数码管
	P0 = table[hour/10]; //显示时十位数
	delay();
}
//蜂鸣器
void Beep()
{
	if(k5 == 1)
	{
		if(flag5 == 0)
		{
				flag5 = 1;
				beep = ~beep;
		}
	}
	if(k5 == 0)
	{
		flag5 = 0;
	}  
}

void set_time()
{
	// 0 down; 1 up
	// k1 hour +
	if(k1 == 0)
	{
		if(flag1 == 0)
		{
			flag1 = 1;
			hour++;
		}
	}
	if(k1 == 1)
	{
		flag1 = 0;
	}
	// k2 hour -
	if(k2 == 0)
	{
		if(flag2 == 0)
		{
			flag2 = 1;
			hour --;
		}
	}
	if(k2 == 1)
	{
		flag2 = 0;
	}
	// k3 minute +
	if(k3 == 0)
	{
		if(flag3 == 0)
		{
			flag3 = 1;
			minute++;
		}
	}
	if(k3 == 1)
	{
		flag3 = 0;
	}
	// k4 minute -
	if(k4 == 0)
	{
		if(flag4 == 0)
		{
			flag4 = 1;
			minute--;
		}
	}
	if(k4 == 1)
	{
		flag4 = 0;
	}
}


void main()
{
	init();
	P2 = 0x00; //关闭交通灯
	while(1)
	{
		display();
		set_time();
	}
}
void timer0()interrupt 1
{
	uchar t0;
	t0++;
	if(t0 == 60)
	{
		t0 = 0;
		second++;
	}
	if(second == 60)
	{
		second = 0;
		minute++;
	}
	if(minute == 60)
	{
		hour++;
		minute=0;
	}
	if(hour == 24)
	{
		hour=0;
	}
	if(minute == 12 && hour == 12)
	{
		Beep();
	}
}

#3.交通表
一个朋友搞出来了。

#include

#define uchar unsigned char
#define uint unsigned int

uchar table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf};
uchar select[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar redt,greent,time,sign;

sbit k1=P3^2;
sbit k2=P3^3;
sbit k3=P3^4;
sbit k4=P3^5;
sbit dxg=P2^0;
sbit dxy=P2^1;
sbit dxr=P2^2;
sbit nbg=P2^3;
sbit nby=P2^4;
sbit nbr=P2^5;

bit flag1,flag2,flag3,flag4;/*以南北向红绿灯为基准调节时间,flag1红灯时间增加,flag2红灯时间增减少,flag3绿灯时间增加,flag4绿灯时间减少*/



void delay()
{
	uint i=1000;
	while(i--);
}

void init()
{
	P0M1=0x00;
	P0M0=0xff;
	P1M1=0x00;
	P1M0=0xff;
	P2M1=0x00;
	P2M0=0xff;
	AUXR&=0x7F;
	TMOD&=0xF0;
	TL0=0xC0;
	TH0=0x63;
	TR0=1;
	ET0=1;
	EA=1;
}

void light(char a)
{
	if(a=='r')
	{
		if((redt-time)>4)
		{
			nbr=1;
			nby=0;
			nbg=0;
			dxr=0;
			dxy=0;
			dxg=1;
		}
		if((redt-time)<=4)
		{
			nbr=1;
			nby=0;
			nbg=0;
			dxr=0;
			dxy=1;
			dxg=0;
		}
	}
	if(a=='g')
	{
		if((greent-time)>4)
		{
			nbr=0;
			nby=0;
			nbg=1;
			dxr=1;
			dxy=0;
			dxg=0;
		}
		if((greent-time)<=4)
		{
			nbr=0;
			nby=1;
			nbg=0;
			dxr=1;
			dxy=0;
			dxg=0;
		}
	}
}

void display_red()
{
	light('r');
	P1=select[0];
	P0=table[(redt-time)%10];
	delay();
	P1=select[1];
	P0=table[(redt-time)/10];
	delay();
	P1=select[6];
	P0=table[(redt-time)%10];
	delay();
	P1=select[7];
	P0=table[(redt-time)/10];
	delay();
}

void display_green()
{
	light('g');
	P1=select[0];
	P0=table[(greent-time)%10];
	delay();
	P1=select[1];
	P0=table[(greent-time)/10];
	delay();
	P1=select[6];
	P0=table[(greent-time)%10];
	delay();
	P1=select[7];
	P0=table[(greent-time)/10];
	delay();
}

void set_time()
{
	if(k1==0)
	{
		if(flag1==0)
		{
			flag1=1;
			redt++;
		}
	}
	if(k1==1)
	{
		flag1=0;
	}
	
	if(k2==0)
	{
		if(flag2==0)
		{
			flag2=1;
			redt--;
		}
	}
	if(k2==1)
	{
		flag2=0;
	}
	
	if(k3==0)
	{
		if(flag3==0)
		{
			flag3=1;
			greent++;
		}
	}
	if(k3==1)
	{
		flag3=0;
	}
	
	if(k4==0)
	{
		if(flag4==0)
		{
			flag4=1;
			greent--;
		}
	}
	if(k4==1)
	{
		flag4=0;
	}
}

void main()
{
	init();
	P1=0x00;
	redt=30;
	greent=30;
	while(1)
	{
		if(sign==0)
		{
			display_red();
		}
		if(sign==1)
		{
			display_green();
		}
		set_time();
	}
}

void timer0()interrupt 1
{
	uchar t0;
	t0++;
	if(t0==50)
	{
		t0=0;
		time++;
	}
	if(sign==1)
	{
		if(greent==time)
		{
			time=0;
			sign=0;
		}
	}
	if(sign==0)
	{
		if(redt==time)
		{
			time=0;
			sign=1;
		}
	}
}



单片机是真的很有意思,可惜这样的电子实习只有八个课时。

你可能感兴趣的:(单片机,算法,c语言)