基于51和Protues仿真的82C55A 软件模拟读写时序


仿真图:

源代码:

/***************   writer:shopping.w   ******************/
#include <reg52.h>
#include <absacc.h>
#define uint unsigned int
#define uchar unsigned char
//#define PA XBYTE[0x0000] 
//#define PB XBYTE[0x0001]
//#define PC XBYTE[0x0002]
//#define COM XBYTE[0x0003]
#define ADDR_CMD              0x03
#define ADDR_A_PORT           0x00
#define ADDR_B_PORT           0x01
#define ADDR_C_PORT           0x02


#define C_PORT_LOW_IN_CMD     (0x01<<0)
#define C_PORT_LOW_OUT_CMD    (~(0x01<<0))
#define B_PORT_IN_CMD         (0x01<<1)
#define B_PORT_OUT_CMD        (~(0x01<<1))
#define C_PORT_UP_IN_CMD      (0x01<<3)
#define C_PORT_UP_OUT_CMD     (~(0x01<<3))
#define A_PORT_IN_CMD         (0x01<<4)
#define A_PORT_OUT_CMD        (~(0x01<<4))

#define _LE_L  (_LE = 0)
#define _LE_H  (_LE = 1)
#define _WR_L  (_WR = 0)
#define _WR_H  (_WR = 1)
#define _RD_L  (_RD = 0)
#define _RD_H  (_RD = 1)
#define _CS_L  (_CS = 0)
#define _CS_H  (_CS = 1)


sbit _LE = P2^7;
sbit _WR = P2^5;
sbit _RD = P2^6;
sbit _CS = P2^4;



uchar code DSY_CODE_Queue[]=
{
	0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
};

uchar code DSY_Index[]=
{
 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80
};

void WriteData8255(uchar addr, uchar dat);
uchar ReadData8255(uchar addr);


void Delay(uint x)
{
 	uchar i;
	while(x--)
	{
	 	for(i=0;i<120;i++);
	}
}

void WriteData8255(uchar addr, uchar dat)
{
		_CS_H;
	  _WR_H;
	  _RD_H;
	  _LE_H;
	  Delay(10);
	  _CS_L;
	  P0 = addr;
	  Delay(10);
	  _LE_L;    //锁存地址 
	  P3 = dat;
	  Delay(10);
	  _WR_L;
	  Delay(10);
	  _WR_H;
	  Delay(10);
	  _CS = 1;
}

uchar ReadData8255(uchar addr)
{
	  uchar rval = 0;
	  _CS_H;
		_WR_H;
		_RD_H;
	
		_LE_H;
		Delay(10);
		_CS_L;
		P0 = addr;
		Delay(10);
		_LE_L;      //锁存地址
		_RD_L;      //启动读信号 
		P3 = 0xff;  //在置位后才能去读
		rval = P3;  
		Delay(10);
		_RD_H;
		Delay(10);
		_CS_H;
	  return rval;
}


void main()
{
	uchar value = 0;
	uchar i,j,k;


#if 1
	_CS = 1;
	_WR = 1;
	_RD = 1;
	//写控制字寄存器地址
	_LE = 1;
	Delay(10);
	_CS = 0;
	P0 = ADDR_CMD;  //A1A0=11,CS=0,写控制字
	Delay(10);
	_LE = 0;  //锁存器锁定地址
	P3 = 0x80; //PA,PB,PC全输出(方式选择)
	Delay(10);
	_WR = 0;
	Delay(10);
	_WR = 1;
	Delay(10);
	_CS = 1;
#endif
  WriteData8255(ADDR_CMD, 0x80); //配置8255工作在MOD0模式,PA,PB,PC全输出。

 
	while(1)
	{
#if 1
		 	for(k=0;k<8;k++)
			{
				WriteData8255(ADDR_C_PORT, DSY_Index[k]);
				WriteData8255(ADDR_A_PORT, DSY_CODE_Queue[k]);
//				/*写B端口数据,A1,A0 = 2'b01*/
//				_CS = 1;
//				_WR = 1;
//				_RD = 1;
//				
//		    _LE = 1;
//				Delay(10);
//				_CS = 0;
//				P0 = ADDR_C_PORT;  
//				Delay(10);
//				_LE = 0;
//			  P3 = DSY_Index[k];
//			  Delay(10); 
//				_WR = 0;
//				Delay(10);
//				_WR = 1;
//				Delay(10);
//				_CS = 1;
//				Delay(10);
//	
//			 	/*写A端口数据,A1,A0 = 2'b00*/
//				_CS = 1;
//				_WR = 1;
//				_RD = 1;
//				
//				_LE = 1;
//				Delay(10);
//				_CS = 0;
//				P0 = ADDR_A_PORT;
//				Delay(10);
//				_LE = 0;
//				P3 = DSY_CODE_Queue[k]; //PA
//				Delay(10);
//				_WR = 0;
//				Delay(10);
//				_WR = 1;
//				Delay(10);
//				_CS = 1;
				Delay(200);
			}
			
#endif
			
//		_CS = 1;
//		_WR = 1;
//		_RD = 1;
//		//写控制字寄存器地址
//		_LE = 1;
//		Delay(10);
//		_CS = 0;
//		P0 = ADDR_CMD;  //A1A0=11,CS=0,写控制字
//		Delay(10);
//		_LE = 0;
//		P3 = 0x82; //PB输入,PA,PC输出(方式选择)
//		Delay(10);
//		_WR = 0;
//		Delay(10);
//		_WR = 1;
//		Delay(10);
//		_CS = 1;
	 WriteData8255(ADDR_CMD, 0x82); //PB输入,PA,PC输出(方式选择)
   // while(1);
//	  /*读B端口数据,A1,A0 = 2'b02*/
//	  _CS = 1;
//		_WR = 1;
//		_RD = 1;
//		  
//		_LE = 1;
//		Delay(10);
//		_CS = 0;
//		P0 = ADDR_B_PORT;
//		Delay(10);
//		_LE = 0;
//		_RD = 0;
//		P3 = 0xff;  //在置位后才能去读
//		value = P3; //读取PB的值
//		Delay(10);
//		_RD = 1;
//		Delay(10);
//		_CS = 1;
		value = ReadData8255(ADDR_B_PORT);
		
		P1 = value; //将读取到的值赋值给P1
		Delay(200);
		while(1);
	}


	
	
	
	
	
	
}


猜你喜欢

转载自blog.csdn.net/graduation201209/article/details/76980618