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

📄 passat_tester_second.c

📁 PIC16f876制作的空调控制器测试仪 源码 图纸全部资料
💻 C
字号:
#include<pic.h>              //包含单片机内部资源预定义
#include"Passat_tester.H" 
__CONFIG (0x3f32);  
//芯片配置字,看门狗关,上电延时开,掉电检测关,低压编程关,加密,4M晶体HS振荡

//------------------------------------------------
//--------应答位检查---------
/*
void  Cack(void)
{ SCL=1;
  SDAIO=1;//输入1
  SCLIO=0;
  err7=SDA;//c=1错误ERR
  SCL=0;
}
///--------发送非应答位---------- 
void  Mnack(void)
{  
   SDA=1;
   SCLIO=0;
   SDAIO=0;//输出
   SCL=1;
   NOP();
   NOP();
   SCL=0;
   SDA=0;
}
//-------发送应答位---------------
void  Mack(void)
{
  SDA=0;
  SCLIO=0;
  SDAIO=0;//输出
  SCL=1;
  NOP();
  NOP();
  SCL=0;
  SDA=1;
}
//--------停止IC总线---------------
void  Stop(void)
{
     SDA=0;
     SCLIO=0;
     SDAIO=0;//输出
     SCL=1;
     NOP();
     NOP();
     SDA=1;
     NOP();
     NOP();
}
//--------;启动IC总线-------------
void  Sta(void)
{ 
    SDAIO=0;//输出
    SCLIO=0;
    SDA=1;
    SCL=1;
    NOP();
    NOP();
    SDA=0;
    NOP();
    NOP();
    SCL=0;
}
    
//--------写一字节------------------------ 
void  Wrbyt(uchar x) 
{
  uchar i;
  acc=x;
  SCLIO=0;
  SDAIO=0;
  for(i=8;i>0;i--)
  {
   
   SDA=acc7;
   SCL=1;
   NOP();
   NOP();
   SCL=0;
   SDA=0; 
   acc=acc<<1;
   }
}
//---------读一字节-----------------
uchar   Rdbyt(void)
{
    uchar i;
    SCLIO=0;
    SDAIO=1;//;输入
    for(i=8;i>0;i--)
     {  
         acc=acc<<1;//接收数据左移寄存器 
         SCL=1;
         acc0=SDA;
         SCL=0;
         NOP();
       }
     return(acc); 
} 
////---------读n个数据----------------
void  Rdnbyt (uchar SLARtemp ,uchar Repromn,uchar temp2) //Repromn传送数据个数
  {   
     uchar i;
     i= temp2;
     do{
        Sta();           //启动IC总线-//     
        Wrbyt(SLARtemp); //写一字节 
        Cack();          //应答位检查-
       } while(err7==1);
     
       while(Repromn--) 
       { 
        Reeprom[i]=Rdbyt();  //读一字节
         i++;
         Mack(); //发送应答位
        } 
      Mnack();  // 发送非应答位
      Stop();   //停止IC总线
}
//---------写n个数据---------------
void  Wrnbyt(uchar SLAwtemp , uchar Wepromn,uchar temp1)
{  uchar i;
   i= temp1; 
    err7=1;

  while(Wepromn--)
     {
       if(err7==1)            //c=1E0,ERR
         {   i=temp1; 
             Sta();           //启动IC总线
             Wrbyt(SLAwtemp); //写一字节 
             Cack();          //应答位检查-
           }
          Wrbyt( Reeprom[i]);// Reeprom[t]);//写一字节 
          i++;
          Cack();//应答位检查
     } 
    Stop();   //停止IC总线    
}
*/
	//主程序开始
void main(void)
{
	 init();
	 system_initial();
	 beep(0x00);
	 refrush_display();
	while(1)
	{			
		key_proccess();	
	}	
}

void  DELAY(unsigned int delay_temp)              //延时程序
    {
     if(delay_temp>0)
     {
	     delay_temp--;
	}
    }
//-------------------------------------------------
void delay(uint control)
{
	unsigned int a=0x00,b=0x00;
	for(a=control;a>0;a--)
		{
			for(b=0x00;b<0xf;b++);
			if(!key_stop)break;
		}
}
 //-----------------------------------------------
 //初始化函数
 void  init(void)               
  {
	TRISB=0b11111001; 		  //RB2 is out other all input 1 is in 0 is out
	TRISC=0b00000000;
	PORTB=0xff;
	PORTC=0xff;
	TRISA=0b00000011;         //设置RA0为输入,RA1为输入,其他为输出
	//-----------------------------
	CCP2CON=0X0F;	//设置PWM模式
	PR2=0XFF;	//Timer2周期(占空比总长度)
   	T2CON=0X7F;
    CCPR2L=0x00;
	
	INTCON=0x00;			/* bit7-bit0:关总中断*/
	PIR1=0X00;	            //清除所有中断
	T1CON=0X01;	//使能Timer1定时器、内部时钟*/
//--------------------------------------------------
	CCPR2L=0x00;//主风扇测试涵数计数器PWM=0V
	//CCPR2L=0xff;//主风扇测试涵数计数器PWM=0V
//--------------------------------------------------            
	ADCON1=0X8E;              //转换结果左对齐,RA0做模拟输入口,其它做普通I/O
	ADCON0=0b01000001;        //系统时钟Fosc/8,选择RA0通道,允许ADC工作
	DELAY(100);                  //保证采样延时
   }
   //system initial
 void system_initial(void)
 {
	 beep_con_off;
	 led_reset;
	 relay_con_0;
	 EEDATA=0x00;            //数据赋初值
     EEADR=0x00;             //地址赋初值
	}
