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

📄 main1.c

📁 自动检测电容值
💻 C
字号:
#include <reg52.h>
//#include <string.h>
#include <intrins.h>
#include <stdio.h>
#include <borad.h> 
#include <SERIAL.H>


#define uint  unsigned int
#define uchar unsigned char
#define CYCLE (-10000)
#define STD_RESISTOR 1056
#define MIN_RESISTOR (STD_RESISTOR*0.82+0.5)
#define MAX_RESISTOR (STD_RESISTOR*1.18+0.5)
#define STD_CAPACITOR 500
#define MIN_CAPACITOR (STD_CAPACITOR-180)
#define MAX_CAPACITOR (STD_CAPACITOR+180)

//#include  "VI2C_C51.h"
//#include "ocm4x8c串.h"

//#define Debugging 1


//sbit  Rf=P3^2;
//sbit  Cf=P3^3;

sbit  BELL=P3^5;

sbit  DAT=P1^0;
sbit  CLK=P1^1;
sbit  CS=P1^2;

sbit  LCM_CS=P1^5;
sbit  LCM_STD=P1^6;
sbit  LCM_SCLK=P1^7;
sbit  LCM_RST=P1^4;
bit NormalTest=0;

sbit  RA=P2^0;
sbit  RB=P2^1;
sbit  RC=P2^2;
sbit  CA=P2^3;
sbit  CB=P2^4;
#define GATE0 0x8

sbit  STEP=P0^1;
sbit  AUTOSTEP=P0^2;

//sbit  Mouse1=P0^5;
//sbit  Mouse2=P0^6;
//sbit  Mouse3=P0^7;

//sbit  MD=P0^4;

//bit   TestModel;

//uchar OKLED;
//uchar NGLED;

#define OK 33
#define NG 44
//uchar   Test_timer;
//uchar   Test_OKNG;
//uchar   Test_012;

 
//uchar Test_Order;
//uchar STEP_check;
//uchar cycle;
//uchar BELL_temp;
//uchar BELL_Flash;
//#define BELL_timer 6
//uint R1,R2,R3,R4,C1,C2;
unsigned int TestData[6];
bit StartTest=1;
uchar tdTH,tdTL;
uchar tn;
uchar tc;
uint wtt;
char OkLed;
#define Led0 1
#define Led1 2
#define Led2 4
#define Led3 8
#define Led4 16
#define Led5 32
#define ALLOK 64


//uint RC_timer;
//uint RCr_timer;


//#define FN 12
//uint RCtemp[FN];
 void Test(char);
 void DiaplayData(char);

//LED显示
void  LedSend8(char d) 
{
	uchar i;
	CLK=0;
	for(i=0;i<8;i++)
	{
		 //if((NGLED>>(6-i))&1) DAT=1;  
	     //else  DAT=0; 
		 d<<=1;
		 DAT=CY;delay (10); 	 
	     CLK=1;delay (10);   
	     CLK=0;delay (10);
	}
}
void  LedShow(char hb,char lb) 
{
	CS=0;
	delay(10);
	hb>>=1;
	if(CY)
	{
		lb|=0x80;
	}
	else
	{
		lb&=~0x80;
	}
	LedSend8(hb);	
	LedSend8(lb);	
	CS=1;
	delay (20);
	CS=0;
}
void LedTest(void)
{
	char d;
	char i;
	d=1;
	for(i=0;i<7;i++)
	{
		LedShow(0xff,~(d));DelayMs(600);
		d<<=1;
	}
	d=1;
	for(i=0;i<7;i++)
	{
		LedShow(~(d),0xff);DelayMs(600);
		d<<=1;
	}
	LedShow(0,0);DelayMs(1200);
	LedShow(0x7f,0x7f);
}
void Bell(unsigned int t)
{
	BELL=0;
	DelayMs(t);
	BELL=1;
}

