Haberler:

Forum kuralları güncellendi LÜTFEN  okuyunuz:  https://bit.ly/2IjR3ME

Ana Menü

STM32F0 SPI kurma

Başlatan baran123, 03 Mayıs 2015, 17:25:04

baran123

Arkadaşlar OLED projesini STM'e çevirmeye çalışıyorum fakat bir türlü çalıştıramadım ekranı.STM32L1 de çalışan kodu 32F0 a geçiremedim.Sanırım SPI kurmada bir hata yapıyorum.

GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef  SPI_InitStructure;

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC,ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);

    GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType  = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd   = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA,&GPIO_InitStructure);

    GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);

    SPI_InitStructure.SPI_Direction         = SPI_Direction_Tx;
    SPI_InitStructure.SPI_Mode              = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize          = SPI_DataSize_8b;
    SPI_InitStructure.SPI_CPOL              = SPI_CPOL_High;
    SPI_InitStructure.SPI_CPHA              = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS               = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
    SPI_InitStructure.SPI_FirstBit          = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial     = 7;
    SPI_Init(SPI1,&SPI_InitStructure);
    SPI_Cmd(SPI1,ENABLE);

    GPIO_InitStructure.GPIO_Mode   = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_OType  = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_Pin    = GPIO_Pin_0 | GPIO_Pin_1;
    GPIO_InitStructure.GPIO_PuPd   = GPIO_PuPd_NOPULL;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOC,&GPIO_InitStructure);


Bu şekilde ayarladım.SPI1 de pinler 5=sck, 6 mıso, = mosı şeklinde bağladım.PC0 ada D/C pinini bağladım.Ben nerede hata yapıyor olabilirim ?

Son olarak kütüphanede şöyle bir fonksiyon vardı.Bunda SPI2 ayarlıydı ben SP1 yaptım.

int WriteSPI1(uint8_t Data)
{
    SPI1->DR = Data;

    while((SPI1->SR & 0x02) == 0);

    while((SPI1->SR & 0x01) == 0);

    return 0;
}

FxDev

Benim Init ayarım aşağıdaki gibiyken oldukça iyi performans almıştım. STM32F0 serisi kullanıyorum.

void General_SPI_Init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	SPI_InitTypeDef  SPI_InitStructure;

  /* Enable GPIO clocks */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  /* Enable the SPI clock */
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

  /* SPI GPIO Configuration --------------------------------------------------*/

  /* Configure I/O for RES, D/C and CS */
  GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_0 | GPIO_Pin_4 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd 		= GPIO_PuPd_UP;
  GPIO_Init(GPIOC, &GPIO_InitStructure);

  /* SPI SCK pin configuration */
  GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_13;
  GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd 		= GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI  MOSI pin configuration */
  GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_15;
  GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd 		= GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* SPI  MISO pin configuration */
  GPIO_InitStructure.GPIO_Pin 		= GPIO_Pin_14;
  GPIO_InitStructure.GPIO_Mode 		= GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd 		= GPIO_PuPd_UP;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  /* Connect SPI pins to AF0 */
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0);
  GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0);
	
	/* SPI configuration -------------------------------------------------------*/
  SPI_I2S_DeInit(SPI2);
  SPI_InitStructure.SPI_Direction 				= SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_DataSize 					= SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL 							= SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA 							= SPI_CPHA_1Edge;
  SPI_InitStructure.SPI_NSS 							= SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
  SPI_InitStructure.SPI_FirstBit 					= SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial 		= 7;
  SPI_InitStructure.SPI_Mode 							= SPI_Mode_Master;
  
	SPI_Init(SPI2, &SPI_InitStructure);
  SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF);
	SPI_CalculateCRC(SPI2, DISABLE);  
	SPI_Cmd(SPI2, ENABLE);
	
	/* drain SPI TX buffer,just in case*/
  while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
  SPI_ReceiveData8(SPI2);
}


void SSD1306_Command(char Command)
{	
	SSD1306_DC_OFF;				// DC is low for command data

	SSD1306_CS_OFF;				// Select the chip

	SPI_SendData8(SPI2, Command);
	while (SPI2->SR & SPI_SR_BSY);
	
	SSD1306_CS_ON;				// Unselect the chip
}

void SSD1306_Data(char Data)
{	
	SSD1306_DC_ON;				// DC is on for data
	
	SSD1306_CS_OFF;				// Select the chip

	SPI_SendData8(SPI2, Data);
	while (SPI2->SR & SPI_SR_BSY);
	
	SSD1306_CS_ON;				// Unselect the chip
}
Forumda bazı bağnaz kişiler tarafından engellenip, atıldım. Tüm bu bağnaz kişilere rağmen Atatürkçülüğü sonuna kadar savunacağım; onlar da bağnazlıklarında boğulacaklar. Haberleşme için: info[at]firatdeveci.com / ©firatdeveci.com - ße Different Everytime!

baran123

Sadece command ve data fonksiyonlarını düzenledim oldu.Galiba ben 32LF1 den 32F0 a geçirirken SPI gönderme kısmında hata yapmışım.Çok teşekkür ederim. :)