在线时间1253 小时
UID43170
ROHM金币2792
注册时间2018-5-2
该用户从未签到
论坛元老
- 最后登录
- 2024-5-13
|
今天有空了,继续来折腾,折腾了半天,底层大部分程序都弄好了,但是读出的值不对,希望大家帮忙分析一下。
采用的是单次转换模式,14bit,采用定时3秒启动转换一次,转换完成后读取并显示,但是读到的值不对。上程序
按照这个进行的
底层驱动程序
void Delay_Nus (uint16_t t) //延时N个US
{
uint16_t i;
for (i = 0; i < t; i ++)
{
__ASM("NOP");
}
}
void I2C_Init (void)
{
Set_SCK();
Set_SDA();
}
void I2C_Start (void)
{
Set_SDA();
Set_SCK();
Delay_Nus (5);
Clr_SDA();
Delay_Nus (5);
Clr_SCK();
Delay_Nus (5);
}
void I2C_Stop (void)
{
Clr_SCK();
Clr_SDA();
Delay_Nus (5);
Set_SCK();
Delay_Nus (5);
Set_SDA();
Delay_Nus (5);
}
void I2C_Ack (void)
{
Clr_SDA();
Delay_Nus (5);
Set_SCK();
Delay_Nus (5);
Clr_SCK();
Delay_Nus (5);
}
void I2C_Nack (void)
{
Set_SDA();
Delay_Nus (5);
Set_SCK();
Delay_Nus (5);
Clr_SCK();
Delay_Nus (5);
}
//故障判断 =0,正常,=1,非应答(判断SDA线,=0表示有应答)
uint8_t I2C_Send_Byte (uint8_t dat)
{
uint8_t i;
uint8_t ack;
for (i = 8; i != 0; i --)
{
if (dat & 0x80)
Set_SDA();
else
Clr_SDA();
Delay_Nus (5);
Set_SCK();
Delay_Nus (5);
Clr_SCK();
dat = dat << 1;
}
Delay_Nus (5);
Set_SDA();
Delay_Nus (5);
Set_SCK();
SDA_IN(); //SDA输入
Delay_Nus (5);
ack = Read_SDA();
SDA_OUT(); //SDA输出
Delay_Nus (5);
Clr_SCK();
Delay_Nus (5);
return ack;
}
uint8_t I2C_Receive_Byte (void)
{
uint8_t i, dat = 0;
for (i = 8; i != 0; i --)
{
Set_SCK();
SDA_IN(); //SDA输入
Delay_Nus (5);
dat = dat << 1;
dat = dat | ((uint8_t)(Read_SDA()));
Delay_Nus (5);
SDA_OUT(); //SDA输出
Clr_SCK();
Delay_Nus (5);
}
return dat;
}
void IIC_Write_ONE (uint8_t address, uint8_t dat)
{
I2C_Start();
I2C_Send_Byte(BM1422_ADD);
I2C_Send_Byte(address);
I2C_Send_Byte(dat);
I2C_Stop();
Delay_Nms(10);
}
void IIC_Write (uint8_t *buff, uint8_t address, uint16_t count)
{
while(count--)
{
I2C_Start();
I2C_Send_Byte(BM1422_ADD);
I2C_Send_Byte(address);
I2C_Send_Byte(*buff);
I2C_Stop();
Delay_Nms(5);
buff ++;
address ++;
}
}
uint8_t IIC_Read_ONE (uint8_t address)
{
uint8_t temp;
I2C_Start();
I2C_Send_Byte(BM1422_ADD);
Delay_Nus (10);
I2C_Send_Byte(address);
I2C_Start();
I2C_Send_Byte(BM1422_ADD + 1);
temp=I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
return temp;
}
void IIC_Read (uint8_t *buff, uint8_t address, uint8_t count)
{
while(count--)
{
I2C_Start();
I2C_Send_Byte(BM1422_ADD);
I2C_Send_Byte(address);
I2C_Start();
I2C_Send_Byte(BM1422_ADD + 1);
*(uint8_t*)buff = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
buff ++;
address ++;
}
}
/*void IIC_Read (uint8_t *buff, uint8_t address, uint8_t count)
{
I2C_Start();
I2C_Send_Byte(BM1422_ADD);
I2C_Send_Byte(address);
I2C_Start();
I2C_Send_Byte(BM1422_ADD + 1);
while(count > 1)
{
count --;
*(uint8_t*)buff = I2C_Receive_Byte();
I2C_Ack();
buff ++;
}
*(uint8_t*)buff = I2C_Receive_Byte();
I2C_Nack();
I2C_Stop();
}*/
/*********************************************************************************************************
* 函数名称:BM1422_Init
* 函数功能:BM1422_初始化。
* 入口参数:无
* 出口参数:无
* 修改时间:2018-11-19
********************************************************************************************************/
void BM1422_Init (void)
{
//uint8_t num;
//uint8_t buff[2];
/*num = IIC_Read_ONE (0x0F); //读测试,读0D,0E,0F分别得到01,01,41
Clear_2Num (0, 0, BLACK); //显示接收到的数据
Display_2Hex (0, 0, num, RED);*/
IIC_Write_ONE (0x1B, 0xC2);
/*Delay_Nms (1000);
num = IIC_Read_ONE (0x1B);
Clear_2Num (0, 0, BLACK); //显示接收到的数据,读到的数据和写的一样
Display_2Hex (0, 0, num, RED);*/
IIC_Write_ONE (0x5C, 0x00);
IIC_Write_ONE (0x5D, 0x00);
IIC_Write_ONE (0x1C, 0x0C);
//IIC_Write_ONE (0x5C, 0x00);
/*buff[0] = 0x01;
buff[1] = 0x00;
IIC_Write (buff, 0x6C, 2);
IIC_Write (buff, 0x72, 2);
IIC_Write (buff, 0x78, 2);*/
/*num = IIC_Read_ONE (0x1C);
Clear_2Num (0, 0, BLACK); //显示接收到的数据
Display_2Hex (0, 0, num, RED);*/
}
int main(void)
{
uint16_t X_Valve, Y_Valve ,Z_Valve;
uint8_t Read_Buff[6];
uint8_t State;
SystemCoreClockUpdate();
Board_Init();
/* Enable and setup SysTick Timer at a periodic rate */
SysTick_Config(SystemCoreClock / 1 / 30);
TIMER32_0_Init (10); //10MS定时中断
GPIO_Init (); //GPIO引脚初始化
OLED_Pin_Init(); //OLED引脚电平初始化
Led_Blink (); //LED闪烁
//
ssd1331_init(); //OLED显示初始化
ssd1331_clear_screen(BLACK); //清屏
ssd1331_display_text(16, 0, "BM1422 Test", GREEN);//显示8*XASCII字符
ssd1331_display_1608_text(0, 16, "X:", RED);//显示8*XASCII字符
ssd1331_display_1608_text(0, 32, "Y:", RED);//显示8*XASCII字符
ssd1331_display_1608_text(0, 48, "Z:", RED);//显示8*XASCII字符
BM1422_Init ();
Board_LED_Set(0, false);
Board_LED_Set(1, false);
Board_LED_Set(2, false);
while (1)
{
if (T0_Count0_Flag == 1) //3S启动一次转换
{
T0_Count0_Flag = 0;
IIC_Write_ONE (0x1D, 0x40);
}
State = IIC_Read_ONE (0x18);
if ((State & 0x40) == 0x40) //为1的话转换完成
//if (Read_RDY() == 1) //为0的话等待转换完成
{
Test_Count ++;
//IIC_Read (Read_Buff, 0x10, 6);
Read_Buff[0] = IIC_Read_ONE (0x10);
Read_Buff[1] = IIC_Read_ONE (0x11);
Read_Buff[2] = IIC_Read_ONE (0x12);
Read_Buff[3] = IIC_Read_ONE (0x13);
Read_Buff[4] = IIC_Read_ONE (0x14);
Read_Buff[5] = IIC_Read_ONE (0x15);
X_Valve = ((uint16_t)Read_Buff[1] << 8) | Read_Buff[0];
Y_Valve = ((uint16_t)Read_Buff[3] << 8) | Read_Buff[2];
Z_Valve = ((uint16_t)Read_Buff[5] << 8) | Read_Buff[4];
Clear_4Num (16, 16, BLACK); //显示读到的数据
Display_4Hex (16, 16, X_Valve, RED);
Clear_4Num (16, 32, BLACK);
Display_4Hex (16, 32, Y_Valve, RED);
Clear_4Num (16, 48, BLACK);
Display_4Hex (16, 48, Z_Valve, RED);
Clear_4Num (64, 48, BLACK);
Display_4Hex (64, 48, Test_Count, GREEN);
}
/*Board_LED_Set(0, false);
Delay_Nms (500);
Board_LED_Set(0, true);
Delay_Nms (500);*/
__WFI();
}
}
BM1422_Init (void)里的读写都验证过了没有问题,读固定寄存器的值得到的和手册上一样的,写然后读也是对的,读到的是写的值
但是读数据寄存器出来的值不对
显示效果,值不对:
|
|