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

📄 main.c

📁 可以读写ID卡,利用AVR单片机.可以响应按键
💻 C
📖 第 1 页 / 共 2 页
字号:

	//timer0_init
	TCCR0=0x00;            //停止定时器	
	TCNT0=0x00;            //初始值	
	TCCR0=0x06;            //1024分频   //06启动定时器下降沿

//	ASSR=8;                //启动异步记数功能
//	TCCR2=0x00;            //停止定时器	
	TCNT2=0x10;            //初始值	
	TCCR2=0x07;            //6 256分频   7;            //1024分频       Page110
//	OCR2=0x80;

	TIMSK=0x41;

}

void parainit(void)
{
#if WENDU	temp_error=1;
	lasttemp_c=1;
	temp_c=0x7ff;
#endif	
	dy=220;
	dl=1;
	pl=500;
	cddy=0;
	Getgl();
	
	seleLedv=0;
	seleLed=seleLed_buffer[seleLedv];
	runLed=0x10;
	oilLed=0;
	chaLed=0;
	
	pltc=0;    		//  频率时间记数=0
	status=STOP; 	//  正常工作状态
	curt[0]=0;		//  本次运行时间=0
	curt[1]=0;		//  本次运行时间=0
	curt[2]=0;		//  本次运行时间=0
	
	hour=0;     	//	时=0
	minute=0;   	//	分=0
	cminute=0;  	//	分=0
	second=0;   	//	秒=0
	count=0;    	//	计数=0
	
	curadch=4;
	maxdy=0;
	maxdy=0;

	mindl=1023;
	mindl=1023;

	maxv=0;
	minv=1023;

	dydlt=0;
	cf=1;
	
	first=0;
	posi=0;
	
	lastch=6;
	
	lum=700;
	
#if PJ
	dlp=0;
#endif
}

ISR(SIG_INTERRUPT0)
{
	Byte i;
	key&=2;
	for(i=0;i<10;i++)       //去抖动
	{
		if(((PIND>>PIND2)&1)==1)
			return;
		_delay_ms(1);
	}
	//while(((PIND>>PIND2)&1)==0) ;  //等待松开按键
	key|=1;
}

ISR(SIG_INTERRUPT1)
{
	Byte i;
	key&=1;
	for(i=0;i<10;i++)       //去抖动
	{
		if(((PIND>>PIND3)&1)==1)
			return;
		_delay_ms(1);
	}
	//while(((PIND>>PIND3)&1)==0) ;  //等待松开按键
	key|=2;
}

ISR (_VECTOR(3))
{
}


ISR (TIMER0_OVF_vect)
{
	if(status<SETUP)
	{
		pt2=TCNT2;
		pltcr=pltc;
		pltc=0;
		TCNT0=251;
		plcount=0;
		if(first==0)
			first=1;
		else
		{
			temppl=pltcr*125+pt2-lastpt2;      //39062.5   8000000  1024分频  记5个下降沿   8000000*5/1024
			//curpl=(390625/temppl+5)/10;
			curpl=(3906250/temppl+5)/10;
		}
		lastpt2=pt2;
	}
}

ISR (TIMER2_OVF_vect)
{
	TCNT2=131;   //125
	time=1;
	if(status<SETUP)
		pltc++;
}

//检测START  机油   蓄电池电压
void Detect(void)
{
	Byte it;
	it=PIND;
	if(status==STOP)          //加电未工作
	{
		startkey=(it&0x80);   //START按键
		if(startkey==1)
			startcount=0;
		else
		{
			iftest=(it&0x04);  //F1按键   按下为调试状态
		}
	}
	if((it&0x40)==0)           //机油检测
	{
		_delay_ms(5);
		if((it&0x40)==0)
		{
			oilLed=(1<<3);
			if(status==WORK)  //工作中
			{
				bhdy=dy;
				bhdl=dl;
				bhpl=pl;
				Stop();       //停止工作
			}
		}
	}
	else
		oilLed=0;
		
	if((status==WORK)&&((it&0x20)==0x20))          //飞轮马达检测   蓄电池充电故障指示
		chaLed=(1<<2);
	else
		chaLed=0;
}

void SPI_MasterTransmit(char cData)
{
/* 启动数据传输 */
	SPDR=cData;
/* 等待传输结束 */
	loop_until_bit_is_set(SPSR,SPIF);	// 等待数据传送完?
	//while(!(SPSR & (1<<SPIF)))
	//	;
}

