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

📄 z7290.c

📁 DSP与周立功ZLG7290的接口程序
💻 C
字号:
/*********************************************************************************
  *		Copyright (c) 2005-2006, Chengdu University of Information Technology  
  *		All rights reserved.
  *		
  *		Filename: Z7290.c
  *		Abstract:  ZLG7290 人机接口测试
  * 			   K0-K9 (S10,S1-S9) 定义为数字键用于输入和修改数字显示
  * 			   ZLG7290的最大的通信速度是32k bit/s,间隔5000~3125*10(-8),
  *				   位时间 32us,DSP的主时钟是100M(0.01us).	   
  *
  *		Current version: v3.0
  *		Author/Modifier: YangSS
  *		Completed: 2005/11/11
  *******************************************************************************/


#include "Z7290.h"
//#define   DPY_CMD      1

/************************************************************
 **				      WARNING                              **
 ** HD7/SCL STEUP TIME(LOW OR HIGH) MUST LONGER THAN 5US!  **
 ************************************************************/
	
  /*******************************************************
    *	FUNCTION: main
    * PURPOSE:  the main function 
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************/
void main()
{
	//int digNum = 6;
	int result = 0;
	int lstBit = 0;
    
	*SWWSR = 0x3610;
	*PMST = 0x0ffa0;
	Intialize();
  
	while(1)
	{
		
		result = I2cRBytes(ZLG_SLA_W, ZLG_SLA_R, SYS_SUB);
		while (result == 0)
		{
			lstBit = i2cRBuf & 0x01;
			if  (lstBit == 1)
			{
				
				I2cRBytes(ZLG_SLA_W, ZLG_SLA_R, KEY_SUB); 			
				keyNum = i2cRBuf;
				
				#ifndef	DPY_CMD
				/* Display the  key number*/ 
				i2cCmdBuf = (int *)0x8000;
				i2cCmdBuf = i2cCmdBuf+4;
				if (keyNum > 9)
				{
					*i2cCmdBuf = *(i2cDpyBuf+keyNum/10); 
					i2cCmdBuf = i2cCmdBuf+1;
					*i2cCmdBuf = *(i2cDpyBuf+keyNum%10);
					i2cCmdBuf = (int *)0x8000;
				    I2cWBytes(ZLG_SLA_W, DPRAM_SUB, 6);
				 }
				else
				{
				    *i2cCmdBuf = *(i2cDpyBuf+keyNum);
				    i2cCmdBuf = i2cCmdBuf+1;
				    *i2cCmdBuf = 0x00;
				    i2cCmdBuf = (int *)0x8000;
				    I2cWBytes(ZLG_SLA_W, DPRAM_SUB, 6);
				}
			    #endif
			 	 
				 /*i2cCmdBuf = (int *)0x8000;
				 *i2cCmdBuf = 0x60+digNum;
				 i2cCmdBuf = i2cCmdBuf+1;
				 *i2cCmdBuf = keyNum;
				 i2cCmdBuf = (int *)0x8000;
				 I2cWBytes(ZLG_SLA_W, CMD_SUB, 2); 			
				 */				
			}
		
			result = 1;
			i2cRBuf = 0;
			keyNum = 0; 
		}
	}	
}


  /*******************************************************
    *	FUNCTION: Delay
    * PURPOSE:  delay
    * PARAMETERS: val_us - time(us) of delay.
    * RETURNS: NONE
    *******************************************************/
void Delay(int val_us)
{
	 int i;		                  
	 
	 for (i=0; i<(val_us * 100); i++)
	 { }
	 
}

/*******************************************************
    *	FUNCTION: Delay
    * PURPOSE:  delay
    * PARAMETERS: val_us - time(us) of delay.
    * RETURNS: NONE
    *******************************************************/
