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

📄 driver.c

📁 编译环境:CodeWorride 5.0 使用软件模拟IIC
💻 C
字号:
#include "HEAD.H"

/*---------------------------------*/
#define _UART         //串口开启
#define _RTI          //实时中断开启
//#define _I2C
/*---------------------------------*/

void delay(word time)   //one CPU cycles = BUS time
{
  word i,count;
  
  while(time--)
  {
    for (i=0;i<9;i++)
    for (count=0;count<145;count++); 
    __RESET_WATCHDOG();
  }
}

void DRV_init(void)
{
    SOPT1 = 0b00010011;
    EnableInterrupts;
    /* Init the clock:Fbus = 4MHz */
    ICSC1_CLKS = 0;  //FLL is selected
    ICSC2_BDIV = 1;  /* Divide the BUSCLK by 1 */
    
    PTBD = 0XFF;
    PTBDD_PTBDD2 = 1;
    PTBDD_PTBDD3 = 1;
    PTBDD_PTBDD4 = 0;
    PTBDD_PTBDD5 = 1;
    PTBDD_PTBDD6 = 1;
    PTBDD_PTBDD7 = 1;
    PTBPE = 0XFF;
    PTAPE = 0XFF; 
} 

//****************************************************************************************

#ifdef _UART   //串口功能
byte ReceivedByte;
byte UARTFLAG;
byte ReceBuf[100];

//==================================
/* 初始化串口 */
void SCI_init(void){
  SCIC1 = 0X00; //normal operation,8Bit mode,idle-line wakeup
  SCIC2 = 0x2C;//Enable SCI receive interrupts, Enable transmitter and receiver
  SCIC3 = 0;   //Disable all error interrupts
  
  SCIBDH = 0x00; //SET baud rate is 19200   = Fbus/([SBR12:SBR0]*16)
  SCIBDL = 0x0E;
  SCIS1_RDRF = 0; // Clears SCI Receiver Full Flag
}
//==================================
//向串口发送一个字符  
void Send_char(byte ch)  
{    
  while (SCIS1_TDRE == 0 ); // Wait for the transmitter to be empty
  SCID = ch;  
}
//==================================
//向串口发送一个字符串,strlen为该字符串长度  
void Send_string( byte *str, word strlen)  
{  
  unsigned int k= 0 ;  
  do  {  
    Send_char(*(str + k));  
    k++;  
  } while (k < strlen);
  //Send_char (0x0D);
  //Send_char (0x0a);  
}
//==================================
//串口接收一个字符
interrupt 15 void Receive_SCI(void)
{
    while(SCIS1_RDRF == 0);
    ReceivedByte = SCID; // Load received data into a global variable
    UARTFLAG = 1;
}



#endif
//**********************************************************************************
#ifdef _RTI         //实时中断


void RTI_Init(void)
{
  SRTISC_RTIE = 0;     //关中断
  SRTISC_RTIACK = 1;   //清标志
  SRTISC_RTICLKS = 0;  //1K时钟源
  SRTISC_RTIS = 7;     //1.024秒溢出
  SRTISC_RTIE = 1;     //开中断
}

/* 实时时钟中断*/
interrupt 23 void RTI_ISR(void)
{
  static count = 0;
  
  SRTISC_RTIE = 0;
  count++;
  
  SRTISC_RTIACK = 1;
  
  LED2 = ~LED2;

  SRTISC_RTIE = 1;
  
}
#endif
//*********************************************************************************
#ifdef _I2C

void I2C_Init(void)
{
    SOPT2 = 0x00;   //选择 I2C的输出
    
    
    #ifdef MASTER  
    configureI2C(0x50);
    I2C_DATA[0]='A';	/* test data */
    #else
    configureI2C(0x52);
    #endif 
     
   					 
    #ifdef MASTER  
    ReadBytesI2C(0x52,100);

  
    WriteBytesI2C(0x52,100);

    while(I2C_STEP>IIC_READY_STATUS)
        __RESET_WATCHDOG(); /* wait for memory to be read */

#endif         
    
    
    
}

