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

📄 k13.c

📁 c51实现音频切换 分配音频切换 分配
💻 C
字号:
/*题目:音频切换分配器*/
/*作者:雷玉霞        */
/*时间:2005年8月22日 */
/*文件名称:k13-501x.c */
/*升级记录:          */
#include	<reg52.h>
#include	<intrins.h>

/*数据类型简洁定义*/
typedef		unsigned char		byte;
typedef		unsigned short		word;
typedef		unsigned long		dword;
/*I/O定义*/
sbit		g_sbtswitch0=	 0x90;/*拨码开关*/
sbit		g_sbtswitch1=	 0x91;
sbit		g_sbtswitch2=	 0x92;
sbit		g_sbtswitch3=	 0x93;
sbit		g_sbtkeypress0=	 0x94;
sbit		g_sbtkeypress1=	 0x95;
sbit		g_sbtkeypress2=	 0x96;
sbit		g_sbtkeypress3=	 0x97;

sbit		g_sbtMc1413_In0= 0x80;/*MC1413输入*/
sbit		g_sbtMc1413_In1= 0x81;
sbit		g_sbtMc1413_In2= 0x82;
sbit		g_sbtMc1413_In3= 0x83;
sbit		g_sbtX5045_Cs=	 0x84;/*X5045*/
sbit		g_sbtX5045_So=	 0x85;
sbit		g_sbtX5045_Sck=	 0x86;
sbit		g_sbtX5045_Si=	 0x87;


sbit		g_sbtLBD_Red0=   0xa0;/*通道状态指示灯*/
sbit		g_sbtLBD_Green0= 0xa1;
sbit		g_sbtLBD_Red1=   0xa2;
sbit		g_sbtLBD_Green1= 0xa3;
sbit		g_sbtLBD_Red2=	 0xa4;
sbit		g_sbtLBD_Green2= 0xa5;
sbit		g_sbtLBD_Red3=	 0xa6;
sbit		g_sbtLBD_Green3= 0xa7;

sbit		g_sbtInput0=	 0xb2;/*故障信号输入*/
sbit		g_sbtInput1=	 0xb3;
sbit		g_sbtInput2=	 0xb4;
sbit		g_sbtInput3=	 0xb5;

#define WREN 0x06
#define WRDI 0x04
#define RDSR 0x05
#define WRSR 0x01
#define READ0 0x03
#define READ1 0x0b
#define WRITE0 0x02
#define WRITE1 0x0a

bit data g_btTi;
bit data g_bComming;
bit data g_btPckey;/*上位机切换标志*/
bit data g_btKeyflag;/*手动切换标志*/
bit data g_binput0;/*故障一标志*/
bit data g_binput1;/*故障二标志*/
bit data g_binput2;/*故障三标志*/
bit data g_binput3;/*故障四标志*/
bit data binput0=1;
bit data btemp0=1;
bit data binput1=1;
bit data btemp1=1;
bit data binput2=1;
bit data btemp2=1;
bit data binput3=1;
bit data btemp3=1;
byte data g_bkeypress;
byte data g_bpBuf[50];	    /*serial receive buffer*/
byte data *g_bpR;			/*read serial receive buffer address*/
byte data *g_bpW;			/*write serial receive buffer address*/
word data g_wLen;			/*serial receive buffer length*/

word data g_wOverTime;		/*time0 counter*/
word data g_wOverTime0;		/*time0 counter*/
word data g_wOverTime1;		/*time0 counter*/
word data g_wOverTime2;		/*time0 counter*/
word data g_wOverTime3;		/*time0 counter*/


byte Read8()
{
	bit data bData;
	byte data cLoop;
	byte data cData;
	for(cLoop=0;cLoop<8;cLoop++)
	{
		g_sbtX5045_Sck=1;
		g_sbtX5045_Sck=0;
		bData=g_sbtX5045_So;
		cData<<=1;
		if(bData)
		{
			cData|=0x01;
		}
	}
	return cData;
}

