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

📄 tele_control.c

📁 基于PIC单片机的智能电话控制系统
💻 C
📖 第 1 页 / 共 2 页
字号:
//***********************************
//***********************************
#include "tele_control.h"
//**************************************************
//define the sign of this program use
//**************************************************
void Initial(void);				//set when power up
void SFRSET(void);				//clear WDT
void Time(void);				//timer and off the time
void KEYScan(void);				//key scan
void interrupt numberint(void);	//interrupt program
void Display(void);
void ds1302_read_time(void);
void ds1302_write_time(void);
void ds1302_write(uchar reg,uchar data);
uchar ds1302_read(uchar reg);
void change_time(void);
void timechange(void);			//time is changed
void voice(unsigned char data);
void calling(void);				//test calling?
void pass_word_test(void);		//test the pass word
void busy_test(void);			//test telephone off?
void system_reset(void);		//reset system
void key_control(void);			//control key
void jdq_indect(void);
void jdq_check(void);
void reset_password(void);
void reset_ringtimes(void);
//***********************************************
//define bit variable
//***********************************************
static volatile bit time_change,calling_bit0,calling_bit1;
static volatile bit calling_bit2,calling_bit3,calling_bit4;
static volatile bit Tel_num_change,password_bit,jdq_delay;
static volatile bit check_bit0,check_bit1;
static volatile bit reset_bit0,reset_bit1,reset_bit2;
//***********************************************
//define variable
//***********************************************
static volatile uchar kcap[4];
static volatile uchar dsbuf[4];
static volatile uchar password_buf[8];
static volatile uchar password1_buf[7];
static volatile uchar password_times;
static volatile uchar Bit_Select;
static volatile uchar year,month,day,week,hour,minute,second,inc4ms;
static volatile uchar Tel_num,timedelay,timedelay0,timedelay1,timedelay2,timedelay3;
static volatile uchar STA,STA1,STA2;//for 2:1
static volatile uchar dot,flash;
static volatile uchar calling_t0,calling_t1,calling_t2,calling_t3;
static volatile uint calling_times,calling_times1;
static volatile uchar voice_times,voice_tone;
static volatile uint voice_delay;
//************************************************
//blow define led7code value
//duan ma:<7:0>=afbgcehd
const char LED7[16]={	0XED,0X28,0XB5,0XB9,	//0,1,2,3
						0X78,0XD9,0XDD,0XA8,	//4,5,6,7
						0XFD,0XF9,0X00,0Xd4,	//8,9,a,b a=black,b=F
						0X6d,0Xec,0X10,0x1d};	//c,d,e,f c=U,d=n,e=-,f=o
//************************************************
//blow define ledbit value
const char bitcode[8]={	0X01,0X02,0X04,0X08,	
						0X10,0X20,0X40,0X80};
//***********************************************
//blow define led4code vale
const char LED4[16]={	0X00,0X20,0X10,0X30,	//0,1,2,3
						0X40,0X60,0X50,0X70,	//4,5,6,7
						0X80,0Xa0,0X90,0Xb0,	//8,9,a,b
						0Xc0,0Xe0,0Xd0,0xf0};	//c,d,e,f

//************************************************
//blow define telephone number Key value 
const char Telnum[16]={	0X0d,0X08,0X04,0X0f,	//d,8,4,#	
						0X02,0X00,0X06,0X0b,	//2,0,6,b
						0X01,0X09,0X05,0X0a,	//1,9,5,a
						0X03,0X0e,0X07,0x0c};	//3,*,7,c
//*************************************************
union{
	struct
		{
		volatile unsigned k0:1;		volatile unsigned k1:1;
		volatile unsigned k2:1;		volatile unsigned k3:1;
		volatile unsigned lk0:1;	volatile unsigned lk1:1;
		volatile unsigned lk2:1;	volatile unsigned lk3:1;
		volatile unsigned kk0:1;	volatile unsigned kk1:1;
		volatile unsigned kk2:1;	volatile unsigned kk3:1;
		}b;
volatile uint allkey;
volatile uchar halfkey[2];
	}key;
//**********************************************************
void main(void)
{	
	Initial();
	SFRSET();
  ////////////////////////////
 //the all cycle of program//
////////////////////////////
	while(1)
	{
		asm("clrwdt");			//clear WDT
		Time();					//timer and off the time
		if(Bit_Select&0x01)
		{	Display();			//display and keyscan
			KEYScan();
			if(STA==0x00)
			{
				ds1302_read_time();
			}
			else if(STA==0x03)
			{
				if(key.allkey==0x0000)
				{
					calling_times++;
					if(calling_times==0x1d4c)	//30S
					{
						calling_times=0x0000;
						STA=0x00;
						STA1=0x00;
						STA2=0x00;
					}
				}
				else
				{
					calling_times=0x0000;
				}
			}
			else if(STA==0x02)
			{
				if(key.allkey==0x0000)
				{
					calling_times++;
					if(calling_times==0x09c4)	//10S
					{
						calling_times=0x0000;
						STA=0x00;
						STA1=0x00;
						STA2=0x00;
					}
				}
				else
				{
					calling_times=0x0000;
				}
			}
			else
			{
				if(Tel_num_change)
				{
					calling_times=0x0000;
				}
				else
				{
					calling_times++;
					if(calling_times==0x1d4c)	//30S
					{
						calling_times=0x0000;
						Tel_num=0x00;
						VOICE=0;
						INTE=1;
						ANALOGUP=0;
						STA=0x00;
						STA1=0x00;
						STA2=0x00;
					}
				}
				if(key.b.kk0|key.b.kk3)
				{
					calling_times=0x0000;
					Tel_num=0x00;
					VOICE=0;
					INTE=1;
					ANALOGUP=0;
					STA=0x00;
					STA1=0x00;
					STA2=0x00;
					key.b.kk0=0;
					key.b.kk3=0;
				}
			}
		}
		else
		{
			Display();			//put display
			inc4ms++;
			if(STA==STA2){STA1=STA;}	//important data 2:1
			else if(STA1==STA2){STA=STA1;}
			else if(STA==STA1){STA2=STA;}
			else{STA=0x00;STA1=0X00;STA2=0X00;}
			switch(STA)
			{
			case 0:
//				ds1302_read_time();
				if((second&0x0f)>0x07)
				{
					dsbuf[0]=year;
					dsbuf[1]=month;
					dsbuf[2]=day;
					dsbuf[3]=((week<<4)&0xf0)|(dsbuf[3]&0x0f);
					dot=0x54;
				}
				else
				{
					dsbuf[0]=hour;
					dsbuf[1]=minute;
					dsbuf[2]=second;
					dsbuf[3]=0xa0|(dsbuf[3]&0x0f);
					dot=0x50;
				}
				flash=0x00;
				key_control();
				calling();
		 	break;
			case 1:		//all join
				dsbuf[0]=0xbc;		//FU
				dsbuf[1]=0xdf;		//N 
				dsbuf[2]=0xff;
				dsbuf[3]=0xa0;
				if(Tel_num_change)
				{
					unsigned char i;
					Tel_num_change=0;
					if(Tel_num==1)	//JDQ indect change
					{
						voice(0x64);
						i=0x08;
						STA=0x05;
					}
					if(Tel_num==2)	//reset password
					{
						voice(0x68);
						i=0x04;
						STA=0x06;
					}
					if(Tel_num==3)	//reset ring times
					{
						voice(0x6c);
						i=0x0c;
						STA=0x07;
					}
					if(Tel_num==4)	//check JDQ sta and heard
					{
						voice(0x70);
						i=0x02;
						STA=0x08;
					}
					if(STA==0x01){voice(0x7c);}
					else{
					dsbuf[0]=0xbc;		//FU
					dsbuf[1]=0xde;		//N
					dsbuf[2]=Tel_num;	//
					dsbuf[3]=i|0xe0;	//
					}
					Tel_num==0;
				}
			break;
			case 2:		//reset all data
				if(key.b.k3){reset_bit0=1;dsbuf[3]=0xe8;}
				if(reset_bit0){if(key.b.k2){reset_bit1=1;reset_bit0=0;dsbuf[3]=0xec;dsbuf[2]=0xee;}}
				if(reset_bit1){if(key.b.k1){reset_bit2=1;reset_bit1=0;dsbuf[3]=0xee;dsbuf[1]=0xee;}}
				if(reset_bit2)
				{
					if(key.b.kk0)
					{
						reset_bit2=0;
						system_reset();
						STA=0X00;
					}
				}
			break;
			case 3:		//time change
				change_time();
			break;
			case 4:		//test password

				if((password_buf[7]&0x0f)>0x06)
				{
					dsbuf[0]=password_buf[3];
					dsbuf[1]=password_buf[4];
					dsbuf[2]=password_buf[5];
					dsbuf[3]=(password_buf[6]&0xf0)|0x08;
					dot=0xfe;
				}
				else
				{
					dsbuf[0]=password_buf[0];
					dsbuf[1]=password_buf[1];
					dsbuf[2]=password_buf[2];
					dsbuf[3]=((password_times<<4)&0xf0)|0x0c;
					dot=0xfc;
				}				
				pass_word_test();
			break;
			case 5:		//jdq indect change
				jdq_indect();
			break;
			case 6:		//reset password
				reset_password();
			break;
			case 7:		//reset tingtimes
				reset_ringtimes();
			break;
			case 8:		//check jdq sta
				jdq_check();
			break;
			}
		STA1=STA2=STA;
		}
	}  
}
//*****************************
//interrupt program
//*****************************
void interrupt numberint(void)
{
	if(INTE&INTF)
	{
		unsigned char i;
		INTF=0;
		HANG1=1;
		HANG0=1;
		i=0x05;while(i--){;}	//8+3*(i-1)=delay 20uS
		Tel_num=Telnum[PORTC&0x0f];
		HANG0=0;
		HANG1=0;
		Tel_num_change=1;
	}
	if(TMR2IE&TMR2IF)
	{
		TMR2IF=0;
		voice_delay++;
		if(voice_times&0x01)
		{
			if(VOICE==0){VOICE=1;}
			else{VOICE=0;}
		}
		if(voice_delay==0x0000)
		{
			voice_times--;	
			if(voice_times&0x01)
			{
				voice_tone<<=1;
				if(voice_tone&0x80){PR2=0x19;}
				else{PR2=0x32;}
			}
			if(voice_tone&0x80)
			{	voice_delay=0xfda8;}	//delay 300mS
			else{voice_delay=0xfed4;}
			if(voice_times==0x00){TMR2IE=0;}
		}
	}
}
//***************************
//voice 
//***************************
void voice(unsigned char i)
{
	if(i&0xe0)
	{
		voice_tone=i&0x1f;
		voice_tone<<=2;
		i=(i<<4)|(i>>4);
		voice_times=i&0x0e;
		if(voice_tone&0x80)
		{	voice_delay=0xfda8;	//delay 300mS
			PR2=0x19;
		}
		else
		{	voice_delay=0xfed4; //delay 300mS
			PR2=0x32;
		}
		TMR2IE=1;
	}
}
//***************************
void pass_word_test(void)
{
	unsigned char i,j;
	if(password_bit)
	{
		password_bit=0;
		for(i=0;i<7;i++)
		{
			if(password_buf[i]==ds1302_read(adr_password+i+i))
			{;}
			else
			{	password_times|=0x80;}
		}
		password_buf[0]=0xaa;		//reload
		password_buf[1]=0xaa;
		password_buf[2]=0xaa;
		password_buf[3]=0xaa;
		password_buf[4]=0xaa;
		password_buf[5]=0xaa;
		password_buf[6]=0xaa;
		password_buf[7]=0x00;

		if(password_times&0x80)
		{
			password_times&=0x7f;
			password_times++;
			if(password_times>=0x03)
			{
				password_times=0x00;
				VOICE=0;
				ANALOGUP=0;
				STA=0x00;
			}
			else{voice(0x7c);}
		}
		else
		{
			voice(0x60);		//000
			password_times=0x00;
			ANALOGUP=1;
			dsbuf[0]=0xbc;		//FU
			dsbuf[1]=0xdf;		//N 
			dsbuf[2]=0xff;
			dsbuf[3]=0xa0;
			dot=0x00;
			STA=0X01;
		}
		INTE=1;
	}
	else
	{
		if(Tel_num_change)
		{	
			Tel_num_change=0;
			i=Tel_num&0x0f;
			j=0xf0;
			if(password_buf[7]&0x01)
			{;}
			else
			{
				i=(i>>4)|(i<<4);
				j=(j<<4)|(j>>4);
			}
			password_buf[(password_buf[7]>>1)&0x07]&=j;
			password_buf[(password_buf[7]>>1)&0x07]|=i;
			password_buf[7]++;
			if(password_buf[7]==0x0e)
			{
				password_buf[7]=0x0d;
			}
		}
		if(Tel_num==0x0f)		//Tel_num==#?
		{	
			password_bit=1;
			INTE=0;
		}
		Tel_num=0x00;
	}
}	
//***************************
void busy_test(void)
{
//	if(busy==0)

}
//**************************************************
//test calling?
//**************************************************
void calling(void)
{
	if(PHONEIN==0)
	{
		calling_t0++;
		if(calling_t0==0x03)
		{
			calling_t1++;
			calling_bit0=1;
		}
	}
	else{calling_t0=0x00;}
	if(calling_bit0){calling_t2++;}
	if(calling_t2==0xfa)	//==1s?
	{
		calling_t2=0x00;
		calling_bit0=0;
		if(calling_t1>=0x14)
		{
			calling_t3++;
			calling_bit1=1;
			calling_bit2=1;
		}
		calling_t1=0x00;
	}
	if(calling_bit1)
	{
		calling_times++;
		if(calling_bit2){calling_bit2=0;calling_times=0x0000;}
		if(calling_times>0x05dc)		//>=6S?
		{
			calling_bit0=0;
			calling_bit2=0;
			calling_bit4=0;
			calling_times1=0x0000;
			calling_t1=0x00;
			calling_t2=0x00;
			calling_bit1=0;
			calling_times=0x0000;
			if(calling_t3<0x03){calling_bit3=1;}
			else{calling_bit3=0;}
			calling_t3=0x00;
			Tel_num_change=0;
		}
	}
	if(calling_bit3)
	{
		calling_times1++;
		if(calling_bit1)
		{
			if(calling_times1<0x0dac)	//<=14s?
			{
				calling_bit4=1;
				calling_bit3=0;
			}
		}
		if(calling_times1>0x0dac)
		{
			calling_bit3=0;
		}
	}
	else{calling_times1=0x0000;}
	if(calling_bit4)
	{
		if(calling_t3>=ds1302_read(adr_ringtimes))
		{
			ANALOGUP=1;			//
			voice(0xa1);
			password_buf[0]=0xaa;
			password_buf[1]=0xaa;
			password_buf[2]=0xaa;
			password_buf[3]=0xaa;
			password_buf[4]=0xaa;
			password_buf[5]=0xaa;
			password_buf[6]=0xaa;
			password_buf[7]=0x00;
			calling_bit0=0;
			calling_bit1=0;
			calling_bit2=0;
			calling_bit3=0;
			calling_bit4=0;
			calling_times1=0x0000;
			calling_times=0x0000;
			calling_t1=0x00;
			calling_t2=0x00;
			calling_t3=0x00;
			Tel_num_change=0;
			STA=0x04;
		}

	}
}
//***************************
void ds1302_read_time(void)
{
	year=ds1302_read(ds1302_year);	//year
	month=ds1302_read(ds1302_month);//month
	day=ds1302_read(ds1302_day);	//day
	week=ds1302_read(ds1302_week);	//week
	hour=ds1302_read(ds1302_hour);	//hout
	minute=ds1302_read(ds1302_min);	//minute
	second=ds1302_read(ds1302_sec);	//second
}
void ds1302_write_time(void)
{
	ds1302_write(ds1302_control,0x00);	//close write protect	
	ds1302_write(ds1302_sec,0x80);		//stop the ds1302 clock
	ds1302_write(ds1302_charger,0xa9);	//trickle charger
	ds1302_write(ds1302_year,year);		//year
	ds1302_write(ds1302_month,month);	//month
	ds1302_write(ds1302_day,day);		//day
	ds1302_write(ds1302_week,week);		//week
	ds1302_write(ds1302_hour,hour);		//hour
	ds1302_write(ds1302_min,minute);	//munite
	ds1302_write(ds1302_sec,second);	//second	
	ds1302_write(ds1302_control,0x80);	//open write protect
}
//***************************
//write or read ds1302 program
//***************************
void ds1302_write(uchar reg,uchar data)	//write ds1302
{
	unsigned char i;
	RST1302=0;
	CLK1302=0;
	RST1302=1;
	for(i=8;i>0;i--)
	{	
		CLK1302=0;
		asm("nop");
		if(reg&0x01){DAT1302=1;}
		else{DAT1302=0;}
		CLK1302=1;
		reg>>=1;
	}
	for(i=8;i>0;i--)
	{
		CLK1302=0;
		if(data&0x01){DAT1302=1;}
		else{DAT1302=0;}
		CLK1302=1;
		data>>=1;
	}
	CLK1302=0;
	RST1302=0;
}
uchar ds1302_read(uchar reg)//read ds1302
{
	unsigned char data=0x00;
	unsigned char i;
	reg+=1;			//read flag
	RST1302=0;
	CLK1302=0;
	RST1302=1;
	for(i=8;i>0;i--)
	{
		CLK1302=0;
		if(reg&0x01){DAT1302=1;}
		else{DAT1302=0;}
		CLK1302=1;
		reg>>=1;
	}
	TRISB|=0x04;
	for(i=8;i>0;i--)
	{
		CLK1302=0;
		data>>=1;
		if(DAT1302){data|=0x80;}
		else{;}
		CLK1302=1;
	}
	TRISB&=~0x04;
	CLK1302=0;
	RST1302=0;
	return(data);
}
//**************************************************
//change time progarm
//**************************************************
void change_time(void)
{
	flash&=0x0f;
	if(flash&0x08)
	{
		dsbuf[0]=hour;
		dsbuf[1]=minute;
		dsbuf[2]=second;
		dsbuf[3]=0xa1;

⌨️ 快捷键说明

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