#ifndef _stm8_iic_h_
#define _stm8_iic_h_
#include "stm8l15x.h"
extern unsigned char BQ25890x_write_reg(unsigned char *buffer,unsigned char addr,unsigned char num);
extern unsigned char BQ25890x_read_reg(unsigned char *buffer,unsigned char addr,unsigned char num);
#define IICSPEED 100000
#define BQ25890_I2C_ADDR (0x6A)
#endif
#define SlaveAddress (BQ25890_I2C_ADDR)
#define IIC_SDA_GPIO_Pin GPIO_Pin_0
#define IIC_SCL_GPIO_Pin GPIO_Pin_1
#define IIC_GPIOx GPIOC
void Delay(void)
{
nop();
nop();
nop();
nop();
nop();
nop();
}
//使用软件模拟IIC //PC0--SDA PC1---SCL
//初始化
void BQ25890H_init(void)
{
GPIO_Init(IIC_GPIOx, IIC_SDA_GPIO_Pin, GPIO_Mode_Out_PP_High_Fast);
GPIO_Init(IIC_GPIOx, IIC_SCL_GPIO_Pin, GPIO_Mode_Out_PP_High_Fast);
}
//读取数据
unsigned char SDA_READ(void)
{
//return GPIO_ReadInputPin(IIC_GPIOx, IIC_SDA_GPIO_Pin);
return GPIO_ReadInputDataBit(IIC_GPIOx, IIC_SDA_GPIO_Pin);
}
//产生起始信号
void IIC_Start(void)
{
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Slow);//set SDA pin as output low
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow);//set SCL pin as output low
Delay();
}
//产生停止信号
void IIC_Stop(void)
{
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Slow);//set SDA pin as output low
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
Delay();
}
//等待响应信号
//0--接收应答成功 1-接收应答失败
unsigned char IIC_Wait_Ack(void)
{
unsigned char ucErrTime = 0;
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_In_PU_No_IT); //set sda as input with pull up
while(SDA_READ())
{
ucErrTime++;
if(ucErrTime > 250)
{
IIC_Stop();
return 1;
}
}
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow); //set SCL pin as output low
Delay();
return 0;
}
//产生ACK信号
void IIC_Ack(void)
{
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output low
Delay(); //延时
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Slow);//set SDA pin as output low
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow); //set SCL pin as output low
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow); //set SCL pin as output low
Delay();
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
Delay();
}
//No ACK
void IIC_NAck(void)
{
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow); //set SCL pin as output low
Delay();
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
Delay(); //延时
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow);//set SCL pin as output low
Delay(); //延时
}
//IIC发送一个字节
void IIC_Send_Byte(unsigned char txd)
{
unsigned char i;
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow); //set SCL pin as output low
Delay();
for(i = 0; i < 8; i++)
{
if(txd & 0x80)
{
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_High_Slow);//set SDA pin as output high
}
else
{
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_Out_PP_Low_Slow);//set SDA pin as output low
}
txd <<= 1;
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow); //set SCL pin as output low
Delay();
}
}
//读取一个字节
unsigned char IIC_Read_Byte(unsigned char ack)
{
unsigned char i, res = 0;
GPIO_Init(GPIOC,GPIO_Pin_0,GPIO_Mode_In_PU_No_IT); //set sda as input with pull up
for(i = 0; i < 8; ++i ){
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow);//set SCL pin as output high
Delay();
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output high
Delay();
res <<= 1;
if(SDA_READ()){
res ++;
}
}
return res;
}
//写数据
unsigned char BQ25890x_write_reg(unsigned char *pBuffer,unsigned char WriteAddr,unsigned char NumToWrite)
{
unsigned char u_wait_err = 0;
IIC_Start();
IIC_Send_Byte(SlaveAddress << 1);//slave addr
u_wait_err |= IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr); //register addr
u_wait_err |= IIC_Wait_Ack();
while(NumToWrite--)
{
IIC_Send_Byte(*pBuffer);
u_wait_err |= IIC_Wait_Ack();
pBuffer++;
}
IIC_Stop();
if(u_wait_err == 0)
return 0;
return 1;
}
//
unsigned char BQ25890x_read_reg(unsigned char *pBuffer,unsigned char WriteAddr,unsigned char NumToRead)
{
unsigned char u_wait_err = 0;
IIC_Start();
IIC_Send_Byte(SlaveAddress<<1); // SlaveAddress 0x7e
u_wait_err |= IIC_Wait_Ack();
IIC_Send_Byte(WriteAddr); // address
u_wait_err |= IIC_Wait_Ack();
IIC_Start();
IIC_Send_Byte((SlaveAddress<<1)+1); // SlaveAddress 0x7e
u_wait_err |= IIC_Wait_Ack();
while(--NumToRead)
{
*pBuffer = IIC_Read_Byte(1);
IIC_Ack();
pBuffer++;
}
*pBuffer = IIC_Read_Byte(1);
IIC_NAck();
IIC_Stop();
if(u_wait_err == 0)
return 0;
return 1;
}
好了,这就是这部分的内容了。
上一篇:单片机驱动 Led 灯应注意
下一篇:触控温度计设计
设计资源 培训 开发板 精华推荐
- 以科技先行,传感新未来 下载艾迈斯半导体 《高精度短距离测量方案 》答题赢好礼!
- 有奖直播:NXP 汽车系统电源管理开讲啦,从功能安全架构到新品FS26,预约有礼~
- 观看TI无线连接专场研讨会 下载TI无线电子书有礼
- 庆元宵答题抽奖,知识与礼物齐飞
- 解锁【W5500-EVB-Pico】,探秘以太网底层,得捷电子Follow me第4期来袭!
- 捉虫行动二:争做EE下载中心黑猫警长!
- 【EEWORLD第三十三届】2011年12月社区明星人物揭晓!
- 更直观的了解5G?观看罗德与施瓦茨主题演讲,填问卷赢好礼!
- 【开发板试用】89美金FPGA开发板试用风暴来袭!
- 有奖直播|安森美领先的成像技术助您推进视觉产品创新