i2cmst.c

来自「基于NXP LPC2368 iic的程序!大家可以」· C语言 代码 · 共 117 行

C
117
字号
/*****************************************************************************
 *   i2cmst.c:  main C entry file for NXP LPC23xx/24xx Family Microprocessors
 *
 *   Copyright(C) 2006, NXP Semiconductor
 *   All rights reserved.
 *
 *   History
 *   2006.07.20  ver 1.00    Prelimnary version, first Release
 *
******************************************************************************/
#include "LPC23xx.h"                        /* LPC23xx/24xx definitions */
#include "type.h"
#include "i2c.h"
#include "uart.h"
#include "fio.h"
#include <stdio.h>

#define BUFFER_SIZE	0x40
char RUN_INFOR[BUFFER_SIZE];

extern volatile DWORD I2CCount;
extern volatile BYTE I2CMasterBuffer[BUFSIZE];
extern volatile DWORD I2CCmd, I2CMasterState;
extern volatile DWORD I2CReadLength, I2CWriteLength;

/*******************************************************************************
**   Main Function  main()
*******************************************************************************/
int main (void)
{
  DWORD i;
  
  UARTInit(0, 115200);	/* baud rate setting */
  if ( I2CInit( (DWORD)I2CMASTER ) == FALSE )	/* initialize I2c IIC2*/
  {
	sprintf( RUN_INFOR, "I2CInit( (DWORD)I2CMASTER ) == FALSE\r\n" );
    UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
	while ( 1 );				/* Fatal error */	
  }
  sprintf( RUN_INFOR, "I2CInit OK.\r\n" );
  UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
  /* the example used to test the I2C interface is 
  a Ram of Atmel's At24C16. LPC2368 is used as an I2C
  master, the At24C16 is an I2C slave.
  
  In order to start the I2CEngine, the all the parameters 
  must be set in advance, including I2CWriteLength, I2CReadLength,
  I2CCmd, and the I2cMasterBuffer which contains the stream
  command/data to the I2c slave device. 
  (1) If it's a I2C write only, the number of bytes to be written is 
  I2CWriteLength, I2CReadLength is zero, the content will be filled 
  in the I2CMasterBuffer. 
  (2) If it's a I2C read only, the number of bytes to be read is 
  I2CReadLength, I2CWriteLength is 0, the read value will be filled 
  in the I2CMasterBuffer. 
  (3) If it's a I2C Write/Read with repeated start, specify the 
  I2CWriteLength, fill the content of bytes to be written in 
  I2CMasterBuffer, specify the I2CReadLength, after the repeated 
  start and the device address with RD bit set, the content of the 
  reading will be filled in I2CMasterBuffer index at 
  I2CMasterBuffer[I2CWriteLength+2].	See At24C16 Read. 
    
  the At24c16 RD/WR protocal is:
  For master read: the sequence is: STA,Addr_Device(W),Addr_RAM(W),Addr_Device(R)data...STO 
  for master write: the sequence is: STA,Addr_Device(W),Addr_RAM(W)data...STO
  */

    
  /* Init buffer */
  for ( i = 0; i < BUFSIZE; i++ )	
  {
	I2CMasterBuffer[i] = i;
  }
  /* write */
  I2CWriteLength = 10;
  I2CReadLength = 0;
  I2CMasterBuffer[0] = AT24C16_ADDR;
  I2CMasterBuffer[1] = 0x00; 	//Write Addres


  if(!I2CEngine())
  {
  	sprintf( RUN_INFOR, "At24C16 Write Error.\r\n" );
  	UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE ); 
  }	 
  sprintf( RUN_INFOR, "At24C16 Write Finished.\r\n" );
  UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE ); 

  /* Read */
  for ( i = 0; i < BUFSIZE; i++ )	
  {
	I2CMasterBuffer[i] = 0;
  }  
  I2CWriteLength = 1;
  I2CReadLength = 8;
  I2CMasterBuffer[0] = AT24C16_ADDR;
  I2CMasterBuffer[1] = 0x00; 	//Ram Addres Read From
  I2CMasterBuffer[2] = AT24C16_ADDR | RD_BIT;
  I2CCmd = AT24C16_READ;
  I2CEngine();
   
  sprintf( RUN_INFOR, "At24C16 IIC Read Finished.\r\n" );
  UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
  
  for( i=0; i<10; i++ )
  {
  	sprintf( RUN_INFOR, "I2CMasterBuffer[%d]=0x%XH\r\n", i, I2CMasterBuffer[ i ] );
    UARTSend( 0, (BYTE *)RUN_INFOR, BUFFER_SIZE );
  }
 
  return 0;
}

/******************************************************************************
**                            End Of File
******************************************************************************/

⌨️ 快捷键说明

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