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

📄 lpc932_adc.c

📁 Keil ,LPC932系列环境下开发的频率测量单片机程序,用定时器来计脉冲数从而得出频率.
💻 C
字号:
#include <reg932.h>
#include <stdio.h>

//at 0xfd00 char code UCFG1=0x78; //SET UCFG1
extern void Get_SD_ADC();

void init(void);
void brkrst_init(void);
void Get_ADC(char channel);
void delay(char aaa);

long batt_slope();

sbit large_current=P1^7;
sbit red_led=P2^7;
sbit green_led=P0^6;
sbit rch_en=P2^6;
sbit batt_size=P2^0;
sbit batt_temp=P2^1;
unsigned int batt_v,timer_minute,current_monitor,time_delay;
unsigned int Result, i;
unsigned char a,b,c,p,timer_msecond,timer_second,slope_number;
int voltage;
unsigned long batt_v2,batt_slope_temp;

data char LowByte,HighByte,CMPx,CMPModex;
char data *CMPbufaddr,CMPbuf[2];

char aaa;
char ii;
main() 
{
	PT0AD=0xFE; //Disable digital inputs P0
	P0= 0xFF;
/*	P0M2= 0x041;    //P0.6  开漏
	P0M1= 0x07E;*/
	P0M2= 0x001;
	P0M1= 0x0FE;

	P1= 0x0FF;
	init();
   	brkrst_init();
	P2= 0x0FF;
	P2M2= 0x0C0;    //P2.7 开漏 P2.6推挽
	P2M1= 0x083;
	TMOD=0x52;     //T0  8位重装定时 T1 16位计数
	TL0=0xa4;      // T0 50us/times
   	TH0=0xa4;
	AUXR1|=0x10;    //ENT0 溢出输出
	TL1=0x30;		//100ms/times
	TH1=0xf8;
	timer_msecond=0;
	timer_second=0;
	timer_minute=0;
	
	TR0=1;
	TR1=1;
	ET1=1;	   
	EA=1;

	TI=1;

	for(ii=0;ii<4;ii++)
	{
	while(!TI);
	SBUF='T';
	TI=0;
	}
	while(1)
	{
		Get_ADC(0);	
	}



	while(1)
	{
	aaa=SBUF;
	while(!RI);
	RI=0;
	SBUF=aaa;
	while(!TI);
	TI=0;
}	
/*	while(1)
	{
		red_led=0;		//red led fight
		green_led=1;
		rch_en=0;
		delay(2);		//delay 200ms
		Get_ADC(0);	
	}*/
	



	for(;;)
	{
		a=0;
		b=0;
		c=0;
		timer_msecond=0;
		timer_second=0;
		timer_minute=0;
		red_led=1;		//red led fight
		green_led=0;
		rch_en=0;
		delay(2);		//delay 200ms
		Get_ADC(0);
		batt_v=Result;
		while(batt_v<1154)		//not find out batt  batt_v<=5v
		{
			rch_en=1;
			large_current=1;
			/*if(timer_minute>3)
			{
				rch_en=1;
				large_current=0;
				delay(2);
				Get_ADC(0);
				batt_v=Result;
				timer_minute=0;
				if(batt_v>1154)	break;
				else
				{
					while(batt_v<1154)
					{
						Get_ADC(0);
						batt_v=Result;
						timer_minute=0;
					}
				}
			}*/
			delay(10);
			rch_en=0;
			Get_ADC(0);
			batt_v=Result;
			timer_minute=0;
			
		}
		timer_msecond=0;
		timer_second=0;
		timer_minute=0;
		rch_en=1;
		large_current=1;
		red_led=1;			//yellow led fight
		green_led=1;
		slope_number=0;
		Get_ADC(0);
		batt_v2=Result;
		while(timer_minute<3)
		{
			Get_ADC(0);
			delay(10);
			if(Result>2310)		//>10v  batt take away
			{
				batt_slope_temp=10000;
				break;
				
			}
			if(batt_temp==0)
			{
				batt_slope_temp=20000;   //batt temp over 70
				break;
				
			}
			batt_v2=Result;
		}
		while(1)
		{
			batt_slope_temp=batt_slope();

			if(batt_slope_temp==30000)
			{
				break;
			}
			
			if(batt_slope_temp==20000) break;	//batt temp over 70
			if(batt_size==1)
			{
				if(timer_minute>80)
				{
					red_led=0;			//green led fight
					green_led=1;
					large_current=0;
					break;	
				}
			}
			else
			{
				if(timer_minute>60)
				{
					red_led=0;			//green led fight
					green_led=1;
					large_current=0;
					break;	
				}
			}
			if(batt_slope_temp==10000)		//batt take away
			{
				red_led=1;			//red led fight
				green_led=0;
				rch_en=0;
				large_current=0;
				break;	
			}
			if(batt_slope_temp==40000)
			{
				break;
			}
			
		}
		if((batt_slope_temp!=20000)&&(batt_slope_temp!=10000))
		{
			red_led=0;			//green led fight
			green_led=1;
			large_current=0;	//100mA recharge
			timer_msecond=0;
			timer_second=0;
			timer_minute=0;
			if(batt_size==1)
			{
				while(timer_minute<150)
				{
					//rch_en=1;
					//delay(1);
					//rch_en=0;
					if(batt_temp==0) batt_slope_temp=20000;
					if(batt_slope_temp==20000) break;	//batt temp over 70
					//delay(2);
					Get_ADC(0);
					batt_v=Result;
					if(batt_v>2307) batt_slope_temp=10000;
					if(batt_slope_temp==10000)		//batt take away
					{
						red_led=1;			//red led fight
						green_led=0;
						rch_en=0;
						large_current=0;
						break;	
					}
					//delay(6);
				}
				
				if(timer_minute>=150) batt_slope_temp=20000;
			}
			else
			{
				while(timer_minute<60)
				{
					//rch_en=1;
					//delay(1);
					//rch_en=0;
					if(batt_temp==0) batt_slope_temp=20000;
					if(batt_slope_temp==20000) break;	//batt temp over 70
					//delay(2);
					Get_ADC(0);
					batt_v=Result;
					if(batt_v>2307) batt_slope_temp=10000;
					if(batt_slope_temp==10000)		//batt take away
					{
						red_led=1;			//red led fight
						green_led=0;
						rch_en=0;
						large_current=0;
						break;	
					}
					//delay(7);
				}
				
				if(timer_minute>=60) batt_slope_temp=20000;
			}
		}
		if(batt_slope_temp==20000)
		{
			red_led=0;			//green led fight
			green_led=1;
			rch_en=0;
			large_current=0;
			Get_ADC(0);
			batt_v=Result;
			while(batt_v>1154)		//batt not take away
			{
				Get_ADC(0);
				batt_v=Result;
				delay(10);
				timer_minute=0;
			}	
		}
		
		
		
		
	
	/*	P1M2= 0xDD;
		P1M1= 0x22;
		SCON = 0x50; //serial port 19200 Baud
		TMOD |= 0x20;
		TH1 = 0xFB;
		TR1 = 1;
		TI = 1;
		printf ("LPC SD ADC\r\n");
		while (1)
		{
			for (i=0;i<4;i++)
			{
				Get_ADC(i);
				Result=LowByte + 256 * HighByte;
				Result= Result/10;
				printf ("%u ", Result);
			}
			printf ("\r\n");
		}*/
	
	}
	
}

void T1_int() interrupt 3
{
	TR1=0;
	TH1=TH1+0xf8;
	TL1=TL1+0x30;
	timer_msecond++;
	if(timer_msecond>9)
	{
		timer_msecond=0;
		timer_second++;
		if(timer_second>59)
		{
			timer_second=0;
			timer_minute++;
		}
	}
	TR1=1;
}

void init(void)
{
P1M1 = 0x00; //push pull except RX  P1.7 推挽
P1M2 = 0xFD;
}
void brkrst_init(void) //This function allows ISP entry through the
//UART break detect
{
AUXR1 |= 0x40; //enable reset on break detect
SCON = 0x50; //select the BRG as UART baud rate source
SSTAT = 0x00;
BRGR0 = 0xf0; //9600 BAUD at7.3728MHZ 
BRGR1 = 0x02;
//BRGR0=0xb0;	//9600	BAUD at(7.3728/4)MHZ		
//BRGR1=0;

//BRGR0 = 0x70; //9600 BAUD at 11.0592 MHz
//BRGR1 = 0x04;
BRGCON = 0x03; //enable BRG
}

void Get_ADC(char channel)
{
	if (channel & 1)
		CMPModex = 0x030; //CinB
	else
		CMPModex = 0x020; //CinA
	if (channel & 2)
		CMPbufaddr = &CMPbuf[1];
	else
		CMPbufaddr = &CMPbuf[0];
	Get_SD_ADC();
	Result=LowByte + 256 * HighByte;
	Result= Result/10;

	while(!TI);
	TI=0;
	SBUF=HighByte;

	while(!TI);
	TI=0;
	SBUF=LowByte;
	
}

void delay(char aaa)
{
	unsigned char b,a;
	for(b=1;b<=aaa;b++)
	{
		a=timer_msecond;
		while(a==timer_msecond);	
	}
	return;
}

long batt_slope()
{
	char i;
	unsigned long batt_buf[5];
	unsigned long slope,batt_v4,batt_v1,batt_v3;
	batt_v1=batt_v2;
	//if(timer_second>50) timer_second=0;
	a=timer_minute;
	i=0;
	while((a+3)>timer_minute)
	{
		Get_ADC(0);
		delay(10);
		if(Result>2310)		//>10v
		{
			slope=10000;
			return slope;
			
		}
		if(batt_temp==0)
		{
			slope=20000;
			return slope;
		}
		for(i=0;i<5;i++)
		{
			batt_buf[i]=Result;
			Get_ADC(0);
			delay(10);
		}
	
	}
	if(batt_buf[1]>batt_buf[0])
	{
		batt_v3=batt_buf[0];
		batt_buf[0]=batt_buf[1];
		batt_buf[1]=batt_v3;
		
	}
	if(batt_buf[2]>batt_buf[0])
	{
		batt_v3=batt_buf[0];
		batt_buf[0]=batt_buf[2];
		batt_buf[2]=batt_v3;
	}
	if(batt_buf[3]>batt_buf[0])
	{
		batt_v3=batt_buf[0];
		batt_buf[0]=batt_buf[3];
		batt_buf[3]=batt_v3;
	}
	if(batt_buf[4]>batt_buf[0])
	{
		batt_v3=batt_buf[0];
		batt_buf[0]=batt_buf[4];
		batt_buf[4]=batt_v3;
	}
	
	if(batt_buf[2]<batt_buf[1])
	{
		batt_v3=batt_buf[1];
		batt_buf[1]=batt_buf[2];
		batt_buf[2]=batt_v3;
	}
	if(batt_buf[3]<batt_buf[1])
	{
		batt_v3=batt_buf[1];
		batt_buf[1]=batt_buf[3];
		batt_buf[3]=batt_v3;
	}
	if(batt_buf[4]<batt_buf[1])
	{
		batt_v3=batt_buf[1];
		batt_buf[1]=batt_buf[4];
		batt_buf[4]=batt_v3;
	}
	
	batt_v4=(batt_buf[2]+batt_buf[3]+batt_buf[4])/3;
	if(batt_v4>batt_v1)
	{
		batt_v3=batt_v1;
		batt_v1=batt_v4;
		batt_v4=batt_v3;
		batt_v2=batt_v1;
		b=timer_minute;
		slope=0;
		
	}
	else
	{
		
		slope=batt_v1-batt_v4;
		c=timer_minute;
		if(slope>25) 
		{
			slope=30000;
			return slope;
		}
		else
		{
			if((c-b)>30)
			{
				slope=40000;
				return slope;
			}
		}
		slope=0;
	}
	batt_v2=batt_v1;
	
	return slope;
	
}

⌨️ 快捷键说明

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