//configure I2C void BMP180_init(void) { P8OUT |= BIT4; //set SW_I2C high to power on all I2C chips __delay_cycles(24000000); //wait 1s (assuming 24MHz MCLK) to allow for power ramp up I2C_Master_Init(S_MCLK,24000000,400000); //Source from SMCLK, which is running @ 24MHz. 4kHz desired BRCLK //max is 3.4MHz }
void I2CMaster_TX(I2C_TypeDef *I2Cx) { unsigned int tempClear = 0x0; unsigned int i; I2C_Master_Init(I2Cx); for(i=0;i<10;i++) { I2Cx->IC_DATA_CMD = 0x00aa + i; //I2C->IC_DATA_CMD = 0xaa; while(1) { if((I2Cx->IC_RAW_INTR_STAT & 0x0010)==0x0010)//tx_empty=1 break; } //uart_printf("tx 0k\r\n"); } while(1) { if((I2Cx->IC_STATUS & 0x20)==0x20)//MST-ACTIVITY=0 check idle break; } while(1) { if((I2Cx->IC_RAW_INTR_STAT & 0x0200)==0x0200)//stop flag detect tempClear = I2Cx->IC_CLR_STOP_DET; //read this reg clear stop flag break; } }
/* I2C Slave with Interrupt Init */ void I2C_SlaveWithInterrupt_Init(I2C_TypeDef* I2Cx, uint32_t freq, int SlaveAddr, pin_t scl, pin_t sda){ NVIC_InitTypeDef i2c_slave_nvic; uint8_t _i2c_er_irq; I2C_Master_Init(I2Cx, freq, scl, sda); I2C_Slave_Mode(I2Cx, 1); I2C_Slave_Address(I2Cx, SlaveAddr); /* Configure the Priority Group to 1 bit */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); if(I2Cx == I2C1){ _i2c_er_irq = I2C1_ER_IRQn; i2c_slave_nvic.NVIC_IRQChannel = I2C1_EV_IRQn; }else if(I2Cx == I2C2){ _i2c_er_irq = I2C2_ER_IRQn; i2c_slave_nvic.NVIC_IRQChannel = I2C2_EV_IRQn; } i2c_slave_nvic.NVIC_IRQChannelPreemptionPriority = 1; i2c_slave_nvic.NVIC_IRQChannelSubPriority = 0; i2c_slave_nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&i2c_slave_nvic); i2c_slave_nvic.NVIC_IRQChannel = _i2c_er_irq; NVIC_Init(&i2c_slave_nvic); /* Enable Error Interrupt */ I2C_ITConfig(I2Cx, (I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE); }
void I2CMaster_RX(I2C_TypeDef *I2Cx) { unsigned int tempClear = 0x0; unsigned int i; unsigned int rxdata[10] = {0x0}; I2C_Master_Init(I2Cx); for(i=0;i<10;i++) { I2Cx->IC_DATA_CMD = 0x100; while(1) { if((I2Cx->IC_RAW_INTR_STAT & 0x0004)==0x0004) break; } rxdata[i] = I2Cx->IC_DATA_CMD; uart_printf("rxdata%d = 0x%x\r\n",i,rxdata[i]); } }
void Main() { Setup(); I2C_Master_Init(); I2C1_Init(100000); while(1){ I2C1_Start(); I2C1_Wr(8 << 1); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(100); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Stop(); Delay_ms(500); I2C1_Start(); I2C1_Wr(8 << 1); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Wr(0); I2C1_Stop(); Delay_ms(500); } }
/* I2C Slave Init */ void I2C_Slave_Init(I2C_TypeDef* I2Cx, uint32_t freq, int SlaveAddr, pin_t scl, pin_t sda){ I2C_Master_Init(I2Cx, freq, scl, sda); I2C_Slave_Mode(I2Cx, 1); I2C_Slave_Address(I2Cx, SlaveAddr); }