void Intialize(void)
{
  //DspFrequ();
  
  (*i2cCmdBuf) = 0x00fc;
  i2cCmdBuf = i2cCmdBuf+1;  
  (*i2cCmdBuf)  =0x60;  
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf) = 0x00da;
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf)  =0x00f2;  
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf) = 0x0066;
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf)  =0x00b6;  
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf) = 0x0000be;
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf)  =0x00e0;  
  i2cCmdBuf = (int *)0x8000;
  I2cWBytes(ZLG_SLA_W, DPRAM_SUB, 8);
  
  Delay(100); 
  (*i2cCmdBuf) = 0x00;
  i2cCmdBuf = i2cCmdBuf+1;  
  (*i2cCmdBuf)  =0x00;  
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf) = 0x00;
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf)  =0x00;  
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf) = 0x00;
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf)  =0x00;  
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf) = 0x00;
  i2cCmdBuf = i2cCmdBuf+1; 
  (*i2cCmdBuf)  =0x00; 
  i2cCmdBuf = (int *)0x8000;
  I2cWBytes(ZLG_SLA_W, DPRAM_SUB, 8);
  
}
  
  /*******************************************************
    *	FUNCTION: I2cStart
    * PURPOSE:  when the SCL is high ,SDA change into high from low!
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************/
void I2cStart(void)
{
	 *GPIOCR = 0x00c0;
	 
	 /* SDA must high,while SCL keep */
	 *GPIOSR = (*GPIOSR) | SDA_1;					
	 Delay(5);
	 *GPIOSR = (*GPIOSR) | SCL_1;
	 /* Start set up need 4.7us */
	 Delay(5);
	 	

	 /* Transmit the Start begining */
	 *GPIOSR = (*GPIOSR) & SDA_0;					
	 Delay(5);
	 /* I2C bus now start really */
	 *GPIOSR = (*GPIOSR) & SCL_0;		
	 /* Transmit or receive data */
	 Delay(20);	
	 // 7.28 
	 //*GPIOCR = 0x0080;
	 								
}

  /*******************************************************
    *	FUNCTION: I2cStop
    * PURPOSE:  when the SCL is high ,SDA change into low from high!
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************/
void I2cStop(void)
{
	/* Emited out maybe */
	 *GPIOCR = 0x00C0;                              
	 Delay(5);
	 
	 /* The data signal for I2C Stop */
	 *GPIOSR = (*GPIOSR) & SDA_0;					
	 Delay(5);
	 
	 /* The clock signal for Stop */
	 *GPIOSR = (*GPIOSR) | SCL_1;		
	 /* Stop set up need 4.7us */
	 Delay(5);	
	 
	 /* I2C bus Stop really */
	 *GPIOSR = (*GPIOSR) | SDA_1;					
	 Delay(20);
}	

  /********************************************************************
    *	FUNCTION: MSendAck
    * PURPOSE:  when master transmition finishes,master should emit a pulse SDA_1 
    *        	to release the bus and then change into input from output status, 
    *         	receive the slave Ack signal!(1----normal,0------abnormal)
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************************/
void MSendAck(int val)
{ 
	/* Emited out maybe */
	
	 *GPIOCR = 0x00C0;                              
	 Delay(5);

	 /*  Change into output */
	 if (val == 1)
	 {
	  	*GPIOSR = (*GPIOSR) | SDA_1;   
	 }
	 else
	 {
	    *GPIOSR = (*GPIOSR) & SDA_0;
	 }							
	 Delay(5);
	 
	 *GPIOSR = (*GPIOSR) | SCL_1;
	 Delay(10);

	 /* Receive the Ack */
	 *GPIOSR = (*GPIOSR) & SCL_0; 
	 Delay(5);					
}

  /********************************************************************
    *	FUNCTION: I2cWByte
    * PURPOSE:  Data(Sla_address,sub_address,data) should put in 
    *			i2cWBuf,and then transmit through SDA at the up of SCL 
    *			one bit by one bit When transmit finished,check ACK !
    * PARAMETERS: wByte -
    * RETURNS: NONE
    *******************************************************************/
