1 | int main(void) {
|
2 |
|
3 | GPIO_InitTypeDef GPIO_InitStruct;
|
4 | SPI_InitTypeDef SPI_InitStructure;
|
5 |
|
6 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
|
7 |
|
8 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
|
9 |
|
10 | /** SPI1 GPIO Configuration
|
11 | PA5 ------> SPI1_SCK
|
12 | PA7 ------> SPI1_MOSI
|
13 |
|
14 | PC0 ------> SPI1_CS
|
15 | */
|
16 |
|
17 | /*Enable or disable the AHB peripheral clock */
|
18 | RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
|
19 |
|
20 | /*Configure GPIO pin */
|
21 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
|
22 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
|
23 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
24 | GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
|
25 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
26 | GPIO_Init(GPIOA, &GPIO_InitStruct);
|
27 |
|
28 | /*Configure GPIO pin alternate function */
|
29 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_5 );
|
30 |
|
31 | /*Configure GPIO pin alternate function */
|
32 | GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_5 );
|
33 |
|
34 | /* Configure SPI1 pins: CS PC0 ----------------------------*/
|
35 | GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
|
36 | GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
|
37 | GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
|
38 | GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
|
39 | GPIO_Init(GPIOC, &GPIO_InitStruct);
|
40 |
|
41 | /*SPI Config*/
|
42 |
|
43 | SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
|
44 | SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
|
45 | SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
|
46 | SPI_InitStructure.SPI_CRCPolynomial = 0;
|
47 | SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
|
48 | SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
|
49 | SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
|
50 | SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
|
51 | SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
|
52 | SPI_Init(SPI1, &SPI_InitStructure);
|
53 |
|
54 | SPI_Cmd(SPI1, ENABLE);
|
55 |
|
56 | //int count = 0;
|
57 |
|
58 | while (1) {
|
59 |
|
60 | GPIO_WriteBit(GPIOC, GPIO_Pin_0, 0); //CS auf LOW
|
61 | while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE ) == RESET)
|
62 | ;
|
63 | SPI_SendData8(SPI1, 0x01);
|
64 | while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE ) == RESET)
|
65 | ;
|
66 | GPIO_WriteBit(GPIOC, GPIO_Pin_0, 1); //CS auf HIGH
|
67 | //for(count = 0; count <= 999; count++){}
|
68 |
|
69 | }
|
70 |
|
71 | /* Program will never run to this line */
|
72 | return 0;
|
73 | }
|