微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > BQ7693003

BQ7693003

时间:10-02 整理:3721RD 点击:
[local]1[/local]最近在用一个电源管理芯片BQ7693003,然后STM32F107在和它通信时,总是收不到应答,大神求助!我用的是模拟I2C,可以和24C02进行读写,和BQ7693003就不行了。

/* defines ------------------------------------------------------------------*/
#define     I2C_PageSize             256
#define     I2C1_SLAVE_ADDRESS7_W    0x10
#define     I2C1_SLAVE_ADDRESS7_R     0x11
#define FALSE   0
#define TRUE    1
#define SDA_PORT      GPIOB
#define SCL_PORT      GPIOB
#define SDA_PIN          GPIO_Pin_7
#define SCL_PIN          GPIO_Pin_6
#define SCL_H             GPIOB->BSRR = GPIO_Pin_6
#define SCL_L             GPIOB->BRR  = GPIO_Pin_6
#define SDA_H             GPIOB->BSRR = GPIO_Pin_7
#define SDA_L             GPIOB->BRR  = GPIO_Pin_7
#define SCL_read        GPIOB->IDR  & GPIO_Pin_6
#define SDA_read        GPIOB->IDR  & GPIO_Pin_7

/*******************************************************************************
* File Name          : SI2C.c
* Author             : zhang
* Version            : V1.0.0
* Date               : 2011.12.08
*******************************************************************************/
#include "I2C.h"
#include "stm32f10x.h"
#include "stm32f10x_it.h"
#include "main.h"
vu8 FRAM_ADDRESS;
/* function ------------------------------------------------------------------*/
/*******************************************************************************
* Function Name  : I2CInit
* Description    : I2CInit
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2CInit(void)
{
       GPIO_InitTypeDef  GPIO_InitStructure;
       RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
       GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//SDA
        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;  
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
//SCL
GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;  
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
SCL_H;  
SDA_H;
}

/*******************************************************************************
* Function Name  : I2C_delay
* Description    : I2C_delay
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_delay(void)
{        
   //u8 i=200;
   //while(i)
   //{
   //  i--;
   //}
   Delay_us(10);
}

/*******************************************************************************
* Function Name  : I2C_Start
* Description    : I2C_Start
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
u8 I2C_Start(void)
{
    SDA_H;
    SCL_H;
    I2C_delay();
    if(!SDA_read)return FALSE;        
    SDA_L;
    I2C_delay();
    if(SDA_read) return FALSE;        
    SCL_L;
    I2C_delay();
    return TRUE;
}

/*******************************************************************************
* Function Name  : I2C_Stop
* Description    : I2C_Stop
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_Stop(void)
{
    SCL_L;
    I2C_delay();
    SDA_L;
    I2C_delay();
    SCL_H;
    I2C_delay();
    SDA_H;
    I2C_delay();
}

/*******************************************************************************
* Function Name  : I2C_Ack
* Description    : I2C_Ack
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_Ack(void)
{        
    SCL_L;
    I2C_delay();
    SDA_L;
    I2C_delay();
    SCL_H;
    I2C_delay();
    SCL_L;
    I2C_delay();
}
/*******************************************************************************
* Function Name  : I2C_NoAck
* Description    : I2C_NoAck
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_NoAck(void)
{        
    SCL_L;
    I2C_delay();
    SDA_H;
    I2C_delay();
    SCL_H;
    I2C_delay();
    SCL_L;
    I2C_delay();
}
/*******************************************************************************
* Function Name  : I2C_WaitAck
* Description    : I2C_WaitAck
* Input          : None
* Output         : None
* Return         : TRUE/FALSE
*******************************************************************************/
u8 I2C_WaitAck(void)         
{
    SCL_L;
    I2C_delay();
    SDA_H;                        
    I2C_delay();
    SCL_H;
    I2C_delay();
    if(SDA_read)
    {
SCL_L;
return FALSE;
    }
    SCL_L;
    return TRUE;
}
/*******************************************************************************
* Function Name  : I2C_SendByte
* Description    : I2C_SendByte
* Input          : SendByte
* Output         : None
* Return         : None
*******************************************************************************/
void I2C_SendByte(u8 SendByte)
{
    u8 i=8;
    while(i--)
    {
      SCL_L;
      I2C_delay();
      if(SendByte&0x80)
        SDA_H;  
      else
        SDA_L;   
      SendByte<<=1;
      I2C_delay();
      SCL_H;
      I2C_delay();
    }
    SCL_L;
}
/*******************************************************************************
* Function Name  : I2C_ReceiveByte
* Description    : I2C_ReceiveByte
* Input          : None
* Output         : None
* Return         : ReceiveByte
*******************************************************************************/
u8 I2C_ReceiveByte(void)  
{
    u8 i=8;
    u8 ReceiveByte=0;
    SDA_H;                                
    while(i--)
    {
      ReceiveByte<<=1;      
      SCL_L;
      I2C_delay();
      SCL_H;
      I2C_delay();        
      if(SDA_read)
      {
        ReceiveByte|=0x01;
      }
    }
    SCL_L;
    return ReceiveByte;
}
/*******************************************************************************
* Function Name  : I2C_FRAM_BufferWrite
* Description    : I2C_FRAM_BufferWrite
* Input            :
* Output         : None
* Return         : None
*******************************************************************************/
u8 I2C_FRAM_BufferWrite(u8* pBuffer, u16 WriteAddr, u16 NumByteToWrite)
{
       u8 Addr = 0, count = 0;  
      Addr = (WriteAddr) / I2C_PageSize;  
      count = (WriteAddr)  % I2C_PageSize;  
      Addr = Addr << 1;
      Addr = Addr & 0x0F;
      FRAM_ADDRESS = I2C1_SLAVE_ADDRESS7_W | Addr;
      if (!I2C_Start())
      return FALSE;
      I2C_SendByte(FRAM_ADDRESS);
      if (!I2C_WaitAck())
       {
              I2C_Stop(); //在这结束,无应答
              return FALSE;
       }
       I2C_SendByte(count);
       I2C_WaitAck();        
       while(NumByteToWrite--)
       {
             I2C_SendByte(* pBuffer);
             I2C_WaitAck();
             pBuffer++;
       }
       I2C_Stop();
       return TRUE;
}
/*******************************************************************************
* Function Name  : I2C_FRAM_BufferRead
* Description    : I2C_FRAM_BufferRead
* Input            :
* Output         : None
* Return         : None
*******************************************************************************/      
u8 I2C_FRAM_BufferRead(u8* pBuffer, u16 WriteAddr, u16 NumByteToRead)
{               
    u8 Addr = 0, count = 0;
    Addr = WriteAddr / I2C_PageSize;
    count = WriteAddr % I2C_PageSize;
    Addr = Addr << 1;
    Addr = Addr & 0x0F;  
    FRAM_ADDRESS = I2C1_SLAVE_ADDRESS7_R | Addr;
    if (!I2C_Start()) return FALSE;
    I2C_SendByte(FRAM_ADDRESS);
    if (!I2C_WaitAck())
    {
        I2C_Stop();
        return FALSE;
    }
    I2C_SendByte(count);   
    I2C_WaitAck();
    I2C_Start();
    I2C_SendByte(FRAM_ADDRESS | 0x01);
    I2C_WaitAck();
    while(NumByteToRead)
    {
        *pBuffer = I2C_ReceiveByte();
        if(NumByteToRead == 1)I2C_NoAck();
        else I2C_Ack();
        pBuffer++;
        NumByteToRead--;
    }
    I2C_Stop();
    return TRUE;
}
void main(void)
{
u8 BQ769_INITAdd[11] ={0x00,0x01,0x02,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b};
u8 BQ769_INITdata[11]={0x08,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0xAC,0x97,0x19};
u8 BQ769_RegAdd[12]  ={0x0c,0x0d,0x0e,0x0f,0x51};
u8 BQ769_Voltage[12] ={0};
bsp_init();
I2CInit();
I2C_FRAM_BufferWrite(BQ769_INITdata,BQ769_INITAdd[0],12);
while(1)
{
   I2C_FRAM_BufferRead(BQ769_Voltage,BQ769_RegAdd[0],10);
   Delay_us(500);
}
}

时序


上一篇:单片机流程
下一篇:stm32正交解码

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

网站地图

Top