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

📄 main.c

📁 i2c的调适成学。ARM7 交流使用
💻 C
字号:
/****************************************Copyright (c)***************************************************
**                               Guangzou ZLG-MCU Development Co.,LTD.
**                                      graduate school
**                                 http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name:			main.c
** Last modified Date:  2004-09-16
** Last Version:		1.0
** Descriptions:		The main() function example template
**
**------------------------------------------------------------------------------------------------------
** Created by:			Chenmingji
** Created date:		2004-09-16
** Version:				1.0
** Descriptions:		The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:         Li Baihua
** Modified date:       2008-04-08
** Version:             1.1
** Descriptions:        I2C总线对CAT1024进行读写操作
**
*********************************************************************************************************/
#include "config.h"
#include "I2CINT.h"

#define     LED1        1 <<  17                                            /*  控制LED1闪烁            */
#define     LED2        1 <<  18                                            /*  控制LED2闪烁            */

#define	    CAT1024		0xA0		                                        /*  CAT1024器件从地址		*/

/*********************************************************************************************************
** Function name:		DelayNS
** Descriptions:		长软件延时
** input parameters:    uiDly	延时参数,值越大,延时越久
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void DelayNS(uint32 uiDly)
{
    uint32 i;
    for(; uiDly > 0; uiDly--)
    {
        for(i=0; i<50000; i++);
    }
}

/*********************************************************************************************************
** Function name:		I2cInit
** Descriptions:		I2C初始化
** input parameters:    uiFi2c	I2C总线频率(最大400K)
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
void I2cInit(uint32 uiFi2c)
{
	if (uiFi2c > 400000)
		uiFi2c = 400000;
		
   	PINSEL0 = (PINSEL0 & (~0xF0)) | 0x50; 	                            /* 设置I2C引脚连接              */
	I2SCLH  = (Fpclk / uiFi2c + 1)  / 2;						        /* 设定I2C时钟 					*/
	I2SCLL  = (Fpclk / uiFi2c) / 2;
	
	I2CONCLR = 0x2C;
	I2CONSET = 0x40;									                /* 使能主I2C 					*/
	
	/* 
	 * 设置I2C中断 
	 */
	VICIntSelect = 0x00000000;							                /* 设置所有通道为IRQ中断 		*/
	VICVectCntl0 = (0x20 | 0x09);						                /* I2C通道分配最高优先级        */
	VICVectAddr0 = (int32)IRQ_I2C;						                /* 设置I2C中断向量 				*/
	VICIntEnable = 1 << 9;							                    /* 使能I2C中断 					*/
}

/*********************************************************************************************************
** Function name:		Main
** Descriptions:		I2C总线对CAT1024进行读写操作
** input parameters:    无
** output parameters:   无
** Returned value:      无
*********************************************************************************************************/
int main (void)
{   
    uint8 i;
	
	uint8 uiDataBuf[32];
	
	PINSEL0 = 0x00000000;					                            /* 设置管脚连接GPIO			    */
	PINSEL1 = 0x00000000;
	
	IO0DIR  = LED1 | LED2;							                    /* 设置LED控制口输出		    */
	IO0SET  = LED1 | LED2;							                   
	
	IRQEnable();							                            /* 使能中断					    */
	
	I2cInit(400000);						                            /* I2C初始化		            */
	
	for (i=0; i<10; i++){
		uiDataBuf[i] = i + '0';				                            /* 数据0~9,转换成ASCII码	    */
	}
	                                                                    /* 从起始地址0x00写入10个数据	*/
	I2C_WriteNByte(CAT1024, ONE_BYTE_SUBA, 0x00, uiDataBuf, 10);
	
	DelayNS(10);

	for (i=0; i<10; i++){                                               /* 清零数据缓冲区,防止出错	    */
		uiDataBuf[i] = 0;
	}

	I2C_ReadNByte(CAT1024, ONE_BYTE_SUBA, 0x00, uiDataBuf, 10);         /* 读回刚才写入的数据           */
	
	/* 
	 * 判断读回的数据是否正确 
	 */
	for (i=0; i<10; i++){
		if (uiDataBuf[i] != (i + '0')){			                        /* 若读出数据错误,LED一直点亮  */ 
			while (1){		
                IO0CLR = LED1 | LED2;
			}
		}
		else{                                                           /* 若读出数据正确,LED闪烁、熄灭*/
	        IO0CLR = LED1 | LED2;
	        DelayNS(50);
	        IO0SET = LED1 | LED2;
	        DelayNS(50);
	    }
	}				
	
	while (1);                                                          
    return 0;
}
/*********************************************************************************************************
**                            End Of File
********************************************************************************************************/

⌨️ 快捷键说明

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