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

📄 adc8.c

📁 门禁系统 interated with LCD
💻 C
字号:
//Sample program for AdvFamLab
//Include required header files here
// ADC2 is the temperature sensoring and fire alarm
// ADC3 is the Ambient control setting code
// ADC4 is the combine of both ADC3 and ADC2
// ADC5 include port to port communication 
// ADC6 include the fan speed control
// ADC8 is the final version

#include<pic1687x.h>
#include<pic.h>
#include"delay.c"
//Declare global variables here
volatile int on_time_counter, off_time_counter;
volatile unsigned char light;
int timer_value=0xFE;
//Declare functions to be used in the program, i.e.
//void initialize_IO_ports(void)
//void initialize_ADC(void)
//int read_ADC_channel(unsigned int)
//void initialize_timer1(int)
//Put the body of all the functions
void initialize_IO_ports(void)
{
//set the digital IO ports as per requirement
TRISA = 0xFF ; //portA as input
TRISB = 0xFF ; //portB as input
TRISC = 0x00 ; //portC as output
TRISD = 0x00 ; //portD as output
ADCON1= 0x82 ; //set PortE as digital io and portA as analog, and VDD and VSS
//clear the output ports at the beginning
PORTD = 0x00 ; //clear portD
PORTC = 0x00 ; //clear portC
}
void initialize_ADC(void)
{
//enable the ADC
ADON=1;
//set clock source for the ADC
ADCS1=1;
ADCS0=0;
}
int read_ADC_channel(unsigned int channel_number)
{
int value;
switch(channel_number)
{
case 0:
//set the channel selector
CHS2=0;
CHS1=0;
CHS0=0;
break;
case 1:
//set the channel selector
CHS2=0;
CHS1=0;
CHS0=1;
default:;
}

//start AD conversion
ADGO=1;
//wait for conversion to finish
while(ADGO)
{};
//read the values in the registers
value=(ADRESH<<8)+ADRESL;
return(value);
}
void BCDtoLPortD(int RD3_val,int RD2_val,int RD1_val,int RD0_val)
{
	RD3=RD3_val;
	RD2=RD2_val;
	RD1=RD1_val;
	RD0=RD0_val;
}	//end of function for BCDtoPortD
void BCDtoUPortD(int RD7_val,int RD6_val,int RD5_val,int RD4_val)
{
	RD7=RD7_val;
	RD6=RD6_val;
	RD5=RD5_val;
	RD4=RD4_val;
}	//end of function for BCDtoPortD

int digit1toBCD(int analog_value1,int base)
{
	// digit1toBCD is the function to convert analog_value1 into one digit
	// Return the one digit value to ConvToBCD, and add up to tenth digit
	if (analog_value1>(base+103)) //digit1 more than 5
	{
		if(analog_value1>(base+103+41)) //digit1 more than 7
		{
			if(analog_value1>(base+103+61))//digit1 more than 8
			{
				if(analog_value1>(base+103+82))//digit1 more than 9 =>BCD = 9
				{
					BCDtoLPortD(1,0,0,1);
					return(9);
				}
				else
				{
					BCDtoLPortD(1,0,0,0); //digit1 less than 9 => BCD = 8
					return(8);
				}
			}
			else	//digit1 less than 8 => BCD = 7
			{
				BCDtoLPortD(0,1,1,1);
				return(7);
			}
		}
		else	//digit1 less than 7
		{
			if(analog_value1>(base+103+20))//digit1 more than 6 => BCD = 6
			{
				BCDtoLPortD(0,1,1,0);
				return(6);
			}
			else //digit1 less than 6 => BCD = 5
			{
				BCDtoLPortD(0,1,0,1);
				return(5);
			}
		}
	}	
	else	//digit1 less than 5
	{
		if(analog_value1>(base+41)) //digit1 more than 2
		{
			if(analog_value1>(base+62))//digit1 more than 3
			{
				if(analog_value1>(base+82))//digit1 more than 4 =>BCD = 4
				{
					BCDtoLPortD(0,1,0,0);
					return(4);
				}
				else //digit1 less than 4 => BCD = 3
				{
					BCDtoLPortD(0,0,1,1);
					return(3);
				}
			}
			else	//digit1 less than 3 => BCD = 2
			{
				BCDtoLPortD(0,0,1,0);
				return(2);
			}
		}
		else	//digit1 less than 2
		{
			if(analog_value1>(base+21))//digit1 more than 1 => BCD = 1
			{
				BCDtoLPortD(0,0,0,1);
				return(1);
			}
			else //digit1 less than 0 => BCD = 0
			{
				BCDtoLPortD(0,0,0,0);
				return(0);
			}
		}
	} // end of convert digit1 to BCD using if-else statements
}	//end of function for digit1toBCD
int ConvToBCD(int analog_value1)
{
	int temp; // temperature reading in integer value
	
	// If-else statement for the digit2, 16to19, 20to29, 30to39, 40to49
	// Call digit1toBCD function to convert the analog_value1 to one digit 
	// add up to tenth digit and one digit, and store it in temp
	if(analog_value1<409)
 	{
 		BCDtoUPortD(0,0,0,1);
 		temp=10;
 		temp=digit1toBCD(analog_value1,205)+temp;
	}
 	else if((analog_value1>=409)&&(analog_value1<614))
 	{
 		BCDtoUPortD(0,0,1,0);
 		temp=20;
 		temp=digit1toBCD(analog_value1,409)+temp;
 	}
 	else if((analog_value1>=614)&&(analog_value1<818))
 	{
 		BCDtoUPortD(0,0,1,1);
 		temp=30;
 		temp=digit1toBCD(analog_value1,614)+temp;
 	}
 	else if((analog_value1>=818)&&(analog_value1<1020))
 	{
 		BCDtoUPortD(0,1,0,0);
 		temp=40;
 		temp=digit1toBCD(analog_value1,818)+temp;
 	}
 	return(temp);
}// end of function for ConvToBCD

void displayBCD(int temp)
{
	int digit1;
	
	//ranging from 16 to 34 temperature
	if(temp<20) 					BCDtoUPortD(0,0,0,1);
	else if((temp>=20)&&(temp<30)) 	BCDtoUPortD(0,0,1,0);
	else if(temp>=30) 				BCDtoUPortD(0,0,1,1);
	
	digit1= temp % 10 ;
	if(digit1>=5)
	{
		if(digit1>=7)
		{
			if(digit1>=8)
			{
				if(digit1==9) 	BCDtoLPortD(1,0,0,1); // digit1 is '9'
				else 			BCDtoLPortD(1,0,0,0); // digit1 is '8'
			}
			else				BCDtoLPortD(0,1,1,1); // digit1 is '7'
		}
		else
		{
			if(digit1==6) 		BCDtoLPortD(0,1,1,0); // digit1 is '6'
			else 				BCDtoLPortD(0,1,0,1); // digit1 is '5'
		}
	}
	else // less than 5
	{
		if(digit1>=2)
		{
			if(digit1>=3)
			{
				if(digit1==4) 	BCDtoLPortD(0,1,0,0); // digit1 is '4'
				else 			BCDtoLPortD(0,0,1,1); // digit1 is '3'
			}
			else				BCDtoLPortD(0,0,1,0); // digit1 is '2'
		}
		else
		{
			if(digit1==1) 		BCDtoLPortD(0,0,0,1); // digit1 is '1'
			else 				BCDtoLPortD(0,0,0,0); // digit1 is '0'
		}
	}
} // end of convert digit1 to BCD using if-else statements for setting temperature

void alarm_buzzer(int HighTemp)
{
	int analog; // for storing the analog value for ADC
	
	RC5=1;		//fire alarm signal activated==> "port to port" to main pic
	PORTD=0xFF; //off light for the led 7 segment

	do
	{
		
	if((RB4==1)||(RB5==1)) //FIRE ALARM(RB4) Enable from Main Pic or local(RB5) enable
	{
		RC6=0; //Off the fan
		RC7=0;
		RC4=1; // fire alarm sound activated
	}
	else
	{
		if((RC6!=1)&&(RC7!=1))
		{
		RC6=1; //On the fan
		RC7=1;
		}
		RC4=0; // fire alarm sound de-activated
	}
	
	analog = read_ADC_channel(0);	
	}while( analog > HighTemp );
    
	RC5=0; // fire alarm signal de-activated
	RC4=0; // fire alarm sound de-activated

} // end of alarm_buzzer function