///外部中断0
void Timer2 (void) interrupt TIMER2INT   // using 1
{
//	TL1=CYCLE % 256;
//	TH1=CYCLE/256;	
	TF2=0;
	EXF2=0;
	if(wtt) wtt--;
	if(td) td--;
}
void Test(char i)
{
	bit rc;
	static unsigned long stdR=10000,stdC=10000;
	long ul;
	CB=0;
	switch(i)
	{
	case 0:
	case 1:
	case 2:
	case 3:
	case 6:
		TMOD|=GATE0;
		rc=1;
		CA=0;
		tc=0;
		break;	//R:rc=1 C:rc=0
	default:
		TMOD&=~GATE0;
		CA=1;
		tc=1;
		rc=0;
		break;
	}

//	TR0=0;
//	TMOD=0X9;	//timer1:mode1,GATE1=0,CT1=0;timer0:mode1,GATE0=1,CT0=0;
	i&=0x7;
	P2&=~0x7;
	P2|=i;
	EX1=ET0=TR0=0;
	tn=TH0=TL0=0;
	if(!rc) CB=1;
	DelayMs(10);
	//begin Test
	if(rc)
	{
		ET0=1;
		TR0=1;
		CB=1;
	}
	StartTest=0;
	wtt=80;  //等待0.4秒
	i=0;
	while(wtt)
	{
//		if(!StartTest) break;
		if(!INT1)
		{
			i++;
			if(i>100)
			{
				if(StartTest)
				{
					
				}
				else
				{
					StartTest=1;
					i=0;
					EA=0;
				}
			}
			else
			{
				i=0;
			}
		}
		else
		{
			continue;
		}
	}
	ul=256L*(tn*256L+TH0)+TL0;
	switch(i)
	{
	case 4:if((ul-=24890)<0) ul=0;break;
	case 5:if((ul-=24870)<0) ul=0;break;
	case 7:if((ul-=23650)<0) ul=0;stdC=ul;break;
	case 6:stdR=ul;break;
	}
	if(i<6)
	{
		if(i<4)
		{
			TestData[i]=1000.0*ul/stdR+0.5;
		}
		else
		{
			TestData[i]=1000.0*ul/stdC+0.5;
		}
	}
	CB=0;
	StartTest=0;
}
void Check(char i)
{
	//char i;
	//OkLed=0;
	if(i<6)
	{
		if(i<4)
		{
			if(TestData[i]>MIN_RESISTOR&&TestData[i]<MAX_RESISTOR)
			{
				OkLed|=1<<i;
			}
		}
		else
		{
			if(TestData[i]>MIN_CAPACITOR&&TestData[i]<MAX_CAPACITOR)
			{
				OkLed|=1<<i;
			}
		}
		LedShow((OkLed)|(0xfe<<i),(~(OkLed))|(0xfe<<i));
	}
	else
	{
		if((OkLed&=0x3f)==0x3f)
		{
			OkLed|=ALLOK;
		}
		LedShow(OkLed,~OkLed);
	}
}
void DisplayData(char i)
{
	unsigned int n;
	char bf[5];
	char j;
	n=TestData[i];
	bf[4]=0;
	if(n>2380)
	{
		bf[0]=bf[1]=bf[2]=bf[3]='-';
	}
	else
	{
		for(j=3;j>0;j--)
		{
			bf[j]=n%10+'0';
			n/=10;
		}
		bf[0]=n+'0';
		if(i<4)
		{
			if(bf[0]!='0')
			{
				bf[3]=bf[2];
				bf[2]='.';
			}
			else
			{
				bf[0]=bf[1];
				bf[1]='.';
			}
		}
		else
		{
			for(j=0;j<3;j++)
			{
				if(bf[j]=='0')
				{
					bf[j]=' ';
				}
				else
				{
					break;
				}
			}
		}
	}
/*	switch(i)
	{
	case 0:LcdPrint("R1=");LcdPrint(bf);LcdPrint(" ");break;
	case 1:LcdPrint("R2=");LcdPrint(bf);LcdPrint("\n");break;
	case 2:LcdPrint("R3=");LcdPrint(bf);LcdPrint(" ");break;
	case 3:LcdPrint("R4=");LcdPrint(bf);LcdPrint("\n");break;
	case 4:LcdPrint("C1=");LcdPrint(bf);LcdPrint(" ");break;
	case 5:LcdPrint("C2=");LcdPrint(bf);LcdPrint("\n");break;
	}  */
	switch(i)
	{
	case 0:LcdPrint("R1=");LcdPrint(bf);LcdPrint(" ");ES=0;printf("R1=  %5.2fMohm\n",TestData[0]/100.0);break;
	case 1:LcdPrint("R2=");LcdPrint(bf);LcdPrint("\n");ES=0;printf("R2=  %5.2fMohm\n",TestData[1]/100.0);break;
	case 2:LcdPrint("R3=");LcdPrint(bf);LcdPrint(" ");ES=0;printf("R3=  %5.2fMohm\n",TestData[2]/100.0);break;
	case 3:LcdPrint("R4=");LcdPrint(bf);LcdPrint("\n");ES=0;printf("R4=  %5.2fMohm\n",TestData[3]/100.0);break;
	case 4:LcdPrint("C1=");LcdPrint(bf);LcdPrint(" ");ES=0;printf("C1=  %5uPF\n",TestData[4]);break;
	case 5:LcdPrint("C2=");LcdPrint(bf);LcdPrint("\n");ES=0;printf("C2=  %5uPF\n",TestData[5]);break;
	}
}
void extint1(void) interrupt INT1INT  // using 2
{
//	if(tc) tc--;
	if(tc)
	{
		TR0=1;
		ET0=1;
		tc--;
	}
	else
	{
		TR0=0;
		StartTest=0;
		EX1=0;
		ET0=0;
	}
}
void Timer0 (void) interrupt TIMER0INT // using 1
{
	tn++;
}
bit Remote(void)
{
	char Key;
	if(RI)
	{
		Key=SBUF;
		RI=0;
	}
	if(Key=='S')
	{
		Key=0;
		return(1);
	}
	else
	{
		return(0);
	}
}
void WaitKey(void)
{
	char i;
	printf("Waiting Key\n");
	while(!STEP)
	{
		if(Remote()) return;
	}
	while(1)
	{
		if(Remote()) return;
		if(STEP)
		{
			i=0;
		}
		else
		{
			i++;
			DelayMs(10);
		}
		if(i>3) break;
	}				
}
bit CheckAuto(bit AUTO)
{
	char i,j;
	i=0;j=0;
	while(AUTO!=AUTOSTEP)
	{
		DelayMs(10);
		if(++i>3)
		{
			AUTO=AUTOSTEP;
			break;
		}
		if(++j>20)	break;
	}
	return(AUTO);
}
void main(void)
{
	char i;
  	bit AUTO=1;

//  init_serialcomm();  //初始化串口
//  init_timer();
  	Initial();

//  EA=1;

  
//  	init_lcd();
	LedShow(0xff,0xff);
//  	P2=0xe0;
	LcdClear();
  	LcdPrint("简易高压测试仪:\nV1.10 2007-5-11\n  珠海晶研佳信\nTel:0756-3366219" );
//  	ES=0;printf("简易高压测试仪:\n");
//	ES=0;printf("V1.10 2007-5-11\n");
//	ES=0;printf("  珠海晶研佳信\n");
//	ES=0;printf("Tel:0756-3366219\n" );
//   	P2=0;
	NormalTest=0;
	LedTest();
	Bell(1000);
	while(1) Test(7);
	Test(7);
	Test(7);
	Test(6);
	Test(6);
	Test(6);
	while(1){Test(7);Test(4);Check(4);DisplayData(4);LcdPrint("\n\n\n\n");} 

	while(1)
	{	
		ES=0;printf("Parameter:\n");
		ES=0;printf("MinR=  %5.2fMohm\n",MIN_RESISTOR/100.0);
		ES=0;printf("MaxR=  %5.2fMohm\n",MAX_RESISTOR/100.0);
		ES=0;printf("MinC=  %5uPF\n",MIN_CAPACITOR);
		ES=0;printf("MaxC=  %5uPF\n",MAX_CAPACITOR);
		WaitKey();
		Bell(200);
		LedShow(0xff,0xff);
		LcdClear();
		LcdPrint("\n   * Testing *");
		AUTO=CheckAuto(AUTO);
		ES=0;printf("TestBegin\n{\n");
		if(!AUTO) LcdClear();
		OkLed=0;
		for(i=0;i<6;i++)
		{
			Test(i);Check(i);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
			if(!AUTO)
			{
				DisplayData(i);	
				if(i<5) WaitKey();
			}
		}
		Check(6);
		NormalTest=1;
		if(AUTO)
		{
		    LcdClear();
			for(i=0;i<6;i++)
			{
				DisplayData(i);
			}
		}
		if(OkLed&ALLOK)
		{
			LcdPrint("  *** PASS ***");
			ES=0;printf("PASS\n");
			Bell(800);
		}
		else
		{
			LcdPrint("    !!FAIL!!");
			ES=0;printf("Fail\n");
			Bell(100);DelayMs(100);Bell(100);
		}
		ES=0;printf("}\nTestEnd\n\n");
		Test(7);
		Test(7);
		Test(7);
		Test(6);
		Test(6);
		Test(6);
	}
  //chn_disp3("  珠海晶研力信  \n",0x80);
  //chn_disp3("  简易高压测试仪\n",0x90);
  //chn_disp3("            V2.1\n",0x98);

  //ES=0;printf("珠海晶研力信自动化设备有限公司\n" );
  //ES=0;printf("简易高压测试仪 V2.1\n\n" );
/*
  for(i=0;i<7;i++)
   {
	  OKLED=NGLED=~(1<<i);
	  LEDshow();
	  BELL=0;
	  delay (0xffff);
	  OKLED=NGLED=0xff;
	  LEDshow();
	  BELL=1;
	  delay (0xffff);
	}


   	for(j=0;j<250;j++)
   {
	 if(MD==0)
	 {
	   if(++i==50)
	   {
		 P2=0; 
		 while(1);
	   }
	 }

   }
  	//clrram();
	//chn_disp3("  STANDBY   \n",0x80);


 while(1)
 {

	switch(Test_Order)
	 {
	  case 0:
			if(AUTOSTEP)TestModel=1;	//自动
            else        TestModel=0;	//单步

	         STEP_check=100; //测试

	       if(STEP==0)
			 { 
			    if(STEP_check<0xff) STEP_check++;
			 }
			 else 		 
			 {
			   if(STEP_check>50)
				{
				  STEP_check=0;

			  	  P2=0xf8;
				//  P2=1;
                  Test_Order=1;
				  Test_012=0;
				  //clrram();
				  Test_GO();
				}
                STEP_check=0;
			  }	 
			  OKLED=0xff;
              NGLED=0xff;
			 LEDshow();
	  break;
	  case 1:    
	  case 2:
	  case 3:
	  case 4: 
	  case 5: 
	  case 6:
             
			 Test(Test_Order);
	  break;
	  }


 }
*/
} 

