ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Суббота
23 ноября
12525
ytrem (29.07.2004 20:14, просмотров: 554)
Tо John Doe(off: IMHO, душевный псевдоним.. ) - kak LPC21XX работает с I2C (код) Этот код обеспечивает работу LPC21XX c I2C I/O extender PCA9555 //-----------------------------------------------------------------void I2C_Init(void) { //--- I2C Timing for 58 MHz (t = 16.954 ns) --- rI2C_I2SCLH = 47; //-- more then 0.6 us - 0.8 rI2C_I2SCLL = 93; //-- more then 1.3 us - 1.4 rI2C_I2CONCLR = 0xFF; rI2C_I2CONSET = 0x40; //-- Port 0 - 00000111 Port 1 - 00000000 I2C_Write(I2C_PCA9555_CONF,0x00000007); I2C_Write(I2C_PCA9555_OUT,0x000000FF); //-- LEDs/Ctrl first init } //---------------------------------------------------------------------------- int I2C_Write(int Mode, int val) { rI2C_I2CONCLR = 0xFF; // Clear all bits rI2C_I2CONSET = 0x40; // Setup Mode MASTER //--Set START condition rI2C_I2CONSET |= I2C_FLAG_I2EN | I2C_FLAG_STA; //--- Slave addr while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- wait SI=1; rI2C_I2DAT = 0x42; //--- 0100 001 0 rI2C_I2CONCLR = I2C_FLAG_STA | I2C_FLAG_SI; //-- Clear START & SI //--- PC9555 - command while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- wait SI=1; rI2C_I2DAT = Mode; rI2C_I2CONCLR = I2C_FLAG_SI; //-- Clear SI //--- Port 0 while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- wait SI=1; rI2C_I2DAT = val & 0x000000FF; rI2C_I2CONCLR = I2C_FLAG_SI; //-- Clear SI //--- Port 1 while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- wait SI=1; rI2C_I2DAT = (val>>8) & 0x000000FF; rI2C_I2CONCLR = I2C_FLAG_SI; //-- Clear SI //-- Set STOP condition while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- wait SI=1; rI2C_I2CONCLR = I2C_FLAG_SI; //-- Clear SI rI2C_I2CONSET |= I2C_FLAG_STO; return 1; //-- O.K } //---------------------------------------------------------------------------- int I2C_Read(int Mode, int * ret_val) { int port0_data; int port1_data; int rd_val; rI2C_I2CONCLR = 0xFF; // Clear all bits //--Set START condition rI2C_I2CONSET |= I2C_FLAG_I2EN | I2C_FLAG_STA; //--- Slave addr - Now WR while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- End START; rI2C_I2DAT = 0x42; //--- 0100 001 0 rI2C_I2CONCLR = I2C_FLAG_STA | I2C_FLAG_SI; //-- Clear START & SI //--- PC9555 - command while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- End Addr ; rI2C_I2DAT = Mode; rI2C_I2CONCLR = I2C_FLAG_SI; //-- Clear SI //--- START condition again while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- End Cmd; rI2C_I2CONCLR = 0xFF; // Clear all bits rI2C_I2CONSET |= I2C_FLAG_I2EN | I2C_FLAG_STA; //--Set START condition //--- Slave addr - Now RD while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- End new START; rI2C_I2DAT = 0x43; //--- 0100 001 1 rI2C_I2CONCLR = I2C_FLAG_STA | I2C_FLAG_SI; //-- Clear START & SI while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- End new address; rI2C_I2CONCLR = I2C_FLAG_SI; //-- Clear SI //--- Port 0 - read while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- End Data0 from slave; port0_data = rI2C_I2DAT; rI2C_I2CONCLR = I2C_FLAG_AA | I2C_FLAG_SI; //-- Clear SI & AA //-- Set STOP condition while(!(rI2C_I2CONSET & I2C_FLAG_SI)); //-- End Data1 from slave; port1_data = rI2C_I2DAT; rI2C_I2CONCLR = I2C_FLAG_SI; //-- Clear SI rI2C_I2CONSET |= I2C_FLAG_AA | I2C_FLAG_STO; rd_val = (port1_data<<8)& 0x0000FF00; rd_val |= port0_data & 0x000000FF; *ret_val = rd_val; return 0; }