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

📄 iic.c

📁 iic总线控制led,我就这是根据linux下面的驱动自己改的
💻 C
字号:
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/*-------------------------------------------------------------------------------*/
/////////////
/////////////
/////////////
#include "../inc/reg2410.h"
#include "lib.h"
#include "/inc/macro.h"
#include "../inc/drivers.h"

#include <string.h>
#include <stdio.h>
///////////////////////////////////////////////////////////////////////////////////
#define LEDADDR 0x0000010    //LED address
#define IICACK  1<<7         //ACK enable
#define IICCLK  1<<6         //IICCLK = FCLK/512
#define TRINT   1<<5         //Tx/Rx interrupt enable
#define ENTXRX  1<<4         //enable Tx/Rx
#define MASTMOD 11<6         //mast transmit mode
#define MASRMOD 10<<6        //mast receive mode

U32 f_nGetACK;

void iic_led_init(void)
{
	set_gpio_ctrl(GPIO_E14|GPIO_MODE_ALT0|GPIO_PULLUP_DIS);//set GPIO_E14 for IICSDA 
	set_gpio_ctrl(GPIO_E15|GPIO_MODE_ALT0|GPIO_PULLUP_DIS);//set GPIO_E15 for IICSCL
	//rSRCPND = rSRCPND;
	//rINTPND = rINTPND;
	
}

void iic_write_led(U32 unSlaveAddr,U32 unAddr,U8 ucData)
{
	//just allow the interrupt, becease the mode when iic inactive is always slave mode ,and you can't change
/*
	When the IIC-bus interface is inactive, it is usually in Slave mode. In other words, the interface should be in
Slave mode before detecting a Start condition on the SDA line (a Start condition can be initiated with a High-to-
Low transition of the SDA line while the clock signal of SCL is High). When the interface state is changed to
Master mode, a data transfer on the SDA line can be initiated and SCL signal generated.
*/
	//
	rIICCON   = IICACK|IICCLK|TRINT;	        // Enable ACK, (interrupt), set IICCLK=MCLK/512  0x11100000  
	rIICSTAT  = ENTXRX;     			        // Enable TX/RX , master tansmit mode  0x1101 0000
    f_nGetACK=1;
      					    // Send device address
    rIICDS = unSlaveAddr;	// 0x70
    rIICSTAT = 0xf0;		// Master Tx,Start  
    while(f_nGetACK == 1);	// Wait ACK
    f_nGetACK=1;
     					
////////////////////////////////////////////////////////////////////////////     					
/////////////////////////////////////////////////////////////////////////////    
 						
     						// Send word address
    rIICDS = unAddr;
    rIICCON = 0xef;			// Resumes IIC operation.
    while(f_nGetACK == 1);	// Wait ACK
 	f_nGetACK = 1;
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////  
  
     						// Send data
     rIICDS = ucData;
    rIICCON = 0xef;			// Resumes IIC operation.
    while(f_nGetACK == 1);	// Wait ACK
 	f_nGetACK = 1;	
    
    						// End send
    rIICSTAT = 0xd0;		// Stop Master Tx condition
	rIICCON = 0xef;			// Resumes IIC operation.
	   
    
    hudelay(5);				// Wait until stop condtion is in effect.
}


void iic_read_led(U32 unSlaveAddr,U32 unAddr,U8 *pData)
{

	rIICCON   = IICACK|IICCLK|TRINT;	        // Enable ACK, (interrupt), set IICCLK=MCLK/512  0x11100000  
	rIICSTAT  = ENTXRX;     			        // Enable TX/RX , master tansmit mode  0x1101 0000
  
	f_nGetACK = 1;
	
    /*--------------------------------------------------------*/	
    //Dummy write				
    						// Send device address 
    rIICDS = unSlaveAddr;	// Write slave address to IICDS
    rIICSTAT = 0xf0;		// Master Rx,Start
    while(f_nGetACK == 1);	// Wait ACK
	f_nGetACK = 1;
	
    						// Send word address 
    rIICDS = unAddr;
    rIICCON = 0xef;			// Resumes IIC operation.
    while(f_nGetACK == 1);	// Wait ACK
    f_nGetACK = 1;
    /*--------------------------------------------------------*/
    
	rIICDS = unSlaveAddr+1;	// Send device address
    rIICSTAT = 0xb0;		// Master Rx,Start
    rIICCON = 0xef;			// Resumes IIC operation.  
    while(f_nGetACK == 1);	// Wait ACK
	f_nGetACK = 1;
	
	
    *pData = rIICDS;
    hudelay(1);
    
	// End receive 
    rIICSTAT = 0x90;		// Stop Master Rx condition 
    rIICCON = 0xef;			// Resumes IIC operation.
 
 	rSRCPND = rSRCPND;
	rINTPND = rINTPND;
	
    hudelay(5);				// Wait until stop condtion is in effect.
}


void IIC_IrqHandler(void) 
{
	rSRCPND = rSRCPND;
	rINTPND = rINTPND;

	f_nGetACK=0;
}

⌨️ 快捷键说明

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