void My_EEPROM_write(Byte uiAddress, Byte ucData)
{
	char cSREG;
	cSREG = SREG; /* 保存SREG */
/* 禁止中断*/
	asm("cli");
/* 等待上一次写操作结束 */
	while(EECR & (1<<EEWE))
		;
/* 设置地址和数据寄存器*/
	EEAR=uiAddress;
	EEDR=ucData;
/* 置位EEMWE */
	EECR |=(1<<EEMWE);
/* 置位EEWE 以启动写操作*/
	EECR |=(1<<EEWE);
	SREG = cSREG; /* 恢复SREG (I 位) */
}

Byte MY_EEPROM_read(Byte uiAddress)
{
/* 等待上一次写操作结束 */
	while(EECR & (1<<EEWE))
		;
/* 设置地址寄存器*/
	EEAR=uiAddress;
/* 设置EERE 以启动读操作*/
	EECR |=(1<<EERE);
/* 自数据寄存器返回数据 */
	return EEDR;
}

void Set_AD()
{
	ADMUX&=0xf0;
	ADMUX|=curadch;
	adm=0;
	ADCSRA|=(1<<ADSC);
}

void Show(void)
{
	Byte i;
	Byte flash=count/32;
	signed int temp;
	if(status==STOP)
	{
		for(i=0;i<11;i++)
			disnum[i]=NEG;
		if(dyerror)
		{
			if(flash==1)
			{
				GetD3(&disnum[0],bhdy);
				for(i=0;i<2;i++)
				{
					if(disnum[i]==0)
						disnum[i]=BLANK;
					else
						break;
				}
			}
			else
			{
				disnum[0]=BLANK;
				disnum[1]=BLANK;
				disnum[2]=BLANK;
			}
		}
		if(plerror)
		{
			if(flash==1)
			{
				if(pl==0)
				{
					disnum[6]=BLANK;
					disnum[7]=BLANK;
					disnum[8]=0;
				}
				else
				{
					GetD3(&disnum[6],bhpl);
					if(disnum[6]==0)
						disnum[6]=BLANK;
					disnum[7]|=0x80;
				}
			}
			else
			{
				disnum[6]=BLANK;
				disnum[7]=BLANK;
				disnum[8]=BLANK;
			}
		}
		if(dlerror)
		{
			temp=bhdl;
			if(flash==1)
			{
				if(temp==0)
				{
					disnum[9]=BLANK;
					disnum[10]=0;
				}
				else if(temp>99)
				{
					temp+=5;
					temp/=10;
					GetD2(&disnum[9],temp);
				}
				else
				{
					GetD2(&disnum[9],temp);
					disnum[9]|=0x80;        //小数点
				}
			}
			else
			{
				disnum[9]=BLANK;
				disnum[10]=BLANK;
			}
		}
	}
	else if(status==MIMA)
	{
		for(i=0;i<6;i++)
		{
			if((posi==i+1)&&(flash==1))
				disnum[i]=BLANK;
			else
				disnum[i]=mimainp[i+1];
		}
		disnum[6]=NEG;
		for(i=7;i<17;i++)
			disnum[i]=mima[i];
	}
	else if(status==SN)
	{
		for(i=0;i<7;i++)
			disnum[i]=NEG;
		for(i=7;i<17;i++)
			disnum[i]=mima[i];
	}
	else
	{
		temp=dy;
		if(((dyerror!=0)||((status==SETUP)&&(posi==0)))&&(flash==1))
		{
			disnum[0]=BLANK;
			disnum[1]=BLANK;
			disnum[2]=BLANK;
		}
		else
		{
			GetD3(&disnum[0],temp);
			for(i=0;i<2;i++)
			{
				if(disnum[i]==0)
					disnum[i]=BLANK;
				else
					break;
			}
		}

		if(((plerror)||((status==SETUP)&&(posi==1)))&&(flash==1))
		{
			disnum[6]=BLANK;
			disnum[7]=BLANK;
			disnum[8]=BLANK;
		}
		else
		{
			if(pl==0)
			{
				disnum[6]=BLANK;
				disnum[7]=BLANK;
				disnum[8]=0;
			}
			else
			{
				GetD3(&disnum[6],pl);
				if(disnum[6]==0)
					disnum[6]=BLANK;
				disnum[7]|=0x80;
			}
		}

		if((gl==0)&&(status!=SETUP))
		{
			disnum[3]=BLANK;
			disnum[4]=BLANK;
			disnum[5]=0;
		}
		else
		{
			GetD3(&disnum[3],gl);
			if((status==SETUP)&&(posi==2)&&(flash==1))
				disnum[3]=BLANK;
			if((status==SETUP)&&(posi==3)&&(flash==1))
				disnum[4]=BLANK;
			if(disnum[3]!=BLANK)
				disnum[3]|=0x80;        //小数点
			if(status==SETUP)
				disnum[5]=BLANK;
		}

		if((dlerror)&&(flash==1))
		{
			disnum[9]=BLANK;
			disnum[10]=BLANK;
		}
		else
		{
			temp=dl;
			if(temp==0)
			{
				disnum[9]=BLANK;
				disnum[10]=0;
			}
			else if(temp>99)
			{
				temp+=5;
				temp/=10;
				GetD2(&disnum[9],temp);
			}
			else
			{
				GetD2(&disnum[9],temp);
				disnum[9]|=0x80;        //小数点
			}
		}

	}
	
	if((status!=MIMA)&&(status!=SN))
	{
#if WENDU
		if(seleLed==OTHER)   //温度
		{
			if((temp_error)||(temp_c==0x7ff))
			{
				disnum[11]=NEG;
				disnum[12]=NEG;
				disnum[13]=NEG;
				disnum[14]=NEG;
			}
			else
			{
				temp=temp_c;
				if(temp<0)
				{
					temp*=-1;
					if(temp>=160)   //<-10度
					{
						disnum[11]=NEG;
						temp/=16;
						disnum[12]=(temp/10)%10;
						disnum[13]=0x80|(temp%10);
						temp=temp_c*5/8;
						disnum[14]=(temp)%10;
					}
					else
					{
						disnum[11]=BLANK;
						disnum[12]=NEG;
						temp*=10;
						temp/=16;
						disnum[13]=0x80|(temp/10)%10;
						disnum[14]=temp%10;
					}
				}
				else
				{
					if(temp>=1600)   //>100度
					{
						temp/=16;
						disnum[11]=(temp/100)%10;
						disnum[12]=(temp/10)%10;
						disnum[13]=0x80|(temp%10);
						temp=temp_c*5/8;
						disnum[14]=(temp)%10;
					}
					else
					{
						disnum[11]=BLANK;
						temp*=10;
						temp/=16;
						if(temp>100)
							disnum[12]=(temp/100)%10;
						else
							disnum[12]=BLANK;
						disnum[13]=0x80|(temp/10)%10;
						disnum[14]=(temp)%10;
						if(temp==0)
							disnum[13]=BLANK;
					}
				}
			}
		}
		else
		{
			if(seleLed==TOTAL)
			{
				GetD2(&disnum[11],totalt[1]);
				GetD2(&disnum[13],totalt[2]);
			}
			else
			{
				GetD2(&disnum[11],curt[1]);
				GetD2(&disnum[13],curt[2]);
			}
			for(i=11;i<14;i++)
			{
				if(disnum[i]==0)
					disnum[i]=BLANK;
				else
					break;
			}
		}
#else
		if(seleLed==TOTAL)
		{
			GetD2(&disnum[11],totalt[1]);
			GetD2(&disnum[13],totalt[2]);
		}
		else
		{
			GetD2(&disnum[11],curt[1]);
			GetD2(&disnum[13],curt[2]);
		}
		for(i=11;i<14;i++)
		{
			if(disnum[i]==0)
				disnum[i]=BLANK;
			else
				break;
		}
#endif			
		temp=cddy;
		if(temp>99)
		{
			temp/=10;
			GetD2(&disnum[15],temp);
		}
		else if(temp==0)
		{
			disnum[15]=BLANK;
			disnum[16]=0;
		}
		else
		{
			GetD2(&disnum[15],temp);
			disnum[15]|=0x80;
		}
	}
	
	if(status!=WORK)
		runLed=0x10;
	disnum[17]=seleLed+runLed+oilLed+chaLed;
	if((count/15)%2==1)
		disnum[17]&=0xf3;   //bit 2 3
	
	PORTB|=4;        //LATCH PB2=1
	SPI_MasterTransmit(~disnum[17]);
	for(i=16;i<200;i--)       //16~0
	{
		t=display_code[disnum[i]&0x1f];
		if(disnum[i]>0x7f)
			t&=(~2);
		SPI_MasterTransmit(t);
	}
	PORTB&=(~4);     //LATCH PB2=0
	_delay_us(2);
	PORTB|=4;        //LATCH PB2=1
}

