PDA

توجه ! این یک نسخه آرشیو شده میباشد و در این حالت شما عکسی را مشاهده نمیکنید برای مشاهده کامل متن و عکسها بر روی لینک مقابل کلیک کنید : ارتباط دو تا stm32 با spi



wantedboy
08-06-2015, 10:31
سلام دوستان
با عرض خسته نباشید
من می خوام بین دو تا STM32F103RET6 با SPI دیتا ارسل کنم. اما هرچی تلاش می کنم و از مثال های خود KEIL هم استفاده کردم باز جواب نداد.
برنامه ای که نوشتم به صورت زیر است.
راستی روی MODE Fullduplx with nss sowft راه انداختم
این برنامه MASTER

/************************************************** ****************************** 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();


/* 1st phase: SPI1 Master */
/* SPI1 Config -------------------------------------------------------------*/
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);




/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);


/* Transfer procedure */
delay_init(72);
delay_ms(8000);
while(1)
{
/* Wait for SPI1 Tx buffer empty */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
/* Send SPI1 data */
SPI_I2S_SendData(SPI1,5);






}


}


/************************************************** *****************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();


/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);


/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();


if (HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enabl e);


/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);


/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);


/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2);


/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);


/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);


/* Enable PLL */
RCC_PLLCmd(ENABLE);


/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{}


/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


/* Wait till PLL is used as system clock source */
while (RCC_GetSYSCLKSource() != 0x08)
{}
}


/* Enable peripheral clocks --------------------------------------------------*/
/* GPIOA, GPIOB and SPI1 clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |
RCC_APB2Periph_SPI1, ENABLE);
}


/************************************************** *****************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;


/* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);


}


/************************************************** *****************************
* Function Name : NVIC_Configuration
* Description : Configures NVIC and Vector Table base location.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}




اینم برنامه ی SLAVE


char byte;int main(void)
{
#ifdef DEBUG
debug();
#endif


/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();


/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();


/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();


/* SPI1 Slave */


/* SPI1 Config -------------------------------------------------------------*/
SPI_InitStructure.SPI_Mode = SPI_Mode_Slave;
SPI_Init(SPI1, &SPI_InitStructure);

/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE);
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable,ENABLE) ;
/* Transfer procedure */


GPIO_ResetBits(GPIOA,GPIO_Pin_13);


while (1)
{

/* Wait for SPI1 data reception */
while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == 0);
/* Read SPI1 received data */
byte = SPI_I2S_ReceiveData(SPI1);
if(byte==5)GPIO_SetBits(GPIOA,GPIO_Pin_12);
else GPIO_ResetBits(GPIOA,GPIO_Pin_12);
GPIO_Write(GPIOC,byte);
}




}


/************************************************** *****************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();


/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);


/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();


if (HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enabl e);


/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);


/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);


/* PCLK2 = HCLK/2 */
RCC_PCLK2Config(RCC_HCLK_Div2);


/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);


/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);


/* Enable PLL */
RCC_PLLCmd(ENABLE);


/* Wait till PLL is ready */
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{}


/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


/* Wait till PLL is used as system clock source */
while (RCC_GetSYSCLKSource() != 0x08)
{}
}


/* Enable peripheral clocks --------------------------------------------------*/
/* GPIOA, GPIOB and SPI1 clock enable */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | ///RCC_APB2Periph_AFIO |
RCC_APB2Periph_SPI1, ENABLE);
}


/************************************************** *****************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;


/* Configure SPI1 pins: SCK, MISO and MOSI ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);


/* Configure LED pins ---------------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);




}


/************************************************** *****************************
* Function Name : NVIC_Configuration
* Description : Configures NVIC and Vector Table base location.
* Input : None
* Output : None
* Return : None
************************************************** *****************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}

mzarkoob
08-06-2015, 11:16
سلام
واقعا جواب نداد!
جالبه. بعضی از این مدهای ارتباطی بین دو میکرو اینطوری هست. مد I2S هم شبیه همین است که به این شکل کار نکرد.
قبلا یه زمان مختصری گذاشتم برای ارتباط spi بین دو میکرو با lpc1768 کار نکرد که البته پیگیریش نکردم.
طول سیم بین دو بردتان چقدره؟

mrm
08-06-2015, 16:16
تنظیمات gpio در هر دو مد اشتباه هست.
به RM0008
Reference manual
صفحه 162 مراجعه کنید.
دوم داخل کد اسلیو چرا تنظیمات spi کامل نیست؟

wantedboy
09-06-2015, 15:53
سلام
واقعا جواب نداد!
جالبه. بعضی از این مدهای ارتباطی بین دو میکرو اینطوری هست. مد I2S هم شبیه همین است که به این شکل کار نکرد.
قبلا یه زمان مختصری گذاشتم برای ارتباط spi بین دو میکرو با lpc1768 کار نکرد که البته پیگیریش نکردم.
طول سیم بین دو بردتان چقدره؟
سلام ممنون
من اومدم مثال داخل خود keil پروگرام کردم.جواب داد البته ارتباط دوتا spi داخل خود میکرو بود spi1 با spi2 . حالا spi2 انتقال دادم به یک stm32 دیگه اول جواب نداد بعد دوباره تست کردم جواب داد ولی خیلی نویز داره همه چیز روش تاثیر داره.طول سیم 10 سانتی متر فعلا.کلافم کرده سه روزه دارم روش کار می کنم راستی سرعت پایین ترین حالت گذاشتم.