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

📄 ok.txt.c

📁 这是一个基于51平台的调速系统的程序
💻 C
字号:
#include "reg51.h"
#include "intrins.h"
#include "absacc.h"
#include "math.h"
#define PA XBYTE[0x2101]
#define PB XBYTE[0x2102]
#define PC XBYTE[0x2103]
#define PCtr XBYTE[0x2100]
#define uchar unsigned char
#define uint unsigned int
static const uchar code table[]={48,49,50,51,52,53,54,55,56,57};
sbit P14=P1^4;
sbit RS=P3^2;
sbit RW=P3^1;
sbit E=P3^0;
#define dataport PA
#define busy 0x80
unsigned char key;
uchar Xpos,Ypos;
uint f=0;
uint hight=500;
uint count1=0;
uchar number;
uint fs=0,speed=80;
uchar flag=0;

void keyscan();
void delay10ms(unsigned char time);

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


void checkbusy(void)
{	
	P1=0x10;
	PCtr=0x00;
	RS=0;
	RW=1;
	_nop_();
	E=1;
	_nop_();
	_nop_();
	while(dataport&busy);
	E=0;
	PCtr=0x03;	
}

void writeIR(uchar CMD,uchar attribc)
{
	if(attribc) checkbusy();
	RS=0;
	RW=0;
	_nop_();
	dataport=CMD;
	_nop_();
	E=1;
	_nop_();
	_nop_();
	E=0;
}

void writeDDR(char c)
{
	checkbusy();
	RS=1;
	RW=0;
	_nop_();
	dataport=c;
	_nop_();
	E=1;
	_nop_();
	_nop_();
	E=0;
}

void lcdpos(uchar Xpos,uchar Ypos)
{
	uchar tmp;
	Xpos&=0x0f;
	Ypos&=0x01;
	tmp=Xpos;
	if(Ypos==1)
	tmp|=0xc0;
	tmp|=0x80;
	writeIR(tmp,0);
}

void lcdreset(void)
{
	writeIR(0x38,0);   
	writeIR(0x38,1);   
	writeIR(0x08,1);  
	writeIR(0x01,1);   
	writeIR(0x06,1);   
	writeIR(0x0c,1);  
}

void writechar(uchar Xpos,uchar Ypos,char c)
{
	lcdpos(Xpos,Ypos);
	writeDDR(c);
}


void writeString(uchar Xpos,uchar Ypos,char s[])
{
	uchar p=0;
	for(;;)
	{
		writechar(Xpos,Ypos,s[p]);
		p++;
		if(s[p]==0)
		break;
		if(++Xpos>=15)
		{
			Xpos=0;
			Ypos^=1;
		}
	}
}

void show(uint n,uchar m)                    //显示转换
{	
	uchar a,b,c;
	a=n/100;
	b=(n-a*100)/10;
	c=n%10;
	if(a!=0)
	{
		writechar(6,m,table[a]);
		writechar(7,m,table[b]);
	}
	if(b!=0)
	writechar(7,m,table[b]);
	writechar(8,m,table[c]);
}


void check(void)                  //判断按键
{	
	while(flag)
	{
		if(key==14)
		{	
			speed=fs;
		}
		else if(key<10)
		{	
			fs=10*fs+key;
		}
		else if(key==15)
		{fs=0;}
		flag=0;
	}
}


void display(void)                //液晶显示
{	
	uchar s1[]="Speed:";
	uchar s2[]="Set:";
	uchar s3[]="r/s";
	P1=0x10;
	PCtr=0x03;
	lcdreset();
	delay();
	writeString(0,0,s1);
	writeString(0,1,s2);
	writeString(10,0,s3);
	writeString(10,1,s3);
	check();
	show(fs,1);
	show(f,0);
	delay();
}

void keyscan()                       //键盘扫描
{	
	uchar temp;
	PB=0xfe;
	temp=PC;
	temp&=0x0f;
	if(temp!=0x0f)
	{
		delay10ms(10);
		temp=PC;
		temp&=0x0f;
		if(temp!=0x0f)
		{
			switch(temp)
			{
				case 0x0e:{key=1;flag=1;}break;
				case 0x0d:{key=4;flag=1;}break;
				case 0x0b:{key=7;flag=1;}break;
				case 0x07:{key=15;flag=1;}break;
			}
		
		}
	}	
	PB=0xfd;
	temp=PC;
	temp&=0x0f;
	if(temp!=0x0f)
	{
		delay10ms(10);
		temp=PC;
		temp&=0x0f;
		if(temp!=0x0f)
		{
			switch(temp)
			{
				case 0x0e:{key=2;flag=1;}break;
				case 0x0d:{key=5;flag=1;}break;
				case 0x0b:{key=8;flag=1;}break;
				case 0x07:{key=0;flag=1;}break;
			}
		
		}
	}
	PB=0xfb;
	temp=PC;
	temp&=0x0f;
	if(temp!=0x0f)
	{
		delay10ms(10);
		temp=PC;
		temp&=0x0f;
		if(temp!=0x0f)
		{
			switch(temp)
			{
				case 0x0e:{key=3;flag=1;}break;
				case 0x0d:{key=6;flag=1;}break;
				case 0x0b:{key=9;flag=1;}break;
				case 0x07:{key=14;flag=1;}break;
			}
			
		}
	}
	PB=0xf7;
	temp=PC;
	temp&=0x0f;
	if(temp!=0x0f)
	{
		delay10ms(10);
		temp=PC;
		temp&=0x0f;
		if(temp!=0x0f)
		{
			switch(temp)
			{
				case 0x0e:{key=10;flag=1;}break;
				case 0x0d:{key=11;flag=1;}break;
				case 0x0b:{key=12;flag=1;}break;
				case 0x07:{key=13;flag=1;}break;
			}
		
		}
	}	
}


void delay10ms(unsigned char time)
{
	int j;
	unsigned char i;
	for(i=0;i<time;i++)
	for(j=0;j<200;j++)
	;
}

void cesu(void)                //测速
{	
	unsigned char count0=0;
	EA=0;
	TMOD=0x51;
	TH1=0x00;TL1=0x00;
	TH0=0xd8;TL0=0xf0;        //计时周期10ms
	TR0=1;TR1=1;
	while(count0<25)
	{ 
		if(TF0==1)
		{
			TF0=0;
			TH0=0xd8;TL0=0xf0;
			count0++;	
		}
	}
				TR1=0;TR0=0;
				f=TH1;
				f=f<<8;
				f+=TL1;
}
			
			

void PWM()                      //脉宽调制
{	
	number=0;
	TMOD=0x02;       
	TH0=0xf6;
	TL0=0xf6;                  //计数0.01ms
	EA=1;
	ET0=1;
	TR0=1;
	while(number<30)
		{;}
	EA=0;
	
}

void tint(void) interrupt 1
{
	count1++;
	if(count1==hight) P14=0;
	else if(count1>=1000) 
	{	
		P14=1;
		count1=0;
		number++;
	}
}

void main()
{	
	uchar e;	
	uchar deta;
	while(1)
	{
		PWM();
		keyscan();
		cesu();
		display();
		e=abs(f-speed);
        if(e>25){deta=100;}
	    else if(e>15){deta=80;}
		else if(e>5){deta=40;}
		else {deta=10;}
		if(f>speed)		
		{hight-=deta;}
		else if(f<speed)
		{hight+=deta;}
	}	
}





⌨️ 快捷键说明

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