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

📄 vs8000.c

📁 一个使用C51编写的视频切换器的控制程序
💻 C
字号:
#include <stdio.h>
#include <AT89X51.H>
#include <intrins.h>
//#include <stdlib.h>

#define CR	0x0d
#define LF	0x0a

#define MAX_ADDR_INPUT	15
#define MAX_ADDR_OUTPUT	3

#define CHIP_CE0		P3_3
#define CHIP_CE1		P3_2
#define CHIP_UPDATE		P3_4
#define CHIP_SHDN		P3_5
#define CHIP_WR			P3_7
#define CHIP_A0			P1_0
#define CHIP_A1			P1_1
#define CHIP_D0			P1_2
#define CHIP_D1			P1_3
#define CHIP_D2			P1_4
#define CHIP_D3			P1_5
#define CHIP_DATA		P1

#define PIN_TEST		P1_6

#define WATCHDOG		P1_7

unsigned char	o_switcher[4];

unsigned char my_atoi( char *buffer )
{
	//unsigned char result;
	if( buffer[0] < '0' || buffer[0] > '9' ) return 0xff;
	if( buffer[1] < '0' || buffer[1] > '9' ) return 0xff;
	return ( (buffer[0] - '0') * 10 + (buffer[1] - '0') );
}

void channel_switch( unsigned char input, unsigned char output, bit display )
{
	if( input > MAX_ADDR_INPUT ) {
		printf( "ERROR\n\r" );
		return;
	}
	if( output > MAX_ADDR_OUTPUT ) {
		printf( "ERROR\n\r" );
		return;
	}

	o_switcher[output] = input << 2;
	o_switcher[output] += output;
	o_switcher[output] &= 0x1f;
	o_switcher[output] |= 0x20;

	CHIP_CE0 = 1;
	CHIP_CE1 = 1;
	CHIP_UPDATE = 1;
	CHIP_WR = 1;
	CHIP_DATA = o_switcher[output];
	PIN_TEST ^= 0x01;
	if( input & 0x08 ) {
		//disable CHIP0
		CHIP_CE0 = 0;
		CHIP_WR = 0;
		_nop_();
		CHIP_WR = 1;
		CHIP_CE0 = 1;
		_nop_();
		_nop_();
		CHIP_UPDATE = 0;
		CHIP_UPDATE = 1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();

		o_switcher[output] &= 0x1f;
		CHIP_DATA = o_switcher[output];
		PIN_TEST ^= 0x01;

		//enable  CHIP1
		CHIP_CE1 = 0;
		CHIP_WR = 0;
		_nop_();
		CHIP_WR = 1;
		CHIP_CE1 = 1;
		_nop_();
		_nop_();
		CHIP_UPDATE = 0;
		CHIP_UPDATE = 1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
	else {
		//disable CHIP1
		CHIP_CE1 = 0;
		CHIP_WR = 0;
		_nop_();
		CHIP_WR = 1;
		CHIP_CE1 = 1;
		_nop_();
		_nop_();
		CHIP_UPDATE = 0;
		CHIP_UPDATE = 1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		
		o_switcher[output] &= 0x1f;
		CHIP_DATA = o_switcher[output];
		PIN_TEST ^= 0x01;

		//enable  CHIP0
		CHIP_CE0 = 0;
		CHIP_WR = 0;
		_nop_();
		CHIP_WR = 1;
		CHIP_CE0 = 1;
		_nop_();
		_nop_();
		CHIP_UPDATE = 0;
		CHIP_UPDATE = 1;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}

	CHIP_DATA = 0xff;
	PIN_TEST ^= 0x01;
	if( display ) printf( "SWITCH %d TO %d\n\r", (int)input, (int)output );
	//if( display ) printf( "OK\n\r" );
}

int main()
{
	bit bInput;
	unsigned char act;
	char cmd[2];
	unsigned char addr_input;
	unsigned char addr_output;

	SCON = 0x50;
	TMOD = 0x21;
	PCON = 0x00;

	P3 = 0xff;
	P1 = 0xff;

	
	TH1 = 0xf4;
	TL1 = 0xf4;
	TR1 = 1;

	TH0 = 0xff;
	TL0 = 0xf0;
	TR0 = 1;
	ET0 = 1;

	TI = 1;
	RI = 0;
	

	ES = 0;
	EA = 1;


	WATCHDOG = 0x01;
	
	//printf( "\n\rINITIALIZING...\n\r" );

	CHIP_SHDN = 0;

	channel_switch( 0, 0, 0 );
	channel_switch( 1, 1, 0 );
	channel_switch( 2, 2, 0 );
	channel_switch( 3, 3, 0 );
	
	printf( "\n\rSTARTED\n\r" );

	bInput = 0;
	act = 0;
	cmd[0] = CR;
	addr_input = 0xff;
	addr_output = 0xff;
	while( 1 ) {
		if( cmd[0] == CR || cmd[0] == LF ) {
			if( bInput ) {
				if( act == 0 ) {
					printf( "ERROR\n\r" );
				}
				else if( act == 1 ) {
					PIN_TEST ^= 0x01;
					printf( "OK\n\r" );
				}
				else if( act == 2 ) {
					channel_switch( addr_input, addr_output, 1 );
				}
				else {
					printf( "ERROR\n\r" );
				}
				bInput = 0;
				act = 0;
			}
			cmd[0] = 0x00;
		}
		else {
			cmd[0] = getchar();
			if( cmd[0] == CR || cmd[0] == LF ) continue;
			bInput = 1;
			cmd[1] = getchar();
			if( cmd[1] == CR || cmd[1] == LF ) {
				cmd[0] = cmd[1];
				continue;
			}
			act = 0;
			if( cmd[0] == 'V' && cmd[1] == 'S' ) {
				act = 1;
				continue;
			}
			if( cmd[0] == 'C' && cmd[1] == 'H' ) {
				act = 2;
				addr_input = 0xff;
				addr_output = 0xff;
				cmd[0] = getchar();
				if( cmd[0] == CR || cmd[0] == LF ) continue;
				cmd[1] = getchar();
				if( cmd[1] == CR || cmd[1] == LF ) {
					cmd[0] = cmd[1];
					continue;
				}
				addr_input = my_atoi(cmd);
				if( addr_input == 0x00 && cmd[0] != '0' ) {
					addr_input = 0xff;
				}
				cmd[0] = getchar();
				if( cmd[0] == CR || cmd[0] == LF ) continue;
				cmd[1] = getchar();
				if( cmd[1] == CR || cmd[1] == LF ) {
					cmd[0] = cmd[1];
					continue;
				}
				if( cmd[0] != 'T' || cmd[1] != 'O' ) {
					addr_input = 0xff;
					continue;
				}
				cmd[0] = getchar();
				if( cmd[0] == CR || cmd[0] == LF ) continue;
				cmd[1] = getchar();
				if( cmd[1] == CR || cmd[1] == LF ) {
					cmd[0] = cmd[1];
					continue;
				}
				addr_output = my_atoi(cmd);
				if( addr_output == 0x00 && cmd[0] != '0' ) {
					addr_output = 0xff;
				}
				continue;
			}
		}
	}
	printf( "\n\rSHUTDOWN\n\r" );
}

void timer0 (void) interrupt TF0_VECTOR
{
	EA = 0;
	
	//PIN_TEST ^= 0x01;
	WATCHDOG ^= 0x01;

	TH0 = 0xff;
	TL0 = 0xf0;
	TR0 = 1;
	ET0 = 1;
	EA = 1;
}

⌨️ 快捷键说明

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