#if WENDU
void reset_ds18b20(void)
{
	clr_ds18b20_dq();
	_delay_us(200);//100us
	_delay_us(200);//100us
	_delay_us(200);//100us
	set_ds18b20_dq();
	_delay_us(20);//20us
}

void ack_ds18b20(void)
{
#if 1
	clr_ds18b20_dq_ddr();           //READ 时临时置为输入
	_delay_us(20);//20us
	temp_error=(in_ds18b20_dq());
	_delay_us(20);//20us
	set_ds18b20_dq_ddr();           //READ 后重新置为输出
#else
#define DELAYT  1000
	Word i;
	clr_ds18b20_dq_ddr();           //READ 时临时置为输入
	for(i=0;i<DELAYT;i++)
	{
		if(!(in_ds18b20_dq()))
			break;
	}
	if(i==DELAYT)
		temp_error=1;

	for(i=0;i<DELAYT;i++)
	{
		if((in_ds18b20_dq()))
			break;
	}
	if(i==DELAYT)
		temp_error=1;
	_delay_us(20);//20us
	set_ds18b20_dq_ddr();           //READ 后重新置为输出
#endif
}

Byte read_ds18b20(void)
{
	Byte data=0;
	Byte i;
	for(i=0;i<8;i++)
	{
		data>>=1;
		clr_ds18b20_dq();
		_delay_us(1);//1us
		set_ds18b20_dq();
		_delay_us(10);//2us
		clr_ds18b20_dq_ddr();           //READ 时临时置为输入
		if(in_ds18b20_dq()) data|=0x80;
		_delay_us(50);//60us
		set_ds18b20_dq_ddr();           //READ 后重新置为输出
		_delay_us(2);
	}
	return(data);
}

