プログラムのソース①

2014/03/07 8:32 に Robo Kirarin が投稿
//くるりんが行動するときに必要な関数です.

#ifdef __USE_CMSIS
#include "LPC43xx.h"
#include "lpc43xx_i2c.h"
#include "lpc43xx_scu.h"   //P386~
#include "lpc43xx_cgu.h"
#endif

#ifdef __BUILD_WITH_EXAMPLE__
#include "lpc43xx_libcfg.h"
#else
#include "lpc43xx_libcfg_default.h"
#endif /* __BUILD_WITH_EXAMPLE__ */


#include <NXP/crp.h>
#include <cr_section_macros.h>
// TODO: insert other include files here

// TODO: insert other definitions and declarations here

int counter = 0;
int state = 0;
int joutai=0;

#define SFSP2_3_CONFIGURE_I2C1_SDA                                      (0x00000001 | MD_ZI | MD_EZI)
#define SFSP2_4_CONFIGURE_I2C1_SCL                                      (0x00000001 | MD_ZI | MD_EZI)

void i2cinit();
void i2cwrite(int ADR,int SADR1,int SADR2);
void i2cread(int ADRW,int ADRR,int RADR);

int main(void) {
 i2cinit();
 i2cwrite(0x32,0x20,0x7F);
 i2cinit();
 i2cread(0x32,0x33,0x28);
return 0 ;
}

void i2cinit(){
 CGU_EntityConnect(CGU_CLKSRC_PLL1, CGU_BASE_APB3);              //クロックの設定やで
 LPC_SCU->SFSP2_3 = SFSP2_3_CONFIGURE_I2C1_SDA;                  //SDA
 LPC_SCU->SFSP2_4 = SFSP2_4_CONFIGURE_I2C1_SCL;                  //SCL
 LPC_I2C1->SCLH = 100;  // SCL High
 LPC_I2C1->SCLL = 70;     // SCL Low
 LPC_I2C1->CONCLR = 0x6c;  // I2C flagClear
 LPC_I2C1->CONSET = 0x40;  //i2c使える I2EN 1 other 0  P1261
 LPC_I2C1->CONSET = 0x20;  // assert Start スタート STA 1 other 0;
}

void i2cwrite(int ADR,int SADR1,int SADR2){
 uint8_t StatValue;
 static int t=1;
 while(StatValue != 0xF8){
 StatValue = LPC_I2C1->STAT;
 switch ( StatValue )
 {
 case 0x08:  //Now Status is Start
  LPC_I2C1->DAT =ADR;  //Device address 1010 000 + Write
  LPC_I2C1->CONCLR = 0x28; //clear interrupt flag + set next action
 break;
 case 0x18:  //Now Status is SLA+W and Receive ACK
  LPC_I2C1->DAT =SADR1; //Address
  LPC_I2C1->CONCLR = 0x08; //clear interrupt flag
 break;
 case 0x28:  //Now Status is sending Data byte and Receive ACK
  if(t==1){  // 1st : Data byte is address
   LPC_I2C1->DAT = SADR2; // write data
   LPC_I2C1->CONCLR = 0x08;
  }else{  // 2nd : Data byte is write data
   LPC_I2C1->CONSET = 0x10; //send stop command
   LPC_I2C1->CONCLR = 0x08; //Clear interrupt
  }
 while(LPC_I2C1->CONSET & 0x00000010); //waiting STOP flag
  LPC_I2C1->CONCLR = 0x48; //Clear interrupt & Disable I2c  }
 ++t;
 break;
 default:
 break;
 }
 }
}

void i2cread(int ADRW,int ADRR,int RADR){
 uint8_t StatValue;
 StatValue= LPC_I2C1->STAT;
 while(StatValue != 0xF8){
 switch ( StatValue ){
 case 0x08:  //Now Status is Start
 LPC_I2C1->DAT =ADRW;  //Device address + Write0
 LPC_I2C1->CONCLR = 0x28; //clear interrupt flag + set next action
 break;
 case 0x18:  //Now Status is SLA+W and Receive ACK
 LPC_I2C1->DAT =RADR; //Address
 LPC_I2C1->CONCLR = 0x08; //clear interrupt flag
 break;
 case 0x28:  //Now Status Data byte and Receive ACK
 LPC_I2C1->CONSET = 0x20; //Set Start again
 LPC_I2C1->CONCLR = 0x08;
 break;
 case 0x10:  //Receive Repeated Start
 LPC_I2C1->DAT = ADRR; //Set Device address & Read1
 LPC_I2C1->CONCLR = 0x28;
 break;
 case 0x40:  //Now Status is SLA+R and Receive ACK
 // LPC_I2C1->CONSET = 0x04; //send ACK (LPC4330 output ACK)
 LPC_I2C1->CONCLR = 0x08;
 break;
 case 0x58:
 LPC_I2C1->CONSET = 0x10; //send stop command
 LPC_I2C1->CONCLR = 0x08; //Clear interrupt & Disable I2c
 while(LPC_I2C1->CONSET & 0x00000010); //waiting STOP flag
 LPC_I2C1->CONCLR = 0x48; //Clear interrupt & Disable I2c
 default:
 break;
 }
 }
}


Comments