#define MASTER

#define IIC_ERROR_STATUS 0                     //错误
#define IIC_READY_STATUS 1                     //准备
#define IIC_HEADER_SENT_STATUS 2               //头发送
#define IIC_DATA_TRANSMISION_STATUS 3          //数据传输
#define IIC_DATA_SENT_STATUS 4                 //数据发送

void configureI2C(unsigned char);
void WriteBytesI2C (unsigned char,unsigned char);
void ReadBytesI2C (unsigned char,unsigned char);

//----I2C Variables -------------------------------------------------------
unsigned char I2C_STEP= IIC_READY_STATUS;
unsigned char I2C_DATA_DIRECTION = 0;      /* 1 Transmit, 0 Read */
unsigned char I2C_LENGTH = 1;
unsigned char I2C_COUNTER = 0;

unsigned char I2C_DATA[128];			/* IIC Buffer */


/* Function to configure the IIC module. */
void configureI2C(unsigned char selfAddress){
    IICC_IICEN = 1;           /* Enable IIC            */
    IICA = selfAddress;      /* IIC Address           */
    IICF = 0x4B;           /* Set IIC frequency     */    
    I2C_STEP = IIC_READY_STATUS;
    IICC_IICIE = 1;      /* Enable IIC interrupts */
    
}


void WriteBytesI2C (unsigned char slaveAddress,unsigned char numberOfBytes)
{
	unsigned char Temp;
  
    I2C_LENGTH = numberOfBytes;
    I2C_COUNTER =0;
    I2C_STEP = IIC_HEADER_SENT_STATUS;
    I2C_DATA_DIRECTION = 1;                    //data output
  
  /* Format the Address to fit in the IICA register and place a 0 on the R/W bit.*/
    slaveAddress &= 0xFE;

    IICC_IICEN = 0;
    IICC_IICEN = 1;
    Temp = IICS;                  /* Clear any pending interrupt */
    IICS_IICIF = 1;

    IICC_MST = 0;
  
    IICS_SRW = 0;
    IICC_TX = 1;				          /* Select Transmit Mode */
	IICC_MST = 1;                 /* Select Master Mode (Send Start Bit) */
    
    for(Temp=0;Temp<3;Temp++);    /* Small delay */
  
    IICD = slaveAddress;						/* Send selected slave address */
     
    return;
}


void ReadBytesI2C (unsigned char slaveAddress,unsigned char numberOfBytes)
{

    unsigned char Temp;
  
    I2C_LENGTH = numberOfBytes;

    I2C_COUNTER =0;
    I2C_STEP = IIC_HEADER_SENT_STATUS;
    I2C_DATA_DIRECTION = 0;                    //data input
  
  /* Format the Address to fit in the IICA register and place a 1 on the R/W bit. */
 
    slaveAddress &= 0xFE;
    slaveAddress |= 0x01;         /* Set the Read from slave bit. */

    Temp = IICS;                  /* Clear any pending interrupt  */
    IICS_IICIF = 1;
  
    IICC_TX = 1;				          /* Select Transmit Mode          */
	IICC_MST = 1;				        /* Select Master Mode (Send Start Bit)*/
  
    IICD = slaveAddress;						/* Send selected slave address   */
  
    return;
}


//-------------------------------------------------------------





/* Interrupt handler routine to manage all the events related
 * to the IIC module.    */

