本文主要是介绍stm8 Slave,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
宏定义
#define SLAVE_ADDR 0x51
#define I2CSPEED 400000 //i2c速度
stm8l051 i2c从机初始化
CLK_PeripheralClockConfig(CLK_Peripheral_I2C1, ENABLE);I2C_DeInit(I2C1);I2C_Init(I2C1, I2CSPEED, SLAVE_ADDR ,I2C_Mode_I2C, I2C_DutyCycle_2,I2C_Ack_Enable, I2C_AcknowledgedAddress_7bit);I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);
stm8s103 i2c从机初始化
I2C_Init(I2CSPEED, SLAVE_ADDR , I2C_DUTYCYCLE_2, I2C_ACK_CURR, I2C_ADDMODE_7BIT, I2CCLK);
I2C_ITConfig(I2C1, (I2C_IT_TypeDef)(I2C_IT_ERR | I2C_IT_EVT | I2C_IT_BUF), ENABLE);
中断处理
{
Uchar temp = 0;if (I2C1->SR1&0x02){temp = I2C1->SR3&0x07;}else if ((I2C1->SR1&0x84)&&(I2C1->SR3&0x04)){temp = 0xA5; I2C1->DR = temp;}else if ((I2C1->SR1&0x40)&&(0 == (I2C1->SR3&0x04))){temp = I2C1->DR; }else if (I2C1->SR1&0x10){I2C1->CR2 = I2C1->CR2;}if (I2C1->SR2&0x01){I2C1->SR2 &= ~0x01;}else if(I2C1->SR2&0x02){I2C1->SR2 &= ~0x02;}else if(I2C1->SR2&0x04){I2C1->SR2 &= ~0x04;}else if(I2C1->SR2&0x08){I2C1->SR2 &= ~0x08;}return;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
以上程序已长时间测试未发生过任何异常,所以他们说stm8的I2C存在硬件BUG应该是谣言。
这篇关于stm8 Slave的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!