LPC1768之I2C流水灯的实验例程

 ***********************************Copyright (c)*****************************************

==========================================================================================
================      **文件名    : main.c            ================
================      **作者      : CaKen               ================
================      **版本号    : V1.0             ================
================      **时间      : 2014.08.18                ================
================      **功能描述  : I2C实验测试            ================
==========================================================================================      
******************************************************************************************
//主函数
#include "lpc17xx.h"
#include "i2c.h"
extern  uint8_t buf[32];
void Delay(uint32_t delaydata)
{
 uint32_t i,j,k;
 for(i=0;i
  for(j=0;j<1000;j++)
   for(k=0;k<100;k++);  
}
int main (void)
{
 uint32_t i;
 SystemInit();
 LPC_GPIO2->FIODIR    = 0x000000ff;              
 LPC_GPIO0->FIODIR   |= 0x03f80000;
 LPC_GPIO0->FIOSET    = 0x03f80000;
 if ( I2C_Init( (uint32_t)I2CMASTER ) == 0 )  
  {
  while ( 1 );        
  }
 for ( i = 0; i < 8; i++ )      
  {
  buf[i]=i+1;
  }
 I2C_WriteNByte(0xa0, 1, 0x00, buf, 8);   
 Delay(50);
 for ( i = 0; i < 8; i++ )     
  {            
  buf[i] =0;
  }
 I2C_ReadNByte (0xa0, 1, 0x00, buf, 8);   
 Delay(50);
 while(1)
  {
  for(i=0;i<8;i++)
   {
   LPC_GPIO2->FIOPIN = 1<<(buf[i]-1);
   Delay(100);
   } 
  }
}
=======================================================================
***********************************Copyright (c)*****************************************
==========================================================================================
================      **文件名    : main.c            ================
================      **作者      : CaKen               ================
================      **版本号    : V1.0             ================
================      **时间      : 2014.08.18                ================
================      **功能描述  : I2C实验测试            ================
==========================================================================================      
******************************************************************************************
//I2C.c
#include "lpc17xx.h"
#include "i2c.h"
uint8_t buf[32]="abcdefghigklmn";
extern void Delay(uint32_t delaydata);
volatile uint8_t     I2C_sla;                                
volatile uint32_t    I2C_suba;                               
volatile uint8_t     I2C_suba_num;                           
volatile uint8_t     *I2C_buf;                               
volatile uint32_t    I2C_num;                                
volatile uint8_t     I2C_end;                                 
volatile uint8_t     I2C_suba_en;                             
void I2C0_IRQHandler(void) 
{
  uint8_t StatValue;
  StatValue = LPC_I2C0->I2STAT & 0xf8;
  switch ( StatValue )
  {
 case 0x08:             
 if (I2C_suba_en == 1)
 { 
   LPC_I2C0->I2DAT = I2C_sla &0xfe; 
 }
 else
 { 
   LPC_I2C0->I2DAT = I2C_sla; 
 } 
 LPC_I2C0->I2CONCLR = (1 << 3)|(1 << 5);
 break;
 case 0x10:              
 LPC_I2C0->I2DAT = I2C_sla;
 LPC_I2C0->I2CONCLR = 0x28;
 break;
 case 0x18:           
 case 0x28:           
 if (I2C_suba_en == 0)
 {  
   if(I2C_num>0)
   { 
    LPC_I2C0->I2DAT = *I2C_buf++;    
    LPC_I2C0->I2CONCLR = 0x28;
    I2C_num--;
    Delay(1);
   }
   else
   {
  LPC_I2C0->I2CONSET = (1 << 4);     
  LPC_I2C0->I2CONCLR = 0x28;      
  I2C_end =1;
   }
 }
 if(I2C_suba_en == 1)
 {
  if(I2C_suba_num == 2)
   {
     LPC_I2C0->I2DAT = ((I2C_suba >> 8) & 0xff);
     LPC_I2C0->I2CONCLR = 0x28;
     I2C_suba_num--;
     break;
   }
  if(I2C_suba_num == 1)
   {
     LPC_I2C0->I2DAT = (I2C_suba & 0xff);
     LPC_I2C0->I2CONCLR = 0x28;
     I2C_suba_num--;
     break;
   }
  if(I2C_suba_num == 0)
    {
      LPC_I2C0->I2CONCLR = 0x08;
      LPC_I2C0->I2CONSET = 0x20;
      I2C_suba_en = 0;
      break;      
       }    
 }
 if ( I2C_suba_en == 2 )
 {
   if(I2C_suba_num > 0)
    {
    if(I2C_suba_num == 2)
     {
     LPC_I2C0->I2DAT = ((I2C_suba >> 8) & 0xff);
     LPC_I2C0->I2CONCLR = 0x28;
     I2C_suba_num--;
     break;
     }
    if(I2C_suba_num == 1)
     {
     LPC_I2C0->I2DAT = (I2C_suba & 0xff);
     LPC_I2C0->I2CONCLR = 0x28;
     I2C_suba_num--;
     I2C_suba_en  = 0;
     break;
     }
    }
 }
 break;
 case 0x40:        
 if (I2C_num <= 1)
 { 
   LPC_I2C0->I2CONCLR = 1 << 2;
 }
 else
  {
  LPC_I2C0->I2CONSET = 1 << 2;  
  }
 LPC_I2C0->I2CONCLR = 0x28;
 break;
 case 0x20:       
 case 0x30:         
 case 0x38:  
 case 0x48:        
 LPC_I2C0->I2CONCLR = 0x28;
 I2C_end = 0xFF;
 break;
 case 0x50:     
 *I2C_buf++ = LPC_I2C0->I2DAT;
 I2C_num--;
 if (I2C_num ==1)       
 { LPC_I2C0->I2CONCLR = 0x2c;    
 }
 else
 { LPC_I2C0->I2CONSET = 0x04;   
  LPC_I2C0->I2CONCLR = 0x28;   
 }
 break;
 case 0x58:       
 *I2C_buf++ = LPC_I2C0->I2DAT;
 LPC_I2C0->I2CONSET = 0X10;  
 LPC_I2C0->I2CONCLR = 0X28;
 I2C_end = 1;
 break;
 default:
 break;
  }
}
uint32_t I2C_Init( uint32_t I2cMode )
{
  LPC_SC->PCONP |= (1 << 19);
  LPC_PINCON->PINSEL1 &= ~0x03C00000;    
  LPC_PINCON->PINSEL1 |=  0x01400000;   
  LPC_I2C0->I2SCLL   = I2SCLL_SCLL;    
  LPC_I2C0->I2SCLH   = I2SCLH_SCLH;
  if ( I2cMode == I2CSLAVE )
  {
 LPC_I2C0->I2ADR0 = 0xA0;
  }   
  NVIC_EnableIRQ(I2C0_IRQn);     
  LPC_I2C0->I2CONSET = I2CONSET_I2EN;
  return( 1 );
}
uint8_t I2C_WriteNByte(uint8_t sla, uint8_t suba_type, uint32_t suba, uint8_t *s, uint32_t num)
{
    if (num > 0)                                             
 {
  if (suba_type == 1)
   {
      I2C_sla         = sla;                              
      I2C_suba        = suba;                             
      I2C_suba_num    = 1;                                
      }
  if (suba_type == 2)
   {
      I2C_sla         = sla;                              
      I2C_suba        = suba;                            
      I2C_suba_num    = 2;                                
      }
  if (suba_type == 3)
   {
      I2C_sla         = sla + ((suba >> 7 )& 0x0e);     
      I2C_suba        = suba;                             
      I2C_suba_num    = 1;                             
      }
         I2C_buf      = s;                              
         I2C_num      = num;                          
         I2C_suba_en  = 2;                               
         I2C_end      = 0;
        LPC_I2C0->I2CONCLR  = (1 << 2)|(1 << 3)|(1 << 5);    
        LPC_I2C0->I2CONSET  = (1 << 5)|(1 << 6);            
     while(I2C_end == 0);
  if(I2C_end)
  {
    Delay(20);
   return 1;
  }
  else
  {
   Delay(20);
   return 0;
  }
   }
     Delay(20);
     return 0;
}
uint8_t I2C_ReadNByte (uint8_t sla, uint8_t suba_type, uint32_t suba, uint8_t *s, uint32_t num)
{
 if (num > 0)
 {
  if (suba_type == 1)
   {
   I2C_sla         = sla + 1;                                 
   I2C_suba        = suba;                                    
   I2C_suba_num    = 1;                                      
   }
  if (suba_type == 2)
   {
   I2C_sla         = sla + 1;                                 
   I2C_suba        = suba;                                   
   I2C_suba_num    = 2;                                      
   }
  if (suba_type == 3)
   {
   I2C_sla         = sla + ((suba >> 7 )& 0x0e) + 1;         
   I2C_suba        = suba & 0x0ff;                            
   I2C_suba_num    = 1;                                    
   }
         I2C_buf      = s;                                   
         I2C_num      = num;                                
          I2C_suba_en   = 1;                                  
         I2C_end      = 0;
     LPC_I2C0->I2CONCLR = (1 << 2)|(1 << 3)|(1 << 5);            
     LPC_I2C0->I2CONSET = (1 << 5)|(1 << 6);                        
 while(I2C_end == 0);
  if(I2C_end)
  {
    Delay(20);
   return 1;
  }
  else
  {
   Delay(20);
   return 0;
  }
 }
   Delay(20);
   return 0;
}
 ==========================================================================================
***********************************Copyright (c)*****************************************
==========================================================================================
================      **文件名    : main.c            ================
================      **作者      : CaKen               ================
================      **版本号    : V1.0             ================
================      **时间      : 2014.08.18                ================
================      **功能描述  : I2C实验测试            ================
==========================================================================================      
******************************************************************************************
//I2C.H
#ifndef __I2C_H
#define __I2C_H
#define I2CMASTER     0x01
#define I2CSLAVE     0x02
#define I2CONSET_I2EN    0x00000040 
#define I2SCLH_SCLH     0x00000080 
#define I2SCLL_SCLL     0x00000080 
extern void I2C0_IRQHandler( void );
extern uint32_t I2C_Init( uint32_t I2cMode );
uint8_t I2C_WriteNByte(uint8_t sla, uint8_t suba_type, uint32_t suba, uint8_t *s, uint32_t num);
uint8_t I2C_ReadNByte (uint8_t sla, uint8_t suba_type, uint32_t suba, uint8_t *s, uint32_t num);
#endif
以上是LPC1768之I2C流水灯的实验例程的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>