微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM8L152模拟IIC程序

STM8L152模拟IIC程序

时间:12-03 来源:互联网 点击:
本篇仍然是模拟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;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top