中级会员
- 积分
- 354
- 金钱
- 354
- 注册时间
- 2015-1-12
- 在线时间
- 65 小时
|
stm8 硬件iic 从机无法接受数据,接受到地址,并且已应答,
void IIC_GPIO_INIT()
{
/* 配置IIC的IO口 为上拉输入*/
//CLK->PCKENR1 |=0X01;
GPIOB->DDR &= ~0x30;
GPIOB->CR1 |= 0x30;
GPIOB->CR2 &= ~0x30;
}
void IIC_INIT(void)
{
I2C->CR1 = 0x00;
I2C->CR2 =0x04; //使能应答
I2C->FREQR = 0x10; // Set I2C Freq value (16MHz)
I2C->OARH |= 0x40;
I2C->OARL = (SLAVE_ADDRESS );
I2C->CR1 = 0x01;
I2C->CR2 = 0X04;
I2C->CR2 |= 0x02;
I2C->ITR |= 0x07; //开启中断
}
/**
* @brief I2C Interrupt routine.
* @param None
* @retval None
*/
INTERRUPT_HANDLER(I2C_IRQHandler, 19)
{
uint8_t temp1=0;
uint8_t temp2=0;
temp1=I2C->SR1;
temp2=I2C->SR3;
uint8_t data;
if((I2C->SR1 & 0X02)==0x02) // 判读地址是否匹配
{
I2C->CR2 |= 0X04;
I2C->SR1&=~(0x04);
}
if((I2C->SR1&0x10)==0x10) //检测到停止位
{
temp1 = I2C->SR1;
I2C->CR2|=0x04;
}
if((I2C->SR1&0x40) == 0x40) //接收数据
{
data= I2C->DR;
if(IIC_RX_Finish==0)
{
IIC_RecBuffer[IIC_RX_NUM]=data;
IIC_RX_NUM++;
if(IIC_RX_NUM>=1){IIC_RX_Finish=1;IIC_RX_NUM=0; I2C->ITR = 0x00;}
I2C->CR2 |= 0X04;//应答
}
}
}
|
-
这是
|