本帖最后由 Di诺 于 2017-5-23 16:27 编辑
试了好久,连芯片ID怎么都读不出来。。。下面模拟iic程序有问题???
void BMP280_CfgSDAOutput(void)
{
nrf_gpio_cfg_output(BMP280_SDA_PIN);
}
/******************************************************************************/
void BMP280_CfgSDAInput(void)
{
nrf_gpio_cfg_input(BMP280_SDA_PIN,NRF_GPIO_PIN_PULLUP);
}
/******************************************************************************/
void BMP280_SetSDA(void)
{
nrf_gpio_pin_set(BMP280_SDA_PIN);
}
/******************************************************************************/
void BMP280_ClearSDA(void)
{
nrf_gpio_pin_clear(BMP280_SDA_PIN);
}
/******************************************************************************/
void BMP280_SetSCK(void)
{
nrf_gpio_pin_set(BMP280_SCK_PIN);
}
/******************************************************************************/
void BMP280_ClearSCK(void)
{
nrf_gpio_pin_clear(BMP280_SCK_PIN);
}
/******************************************************************************/
void BMP280_Init(void)
{
nrf_gpio_cfg_output(BMP280_EN_PIN);
nrf_gpio_pin_set(BMP280_EN_PIN);
nrf_gpio_cfg_output(BMP280_CSB_PIN);
BMP280_CfgSDAOutput();
nrf_gpio_cfg_output(BMP280_SCK_PIN);
nrf_gpio_cfg_output(BMP280_SDO_PIN);
nrf_gpio_pin_clear(BMP280_SDO_PIN);
nrf_gpio_pin_set(BMP280_CSB_PIN);
nrf_gpio_pin_clear(BMP280_EN_PIN);
BMP280_SetSDA();
nrf_delay_us(5);
BMP280_SetSCK();
nrf_delay_us(200000);
}
/******************************************************************************/
void IIC_Start(void)
{
BMP280_CfgSDAOutput();
BMP280_SetSDA();
BMP280_SetSCK();
nrf_delay_us(5);
BMP280_ClearSDA();
nrf_delay_us(5);
BMP280_ClearSCK();
// nrf_delay_us(1);
}
/******************************************************************************/
void IIC_Stop(void)
{
BMP280_CfgSDAOutput();
BMP280_ClearSCK();
BMP280_ClearSDA();
nrf_delay_us(5);
BMP280_SetSCK();
nrf_delay_us(5);
BMP280_SetSDA();
nrf_delay_us(5);
}
/*******************************************************************************
*/
uint8_t IIC_WaitAck(void)
{
uint8_t ack;
BMP280_CfgSDAInput();
BMP280_SetSDA();
nrf_delay_us(1);
BMP280_SetSCK();
nrf_delay_us(5);
if(nrf_gpio_pin_read(BMP280_SDA_PIN) == 1)
{
ack = 1;
}
else
{
ack = 0;
}
BMP280_ClearSCK();
nrf_delay_us(5);
return ack;
}
/******************************************************************************/
void IIC_Ack(void)
{
BMP280_ClearSCK();
BMP280_CfgSDAOutput();
BMP280_ClearSDA();
nrf_delay_us(5);
BMP280_SetSCK();
nrf_delay_us(5);
BMP280_ClearSCK();
// BMP280_SetSDA();
}
/******************************************************************************/
void IIC_NoAck(void)
{
BMP280_ClearSCK();
BMP280_CfgSDAOutput();
BMP280_SetSDA();
nrf_delay_us(5);
BMP280_SetSCK();
nrf_delay_us(5);
BMP280_ClearSCK();
}
/******************************************************************************/
void IIC_SendByte(uint8_t dat)
{
uint8_t i, d;
BMP280_CfgSDAOutput();
BMP280_ClearSCK();
// nrf_delay_us(1);
d = dat;
for(i = 0; i < 8; i++)
{
if((d & 0x80)>>7)
{
BMP280_SetSDA();
}
else
{
BMP280_ClearSDA();
}
d<<=1;
nrf_delay_us(5);
BMP280_SetSCK();
nrf_delay_us(5);
BMP280_ClearSCK();
nrf_delay_us(5);
}
}
/******************************************************************************/
uint8_t IIC_ReadByte(void)
{
uint8_t d, i;
BMP280_CfgSDAInput();
nrf_delay_us(1);
d = 0;
for(i = 0; i < 8; i++)
{
BMP280_ClearSCK();
nrf_delay_us(5);
BMP280_SetSCK();
d <<= 1;
if(nrf_gpio_pin_read(BMP280_SDA_PIN) == 1)
{
d++;
}
}
return d;
}
/******************************************************************************/
uint8_t BMP280_ReadOneByte(uint8_t addr)
{
uint8_t flag, tmp;
IIC_Start();
IIC_SendByte(BMP280_SLAVE_ADDR );
IIC_WaitAck();
// if(== 0)
{
IIC_SendByte(addr);
IIC_WaitAck();
// if( == 0)
{
IIC_Start();
IIC_SendByte(BMP280_SLAVE_ADDR | 0x01);
IIC_WaitAck();
//if( == 0)
{
tmp = IIC_ReadByte();
IIC_NoAck();
}
}
}
IIC_Stop();
return tmp;
}
/******************************************************************************/
uint16_t BMP280_ReadTwoByte(uint8_t addr)
{
uint8_t flag;
uint16_t d;
IIC_Start();
IIC_SendByte(BMP280_SLAVE_ADDR);
d = 0;
if(flag == 0)
{
IIC_SendByte(addr<<1);
IIC_Start();
IIC_SendByte(BMP280_SLAVE_ADDR | 0x01);
d = ((uint16_t)IIC_ReadByte())<<8;
IIC_Ack();
d |= (uint16_t)IIC_ReadByte();
IIC_NoAck();
IIC_Stop();
}
return d;
}
/******************************************************************************/
void BMP280_WriteOneByte(uint8_t addr, uint8_t dat)
{
IIC_Start();
IIC_SendByte(BMP280_SLAVE_ADDR);
IIC_SendByte(addr);
IIC_SendByte(dat);
IIC_Stop();
}
/******************************************************************************/
uint8_t bmp280_id;
void BMP280_CS(void)
{
bmp280_id = BMP280_ReadOneByte(BMP280_CHIPID_REG);
}
/******************************************************************************/
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>