//----------------------------------------------

 void refrush_display(void)
 {	
	 char a=0x00;
	 for(a=0;a<9;a++)
	 {			
					   char num=0x00,c=0x00;
					   num=tab[disp_data_buffer[a]];
					   if(a==2|a==6)
					   {
						   num=num|0x80;//add point
					   }
					   for(c=0;c<8;c++)
					        {
								led_status_clk_0;
								nop();
								if(num&0x01)
									{
										led_status_dat_1;	
									}
								else led_status_dat_0;	
								led_status_clk_1;
								nop();
							  num>>=1;             
					        }	
	 }	 
 }
 void out_voltage_control(uchar level)
 {
	CCPR2L=PWM[level];
	}
 ///////////////////////////////////////////
 void auto_test_process(void)
 {
	 relay_con_1;
	 led_reset;
	 for(auto_step=1;auto_step<8;auto_step++)
		 {	
			out_voltage_control(auto_step);
			disp_data_buffer[0]=auto_step;
			//refrush_display();
			led_reset;
			delay(delay_rang_mid);
			ad_go(0x01);
			ad_go(0x00);	
			//quality_look(auto_step);
			/*
			if(quality_look(auto_step)==0)
			{
				refrush_display();
			}
			else if(quality_look(auto_step)==1)
			{
				uint current=0x00;
				current=disp_data_buffer[1]*1000+disp_data_buffer[2]*100+disp_data_buffer[3]*10+disp_data_buffer[4];
				AD_GET[0]=(current-30)/100.0;										
				data_caculator_change_format();
				refrush_display();
			}
			else if(quality_look(auto_step)==2)
			{
				uint current=0x00;
				current=disp_data_buffer[1]*1000+disp_data_buffer[2]*100+disp_data_buffer[3]*10+disp_data_buffer[4];
				AD_GET[0]=(current+30)/100.0;										
				data_caculator_change_format();
				refrush_display();
			}*/	
			refrush_display();		
			if(quality_look(auto_step)==3){auto_step=9;}//not necessary to add ;
	     }
	  CCPR2L=0x00;
	  relay_con_0;
	  hand_step=0;
}
 void hand_test_process(void)
 {			
	 		hand_step++;
	 		led_reset;
	 		relay_con_1;	
			out_voltage_control(hand_step);
			disp_data_buffer[0]=hand_step;
			disp_data_buffer[1]=disp_data_buffer[2]=disp_data_buffer[3]=disp_data_buffer[4]=0;
			disp_data_buffer[5]=disp_data_buffer[6]=disp_data_buffer[7]=disp_data_buffer[8]=0;
			refrush_display();
			delay(delay_rang_mid);
			ad_go(0x01);
			ad_go(0x00);	
			quality_look(hand_step);
			refrush_display();
			/*
			else if(quality_look(hand_step)==1)
			{
				uint current=0x00;
				current=disp_data_buffer[1]*1000+disp_data_buffer[2]*100+disp_data_buffer[3]*10+disp_data_buffer[4];
				AD_GET[0]=(current-30)/100.0;										
				data_caculator_change_format();
				refrush_display();
			}
			else if(quality_look(hand_step)==2)
			{
				uint current=0x00;
				current=disp_data_buffer[1]*1000+disp_data_buffer[2]*100+disp_data_buffer[3]*10+disp_data_buffer[4];
				AD_GET[0]=(current+30)/100.0;										
				data_caculator_change_format();
				refrush_display();
			}
			refrush_display();*/
	 		if(hand_step==7)hand_step=0;	   	
	}
