( - MCTR601 - ) 10 - I2C Code (Supplementary Notes)
( - MCTR601 - ) 10 - I2C Code (Supplementary Notes)
void init() {
RCC->APB2ENR |= RCC_APB2ENR_IOPBEN|RCC_APB2ENR_IOPAEN;
RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;
GPIOB->CRH = 0x4444FF44; // AF open drain
GPIOA->CRL = 0x44444441; // LED PA0 output G.P. push-pull
I2C2->CR1 &= ~ I2C_CR1_PE;
I2C2->CR1 |= (1 << 15); // reset the I2C
I2C2->CR1 &= ~(1 << 15); // Normal operation
I2C2->CR2 |= 8; // I2C APB clock frequency
I2C2->CCR = 40; // I2C Standard Mode
I2C2->TRISE = 9;
I2C2->CR1 |= I2C_CR1_PE;
}
int main(){
init();
I2C_PWR();
while(1){
I2C_read();
if(data_x>0)
GPIOA->ODR|=1;
else
GPIOA->ODR&=~1;
}
}
Prof. Ayman A. El-Badawy
Department of Mechatronics Engineering
Faculty of Engineering and Material Science
void I2C_PWR(){
I2C2->CR1 |= (1 << 9); // Stop I2C before the end of the last byte
I2C2->CR1 &= ~I2C_CR1_ACK; // Stop ACK
void Delay(){
for(int i=0;i<=delay_time;i++);
}