我开始在STM32上做一个设备的IIC的实验时 直接使用说stm32的库开发 结果很好 数据正常
但是加载到UC/GUI上之后 数据就读不出来了,我仔细检查了他的配置没问题 时钟设置也没问题 应答也允许了
跟之前的程序一样的 为什么IIC就是不能正常工作呢 哪里出了问题 急急急
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
/* PB6,7 SCL and SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIO_Pin_6->SCL, GPIO_Pin_7->SDA
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; //设置管脚为复用功能开漏输出
GPIO_Init(GPIOB, &GPIO_InitStructure); //I2C接口使用的GPIO管脚初始化
I2C_DeInit(I2C1);
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; //设置I2C接口模式
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; //设置I2C接口的高低电平周期
I2C_InitStructure.I2C_OwnAddress1 = 0xA0; //设置I2C接口的主机地址
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; //设置是否开启ACK响应
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 200000; //100K速度
// I2C_SoftwareResetCmd(I2C1,ENABLE);
// I2C_SoftwareResetCmd(I2C1,DISABLE);
I2C_Cmd(I2C1, ENABLE); //使能I2C接口
I2C_Init(I2C1, &I2C_InitStructure); //I2C接口初始化
/*允许1字节1应答模式*/
// I2C_AcknowledgeConfig(I2C1, ENABLE); //使能I2C接口响应
}
/****************IIC器件测试程序*********************************/
float I2C_GY26_Test(void)
{
u8 Buffer[8];
u16 Angle;
float dat=0.0;
// I2C_Configuration();
//u16 Temp,Comp,Cal;
I2C_Single_Write(0xE0,0x00,0x31); //测量一次角度
// Delay(100);
I2C_Single_Write(0xE0,0x00,0x35); //测量一次温度
// Delay(100);
I2C_Multiple_Read(0xE0,0x00,Buffer,8); //读取数据
Delay(200);
Angle=(Buffer[1]<<8)+Buffer[2];
dat=conversion(Angle);
printf("当前测量角度值为:0x%X, 转换结果为:%g \n\r",Angle,dat);
return dat;
}
/***********界面处理*****************/
void _ExecCalibration(void)
{
// unsigned char c=1;
float dat=0.0;
int Key=0;
GUI_TOUCH_SetDefaultCalibration();
/* _Calibrate upper left */
GUI_SetBkColor(GUI_BLUE);
GUI_Clear();
/* 界面构图 */
GUI_SetColor(GUI_WHITE);
GUI_DrawLine(0, 46, 320-1,46);
GUI_DrawLine(0, 92, 320-1,92);
GUI_DrawLine(0, 136, 320-1, 136);
GUI_DrawLine(0, 180, 320-1, 180);
GUI_DrawLine(0, 2, 320-1, 2);
GUI_DrawLine(88, 2, 88, 179);
GUI_SetFont(&GUI_FontComic18B_ASCII);
GUI_SetColor(GUI_RED);
GUI_DispStringAt("Lean_Ang", 2,15);
GUI_DispStringAt("Comp_Pnt", 2,61 );
GUI_DispStringAt("Sonic_Dete", 2, 106);
GUI_DispStringAt("TriAx_Acc ", 2, 149);
WM_ExecIdle(); //刷新屏幕
/* 建立2个按钮 */
ahButton[0] = BUTTON_Create(2, 195, 100,43, GUI_KEY_F1 , WM_CF_SHOW | WM_CF_STAYONTOP | WM_CF_MEMDEV);
ahButton[1] = BUTTON_Create(214, 195, 100,43, GUI_KEY_F2 , WM_CF_SHOW | WM_CF_STAYONTOP | WM_CF_MEMDEV);
/*设置按钮的属性 */
BUTTON_SetFont(ahButton[0],&GUI_Font8x16);
BUTTON_SetFont(ahButton[1],&GUI_Font8x16);
BUTTON_SetBkColor(ahButton[0],0,GUI_BLACK);
BUTTON_SetBkColor(ahButton[1],0,GUI_BLACK);
BUTTON_SetTextColor(ahButton[0],0,GUI_WHITE);
BUTTON_SetTextColor(ahButton[1],0,GUI_WHITE);
BUTTON_SetText(ahButton[0], "START");
BUTTON_SetText(ahButton[1], "EXIT");
do
{
Key = GUIDEMO_WaitKey();
switch (Key)
{
case 40:
{
dat=I2C_GY26_Test(); //GY_26器件操作 程序运行到这里就无法继续了 ,请教
GUI_SetFont(&GUI_FontComic24B_1);
GUI_SetColor(GUI_BLACK);
GUI_GotoXY(105,15);
GUI_DispStringAt("DATA0: ", 105, 15);
GUI_GotoXY(175,15);
GUI_DispFloatFix(dat,4,1);
GUI_GotoXY(105,61);
GUI_DispStringAt("DATA1 ", 105, 61);
GUI_GotoXY(105,106);
GUI_DispStringAt("DATA2", 105, 106);
GUI_GotoXY(105,149);
GUI_DispStringAt("DATA3 ", 105, 149);
GUI_DispFloatFix(dat,4,1);
// WM_ExecIdle();
// c=1;
}
break;
case 41: break;
// default: ;
}
}while (Key!=41);
BUTTON_Delete(ahButton[0]);
BUTTON_Delete(ahButton[1]);
}
|