void Initial(void)
{
	EA=0;
	P0=0xff;
	IT0=IT1=1;
	P1=P2=P3=0xff;
	INPORT=0XFF;
	TMOD=0X1;	//timer0:mode1(low 4bit)
	EX0=0;

//	ET1=1;
//	TR1=1;
//	TL1=CYCLE % 256;
//	TH1=CYCLE/256;
	TL2=RCAP2L=CYCLE % 256;
	TH2=RCAP2H=CYCLE/256;
	T2CON=0;
	TR2=1;
	ET2=1;

    SCON  = 0x50;       //串口模式:  模式1 接收允许
    TMOD |= 0x20;       //定时器模式:定时器工作在模式2
    PCON |= 0x80;       //SMOD=1; 倍波特率
    TH1   = 0xF3;       //Baud:9600fosc=24MHz
   	//ES    = 1;          //串行中断允许
    TI = 1;
    TR1 = 1;          //定时器1启动 

	EA=1;
} 
/*void Time10ms (void) interrupt TIMER0INT using 1
{
	static Char CtrlP=0

//	#define FLASHTIME 60
//	static unsigned char ft=FLASHTIME;
//	static char InP1=0,InP2=0;
//	char InP,InP3;
	
	//TL0=CYCLE % 256;
	//TH0=CYCLE/256;

	InP=INPORT^P2;
	InP3=InP&InP2;			//count 3
	InP2=InP&InP1&(~InP3);
	InP1=InP&(~InP3);
	InP^=INPORT;			//InP=PP1
	InP&=InP3;
	INPORT&=~InP3;
	INPORT|=InP;

	Next=Stop=!(INPORT&0x40);
	Start=!(INPORT&0x80);	
	//if(td) td--;	
	//return;
}  */
void LcdClear(void)
{
	init_lcd();
	CurrentX=CurrentY=0;
}
void LcdPrint(char * DisBuf)
{
	code unsigned char Line[]={0x80,0x90,0x88,0x98}; 
	char kk=0;
//	ES=0;printf(DisBuf);
	wr_lcd(comm,0x30);
	while(DisBuf[kk])
	{
		if(DisBuf[kk]=='\n'||CurrentX>=16)
		{
			CurrentX=0;
			CurrentY++;
			if(CurrentY<4) wr_lcd(comm,Line[CurrentY]);
		}
		else
		{
			if(CurrentY>=4)
			{
				LcdClear();
			}
			wr_lcd(dat,DisBuf[kk]);
			CurrentX++;
		}
	 	kk++;
	}
	return;
}
/*bit StartCheck(void)
{
	static unsigned char cn=0;
	bit NowInst=0;
	static bit CurInst=0;
	char pt;
	P0=0xfc;
	DelayMs(10);
	if((pt=P1&0xfc)^0xfc)
	{
		P0=0;
		DelayMs(10);
		if(pt!=(P1&0xfc)) NowInst=1;
	}
	else
	{
		P0=0;
		DelayMs(10);
	}
	if(NowInst!=CurInst)
	{
		cn++;
		if(cn>=30)
		{
			CurInst=NowInst;
			cn=0;
//			if(CurInst) return(1);
		}
	}
	else
	{
		cn=0;
	}
//	return(0);
	if(CurInst)
	{
//		CurInst=0;
		return(1);
	}
	else
	{
		return(0);
	}
}
bit keyWait(bit chk)
{
	static bit NextUp=0,StartUp=0,CheckUp=0;
	if(chk)
	{
		if(Next)
		{
			if(NextUp)
			{
				NextUp=0;
				return(0);   //Next OK
			}
		}
		else
		{
			NextUp=1;
		}
	}
	if(Start)
	{
		if(StartUp)
		{
			StartUp=0;
			longjmp(env,2);   //Start OK
		}
	}
	else
	{
		StartUp=1;
	}
	if(StartCheck())
	{
		if(CheckUp)
		{
			CheckUp=0;
			longjmp(env,2);   //Start OK
		}
	}
	else
	{
		CheckUp=1;
	}
	return(1);
}
*/
void DelayMs(int a)
{
	td=a/5;
	while(td);
} 


