STM32F070控制BH1750的程序问题

2019-08-13 22:56发布

使用STM32F070CB控制BH1750测量光照,网上找了个例程,可以调试一直卡在了 while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET);这个位置,之前F103用的多,070还是第一次用,也没有开发资料。哪位仁兄有过类似经验,给帮忙看看啊,不胜感激~#define  DVI_1  GPIO_SetBits(GPIOB, GPIO_Pin_5);         //BH1750的DVI控制
#define  DVI_0  GPIO_ResetBits(GPIOB, GPIO_Pin_5);

#define SlaveAddress 0xB8                                //BH1750地址

#define BH1750FVI_POWER_DOWM                      0x00   //BH1750寄存器地址
#define BH1750FVI_POWER_ON                        0x01
#define BH1750FVI_RESET                           0x07
#define BH1750FVI_CONTINUOUSLY_H_RESOLUTION_MODE  0x10
#define BH1750FVI_CONTINUOUSLY_H_RESOLUTION_MODE2 0x11
#define BH1750FVI_CONTINUOUSLY_L_RESOLUTION_MODE  0x13
#define BH1750FVI_ONE_TIME_H_RESOLUTION_MODE      0x20
#define BH1750FVI_ONE_TIME_H_RESOLUTION_MODE2     0X21
#define BH1750FVI_ONE_TIME_L_RESOLUTION_MODE      0x23


main()
{
    DVI_0;
    LED_Delay(0x0FFFFF);
    DVI_1;
    Single_Write_BH1750(BH1750FVI_POWER_ON);
    Single_Write_BH1750(BH1750FVI_CONTINUOUSLY_H_RESOLUTION_MODE);
    while (1)
    {
        Light = Read_BH1750();
        LED_Delay(0x0FFFFF);

     }
}


void Single_Write_BH1750(uint8_t REG_Data)
{
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);//检测是否总线忙
    I2C_TransferHandling(I2C1,SlaveAddress,1,I2C_SoftEnd_Mode,I2C_Generate_Start_Write);
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET);// 检测发送,卡在了这个位置
    I2C_SendData(I2C1,REG_Data);
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TC) == RESET);
    I2C_GenerateSTOP(I2C1,ENABLE);
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) == RESET);//是否停止
}

uint16_t Read_BH1750()
{
    uint16_t Recev = 0x00;//光强为16位。先发送高8后低8
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);//IF BUSY
    I2C_TransferHandling(I2C1,SlaveAddress+1,2,I2C_AutoEnd_Mode,I2C_Generate_Start_Read);//这里设置后就不需要 I2C_GenerateSTOP 了
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET);
    Recev |= I2C_ReceiveData(I2C1);Recev<<=8;
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET);
    Recev |= I2C_ReceiveData(I2C1);
    while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) == RESET);
    return Recev;
}


void I2C_EEPROM_24C0x_Init(void)
{
    I2C_InitTypeDef  I2C_InitStructure;

    GPIO_InitTypeDef  GPIO_InitStructure;

    I2C_EEPROM_24C0x_DeInit();

    /* sEE_I2C_SCL_GPIO_CLK and sEE_I2C_SDA_GPIO_CLK Periph clock enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

    /* sEE_I2C Periph clock enable */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

    /* Connect PXx to I2C_SCL*/
    GPIO_PinAFConfig(sEE_I2C1_PORT, GPIO_PinSource6, GPIO_AF_1);

    /* Connect PXx to I2C_SDA*/
    GPIO_PinAFConfig(sEE_I2C1_PORT, GPIO_PinSource7, GPIO_AF_1);

    /* GPIO configuration */  
    /* Configure sEE_I2C pins: SCL */
    /***
    GPIO_InitStructure.GPIO_Pin = I2C1_SCL;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(sEE_I2C1_PORT, &GPIO_InitStructure);

    /* Configure sEE_I2C pins: SDA */
    //GPIO_InitStructure.GPIO_Pin = I2C1_SDA;

    //GPIO_Init(sEE_I2C1_PORT, &GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = I2C1_SCL | I2C1_SDA;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//这个我觉得影响不大,只要是推挽模式这个可以换成NOPULL
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    /* I2C configuration */
    /* sEE_I2C configuration */
    I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
    I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
    I2C_InitStructure.I2C_DigitalFilter = 0x00;
    I2C_InitStructure.I2C_OwnAddress1 = 0x00;
    I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
    I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    I2C_InitStructure.I2C_Timing = sEE_I2C_TIMING;

    /* Apply sEE_I2C configuration after enabling it */
    I2C_Init(I2C1, &I2C_InitStructure);

    /* sEE_I2C Peripheral Enable */
    I2C_Cmd(I2C1, ENABLE);

    /*选择要控制BH1750的DVI引脚*/                                                                                                                           
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;        

    /*设置要控制的GPIOB引脚为输出模式*/        
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;

    /*设置引脚速率为50MHz */   
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    /*设置引脚模式为通用推挽输出*/
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

    /*设置引脚模式为无上下拉*/
    GPIO_InitStructure.GPIO_PuPd   = GPIO_PuPd_NOPULL;  

    /*调用库函数,初始化GPIOB*/
    GPIO_Init(GPIOB, &GPIO_InitStructure);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。