本帖最后由 哈喽喽 于 2015-8-13 14:06 编辑
以下是我的模拟IIC的程序,不知道哪有问题,收到的数据有误,好像没有进行通信,不知道该怎么改,求帮助!
/³õʼ»¯IIC
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;//GPIO??????
RCC_AHBPeriphResetCmd(RCC_AHBPeriph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_7;//SCL
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//??????
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//??
GPIO_Init(GPIOB, &GPIO_InitStructure);//???GPIO
IIC_SDA_0;
IIC_SCL_1;
}
void IIC_Start(void)
{
SDA_OUT(); //sdaÏßÊä³ö
IIC_SDA_1;
IIC_SCL_1;
delay_us(5);
IIC_SDA_0;//START:when CLK is high,DATA change from high to low
delay_us(4);
IIC_SCL_0;//ǯסI2C×ÜÏߣ¬×¼±¸·¢ËÍ»ò½ÓÊÕÊý¾Ý
delay_us(2);
}
//²úÉúIICÍ£Ö¹ÐźÅ
void IIC_Stop(void)
{
SDA_OUT();//sdaÏßÊä³ö
IIC_SCL_0;
delay_us(1);
IIC_SDA_0;//STOP:when CLK is high DATA change form low to high
delay_us(4);
IIC_SCL_1;
delay_us(4);
IIC_SDA_1;//·¢ËÍI2C×ÜÏß½áÊøÐźÅ
delay_us(5);
}
//µÈ´ýÓ¦´ðÐźŵ½À´
//·µ»ØÖµ£º1£¬½ÓÊÕÓ¦´ðʧ°Ü
// 0£¬½ÓÊÕÓ¦´ð³É¹¦
uint8_t IIC_Wait_Ack(void)
{
u8 ucErrTime=0;
SDA_IN(); //SDAÉèÖÃΪÊäÈë
IIC_SDA_1;
delay_us(1);
IIC_SCL_1;
delay_us(1);
while(SDA_read)//while(Read_SDA)
{
ucErrTime++;
if(ucErrTime>250)//250¸öʱÖÓÖÜÆÚ»¹Ã»ÊÕµ½Ó¦´ð£¬ÔòÈÏΪӦ´ðʧ°Ü£¬²¢½áÊø´Ë´ÎͨÐÅ
{
IIC_Stop;
return 1;
}
}
/*if(SDA_read)
{
IIC_SCL_0;
return 1;
} */
IIC_SCL_0;//ʱÖÓÊä³ö0
return 0;
}
//²úÉúACKÓ¦´ð
//Ö÷»úÔÚ½ÓÊÕµ½´Ó»úÊý¾Ýºó£¬Ïò´Ó»ú·µ»ØÒ»¸öÓ¦´ðÐźţ¬ÔÚSCLΪ¸ß£¬
//SDAһֱΪµÍÔò±íʾӦ´ð
void IIC_Ack(void)
{
IIC_SCL_0;
SDA_OUT();
IIC_SDA_0;
delay_us(2);
IIC_SCL_1;
delay_us(4);
IIC_SCL_0;
}
//²»²úÉúACKÓ¦´ð
void IIC_NAck(void)
{
IIC_SCL_0;
SDA_OUT();
IIC_SDA_1;
delay_us(2);
IIC_SCL_1;
delay_us(4);
IIC_SCL_0;
}
//IIC·¢ËÍÒ»¸ö×Ö½Ú
//·µ»Ø´Ó»úÓÐÎÞÓ¦´ð
//1£¬ÓÐÓ¦´ð
//0£¬ÎÞÓ¦´ð
void IIC_Send_Byte(u8 txd)
{
u8 t;
SDA_OUT();
IIC_SCL_0;//À µÍʱÖÓ¿ªÊ¼Êý¾Ý´«Êä
for(t=0;t<8;t++)
{
IIC_SDA=(txd&0x80)>>7;
//if(txd&0x80) {IIC_SDA_1;}
//else IIC_SDA_0;
txd<<=1;
delay_us(2); //¶ÔTEA5767ÕâÈý¸öÑÓʱ¶¼ÊDZØÐëµÄ
IIC_SCL_1;
delay_us(2);
IIC_SCL_0;
delay_us(2);
}
//IIC_SDA_1;
//delay_us(5);
}
//¶Á1¸ö×Ö½Ú£¬ack=1ʱ£¬·¢ËÍACK£¬ack=0£¬·¢ËÍnACK
uint8_t IIC_Read_Byte(uint8_t ack)
{
unsigned char i,receive=0;
SDA_IN();//SDAÉèÖÃΪÊäÈë
for(i=0;i<8;i++ )
{
IIC_SCL_0;
delay_us(2);
IIC_SCL_1;
receive<<=1;
if(SDA_read) receive++;
//receive=(receive<<1)|SDA_read; //GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_7)
delay_us(5);
}
if (!ack)
IIC_NAck();//·¢ËÍnACK
else
IIC_Ack(); //·¢ËÍACK
//IIC_SCL_0;
return receive;
}
/******µ¥×Ö½ÚдÈë*******************************************/
void IIC_Single_Write(u8 SlaveAddress,u8 REG_Address,u8 REG_data)
{
IIC_Start(); //ÆðʼÐźÅ
IIC_Send_Byte(SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
IIC_Wait_Ack(); //µÈ´ýÓ¦´ð
IIC_Send_Byte(REG_Address); //ÄÚ²¿¼Ä´æÆ÷µØÖ·£¬ //Çë²Î¿¼ÖÐÎÄpdf22Ò³
IIC_Wait_Ack(); //µÈ´ýÓ¦´ð
IIC_Send_Byte(REG_data); //ÄÚ²¿¼Ä´æÆ÷Êý¾Ý£¬ //Çë²Î¿¼ÖÐÎÄpdf22Ò³
IIC_Wait_Ack(); //µÈ´ýÓ¦´ð
IIC_Stop(); //·¢ËÍÍ£Ö¹ÐźÅ
}
/********µ¥×Ö½Ú¶ÁÈ¡*****************************************/
u8 IIC_Single_Read(u8 SlaveAddress, u8 REG_Address)
{
u8 REG_data;
IIC_Start(); //ÆðʼÐźÅ
IIC_Send_Byte(SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
IIC_Wait_Ack(); //µÈ´ýÓ¦´ð
IIC_Send_Byte(REG_Address); //·¢ËÍ´æ´¢µ¥ÔªµØÖ·£¬//´Ó0¿ªÊ¼
IIC_Wait_Ack(); //µÈ´ýÓ¦´ð
IIC_Start(); //ÆðʼÐźÅ
IIC_Send_Byte(SlaveAddress+1); //·¢ËÍÉ豸µØÖ·+¶ÁÐźÅ
IIC_Wait_Ack(); //µÈ´ýÓ¦´ð
REG_data = IIC_Read_Byte(0); //¶Á³ö¼Ä´æÆ÷Êý¾Ý
IIC_Stop(); //Í£Ö¹ÐźÅ
return REG_data;
}
//??1??? ?????? ????? ????? ????(M24C02)
void IIC_ReadByte(uint8_t* pBuffer,uint8_t length,uint8_t ReadAddress,uint8_t DeviceAddress)
{
// uint8_t ack;
//if(!I2C_Start())return FALSE;
IIC_Send_Byte(DeviceAddress|0x0);
if(!IIC_Wait_Ack())
{
IIC_Stop();
//return FALSE;
}
IIC_Send_Byte(ReadAddress);
IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte(DeviceAddress|0x1);
IIC_Wait_Ack();
/*
IIC_Send_Byte(DeviceAddress+1);
IIC_Wait_Ack();*/
while(length--)
{
*pBuffer = IIC_Read_Byte(1);
if(length == 1)
IIC_NAck();
else
IIC_Ack();
pBuffer++;
}
IIC_Stop();
}
此帖出自
小平头技术问答
一周热门 更多>