void Write8(byte cData)
{
	byte data cLoop;
	for(cLoop=0;cLoop<8;cLoop++)
	{
		if((cData&0x80)==0)
		{
			g_sbtX5045_Si=0;
		}
		else
		{
			g_sbtX5045_Si=1;
		}
		g_sbtX5045_Sck=0;
		g_sbtX5045_Sck=1;
		cData<<=1;
		}
}

byte ReadSR()
{
	byte data cData;
	g_sbtX5045_Cs=0;
	Write8(RDSR);
	cData=Read8();
	g_sbtX5045_Cs=1;
	return cData;
}

byte WriteSR(byte cData)
{
	byte data cTemp;
	cTemp=ReadSR();
	if((cTemp&0x01)==1)
	return 0;
	g_sbtX5045_Cs=0;
	Write8(WREN);
	g_sbtX5045_Cs=1;
	g_sbtX5045_Cs=0;
	Write8(WRSR);
	Write8(cData);
	g_sbtX5045_Cs=1;
	return 1;
}
/* 写入一个字节,cData为写入的数,cAddress为写入地址,bRegion为页 */
void Write1B(byte cData,byte cAddress,bit bRegion)
{
	while((ReadSR()&0x01)==1)
	{
	}
	g_sbtX5045_Cs=0;
	Write8(WREN);
	g_sbtX5045_Cs=1;
	g_sbtX5045_Cs=0;
	if(bRegion==0)
	{
		Write8(WRITE0);
	}
	else
	{
		Write8(WRITE1);
	}
	Write8(cAddress);
	Write8(cData);
	g_sbtX5045_Sck=0;
	g_sbtX5045_Cs=1;
}

/* 读入一个字节,cAddress为读入地址,bRegion为页 */
byte Read1B(byte cAddress,bit bRegion)
{
	byte data cData;
	while((ReadSR()&0x01)==1)
	{
	}
	g_sbtX5045_Cs=0;
	if(bRegion==0)
	{
		Write8(READ0);
	}
	else
	{
		Write8(READ1);
	}
	Write8(cAddress);
	cData=Read8();
	g_sbtX5045_Cs=1;
	return cData;
}
/*看门狗喂狗信号 */
void watchdog(void)
{
	g_sbtX5045_Cs=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	g_sbtX5045_Cs=0;
}
/* 拨码开关选择故障延时时间 */
word switch0(void) using 2
{
	byte data bStatu;
	bStatu=P1&0X0F;
	switch(bStatu)
	{
		case 0x0f:
			return 1200;
			break;
		case 0x0e:
			return 1000;
			break;
		case 0x0d:
			return 800;
			break;
		case 0x0c:
			return 600;
			break;
		default:
			return 600;
			break;
	}
}
/* 根据故障状态自动切换通道 */
void inputstatus(void)
{
	byte data g_bflash;
	if (g_binput0==1)
	{
		g_bflash=0X10;
	}
	else
	{
		g_bflash=0X00;
	}
	g_sbtLBD_Red0=!(g_bflash&0x10);	
	if (g_binput1==1)
	{
		g_bflash+=0X20;
	}
	else
	{
	}
	g_sbtLBD_Red1=!(g_bflash&0x20);
	if (g_binput2==1)
	{
		g_bflash+=0X40;
	}
	else
	{
	}
	g_sbtLBD_Red2=!(g_bflash&0x40);
	if (g_binput3==1)
	{
		g_bflash+=0X80;
	}
	else
	{
	}
	g_sbtLBD_Red3=!(g_bflash&0x80);

	if ((g_btKeyflag==0) && (g_btPckey==0))
	{
		if (g_bflash==0X00)
		{
			g_sbtLBD_Green0=0;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=1;
		}
		else if ((~g_bflash)&0X10)
		{
			g_sbtLBD_Green0=0;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=1;
		}
		else if ((~g_bflash)&0X20)
		{
			g_sbtLBD_Green0=1;
			g_sbtLBD_Green1=0;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=1;
		}
		else if ((~g_bflash)&0X40)
		{

			g_sbtLBD_Green0=1;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=0;
			g_sbtLBD_Green3=1;

		}
		else if ((~g_bflash)&0X80)
		{
			g_sbtLBD_Green0=1;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=0;
		}
		else 
		{
			g_sbtLBD_Green0=0;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=1;

		}
		g_sbtMc1413_In0=!(g_sbtLBD_Green0);
		g_sbtMc1413_In1=!(g_sbtLBD_Green1);
		g_sbtMc1413_In2=!(g_sbtLBD_Green2);
		g_sbtMc1413_In3=!(g_sbtLBD_Green3);
	}
}
/*手动切换通道*/
void keypress(void) 
{
	if (g_bkeypress==0x01)
	{

			g_sbtLBD_Green0=0;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=1;
			g_sbtMc1413_In0=1;
			g_sbtMc1413_In1=0;
			g_sbtMc1413_In2=0;
			g_sbtMc1413_In3=0;
	}
	else if (g_bkeypress==0x02)
	{

			g_sbtLBD_Green0=1;
			g_sbtLBD_Green1=0;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=1;
			g_sbtMc1413_In0=0;
			g_sbtMc1413_In1=1;
			g_sbtMc1413_In2=0;
			g_sbtMc1413_In3=0;
	}
	else if (g_bkeypress==0x03)
	{

			g_sbtLBD_Green0=1;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=0;
			g_sbtLBD_Green3=1;
			g_sbtMc1413_In0=0;
			g_sbtMc1413_In1=0;
			g_sbtMc1413_In2=1;
			g_sbtMc1413_In3=0;
	}		
	else if (g_bkeypress==0x04)
	{

			g_sbtLBD_Green0=1;
			g_sbtLBD_Green1=1;
			g_sbtLBD_Green2=1;
			g_sbtLBD_Green3=0;
			g_sbtMc1413_In0=0;
			g_sbtMc1413_In1=0;
			g_sbtMc1413_In2=0;
			g_sbtMc1413_In3=1;
	}
	g_bkeypress=0x00;
}
void Serial_Interrupt(void) interrupt 4 using 1
{
	byte data bSio;

	if (TI)
	{
		TI=0;
		g_btTi=1;
	}
	else
	{
		RI=0;
		bSio=SBUF;
		if (g_bpW >= (g_bpBuf+50))
			g_bpW = g_bpBuf;
		*g_bpW = bSio;
		g_bpW++;
		g_wLen ++;
	}
}
void Int_Time0(void) interrupt 1 using 2
{
	static byte  data binputstatus = 0;
	TH0=0xC4;/*10ms*/
	TL0=0x00;
	if (g_wOverTime)
		g_wOverTime--;
	if (g_wOverTime0)
	{
		g_wOverTime0--;
		if (g_wOverTime0==0)
		{
			g_binput0=1;
		}
		else 
		{
			g_binput0=0;
		}

	}
	if (g_wOverTime1)
	{
		g_wOverTime1--;
		if (g_wOverTime1==0)
		{
			g_binput1=1;
		}
		else 
			g_binput1=0;
	}
	if (g_wOverTime2)
	{
		g_wOverTime2--;
		if (g_wOverTime2==0)
		{
			g_binput2=1;
		}
		else 
			g_binput2=0;
	}
	if (g_wOverTime3)
	{
		g_wOverTime3--;
		if (g_wOverTime3==0)
		{
			g_binput3=1;
		}
		else
			g_binput3=0;
	}
	if (binputstatus <= 1)
	{
		if (!g_sbtInput0)
		{
			binput0=1;
			btemp0=binput0;
		}
		else
		{
			binput0=0;
		}
		if (btemp0==1 && binput0==0)
		{
			g_wOverTime0=switch0();
		}
		else if (btemp0==1 && binput0==1)
		{
			g_wOverTime0=switch0();
		}

		if (!g_sbtInput1)
		{
			binput1=1;
			btemp1=binput1;
		}
		else
		{
			binput1=0;
		}
		if (btemp1==1 && binput1==0)
		{
			g_wOverTime1=switch0();
		}
		else if (btemp1==1 && binput1==1)
		{
			g_wOverTime1=switch0();
		}
		if (!g_sbtInput2)
		{
			binput2=1;
			btemp2=binput2;
		}
		else
		{
			binput2=0;
		}
		if (btemp2==1 && binput2==0)
		{
			g_wOverTime2=switch0();
		}
		else if (btemp2==1 && binput2==1)
		{
			g_wOverTime2=switch0();
		}
		if (!g_sbtInput3)
		{
			binput3=1;
			btemp3=binput3;
		}
		else
		{
			binput3=0;
		}
		if (btemp3==1 && binput3==0)
		{
			g_wOverTime3=switch0();
		}
		else if (btemp3==1 && binput3==1)
		{
			g_wOverTime3=switch0();
		}

		binputstatus++;
		if (binputstatus == 2)
		{
			binputstatus=0;
			binput0=0;
			btemp0=0;
			binput1=0;
			btemp1=0;
			binput2=0;
			btemp2=0;
			binput3=0;
			btemp3=0;
		}
	}
	if (!g_sbtkeypress0)
	{
	    g_btKeyflag=1;
		g_bkeypress=0x01;
	}
	if (!g_sbtkeypress1)
	{
	    g_btKeyflag=1;
		g_bkeypress=0x02;
	}
	if (!g_sbtkeypress2)
	{
	    g_btKeyflag=1;
		g_bkeypress=0x03;
	}
	if (!g_sbtkeypress3)
	{
	    g_btKeyflag=1;
		g_bkeypress=0x04;
	}
}

/*****************************************************************
函数功能:	从接收缓冲区读取数据
入口参数:	读取的数据
出口参数:	0/1:	无/有数据
*****************************************************************/
byte ReceiveOne(byte *bpData)
{
	g_wOverTime = 5;
	while(1)
	{
		if(g_wLen)
		{
			if(g_bpR >= (g_bpBuf + 50))
				g_bpR = g_bpBuf;
			*bpData = *g_bpR;
			EA = 0;
			g_wLen--;
			EA = 1;
			g_bpR++;
			return 0x01;
		}
		else if(g_bComming)
		{
			if(g_wOverTime == 0)
				return 0x00;
		}
		else
			return 0x00;
	}
}
/*****************************************************************
函数功能:	向上位机发送数据
入口参数:	bpData:	数据首地址
			wLen:	数据长度
出口参数:	无
*****************************************************************/
void SendData(byte *bpData,word wLen)
{
	g_btTi = 0;
	while(wLen--)
	{
		SBUF = *bpData++;
		while(!g_btTi);
		g_btTi = 0;
	}
}


/*****************************************************************
函数功能:	解释上位机命令
入口参数:	无
出口参数:	无
*****************************************************************/
void ExecuteCommand(void)
{

    byte  data bData;
	byte  data bReturn;
	byte  data temp0;
	static byte  data bStatusCommand = 0;
	byte  bsenddata[2];
	struct
	{
		byte bCommand1;
		byte bCommand2;
		byte bCommand3;
		byte bLen;
		byte bpBuf[2];
		byte bchk; 
	}Receive;	
	g_bComming = 0;
	while(1)
	{
		bReturn = ReceiveOne(&bData);
		if(bReturn == 0)
		{
			if(bStatusCommand)
				bStatusCommand = 0;
			return;
		}
		switch (bStatusCommand)
		{
			case 0:
				Receive.bCommand1 = bData;
				Receive.bchk += Receive.bCommand1; 
				g_bComming = 1;
				bStatusCommand++;
				break;
			case 1:
				Receive.bCommand2 = bData;
				Receive.bchk += Receive.bCommand2; 
				g_bComming = 1;
				bStatusCommand++;
				break;
			case 2:
				Receive.bCommand3 = bData;
				Receive.bchk += Receive.bCommand3; 
				g_bComming = 1;
				bStatusCommand++;
				break;
			case 3:
				Receive.bLen = bData;
				Receive.bchk += Receive.bLen; 
				g_bComming = 1;
				bStatusCommand++;
				break;
			case 4:
				Receive.bpBuf[0] = bData;
				Receive.bchk += Receive.bpBuf[0]; 
				g_bComming = 1;
				bStatusCommand++;
				break;
			case 5:
				Receive.bpBuf[1] = bData;
				Receive.bchk += Receive.bpBuf[1]; 
				g_bComming = 1;
				bStatusCommand++;
				break;
			case 6:
				if (Receive.bchk == bData)
				{
					if (Receive.bCommand1 == 0x01)/*联机并确认机器号*/
					{
						temp0 = Read1B(0x00,0);
						if (Receive.bpBuf[0] == temp0)
						{
							bsenddata[0] = 0xaa;
							SendData(&bsenddata,1);
						}
						else
						{
							bsenddata[0] = 0x55;
							SendData(&bsenddata,1);
						}

	
					}
					else if (Receive.bCommand1 == 0x10)/*上传通道状态*/
					{
						bsenddata[0] = P2;
						SendData(&bsenddata,1);
					}
					else if (Receive.bCommand1 == 0x20)/*切换通道*/
					{
						g_btPckey=1;
						if (Receive.bCommand2 == 0x01)
						{
							g_sbtLBD_Green0 = 0;
						}
                        else
						{
							g_sbtLBD_Green0 = 1;
						}
						if (Receive.bCommand2 == 0x02)
						{
							g_sbtLBD_Green1 = 0;
						}
                        else
						{
							g_sbtLBD_Green1 = 1;
						}
						if (Receive.bCommand2 == 0x04)
						{
							g_sbtLBD_Green2 = 0;
						}
                        else
						{
							g_sbtLBD_Green2 = 1;
						}
						if (Receive.bCommand2 == 0x08)
						{
							g_sbtLBD_Green3 = 0;
						}
                        else
						{
							g_sbtLBD_Green3 = 1;
						}
						g_sbtMc1413_In0=!g_sbtLBD_Green0;
						g_sbtMc1413_In1=!g_sbtLBD_Green1;
						g_sbtMc1413_In2=!g_sbtLBD_Green2;
						g_sbtMc1413_In3=!g_sbtLBD_Green3;
						bsenddata[0] = 0xaa;
						SendData(&bsenddata,1);

					}
				}
				Receive.bchk = 0;
				bStatusCommand = 0;
				break;
		}
		if(bStatusCommand == 0)
		{
			g_bComming = 0;
		}
	}
}
/*上电初始化*/
void PowerInit(void)
{
	TMOD = 0x21;/*T1 8位 T0 16位*/
	SCON = 0X50;/*串口工作方式1,允许串口接收*/
	TCON = 0x50;/*T1启动  T0启动*/
	TH1 = 0xFD;
	TL1 = 0xFD;
	IE = 0x12;/*串口中断开,定时器0开中断*/
	IP = 0x10;/*串口中断优先*/
	g_wLen = 0;
	g_bpR = g_bpBuf;
	g_bpW = g_bpBuf;
	EA = 1;
	WriteSR(0x00);
}
void main()
{
	/*SP=0x60;*/
	PowerInit();
	while(1) 
    {   
		watchdog();
		inputstatus();
		keypress();
		ExecuteCommand();
	}
}

⌨️ 快捷键说明

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