STM8L152模擬IIC程序
#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);
}
//讀取數(shù)據(jù)
unsigned char SDA_READ(void)
{
//return GPIO_ReadInputPin(IIC_GPIOx, IIC_SDA_GPIO_Pin);
return GPIO_ReadInputDataBit(IIC_GPIOx, IIC_SDA_GPIO_Pin);
}
//產(chǎn)生起始信號(hào)
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();
}
//產(chǎn)生停止信號(hào)
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();
}
//等待響應(yīng)信號(hào)
//0--接收應(yīng)答成功 1-接收應(yīng)答失敗
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;
}
//產(chǎn)生ACK信號(hào)
void IIC_Ack(void)
{
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_High_Slow);//set SCL pin as output low
Delay(); //延時(shí)
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(); //延時(shí)
GPIO_Init(GPIOC,GPIO_Pin_1,GPIO_Mode_Out_PP_Low_Slow);//set SCL pin as output low
Delay(); //延時(shí)
}
//IIC發(fā)送一個(gè)字節(jié)
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();
}
}
//讀取一個(gè)字節(jié)
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;
}
//寫(xiě)數(shù)據(jù)
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;
}
好了,這就是這部分的內(nèi)容了。
評(píng)論