void I2cWByte(int wByte)
{
	int transCnt = 0;
	int mstBit = 0;
	
	i2cWBuf = wByte;
	//*GPIOCR = 0x00c0;
	Delay(5);
	*GPIOSR = (*GPIOSR) & SCL_0;
	
	while (transCnt < 8)
	{
		Delay(7);
	 	  
		//i2cWBuf = wByte;
		mstBit = (i2cWBuf << transCnt)&0x0080;
		
		/* Transmit frame ,high bit first! */
		if ( mstBit == 128)	
		{
			*GPIOSR = (*GPIOSR) | SDA_1;
		}
		else
		{
			*GPIOSR = (*GPIOSR) & SDA_0;
		}
	            
		Delay(7);   
		
		/* High interval big 4.7us */	
		*GPIOSR = (*GPIOSR) | SCL_1;				
		Delay(20);
		*GPIOSR = (*GPIOSR) & SCL_0;
		transCnt ++;
	 }
	 
 	I2cRecAck();		   	 	
}

/********************************************************************
    *	FUNCTION: I2cRecAck
    * PURPOSE:   After write a byte ,master must wait the ACK signal
    *		    from slave to juge whether continue communitation.
    *		    checkAck=0 - communication continue !
    *	    	checkAck=1 - communication must stop for error!
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************************/
void I2cRecAck(void)
{
	/* SCL must be low first */
	*GPIOSR = (*GPIOSR) & SCL_0;
	/* SCL low steup time big 5us*/ 
	Delay(5);
	
	/* MASTER Release the bus & wait Ack */
	*GPIOSR = (*GPIOSR) | SDA_1;					
	Delay(5);
	
	/* HD6/SDA change to input */
	*GPIOCR = (*GPIOCR) & 0x0bf;
	Delay(0);
	
	*GPIOSR = (*GPIOSR) | SCL_1;
	Delay(5);
	
	/* Check whether SDA is pull to low*/ 
	do 
	{
	  if ( ((*GPIOSR) & 0x0040) == SDA_1)
	  {
		     checkAck = 1;
      }
	  else 
	  {
		     checkAck = 0;
	  }
	}while (checkAck != 0);
	Delay(5);
	
	/* HD6/SDA change to output again*/
	*GPIOCR = (*GPIOCR) | 0x0040;
	Delay(5);
	
	*GPIOSR = (*GPIOSR) & SCL_0;
	Delay(5);
	
}
  /********************************************************************
    *	FUNCTION: I2cRByte
    * PURPOSE:  Read out the bits(MSB first) from the I2C bus at the up 
    *           of  SCL pulse.
    *			Not transmit Ack signal to SLAVE.
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************************/
void I2cRByte(void)
{
	  int reiveCnt=0;
	  Delay(5);
	  
	  /*  The HD6/SDA status is input */
	  *GPIOSR = (*GPIOSR) | SDA_1;					
	  *GPIOCR = (*GPIOCR) & 0x0bf;
	  Delay(5);
	  
	 /* Receive frame */
	  while (reiveCnt < 8)    						
	  {	
	  	  Delay(5);	
	  	  
		  /* Clock is low and prepare to receive data!*/
	  	  *GPIOSR = (*GPIOSR) & SCL_0;				
	  	  Delay(10);
	  	  
		  /* Clock is high and enable data on SDA */
	  	  *GPIOSR = (*GPIOSR) | SCL_1;				
	  	  Delay(10);
	  	  
	  	  i2cRBuf = i2cRBuf << 1;
	  	  rFlag = (*GPIOSR) & SDA_1;
		  
	  	  if (rFlag == SDA_1)
	  	  {
			i2cRBuf = i2cRBuf | 0x01;
	  	  }
	  	  else
	  	  {
			i2cRBuf = i2cRBuf | 0x00;
	  	  }
		  
	  	  
	  	  Delay(5);
	  	  //*GPIOSR = (*GPIOSR) & SCL_0;
	  	  reiveCnt++;
	  	  
	   }
	 i2cRBuf = i2cRBuf & 0x0ff;
	  
	 Delay(5);
	 *GPIOSR = (*GPIOSR) & SCL_0;  
	 Delay(5); 
	 
	 MSendAck(1);
	 I2cStop();
}

  /********************************************************************
    *	FUNCTION: I2cWBytes
    * PURPOSE:  Write 1 byte into the specified address.
    *		    return 0:means the performance successfui
    *		    return 1:means the performance fall
    * PARAMETERS: slaAdre - 
    *			  subAdres - 
    *			  wData -
    * RETURNS: 1 - error, 0 - success
    *******************************************************************/