void write_ds18b20(Byte data)
{
	Byte i;
	for(i=0;i<8;i++)
	{
		if(data&0x01){
			clr_ds18b20_dq();
			_delay_us(8);//8us
			set_ds18b20_dq();
			_delay_us(80);//55us
		}
		else
		{
			clr_ds18b20_dq();
			_delay_us(80);//55us
			set_ds18b20_dq();
			_delay_us(40);//8us
		}
		data>>=1;
	}	
}

void convert_ds18b20(void)
{
	reset_ds18b20();
    ack_ds18b20();
    _delay_ms(1);
    write_ds18b20(ds18b20_skip_rom);
    write_ds18b20(ds18b20_convert_tem);
}

void read_temperature(void)
{
	Byte tl;
	signed int temp;
	temp_error=0;
	reset_ds18b20();
	ack_ds18b20();
	if(!temp_error)
	{
		_delay_ms(1);
		write_ds18b20(ds18b20_skip_rom);
		write_ds18b20(ds18b20_read_ram);
		tl=read_ds18b20();
		temp=read_ds18b20();
//		temp=0;
//		tl=8;
		temp<<=8;
		temp+=tl;
//		temp-=(0x10*40);
		
		if((lasttemp_c==0)||((temp_c<temp+80)&&(temp<temp_c+80)))
		{
			temp_c=temp;
			lasttemp_c=1;
		}
		else
			lasttemp_c=0;
	}		
	convert_ds18b20();
}
#endif

void Stop(void)
{
	status=STOP;
	PORTC|=3;     //停止工作
	curadch=4;
	Set_AD();
	startcount=0;
}

void GetD3(Byte * dv,Word temp)
{
	dv[0]=(temp/100)%10;
	temp%=100;
	GetD2(&dv[1],temp);
}

void GetD2(Byte * dv,Byte temp)
{
	dv[0]=(temp/10)%10;
	dv[1]=(temp)%10;
}

Word Getdl()
{
	Long temp;
	temp=gl;
	temp*=100;
	temp/=dy;
	return temp;
}

void Getgl(void)
{
	Long temp;
	temp=dy;
	temp*=dl;
	temp+=50;
	temp/=100;
	gl=temp;
}

void testdy(void)
{							
#if BH
	if(dy>maxmdy)                             //超压
	{
		if(dyerror!=1)
		{
			if(ssecond<QDSJ)
				//dyerrt=CYSJ;
				dyerrt=ssecond-QDSJ;
			else
				dyerrt=0;
		}
		dyerror=1;
	}
	else if(dy<minmdy)                        //欠压
	{
		if(dyerror!=2)
		{
			if(ssecond<QDSJ)
				dyerrt=ssecond-QDSJ;
			else
				dyerrt=0;
		}
		dyerror=2;
	}
	else                                      //正常
	{
		dyerrt=0;
		dyerror=0;
	}
#endif								
}

void testdl(void)
{							
#if BH
	if(dl>maxmdl)           //超10%以上
	{
		if(dlerror!=1)
			dlerrt=0;
		dlerror=1;
	}
	else if(dl>mdl)         //超10%以内
	{
		dlerror=2;
	}
	else                    //正常
	{
		dlerrt=0;
		dlerror=0;
	}
#endif								
}

⌨️ 快捷键说明

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