⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 自动生升旗系统程序
💻 C
字号:



#include<at89x52.h>

sfr PortCode = 0x80;  // P0
sfr PortBit  = 0xa0;  // P2


#include <AT89X52.H>

extern void ee_write(unsigned int addr, unsigned char outdat);
extern unsigned char ee_read(unsigned int addr);

unsigned char zhengz[]={0xf1,0xf3,0xf2,0xf6,0xf4,0xfc,0xf8,0xf9};	 	// ** 正转
unsigned char fanz[]  ={0xf9,0xf8,0xfc,0xf4,0xf6,0xf2,0xf3,0xf1};	  	// ** 反转 

bit		run;			// ** 开启
bit		dir;			// ** 方向
unsigned int speed;		// ** 速度 
unsigned char tmp ;
unsigned int hight;		
unsigned char DispHight;
unsigned char SecCnt;
unsigned char Sec;

unsigned char code displaybit[9] = {
0x7f,0xbf,0xdf,0xef,0xf7,0xfb,0xfd,0xfe,0xff};       //数码管位分布(自左至右)

unsigned char code displaycode[] = {
0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};  //数码管字段0,1,2,3,4,5,6,7,8,9 

void delay(unsigned int Ms)
{
	unsigned char i;
	
	for( ; Ms != 0; Ms-- )
	{
		for(i = 1; i != 0; i-- );	
	}
}

void Display(unsigned long dat)
{
	unsigned char i;

	for(i = 0; i != 8; i++)
	{ 	
		PortBit = 0xFF;    //消除重影

		PortCode = displaycode[dat%10];

		if (i==3||i==4) PortCode = 0x2;

		PortBit = displaybit[i];
		dat /= 10;
		delay(255);	
	}
}

void initTimer(void)
{
	TMOD=0x11;
	TH0=0xff;
	TL0=0xff;
	TH1=0xff;
	TL1=0xff;
}

sbit half = P1^6;
bit changedir;
bit UpAndDown;

unsigned char cnt;
unsigned char time;

unsigned int  Cnt1Sec;
unsigned int  TimCnt;
unsigned char Current_Time;

main()
{
	initTimer();
	TR0=1;
	ET0=1;
	ET1=1;
	run = 0;
	tmp = 0;
	dir = 0;
	EA=1;	
	hight = ee_read(9)*60.6667+1;
	half = 1;
	time = 43;	
	Cnt1Sec = 10010/time;
	speed = 65535-101.01*time;	
	P3_7=1;
	while(1)
	{
		if (hight!=10010 && P3_0==0)
		{
			delay(100);
			if (P3_0==0)	// ** 上升
			{
				
				run = 1;	
				UpAndDown = 1;
				changedir = 0;
				if(time==43&&P3_0==0)
				P3_7 = 0;

			}
		}
		else if (hight!=0&&P3_1==0)
		{
			delay(100);
			if (P3_1==0)	// ** 下降
			{
				run = 1;
				UpAndDown = 0;
				changedir = 0;
			}
		}
		else if (P3_2==0)
		{
			delay(100);
			if (P3_2==0)	// ** 停止
			{
				EA = 0;
				run = 0;				
				ee_write(9,hight/60.6667);
				EA = 1;
				Sec = 0;
			}
		}

		if (P3_3==0)
		{
			delay(2);
			cnt++;
			if (P3_3==0 &&cnt==4)
			{
				cnt = 0;
				if (time<120)time++;
				speed = 65535-101.01*time;  //40s走9900步,1毫秒走40000/9900=101.01
				Cnt1Sec = 10010/time;
			}
		}
		if(P3_4==0)
		{
			delay(2);
			cnt++;	
			if (P3_4==0&&cnt==4)
			{
				cnt = 0;
				if (time>30)time--;
				speed = 65535-101.01*time;  
				Cnt1Sec = 10010/time;
			}
		}

		if (!half && (!run))	// ** 显示速度--当前高度
			Display(time*100000+hight/60.6667+15);
		else				// ** 显示时间--当前高度
			Display(Current_Time*100000+hight/60.6667+15);	  //  10010/165=60.6667
		
	}
}
void timer0(void) interrupt 1
{
	TH0=speed/256;
	TL0=speed%256;		

	if (run)
	{
		TimCnt++;
		if (TimCnt==Cnt1Sec){Current_Time++;TimCnt=0;}		  //时间显示:Cnt1Set1s走那么多步,10010/time

		if(!half)			// ** 半旗
		{
			if (UpAndDown)	// ** 上升
			{
				if(changedir==0)
				{
				hight++;
				dir = 1;
				if(changedir==0&&time==43) 
				P3_7=0;
				}
				else {hight--;dir = 0;P3_7=1;}

				if (changedir && hight == 6370)	// ** 降到120cm
				{
					run = 0;		// ** 停止						
					ee_write(9,hight/60.6667);
				}

				if (hight==10010)	// ** 到达顶端
				{
					changedir = 1;					
				}
			}
			else		// ** 下降
			{
				if(changedir==0){hight++;dir = 1;P3_7=1;}
				else {hight--;dir = 0;}
				if (hight==10010)	// ** 到达顶端	
				{
					changedir = 1;										
				}
				if (changedir && hight == 0)	// ** 降到120cm
				{
					Current_Time=0;
					run = 0;		// ** 停止	
					ee_write(9,hight/60.6667);	
				}
			}
		}
		else
		{
			if (UpAndDown)	// ** 上升
			{
				hight++;
				dir = 1;
				if (hight==10010)	// ** 到达顶端
				{
					run = 0;		// ** 停止	
					ee_write(9,hight/60.6667);
					P3_7 = 1;
				}
			}
			else		// ** 下降
			{
				hight--;
				dir = 0;
				if (hight==0)	// ** 到达顶端
				{
					run = 0;		// ** 停止	
					ee_write(9,hight/60.6667);
				}
			}
	    }
		if (P3_5==0&&UpAndDown){ee_write(9,hight/60.6667);run = 0;}
		if (P3_6==0&&(!UpAndDown)){run=0;hight=0;Current_Time=0;ee_write(9,hight/60.6667);}
	}
	if (!run) P1 = 0xFF;
	else
	{
		if (dir)
		P1 = zhengz[tmp];	
		else
		P1 = fanz[tmp];		
	   	tmp ++ ;
		if (tmp>=8)tmp = 0;
	}	
}



⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -