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

📄 microwire.c

📁 microwire协议
💻 C
字号:
/********************** Micro_wire基本操作函数******************************
[文件名] microwire.c+microwire.H+ASMselect.h
[版本] TV.1
[创建] 阿春x
[修正] 阿春x
[最后修改时间] 	2006.04.20 20:30
[验 正 时  间]		2006.04.24 15:30
[功能说明]指定地址数据块读写,需指定数据格式(16位或8位)和数据输入输出线是否共用,
参数:片选io口,地址,地址位数,数据个数,数据缓冲区地址。
***************************************************************************/
#include "ASMselect.h"
#if ASMmicrowire
#include "reg51x.h"
#include "tab.h"
#include <stdio.h>
#include <intrins.h>
//#include "ioprot.h"
//---------------------
sbit DO= P1^1;//if DO,DI short,DO=P1^1;
sbit DI=P1^1;
sbit SK=P1^2;
sbit CS_93C46=P1^3;	
/*	TSSOP PDIP SOIC
PIN 1- CS  HIGH active
PIN 2- SK  Clock
PIN 3- DI   Data Input  DI and DO can short
PIN 4- DO  Data Output
PIN 5- GND 
PIN 6- VCC 1.8/2.5/2.7~5.5V
PIN 7- ORG  '1' or open data is 16 bit, '0' data 8 bit
PIN 8- NC   
93c46 1K: 128 x 8 or 64 x 16(addr 7 bit or 6 bit)
93c56 2K: 256 x 8 or 128 x 16(addr 8 bit or 7 bit)
93c66 4K: 512 x 8 or 256 x 16(addr 9 bit or 8 bit)  */
sbit CS_lcd=P1^3;
sbit CS_timer=P1^3;
//---------------------
extern void DELAY_Xms(unsigned int count);
uchar Micro_read_8bit();
Micro_write_nbit(uint txdata,uchar txcount);
cs_process(uchar cs_pin,bit hilow);
//Micro_write_8bit(uchar idata txdata);
//void DELAY_X20us( int xus);
#if store_data_8bit
Micro_write_block_data8(uchar cs_pin,uint addr ,uchar ucaddr_count,uchar ucdata_count,uchar *ucdata_buf){
	uchar i;
	SK=0;
for(i=0;i<ucdata_count;i++){
	cs_process(cs_pin,0);
	cs_process(cs_pin,1);
	DI=1;SK=1;SK=0;//Start bit			//cs _/------------------------\_	
	Micro_write_nbit(0x060,9);//write en //di      ---\ _   _ / -  -\    _   _  _  _       =1 0011xxxxx write enable 
	cs_process(cs_pin,0);			//sk      -\_ -\_-\_-\_-\_-\_-\_-\_-\_
	//---------
	cs_process(cs_pin,1);
	DI=1;SK=1;SK=0;//Start bit			//cs _/---------	
	DI=0;SK=1;SK=0;//command:01 write//di      ---\ _ / -	=1 01 write command
	DI=1;SK=1;SK=0;//command		//sk      -\_ -\_-\_-	
	Micro_write_nbit(addr,ucaddr_count);	//wirte addr
	Micro_write_nbit(*ucdata_buf,8);		//wirte tx 8 data
//	Micro_write_8bit(*ucdata_buf);		//wirte tx 8 data
	cs_process(cs_pin,0);
	DELAY_Xms(11);
	//----------
	cs_process(cs_pin,1);
	DI=1;SK=1;SK=0;//Start bit				//cs _/------------------------\_	
	Micro_write_nbit(0x000,9);//write ds	//di      ---\ _   _  _   _   _   _   _  _  _       =1 0000xxxxx  write disable 
	cs_process(cs_pin,0);				//sk      -\_ -\_-\_-\_-\_-\_-\_-\_-\_
	addr++;
	ucdata_buf++;
	}

}
Micro_read_block_data8(uchar cs_pin,uint addr ,uchar ucaddr_count,uchar ucdata_count,uchar *ucdata_buf){
	uchar i;
	cs_process(cs_pin,0);
	for(i=0;i<ucdata_count;i++){
		SK=0;
		cs_process(cs_pin,1);
		DI=1;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///Start bit			//cs _/---------	
		DI=1;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///command:01 write//di      ----- \ _ __	=1 10 read command
		DI=0;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///command		//sk      -\_ -\_-\_-
		Micro_write_nbit(addr,ucaddr_count);//rx 8 bit data 
		DI=1;
		*ucdata_buf=Micro_read_8bit();
		#if debug
		printf("rx%bx=%bx",i,*ucdata_buf);
		#endif
		addr++;
		ucdata_buf++;
		cs_process(cs_pin,0);
		DELAY_Xms(1);
	}
}
#endif
#if store_data_16bit
Micro_write_block_data16(uchar cs_pin,uint addr ,uchar ucaddr_count,uchar ucdata_count,uint *uidata_buf){
	uchar i;
	SK=0;
for(i=0;i<ucdata_count;i++){
	cs_process(cs_pin,0);
	cs_process(cs_pin,1);
	DI=1;SK=1;/*DELAY_X20us(1);*/
	SK=0;/*DELAY_X20us(1);*///Start bit	//cs _/------------------------\_	
	Micro_write_nbit(0x30,8);//write en		 //di      ---\ _   _ / -  -\    _   _  _  _       =1 0011xxxx write enable 
	cs_process(cs_pin,0);				//sk      -\_ -\_-\_-\_-\_-\_-\_-\_-\_
	//---------
	cs_process(cs_pin,1);
	DI=1;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///Start bit			//cs _/---------	
	DI=0;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///command:01 write//di      ---\ _ / -	=1 01 write command
	DI=1;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///command		//sk      -\_ -\_-\_-	
	Micro_write_nbit(addr,ucaddr_count);	//wirte addr
	Micro_write_nbit(*uidata_buf,16);		//wirte rx 16 bit data
	cs_process(cs_pin,0);
	DELAY_Xms(11);
	//----------
	cs_process(cs_pin,1);
	DI=1;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///Start bit			//cs _/------------------------\_	
	Micro_write_nbit(0x00,8);//write ds	//di      ---\ _   _  _   _   _   _   _  _  _       =1 0000xxxx  write disable 
	cs_process(cs_pin,0);			//sk      -\_ -\_-\_-\_-\_-\_-\_-\_-\_
	addr++;
	uidata_buf++;
	}

}
Micro_read_block_data16(uchar cs_pin,uint addr,uchar ucaddr_count,uchar ucdata_count,uint *uidata_buf){
	uchar i;
	union {
		uint uidata;
		uchar ucdata[2];
		}datax;
	cs_process(cs_pin,0);
	for(i=0;i<ucdata_count;i++){
		SK=0;
		cs_process(cs_pin,1);
		datax.uidata=0;
		DI=1;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///Start bit		//cs _/---------	
		DI=1;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///command:01 write//di      ----- \ _ __	=1 10 read command
		DI=0;SK=1;/*DELAY_X20us(1);*/SK=0;/*DELAY_X20us(1);*///command		//sk      -\_ -\_-\_-		Micro_write_nbit(addr,ucaddr_count);
		Micro_write_nbit(addr,ucaddr_count);//tx addr
		DI=1;
		datax.ucdata[0]=Micro_read_8bit();//rx 16 bit data
		datax.ucdata[1]=Micro_read_8bit();
		*uidata_buf=datax.uidata;
		addr++;
		uidata_buf++;
		cs_process(cs_pin,0);
		DELAY_Xms(1);
	}

}
#endif
/*
Micro_write_8bit(uchar txdata){
	char i;
	for(i=0;i<8;i++){
		if((txdata & 0x80 )==0){
			DI=0;
		}
		else {
			DI=1;	
		}		
		SK=1;
		txdata<<=1;
		SK=0;
		}
	}*/
Micro_write_nbit(uint txdata,uchar txcount){
	uchar i;
	for(i=16;i>0;i--){
		if(i<=txcount){
			if((txdata&0X8000)!=0){
				DI=1;
				SK=1;/*DELAY_X20us(1);*/
				}
			else {
				DI=0;
				SK=1;/*DELAY_X20us(1);*/
				}
			SK=0;/*DELAY_X20us(1);*/
			}
		txdata<<=1;
		}
	}
uchar Micro_read_8bit(){
	uchar  rxdata=0;
	uchar i;
	for(i=0;i<8;i++){
		SK=0;/*DELAY_X20us(1);*/
		SK=1;/*DELAY_X20us(1);*/
		rxdata<<=1;
		if(DO)rxdata++;		
		}	
	return(rxdata);
	}

cs_process(uchar cs_pin,bit hilow){
	switch(cs_pin){
		case 1:
			CS_93C46=hilow;
			break;
		case 2:
			CS_lcd=hilow;
			break;
		case 3:
			CS_timer=hilow;
			break;
		default:
			break;
		}
}
//if define delay_xms not define//=================================	
void DELAY_Xms(unsigned int count)//  DELAY X ms time	f=12M
{
  unsigned char data i,j;
  for (i = 0;i < count;i++)
  {
   for (j = 0;j <249;j++)
   {
    _nop_();
   }
  }
} //*/
/*/======================DELAY_X20us(delay=20*xs+10us(f=12M)D 30us~655ms==================
void DELAY_X20us( int xus){
 	int s1;
	for (s1=1;s1<xus;s1++){
	_nop_();_nop_();_nop_();_nop_();
	}

}//*/
#endif

⌨️ 快捷键说明

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