You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
2.8 KiB

#include "myiic.h"
#include "delay.h"
//////////////////////////////////////////////////////////////////////////////////
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><EFBFBD>ѧϰʹ<EFBFBD>ã<EFBFBD>δ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>κ<EFBFBD><EFBFBD><EFBFBD>;
//ALIENTEK<EFBFBD><EFBFBD>ӢSTM32<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//IIC<EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ԭ<EFBFBD><EFBFBD>@ALIENTEK
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̳:www.openedv.com
//<EFBFBD>޸<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>:2012/9/9
//<EFBFBD><EFBFBD><EFBFBD>V1.0
//<EFBFBD><EFBFBD>Ȩ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ؾ<EFBFBD><EFBFBD><EFBFBD>
//Copyright(C) <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>޹<EFBFBD>˾ 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////
//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>IIC
void MYIIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE ); //ʹ<EFBFBD><EFBFBD>GPIOBʱ<EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_14|GPIO_Pin_13);
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IIC<EFBFBD><EFBFBD>ʼ<EFBFBD>ź<EFBFBD>
void MYIIC_Start(void)
{
SDA_OUT(); //sda<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IIC_SDA=1;
IIC_SCL=1;
delay_us(4);
IIC_SDA=0;//START:when CLK is high,DATA change form high to low
delay_us(4);
IIC_SCL=0;//ǯסI2C<EFBFBD><EFBFBD><EFBFBD>ߣ<EFBFBD>׼<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>IICֹͣ<EFBFBD>ź<EFBFBD>
void MYIIC_Stop(void)
{
SDA_OUT();//sda<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IIC_SCL=0;
IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
delay_us(4);
IIC_SCL=1;
IIC_SDA=1;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>I2C<EFBFBD><EFBFBD><EFBFBD>߽<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ź<EFBFBD>
delay_us(4);
}
//<EFBFBD>ȴ<EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>źŵ<EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
// 0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>
u8 MYIIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IIC_SDA=1;delay_us(1);
IIC_SCL=1;delay_us(1);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
MYIIC_Stop();
return 1;
}
}
IIC_SCL=0;//ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>0
return 0;
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<EFBFBD><EFBFBD>
void IIC_Ack(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=0;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACKӦ<EFBFBD><EFBFBD>
void IIC_NAck(void)
{
IIC_SCL=0;
SDA_OUT();
IIC_SDA=1;
delay_us(2);
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
}
//IIC<EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD>شӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>
//1<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>
//0<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL=0;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ӿ<EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD><EFBFBD><EFBFBD>
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
txd<<=1;
delay_us(2); //<EFBFBD><EFBFBD>TEA5767<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>DZ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
IIC_SCL=1;
delay_us(2);
IIC_SCL=0;
delay_us(2);
}
}
//<EFBFBD><EFBFBD>1<EFBFBD><EFBFBD><EFBFBD>ֽڣ<EFBFBD>ack=1ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK<EFBFBD><EFBFBD>ack=0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK
u8 IIC_Read_Byte(unsigned char ack)
{
unsigned char i,receive=0;
SDA_IN();//SDA<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
for(i=0;i<8;i++ )
{
IIC_SCL=0;
delay_us(2);
IIC_SCL=1;
receive<<=1;
if(READ_SDA)receive++;
delay_us(1);
}
if (!ack)
IIC_NAck();//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK
else
IIC_Ack(); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK
return receive;
}