#include "iic.h"
void delay_us(uint16_t nus);
void iic_Delay(uint16_t value )
{
uint16_t i;
for (i = 0; i < value; i++);
}
void IIC_Init(void)
{
//使能GPIOB
RCC->IOPENR |= 1 << 1;
//重置GPIO端口B
//配置SCL管脚 PB12
GPIOB->MODER &= ~(0x3 << 2 * 12);
GPIOB->MODER |= 1 << 2 * 12;
GPIOB->OTYPER &= ~(1 << 12); //推挽输出
GPIOB->OSPEEDR |= 0x3 << 2 * 12;
//配置SDA管脚 PB13
GPIOB->MODER &= ~(0x3 << 2 * 13);
GPIOB->MODER |= 0 << 2 * 13;
// GPIOB->OTYPER |= (1 << 13); //开漏输出
GPIOB->OTYPER &= ~(1 << 13); //推挽输出
GPIOB->OSPEEDR |= 0x3 << 2 * 13;
GPIOB->ODR |= 0x3 << 12;
}
//产生IIC起始信号
void IIC_Start(void)
{
Set_SDA_OUTPUT(); //sda线输出
Set_SDA_HIGH();
Set_SCL_HIGH();
iic_Delay(20);
Set_SDA_LOW();//START:when CLK is high,DATA change form high to low
iic_Delay(20);
Set_SCL_LOW();//钳住I2C总线,准备发送或接收数据
}
//产生IIC停止信号
void IIC_Stop(void)
{
Set_SDA_OUTPUT();//sda线输出
Set_SCL_LOW();
Set_SDA_LOW();//STOP:when CLK is high DATA change form low to high
iic_Delay(20);
Set_SCL_HIGH();
iic_Delay(20);
Set_SDA_HIGH();//发送I2C总线结束信号
iic_Delay(20);
}
//等待应答信号到来
//返回值:1,接收应答失败
// 0,接收应答成功
uint8_t IIC_Wait_Ack(void)
{
uint8_t ucErrTime=0;
Set_SDA_INPUT(); //SDA设置为输入
Set_SDA_HIGH();iic_Delay(10);
Set_SCL_HIGH();iic_Delay(10);
while(Read_SDA())
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
Set_SCL_LOW();//时钟输出0
return 0;
}
//产生ACK应答
void IIC_Ack(void)
{
Set_SCL_LOW();
Set_SDA_OUTPUT();
Set_SDA_LOW();
iic_Delay(20);
Set_SCL_HIGH();
iic_Delay(20);
Set_SCL_LOW();
iic_Delay(20);
Set_SDA_HIGH();
}
//不产生ACK应答
void IIC_NAck(void)
{
Set_SCL_LOW();
Set_SDA_OUTPUT();
Set_SDA_HIGH();
iic_Delay(20);
Set_SCL_HIGH();
iic_Delay(20);
Set_SCL_LOW();
}
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答
void IIC_Send_Byte(uint8_t txd)
{
uint8_t t;
Set_SDA_OUTPUT();
Set_SCL_LOW();//拉低时钟开始数据传输
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
{
Set_SDA_HIGH();
}
else
{
Set_SDA_LOW();
}
txd<<=1;
iic_Delay(20); //对TEA5767这三个延时都是必须的
Set_SCL_HIGH();
iic_Delay(20);
Set_SCL_LOW();
iic_Delay(20);
}
Set_SCL_LOW();
}
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
uint8_t IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
Set_SDA_INPUT();//SDA设置为输入
for(i=0;i<8;i++ )
{
Set_SCL_LOW();
iic_Delay(20);
Set_SCL_HIGH();
receive<<=1;
if(Read_SDA())receive++;
iic_Delay(20);
}
if (!ack)
IIC_NAck();//发送nACK
else
IIC_Ack(); //发送ACK
return receive;
}
uint8_t i2c_CheckDevice(uint8_t _Address)
{
uint8_t ucAck;
if ( Read_SDA() && Read_SCL())
{
IIC_Start( ); /* 发送启动信号 */
/* 发送设备地址+读写控制bit(0 = w, 1 = r) bit7 先传 */
IIC_Send_Byte( _Address | 1);
ucAck = IIC_Wait_Ack(); /* 检测设备的ACK应答 */
IIC_Stop(); /* 发送停止信号 */
return ucAck;
}
return 1; /* I2C总线异常 */
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>