//main function
int main()
{
//Declare the variables for the main function here
int analog_value1;
int HighTemp = 1020;	//this is 10bit representation of 5v and 50 deg celsius
int LowTemp  = 328;		//this is 10bit representation of 1.6v and 16 deg celsius
int STemp_low = 16;		//this is lower limit setting for ambient control
int STemp_high = 26;		//this is upper limit setting for ambient control
int Temp;				//the temperature reading from LM35 temp sensor
initialize_IO_ports();
initialize_ADC();

//put the infinite loop here
while(1)
{
	
 //read the analog channel and store the value in the variable;
analog_value1=read_ADC_channel(0);

//check the range of the value and set or clear the RC5 accordingly
	
	//volt more than 5.0V signal => more than 50 deg celsius => high temp
	if( analog_value1 > HighTemp )	alarm_buzzer(HighTemp);
	
	if((analog_value1<=HighTemp)&&(analog_value1>=LowTemp)) // Within the range 16to50deg
	{
		// Convert to 2 digit BCD, and store Temperature reading value.
 		Temp=ConvToBCD(analog_value1);
 		
 		if(Temp > STemp_high ) // Temperature reading higher than high temperature
 		{
 				RC6=1; //this is high fan speed
 				RC7=1;
 		}
 		else if(Temp < STemp_low ) // Temperature reading lower than low temperature
 		{
 				RC6=0; // this is switch off for the fan
 				RC7=0;
 		}
 		else // Temperature reading is in-between low and high temperature
 		{
 			
 			// Temperature in the upper portion of the Ambient Temperature Range
 			if( Temp - STemp_low >= STemp_high - Temp ) 
 			{
 				RC6=0; // this is the low fan speed
 				RC7=1;
 			}
 			// Temperature in the lower portion of the Ambient Temperature Range
 			else if( Temp - STemp_low < STemp_high - Temp )
 			{
 				RC6=1; // this is the middle fan speed
 				RC7=0;
 			}
 		}
	}
	
	if(RB3==1) //SET Pushbutton being press for setting the AMBIENT TEMPERATURE RANGE
	{
		RC0=1; // LED light on for Set Temp
		RC3=1; // LED light on for Low Temp
		DelayS(1);
		
		while((RC0==1)&&(RC3==1)) // while loop for setting low temperature
		{
			displayBCD(STemp_low); // display low temperature
		 	
		 	if(RB3==1) // exit whille loop for setting low temperature
		 	{
		 		RC3=0; // LED light off for Low Temp
		 		RC2=1; // LED light on for High Temp
		 		DelayS(1);
		 		break;
		 	}
		 	else if(RB1==1) // up by 1 for STemp_low
		 	{
		 		if(STemp_low < 29)	//move up by 1 for STemp_low and STemp_high til the max 
		 		{
		 			if(STemp_high - STemp_low > 5) // to keep the range of mini 5 deg
		 			{
		 				STemp_low++;
		 				displayBCD(STemp_low);
		 				DelayS(1);
		 			}
		 			else
		 			{
		 				STemp_low++;
		 				STemp_high++;
				 		displayBCD(STemp_low);
				 		DelayS(1);
		 			}
		 		}
			}
			else if(RB2==1) // down by 1 for STemp_low
			{
				if(STemp_low > 16)
				{
			 		STemp_low--;
			 		displayBCD(STemp_low);
			 		DelayS(1);
			 	}
			}
			 
			analog_value1=read_ADC_channel(0);
			if(analog_value1>HighTemp)	break;
		}; // End of while loop for setting low temperature
		
		//check the Fire alarm before enter while loop for setting high temperature
		if(analog_value1<HighTemp)
		{
		while((RC0==1)&&(RC2==1)) // whille loop for setting high temperature
		{
		 	displayBCD(STemp_high); // display high temperature
		 	
		 	if(RB3==1) // exit whille loop for setting high temperature
		 	{
		 		RC2=0; // LED light off for High Temp
		 		RC0=0; // LED light off for Set Temp
		 		DelayS(1);
		 		break;
		 	}
		 	else if(RB1==1) // up by 1 for STemp_low
		 	{
				if(STemp_high < 34)
				{
					STemp_high++;
				 	displayBCD(STemp_high);
				 	DelayS(1);
		 		}
			}
			else if(RB2==1) // down by 1 for STemp_low
			{
				if(STemp_high>21)
			 	{
			 		if(STemp_high - STemp_low > 5) // to keep the range of mini 5 deg
			 		{
			 			STemp_high--;
			 			displayBCD(STemp_high);
			 			DelayS(1);
			 		}
			 		else
			 		{
		 				STemp_high--;
		 				STemp_low--;
		 				displayBCD(STemp_high);
		 				DelayS(1);
		 			}
		 		}
		 	}
		 	
		 	analog_value1=read_ADC_channel(0);
			if(analog_value1>HighTemp) break;
			
		}; // End of while loop for setting high temperature
		} // If-statement for fire_alarm check
		
		RC0=0; // LED light off for Set Temp
		RC2=0; // LED light off for High Temp
		RC3=0; // LED light off for Low Temp
		PORTD=0x00;
	} // if-statement of SET THE AMBIENT TEMPERATURE RANGE
	
};//end of while loop of "while(1);" that never ending
return(1);
}
//the end of AdvFamLab sample program

⌨️ 快捷键说明

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