Advertisement
rezasurmar

Untitled

May 7th, 2016
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1.         void SPI2_CS5532_Config(void){
  2.         GPIO_InitTypeDef    GPIO_InitStruct;
  3.         SPI_InitTypeDef     SPI_InitStrct;
  4.        
  5.         /* Chip Select CS5532*/
  6.         /* Enable GPIOs clock */
  7.         RCC_AHBPeriphClockCmd(CS5532_CS_GPIO_CLK, ENABLE);
  8.  
  9.         /* Configure CS in Output Push-Pull mode */
  10.         GPIO_InitStruct.GPIO_Pin = CS5532_CS_PIN;
  11.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
  12.         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  13.         GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  14.         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_40MHz;
  15.         GPIO_Init(CS5532_CS_GPIO_PORT, &GPIO_InitStruct);
  16.  
  17.         /* Set chip select pin high */
  18.         CS5532_CS_HIGH;
  19.        
  20.         /* Configure SPI for CS5532 */
  21.         /* Enable GPIOs clock */
  22.         RCC_AHBPeriphClockCmd(SPI_MOSI_GPIO_CLK | SPI_MISO_GPIO_CLK | SPI_SCK_GPIO_CLK, ENABLE);
  23.  
  24.         /* Enable SPI clock */
  25.         RCC_APB1PeriphClockCmd(CS5532_SPI_CLK, ENABLE);
  26.  
  27.         /* Configure SPI SCK pin */
  28.         GPIO_InitStruct.GPIO_Pin = SPI_SCK_PIN;
  29.         GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  30.         GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz;
  31.         GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  32.         GPIO_InitStruct.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  33.         GPIO_Init(SPI_SCK_GPIO_PORT, &GPIO_InitStruct);
  34.        
  35.         /* Configure SPI MISO pin */
  36.         GPIO_InitStruct.GPIO_Pin = SPI_MISO_PIN;
  37.         GPIO_Init(SPI_MISO_GPIO_PORT, &GPIO_InitStruct);
  38.  
  39.         /* Configure SPI MOSI pin */
  40.         GPIO_InitStruct.GPIO_Pin = SPI_MOSI_PIN;
  41.         GPIO_Init(SPI_MOSI_GPIO_PORT, &GPIO_InitStruct);
  42.  
  43.         /* Connect SCK, MISO and MOSI pins to SPI alternate */
  44.         GPIO_PinAFConfig(SPI_SCK_GPIO_PORT, SPI_SCK_SOURCE, SPI_SCK_AF);
  45.         GPIO_PinAFConfig(SPI_MISO_GPIO_PORT, SPI_MISO_SOURCE, SPI_MISO_AF);
  46.         GPIO_PinAFConfig(SPI_MOSI_GPIO_PORT, SPI_MOSI_SOURCE, SPI_MOSI_AF);
  47.  
  48.         /* Configure SPI for CS5532*/
  49.         SPI_InitStrct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // transmisja z wykorzystaniem jednej linii, transmisja jednokierunkowa
  50.         SPI_InitStrct.SPI_Mode = SPI_Mode_Master;                           // Tryb Pracy SPI
  51.         SPI_InitStrct.SPI_DataSize = SPI_DataSize_8b;                   // 8-bitowa ramka danych
  52.         SPI_InitStrct.SPI_CPOL = SPI_CPOL_Low;                              // stan sygnalu taktujacego przy braku transmisji - niski
  53.         SPI_InitStrct.SPI_CPHA = SPI_CPHA_1Edge;                            // aktywne zbocze sygnalu taktujacego - 1-sze zbocze
  54.         SPI_InitStrct.SPI_NSS = SPI_NSS_Soft;                                   // softwerowa obsluga linii NSS (CS)
  55.         SPI_InitStrct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // 32MHz/16 = 2MHz
  56.         SPI_InitStrct.SPI_FirstBit = SPI_FirstBit_MSB;              // pierwszy bit w danych najmniej znaczacy
  57.         SPI_Init(SPI2, &SPI_InitStrct);                                             // inicjalizacja SPI                           
  58.        
  59.         // Wlacz SPI2
  60.         SPI_Cmd(SPI2, ENABLE); 
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement