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

📄 chime_strobe.c

📁 Chime报警器
💻 C
字号:
//Checksum=0xBEEF Compile Date=2008.08.21 Recompile Date=2008.08.22 Hi-Tech PICC V9.5PL
//Add sync function
//Checksum=0x58d1 Compile Date=2008.08.21 Recompile Date=2008.08.26 Hi-Tech PICC V9.5PL
//Modified Command Pulses detect
//Checksum=0x0ab6 Recompile Date=2008.08.26 Hi-Tech PICC V9.5PL
//Modified Command Pulses detect
//Checksum=0x04cd Recompile Date=2008.10.10 Hi-Tech PICC V9.5PL
//修正single模式下,上电即发出chime余音的不正常现象

//Configuration bits OSC=Int WDT=OFF PWUT=OFF MCE=Int BOD=ON CP=OFF EEPROM Protect=OFF
#include <htc.h>
#define command_detect_speed 1
#define wobble_delay 12
#define offline_delay 15
#define volt_low 0xA7
#define volt_high 0xAE

unsigned char sync_pulse_start,single_first_pulse;
unsigned char step,horn_pulse_counter,whoop_delay,temp3_step,tmr1_counter,rec_code,key_map;
unsigned char tmf_sync_quantity,bit_1_counter,bit_0_counter,bit_counter,test;
unsigned int sync_long_delay;
	unsigned char ca_counter,ca_1_counter,ca_0_counter;
bit sync_in,command_retrun,key_changed;
bit flag_bits_pulse,flag_bits_hornsw;
bit command_start;//sync & command var
bit temp3,march,fastmarch,soundopen,temp_sw,horn_sw,ca_horn,ca_horn_temp,manu_TMR1IF;
//bit command_available;

unsigned char recbit @ 0x51;
bit bit0@ (unsigned)&recbit*8+0;
bit bit1@ (unsigned)&recbit*8+1;
bit bit2@ (unsigned)&recbit*8+2;
bit bit3@ (unsigned)&recbit*8+3;
bit bit4@ (unsigned)&recbit*8+4;
bit bit5@ (unsigned)&recbit*8+5;
bit bit6@ (unsigned)&recbit*8+6;
bit bit7@ (unsigned)&recbit*8+7;

void delay(unsigned char t);
void scankey(void);
void init(void);// Init Port and Reg

void var_init(void)
{
	soundopen=1;
	march=0;
	fastmarch=0;
	temp3=0;
	temp3_step=0;
	tmr1_counter=0;
	temp_sw=0;
	horn_sw=0;
//	command_available=0;
	
	manu_TMR1IF=0;
	sync_in=0;
	tmf_sync_quantity=1;
	bit_1_counter=0;
	bit_0_counter=0;
	bit_counter=21;
	ca_horn=0;
	key_changed=0;
	key_map=0;
	
	recbit=0;
	whoop_delay=10;
	flag_bits_hornsw=1;
	flag_bits_pulse=0;
	horn_pulse_counter=5;
	single_first_pulse=0;
	
	rec_code=0;
	sync_pulse_start=0;
	command_start=0;
	sync_long_delay=400;
	step=4;
}
void interrupt ISR(void)
{
	if(T0IE & T0IF)
	{
		T0IF=0;
		if(soundopen & manu_TMR1IF)
		{
			soundopen=0;
			single_first_pulse++;
			if(single_first_pulse>=2)
			{
				single_first_pulse=2;
				GPIO5=1;
			}else
			{
				GPIO5=0;
			}
		}else
		{
			GPIO5=0;
		}
	}
	if(TMR1IE & TMR1IF & (!command_start))
		{//TIME BASE 0.25s
			TMR1IF=0;
			TMR1L=0x4D;
			TMR1H=0x85;	
			manu_TMR1IF=!manu_TMR1IF;
			if(temp3)
				{
				switch(temp3_step)
				{
					case 0:
					{
						tmr1_counter++;
						if(tmr1_counter==2)
						{
							temp_sw=1;
							temp3_step=1;
							tmr1_counter=0;
						}
						break;
					}
					case 1:
					{
						tmr1_counter++;
						if(tmr1_counter==2)
						{
							temp_sw=0;
							temp3_step=2;
							tmr1_counter=0;
						}
						break;
					}					
					case 2:
					{
						tmr1_counter++;
						if(tmr1_counter==2)
						{
							temp_sw=1;
							temp3_step=3;
							tmr1_counter=0;
						}
						break;
					}					
					case 3:
					{
						tmr1_counter++;
						if(tmr1_counter==2)
						{
							temp_sw=0;
							temp3_step=4;
							tmr1_counter=0;
						}
						break;
					}					
					case 4:
					{
						tmr1_counter++;
						if(tmr1_counter==2)
						{
							temp_sw=1;
							temp3_step=5;
							tmr1_counter=0;
						}
						break;
					}
					case 5:
					{
						tmr1_counter++;
						if(tmr1_counter==6)
						{
							temp_sw=0;
							tmr1_counter=0;
							temp3_step=0;
						}
					}				
				}//end switch(temp3_step)
				soundopen=!temp_sw;
				}//end if(temp3)
			else
			if(march)
				{
					tmr1_counter++;
					if(tmr1_counter==2)
						{temp_sw=!temp_sw;tmr1_counter=0;}
					soundopen=temp_sw;
				}
			else	
			if(fastmarch)
				{
					temp_sw=!temp_sw;
					soundopen=temp_sw;
				}
		}//TMR1 END;
}

void main(void)
{
	flag1:
	init();
	var_init();
	delay(50);
	CMCON=CMCON;
	VRCON=volt_low;
	CMCON=CMCON;	
	VRCON=volt_high;
	
	while(1)
	{	
		unsigned char rec_code_temp;
		//////////////////start sync and command detect
	if(ca_horn)
	{
		if(VRCON==volt_high)
		{
			if(COUT)
			{
				if(!sync_pulse_start)
				{
					delay(100);
					if(COUT)
					{
						delay(115);
					}
					if(COUT)
					{
						sync_pulse_start=0xFF;
						sync_in=1;
						sync_long_delay=700;
					}
				}
				if(sync_pulse_start)
				{
					if(!command_retrun)
					{						
						VRCON=volt_low;
						CMCON=CMCON;
					}else
					{
						delay(0xff);
						delay(0xff);
						delay(0xff);
					}
				}
			}else
			{
				sync_pulse_start=0;
				command_retrun=0;
			}
		}else // command_detected
		{
			if(COUT)
			{
				if(!command_start)
				{
					delay(50);
				}
				if(COUT)
				{
				if(!command_start)
				{
					command_start=1;
					GPIE=0;	
				}
				}
			}
			if(command_start)
			{
				GPIO5=0;
				bit_1_counter=0;bit_0_counter=0;bit_counter=11;
				switch(step)
				{
					case 4:{GPIO4=1;
							GIE=0;
							while(bit_counter)
							{
								bit_counter--;GPIO4=0;
								if(COUT){bit_1_counter++;}else{bit_0_counter++;}GPIO4=1;
							}GPIO4=0;
							if(bit_1_counter>10){bit3=1;}
							if(bit_0_counter>10){bit3=0;}
							delay(114);
							break;
							}
					case 3:{GPIO4=1;
							while(bit_counter)
							{
								bit_counter--;GPIO4=0;
								if(!COUT){bit_0_counter++;}else{bit_1_counter++;}GPIO4=1;								
							}GPIO4=0;
							if(bit_1_counter>10){bit2=1;}
							if(bit_0_counter>10){bit2=0;}
							delay(89);
							break;
							}
					case 2:{GPIO4=1;
							while(bit_counter)
							{
								bit_counter--;GPIO4=0;
								if(!COUT){bit_0_counter++;}else{bit_1_counter++;}GPIO4=1;
							}GPIO4=0;
							if(bit_1_counter>10){bit1=1;}
							if(bit_0_counter>10){bit1=0;}
							delay(109);
							break;
							}
					case 1:{GPIO4=1;
							while(bit_counter)
							{
								bit_counter--;GPIO4=0;
								if(COUT){bit_1_counter++;}else{bit_0_counter++;}GPIO4=1;
							}GPIO4=0;
							if(bit_1_counter>10){bit0=1;}
							if(bit_0_counter>10){bit0=0;}
							command_start=0;
							step=5;
							T0IF=0;
							VRCON=volt_high;
							CMCON=CMCON;
							command_retrun=1;
							GIE=1;
							break;
							}
				}
				step--;
			}
			sync_long_delay--;
			if(sync_long_delay==0)
			{
				VRCON=volt_high;
			}
		}
	}else
	{
		if(key_changed)
		{
			soundopen=1;
			manu_TMR1IF=1;
			march=1;
			command_retrun=0;
			tmf_sync_quantity=1;
			sync_in=0;
			TMR1IE=1;
			T0IF=0;
			TMR0=0;
			T0IE=1;
			key_changed=0;
			rec_code=0xff;
		}
	}
		//////////////////end sync and command detect
		if(sync_in)
		{
			tmf_sync_quantity--;
			sync_in=0;
			if(tmf_sync_quantity==0)
			{
				if(temp3)
				{
					tmf_sync_quantity=4;
					tmr1_counter=5;
					temp3_step=5;
					TMR1IF=1;
					manu_TMR1IF=0;
				}else
				if(march)
				{	
					tmf_sync_quantity=1;
					tmr1_counter=1;
					temp_sw=0;TMR1IF=1;manu_TMR1IF=0;
				}else
				if(fastmarch)
				{
					tmf_sync_quantity=1;
					temp_sw=0;TMR1IF=1;manu_TMR1IF=0;
				}				
			}
			
		}
		scankey();
		if(command_retrun)
			{
				rec_code_temp=rec_code;
				rec_code=recbit & 0xF;
				if(rec_code_temp!=rec_code)
				{
				GIE=0;
				switch(rec_code)
				{
					case 0x9:{horn_sw=0;TMR1IE=0;T0IE=0;GPIO5=0;break;}//1001	HORN OFF
					case 0xB:{horn_sw=1;T0IE=1;TMR1IE=1;fastmarch=0;temp3=0;march=1;break;}//1011	HORN ON
					case 0x8:{horn_sw=1;TMR1IE=1;T0IE=1;temp3=1;march=0;fastmarch=0;break;}//1000	TEMPORAL
					case 0xE:{horn_sw=1;TMR1IE=1;T0IE=1;temp3=0;march=1;fastmarch=0;break;}//1110	MARCH TIME
					case 0xD:{horn_sw=1;TMR1IE=1;T0IE=1;temp3=0;march=0;fastmarch=1;break;}//1101	Fast March time
					default:{;}
				}
				GIE=1;manu_TMR1IF=0;
				if(temp3){tmf_sync_quantity=4;}else
				if(march){tmf_sync_quantity=1;}else
				if(fastmarch){tmf_sync_quantity=1;}
				}
			}
	}
}

void init(void)
{			
	OPTION_REG=3;
	T1CON=0x31;
//	ANSEL=0x02;//0x02
	CMCON=0x04;
	TRISIO=0x0F;//0x1F
	GPIO=0x0F;
	WPU=0x1D;
	IOCB=0x0;
	VRCON=volt_high;//volt_low
	CMCON=CMCON;
	TMR1L=0xED;
	TMR1H=0x85;

	TMR1IF=0;
	T0IF=0;
	CMIF=0;
	
	GIE=1;
	GPIE=0;
	T0IE=0;
	TMR1IE=0;
	CMIE=0;
	PEIE=1;
}

void delay(unsigned char t)
{
	while(t)
	{
		t--;
	}
}

void scankey(void)
{
unsigned char key_map_temp;
	ca_counter=5;
	ca_1_counter=0;
	ca_0_counter=0;
	ca_horn_temp=ca_horn;
	while(ca_counter)
	{
		ca_counter--;
		if(GPIO2){ca_1_counter++;}else{ca_0_counter++;}		
	}
	key_map_temp=key_map;
	if(ca_1_counter>ca_0_counter)
	{
		ca_horn=1;key_map=0xFF;single_first_pulse=0;
	}else
	{
		ca_horn=0;key_map=0x77;
	}
	if(key_map_temp!=key_map)
	{
		if(key_map==0x77)
		{
			key_changed=1;
		}
	}
}

⌨️ 快捷键说明

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