//-----------------------------------------------
void key_proccess(void)
{
	while(key_auto==0)
	{
		DELAY(100);
		while(key_auto==0);
		beep(0x00);
		auto_test_process();
		refrush_display();
	}
	while(key_hand==0)
	{
		DELAY(100);
		while(key_hand==0);
		beep(0x00);
		hand_test_process();
		refrush_display();
	}
		while(key_save==0)
	{
		DELAY(100);
		while(key_save==0);
		beep(0x00);
	}
				if(!key_stop)
			{
				CCPR2L=0x00;//stop Fan	
				relay_con_0;
				beep_con_off;				
			}	
}
/////---------------------------------------
void ad_go(char channel)
{
	char a=0x00;
	unsigned short int temp[20];
	unsigned short int ad[2];
	INTCON=0X00;
for(a=0x00;a<20;a++)
{
		if(channel==0x01)
	{
		ADCON0=0b01001001;        //系统时钟Fosc/8,选择RA1通道,允许ADC工作
		DELAY(100);	
	}
	if(channel==0x00)
	{
		ADCON0=0b01000001;        //系统时钟Fosc/8,选择RA0通道,允许ADC工作
		DELAY(100);	
	}
		ADIF=0;       //清除A/D转换标志
		ADIE=0;
		PEIE=0;
		ADGO=1;       //启动A/D转换
		while(ADGO==1);
		adresult.ad[0]=ADRESL;
		adresult.ad[1]=ADRESH;
		//ad[channel]=adresult.Y1;
		temp[a]=adresult.Y1;
		delay(delay_rang_short);
}
ad[channel]=(temp[0]+temp[1]+temp[2]+temp[3]+temp[4]+temp[5]+temp[6]+temp[7]+temp[8]+temp[9]+temp[10]+temp[11]+temp[12]+temp[13]+temp[14]+temp[15]+temp[16]+temp[17]+temp[18]+temp[19])/20;
			if(channel)
			{
				AD_GET[channel]=ad[channel]*5.0/1024;    //得到真实电压
			}
			else 
			{
				AD_GET[channel]=ad[channel]*5.0*2.85/1024;	//得到真实电压
			}    											
			data_caculator_change_format();
}
//////////////////////////////////////////////////
void data_caculator_change_format(void)
{	
	uint temp;
	temp=(uint)(AD_GET[0]*100);
	disp_data_buffer[1]= temp/1000;
	disp_data_buffer[2]= temp%1000/100;
	disp_data_buffer[3]= temp%100/10;
	disp_data_buffer[4]= temp%10;//above channel 0 is motor feedback vol
	temp=(uint)(AD_GET[1]*100);
	disp_data_buffer[5]= temp/1000;
	disp_data_buffer[6]= temp%1000/100;
	disp_data_buffer[7]= temp%100/10;
	disp_data_buffer[8]= temp%10;//above channel 1 is motor control vol
}

//-----------------------------------------------
uchar quality_look(uchar step)
{	
	uint standder=0x00;
	uint current =0x00;	 
	current=disp_data_buffer[1]*1000+disp_data_buffer[2]*100+disp_data_buffer[3]*10+disp_data_buffer[4];
	 if(step==0x01)
	 {
		 standder=standder_data[0]*1000+standder_data[1]*100+standder_data[2]*10+standder_data[3];
	 }
	 if(step==0x02)
	 {
		 standder=standder_data[4]*1000+standder_data[5]*100+standder_data[6]*10+standder_data[7];
	 }
	 if(step==0x03)
	 {
		 standder=standder_data[8]*1000+standder_data[9]*100+standder_data[10]*10+standder_data[11];

	 }
	 if(step==0x04)
	 {
		 standder=standder_data[12]*1000+standder_data[13]*100+standder_data[14]*10+standder_data[15];
	 }
	 if(step==0x05)
	 {
		 standder=standder_data[16]*1000+standder_data[17]*100+standder_data[18]*10+standder_data[19];
	 }
	 if(step==0x06)
	 {
		 standder=standder_data[20]*1000+standder_data[21]*100+standder_data[22]*10+standder_data[23];
	 }
	 if(step==0x07)
	 {
		 standder=standder_data[24]*1000+standder_data[25]*100+standder_data[26]*10+standder_data[27];
	 }
	 
	if(((standder+rang_left_right)>current)&(current>(standder-rang_left_right)))
	 {
		 pass_led_status;
		 beep(0x00);     //pass
		 return 0;
   	 }
/*
	else if(((standder+50)>current)&(current>(standder-50)))
	 {
			 if(!(current>(standder-20)))
		{
			 pass_led_status;
			 beep(0x00);     //pass
			 return 2;
		}
			 if(!(current<(standder+20)))
		{
			 pass_led_status;
			 beep(0x00);     //pass
			 return 1;
		}
   	 } */
	else
	{
		 failure_led_status;
		 beep(0x01);     //fail
		 return 3;	
	}
}

void beep(uchar type)
{	
	switch(type)
	{
		case 0x00:
			{	
				beep_con_on;
				delay(delay_rang_mid);
				beep_con_off;
			}break;
		case 0x01:
			{
					beep_con_on;
					delay(delay_rang_mid);
					delay(delay_rang_mid);
					delay(delay_rang_mid);
					if(!key_stop)break;
					beep_con_off;
					delay(delay_rang_mid);
					if(!key_stop)break;
					beep_con_on;
					delay(delay_rang_mid);
					if(!key_stop)break;
					beep_con_off;
					delay(delay_rang_mid);
					beep_con_on;
					delay(delay_rang_mid);
					if(!key_stop)break;
					beep_con_off;	
			}break;
	default:break;
	}
}



  
  
  

⌨️ 快捷键说明

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