/*------------------初始化-----------------*/
void init_lcd (void)
{
  rst=1;
 // psb=0;
  wr_lcd (comm,0x30);  /*30---基本指令动作*/   
  wr_lcd (comm,0x01);  /*清屏,地址指针指向00H*/
  delay (100);
  wr_lcd (comm,0x06);  /*光标的移动方向*/
  wr_lcd (comm,0x0c);  /*开显示,关游标*/
}

/*----------------------------------------------*/
void wr_lcd (uchar dat_comm,uchar content)
{
  uchar a,i,j;
  delay (50);
  a=content;
  cs=1;
  sclk=0;
  std=1;
  for(i=0;i<5;i++)		//先输入五个连续的1   -----》是同步位字符串,此时输入记数被重置并且串行传输被同步。
  {
    sclk=1;
    sclk=0;
  }
  std=0;				//RW位
  sclk=1;
  sclk=0;
  if(dat_comm)		   //RS位
    std=1;   //data
  else
    std=0;   //command
  sclk=1;
  sclk=0;			   //第8位规定为0
  std=0;
  sclk=1;
  sclk=0;
  for(j=0;j<2;j++)
  {
    for(i=0;i<4;i++)   //每一个8位指令将被分为两个字节接收到.
    {
      a=a<<1;
      std=CY;
      sclk=1;
      sclk=0;
    }
    std=0;
    for(i=0;i<4;i++)
    {
      sclk=1;
      sclk=0;
    }
  }
}
/*-----------------------------------*/
void delay (uint us)   //delay time
{
  while(us--);
}

⌨️ 快捷键说明

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