int I2cWBytes(int slaAdreWrite, int subAdre, int Nm)
{
     int i = 0;
	 i2cWBuf = (*i2cCmdBuf);
	 
	 I2cStart();
	 Delay(5);
	 
	 I2cWByte(slaAdreWrite);
	 Delay(5);
	 if (checkAck == 1)
	 {
	 	return (1);
	 }
	 
	 I2cWByte(subAdre);
	 if (checkAck == 1)
	 {
	 	return (1);
	 }
	 
	 /* Write the first 8 bytes to 7290 
	 I2cWByte(i2cWBuf);
	 if (checkAck == 1)
	 {
	 	return (1);
	 }
	 
	  Write next 8 bytes to 7290
	 i2cWBuf = i2cWBuf>>8;
	 I2cWByte(i2cWBuf);
	 if (checkAck == 1)
	 {
	 	return (1);
	 }
	 
	 if (checkAck == 1)
	 {
	 	return (1);
	 }*/
	 
	 /* Standard write many bytes */
	 for (i=0 ; i<Nm; i++)
	 {
	 	I2cWByte(i2cWBuf);
	 	if (checkAck == 1)
	 	{
	 		return (1);
		}
		i2cCmdBuf = i2cCmdBuf+1; 
		i2cWBuf = (*i2cCmdBuf);
	 }
	 
	 i2cCmdBuf = (int *)0x8000;
	 I2cStop();
	 
	 return(0);
}

  /********************************************************************
    *	FUNCTION: I2cRBytes
    * PURPOSE:  read  out 1 byte from the specified address of slave!
    *      		  return 1:means the performance successful
    *		         return 0:means the performance fall@
    * PARAMETERS: slaAdre - 
    *				subAdres - 
    * RETURNS: 1 - error, 0 - success
    *******************************************************************/
int I2cRBytes(int slaAdreWrite, int slaAdreRead, int subAdres)
{
	/* Start I2C bus */
	 I2cStart();
	
	/* Tranmit Slave address*/ 
	
	 I2cWByte(slaAdreWrite);							
	
	 if (checkAck == 1)
	 {
	 	return (1);
	 }
	 
	 /* Tranmit subaddress of the specified slave*/
	 I2cWByte(subAdres);
	 
	 if (checkAck == 1)
	 {
	 	return (1);
	 }
	 
	 I2cStart();		
	 I2cWByte(slaAdreRead);
	 if (checkAck == 1)
	 {
	 	return (1);
	 }
	 
	 I2cRByte();
	 return (0);
}

/********************************************************************
    *	FUNCTION: UartRest
    * PURPOSE:  Dsp Frequency 10M
    * PARAMETERS: NONE
    * RETURNS: NONE
    *******************************************************************/
void DspFrequ(void)
{
  *CLKMD = 0x00;
  Delay(1);
  
  while ((*CLKMD)&0x0001 != 0)
  {
  	Delay(1);	
  }
  
  *CLKMD = 0x03ef;
  Delay(1);
  
}

⌨️ 快捷键说明

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