大家好,小弟初来乍到,请大家多多指教。
我最近在写一个有关stm32上的I2C的驱动程序,从ARM上向从机写数据,出了一些问题:
在主机stm32发送 从地址后,从机没有ACK。
我查看过I2C的寄存器,SR1的值为0x0400,也就是AF位置1,应答失败。
附上程序:(程序是在stm32给的例子上改的)
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
typedef enum { FAILED = 0, PASSED = !FAILED} TestStatus;
/* Private define ------------------------------------------------------------*/
#define I2C_SLAVE_ADDRESS7 0x00(主机地址)
#define I2C_SLAVE1_ADDRESS7 0x82(从机地址)
#define TIME_OUT 200
#define BufferSize 2
#define ClockSpeed 93000(此频率是参考从机手册,通过计算得到)
/* Private macro -------------------------------------------------------------*/
u8 I2C2_Buffer1_Tx[BufferSize], I2C2_Buffer2_Rx[BufferSize];(主机的数据缓存)
u8 Tx_Idx = 0, Rx_Idx = 0,k=0;
/* Private variables ---------------------------------------------------------*/
I2C_InitTypeDef I2C_InitStructure;
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;
ErrorStatus HSEStartUpStatus;
/* Private functions ---------------------------------------------------------*/
void RCC_Configuration(void); (外部时钟,通用接口,嵌入中断的一些配置)
void GPIO_Configuration(void);
void NVIC_Configuration(void);
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
I2C_ITConfig(I2C2,I2C_IT_EVT|I2C_IT_BUF,ENABLE);(将CR2寄存器中的ITEVT和ITBUF置1)
/* Enable I2C2 ----------------------------------------------------*/
I2C_Cmd(I2C2, ENABLE);
/* I2C2 configuration ------------------------------------------------------*/
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS7;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = ClockSpeed;
I2C_Init(I2C2, &I2C_InitStructure);
/*----- First transmission Phase -----*/
/* Send I2C2 START condition */
I2C_GenerateSTART(I2C2, ENABLE);
/* Test on I2C2 EV5 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))(通过START的产生,确定主机模式)
{k++;
if(k>=TIME_OUT)
return 0x11;
}
k=0;
/* Send I2C2 slave Address for write */
I2C_Send7bitAddress(I2C2, I2C_SLAVE1_ADDRESS7, I2C_Direction_Transmitter);(送7位地址)
/* Test on I2C2 EV6 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))(问题就出在这里,在选择主机发送模式时,无法跳出循环,此时SR1=0x0400,SR2=0x0003)
{k++;
if(k>=TIME_OUT)
return 0xff;
}
I2C2_Buffer1_Tx[0] =0x00;
I2C2_Buffer1_Tx[1] =0x0ff;
/* Send data */
while (Rx_Idx < BufferSize)
{
/* Send I2C2 data */
I2C_SendData(I2C2, I2C2_Buffer1_Tx[Tx_Idx++]);
/* Test on I2C1 EV8 and clear it */
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* Send I2C1 STOP Condition */
I2C_GenerateSTOP(I2C2, ENABLE); |