STM8L152模拟IIC程序

发布者:创意旅程最新更新时间:2016-10-05 来源: eefocus关键字:STM8L152  模拟IIC 手机看文章 扫描二维码
随时随地手机看文章
本篇仍然是模拟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;
}


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

关键字:STM8L152  模拟IIC 引用地址:STM8L152模拟IIC程序

上一篇:单片机驱动 Led 灯应注意
下一篇:触控温度计设计

小广播
添点儿料...
无论热点新闻、行业分析、技术干货……
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved