|
楼主 |
发表于 2019-9-12 23:05:10
|
显示全部楼层
这是IIC.C里的程序
#include "myiic.h"
#include "delay.h"
void SDA_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void SDA_IN(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
}
void IIC_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
SCL_H;
SDA_H;
}
void IIC_Start(void)
{
SDA_OUT();
SDA_H;
SCL_H;
delay_us(4);
SDA_L;
delay_us(4);
SCL_L;
}
void IIC_Stop(void)
{
SDA_OUT();
SCL_L;
SDA_L;
delay_us(2);
SCL_H;
SDA_H;
delay_us(4);
}
void IIC_Ack(void)
{
SCL_L;
SDA_OUT();
SDA_L;
delay_us(2);
SCL_H;
delay_us(2);
SCL_L;
}
void IIC_NAck(void)
{
SCL_L;
SDA_OUT();
SDA_H;
delay_us(2);
SCL_H;
delay_us(2);
SCL_L;
}
void IIC_Write_Byte(u_8 txd)
{
u_8 t;
SDA_OUT();
SCL_L;
for(t = 0; t < 8; t++)
{
if ((txd << t) & 0x80)
SDA_H;
else
SDA_L;
delay_us(2);
SCL_H;
delay_us(2);
SCL_L;
delay_us(2);
}
}
u8 IIC_Read_Byte(void)
{
uint8_t i = 0;
uint8_t data = 0;
SDA_H;
SDA_IN();
for (i = 0; i < 8; i++)
{
SCL_L;
delay_us(4);
SCL_H;
delay_us(1);
data = (data << 1);
if (READ_SDA)
data++;
delay_us(2);
}
return data;
}
这是BQ34Z100.C里的程序
#include "bq34.h"
u8 IIC_Wait_Ack(void)
{
u16 ucErrTime = 0;
SDA_IN();
SDA_H;
delay_us(4);
SCL_H;
delay_us(4);
while(READ_SDA)
{
ucErrTime++;
if(ucErrTime > 250)
{
IIC_Stop();
return 1;
}
}
SCL_L;
return 0;
}
void Bq34z100_Write_nByte(u8 *pBuff, u8 num)
{
u8 i = 0;
IIC_Start();
IIC_Write_Byte(BQ34Z100_WRITE);
IIC_Wait_Ack();
for(i = 0;i < num; i++)
{
IIC_Write_Byte(*pBuff);
IIC_Wait_Ack();
pBuff++;
}
// IIC_Stop();
}
void Bq34z100_Read_nByte(u8 *pBuff, u8 num)
{
u8 i = 0;
IIC_Start();
IIC_Write_Byte(BQ34Z100_READ);
IIC_Wait_Ack();
for(i = 0;i < num; i++)
{
*pBuff = IIC_Read_Byte();
if (i != (num - 1))
IIC_Ack();
pBuff++;
}
IIC_NAck();
IIC_Stop();
}
void Bq34z100_Read_nByte , void Bq34z100_Write_nByte这两句不懂什么意思?
|
|