interrupt 17 void Viic_isr(void)
{

    unsigned char Temp;

	Temp = IICS;              /* ACK the interrupt */
    IICS_IICIF = 1;
 
    if(IICS_ARBL==1)
    {         /* Verify the Arbitration lost status */	     
        IICS_ARBL= 1;
	    IICC_MST = 0;	
	    I2C_STEP = IIC_ERROR_STATUS;
	        
        return;     
	}										       /* If Arbitration is OK continue */  
		

	
	if(IICC_MST==1)
	{          /* If we are the IIC Master */
	 
	 				 	 
        if(IICS_RXAK==1)
        {      /* Verify if byte sent was ACK */
	        IICC_MST = 0;
	        I2C_STEP = IIC_ERROR_STATUS;
	   
	        return;
	 	}
	 			             
        if(I2C_STEP == IIC_HEADER_SENT_STATUS)
        { /* Header Sent */
            IICC_TX = I2C_DATA_DIRECTION;
          
            I2C_STEP = IIC_DATA_TRANSMISION_STATUS; 
          
            if(IICC_TX==0)
            {  /* If we are reading data clock in first slave byte */
                Temp = IICD;
                return;
            }
        }
              
        if(I2C_STEP == IIC_DATA_TRANSMISION_STATUS)
        {	 /* If byte transmision is in progress.*/
           
            if(IICC_TX==1)
            {				               /* If Master is sending data to slave */     	 
        	            	    
        	    IICD = I2C_DATA[I2C_COUNTER];	     /* Send the next byte */
        	         	 
        	    I2C_COUNTER++;
        	    if(I2C_LENGTH <= I2C_COUNTER)
        	    {        	     
        	        I2C_STEP=IIC_DATA_SENT_STATUS;     /* Mark we are done sending Bytes */   	  
        	    }
        	    return;        	 								     /* wait until last byte sent      */
        	} 
        	else
        	{										               /* If master is reading data from slave */
        	      
        	    if((I2C_COUNTER+1) == I2C_LENGTH)  /* Master should not ACK the last byte */
        	        IICC_TXAK = 1;							   /* to indicate end of transfer         */
        	      
        	        I2C_DATA[I2C_COUNTER] = IICD;	     /* Read the next byte */       	         	 
        	        I2C_COUNTER++;
        	      																 
        	        if(I2C_LENGTH <= I2C_COUNTER)
        	        {        	     
        	            I2C_STEP=IIC_DATA_SENT_STATUS;   /* Mark we are done sending Bytes */   	  
        	        }   
        	      
        	    return;           	    					 /* Return until next byte is read */
        	}	  
        }
                   
        if(I2C_STEP==IIC_DATA_SENT_STATUS)
        {	       /* We are done with the transmition.*/ 
        	 
            I2C_STEP=IIC_READY_STATUS;	             /* Reset our status flag            */
            Temp = IICS;                            /* ACK the interrupt                */
            IICS_IICIF=1;
        	 
            IICC_TX=0;
            IICS_SRW=0;
            IICC_MST=0;
        	 			                                   /* Generate a stop condition        */        	  
            return;
        }        
    }
    else
    {			 /*  SLAVE OPERATION  */  
                   																						 

        if(I2C_STEP <= IIC_READY_STATUS)
        {	 				 /* If it is the first byte tranmited */
            I2C_STEP = IIC_DATA_TRANSMISION_STATUS;
            IICC_TX = IICS_SRW;				           /* Set the transmision reception status */
            I2C_COUNTER = 1;
           
           /* If we are receiving data read IIC1D to get free bus and get the next byte */
            if(IICC_TX==0)
            {
                Temp = IICD;
                return;
            }
        }
       	
       
		if(IICS_TCF==1)
		{
            if(IICC_TX == 0)
            {	        /* If data is received store it on the buffer */              
                I2C_DATA[I2C_COUNTER]=IICD;
                I2C_COUNTER++;
                return;          
            } 
            else
            {	                      /* Data sent by the slave */              
              
                if(IICS_RXAK==1)
                {      /* If byte is not ACK end transmision. */
                    IICC_TX = 0;
                    Temp = IICD;
                    I2C_STEP = IIC_READY_STATUS;
                    return;
                }
              
                IICD = I2C_DATA[I2C_COUNTER];
                I2C_COUNTER++;
                return;          
            }
        }
   }
}




#endif

⌨️ 快捷键说明

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