您的位置 首页 知识

STM8L152模仿IIC程序

本篇仍然是模拟IIC,基本思路仍然是STM32的模拟IIC思路。具体内容还是参考STM32使用模拟IIC操作EEPROM。示例代码如下:stm8_iic.h文件内容如…

本篇仍然是模仿IIC,基本思路仍然是STM32的模仿IIC思路。具体内容仍是参阅STM32运用模仿IIC操作EEPROM。

示例代码如下:
stm8_iic.h文件内容如下:

#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

stm8_iic.c内容如下:


#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;
}

好了,这便是这部分的内容了。

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/zhishi/252320.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部