STM32F4-Discovery I2C Problemi

Başlatan Bertan, 01 Ocak 2016, 18:39:33

Bertan

Merhabalar

     I2C kullanarak kitin üstündeki CS43L22 entegresinden veri okumaya çalşıyorum.Ama program her seferinde "while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));" satırında takılıyor.Programın I2C kısmını https://www.picproje.org/index.php?topic=59518.0 başlığındaki gibi yapıyorum.

Program :
#include "stm32f4xx.h"                  // Device header
#include <stdio.h>
#include <stdlib.h>

uint8_t readI2CData(uint8_t bytesToregs)
{
  uint8_t tmp=0;

  I2C_GenerateSTART(I2C1, ENABLE);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  I2C_Send7bitAddress(I2C1, 0x94, I2C_Direction_Transmitter); // 0x94 cihaz i2c adresi
	
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));   // Program bu satırda takılıyor.

  I2C_SendData(I2C1, bytesToregs);

  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));

  I2C_GenerateSTART(I2C1, ENABLE);
 
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));

  I2C_Send7bitAddress(I2C1, 0x94, I2C_Direction_Receiver);
 
  while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));

  I2C_GenerateSTOP(I2C1, ENABLE);
 
  while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED));

  tmp = I2C_ReceiveData(I2C1);

  return (tmp);
}


int main()
{	
	
	uint8_t data;
	char str[50];
	
	GPIO_InitTypeDef GPIO_InitStructure ;
	USART_InitTypeDef USART_InitStructure ;
	I2C_InitTypeDef I2C_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE) ;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);

	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF ;                    //USART
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_2 | GPIO_Pin_3 ;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2) ;
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2) ;
	
	GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF ;                      //I2C
	GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6 | GPIO_Pin_9 ;
	GPIO_InitStructure.GPIO_OType = GPIO_OType_OD ;
	GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz ;
	GPIO_Init(GPIOB, &GPIO_InitStructure);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);	// SCL
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_I2C1);      // SDA
		
	USART_InitStructure.USART_BaudRate            = 57600 ;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
	USART_InitStructure.USART_Mode                = USART_Mode_Tx | USART_Mode_Rx ;
	USART_InitStructure.USART_Parity              = USART_Parity_No ;
	USART_InitStructure.USART_StopBits            = USART_StopBits_1 ;
	USART_InitStructure.USART_WordLength          = USART_WordLength_8b ;
	USART_Init(USART2, &USART_InitStructure) ;
	USART_Cmd(USART2, ENABLE) ;
	
	I2C_DeInit(I2C1);
	
	I2C_InitStruct.I2C_ClockSpeed = 100000; 		
	I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;		
	I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;	
	I2C_InitStruct.I2C_OwnAddress1 = 0x00;			
	I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;		
	I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; 
	I2C_Init(I2C1, &I2C_InitStruct);
	I2C_Cmd(I2C1, ENABLE);

	data = readI2CData(0x01)>>3;	
	sprintf(str,"%d\n",data);
	USART_put(USART2 ,str);
	
  while(1);
	
}


Yardımcı olursanız sevinirim.Teşekkürler.

DaRt

cs43l22'nin enable pinini yüksek yapmadan çip i2cden iletişim kurmaz. Discovery kitte bu pin d4'e bağlıdır. Sanırım siz bu pini yüksek yapmamışsınız. Bu şekilde deneyin.

Bertan

Teşekkür ederim , dediğiniz gibi yaptım çalıştı  :)