提交 | 用户 | age
|
483170
|
1 |
#include "stm32f0xx.h" |
Q |
2 |
|
|
3 |
void SPI2_Int() |
|
4 |
{ |
|
5 |
/* |
|
6 |
SPI_InitTypeDef SPI_InitStruct; |
|
7 |
|
|
8 |
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;//全双工模式 |
|
9 |
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; |
|
10 |
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; |
|
11 |
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;//第一个边沿 |
|
12 |
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;//上升沿捕获 |
|
13 |
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; |
|
14 |
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; // 6MHz |
|
15 |
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; |
|
16 |
SPI_InitStruct.SPI_CRCPolynomial = 7; |
|
17 |
SPI_Init(SPI2,&SPI_InitStruct); |
|
18 |
|
|
19 |
SPI_RxFIFOThresholdConfig(SPI2, SPI_RxFIFOThreshold_QF); |
|
20 |
SPI_Cmd(SPI2, ENABLE ); |
|
21 |
*/ |
|
22 |
} |
|
23 |
|
|
24 |
/*! |
|
25 |
* @brief Sends txBuffer and receives rxBuffer |
|
26 |
* |
|
27 |
* @param [IN] txBuffer Byte to be sent |
|
28 |
* @param [OUT] rxBuffer Byte to be sent |
|
29 |
* @param [IN] size Byte to be sent |
|
30 |
*/ |
|
31 |
#define TIMEOUT 50 |
|
32 |
uint8_t SpiInOut( uint8_t txBuffer) |
|
33 |
{ |
|
34 |
/* |
|
35 |
while( SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);//当发送buffer为空时(说明上一次数据已复制到移位寄存器中)退出,这时可以往buffer里面写数据 |
|
36 |
SPI_SendData8(SPI2, txBuffer); |
|
37 |
|
|
38 |
while( SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);//当接收buffer为非空时退出 |
|
39 |
return SPI_ReceiveData8(SPI2); |
|
40 |
|
|
41 |
*/ |
|
42 |
|
|
43 |
int timeout_cnt= 0; |
|
44 |
uint8_t value; |
|
45 |
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; } |
|
46 |
LL_SPI_TransmitData8(SPI1,txBuffer); |
|
47 |
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) {timeout_cnt++; if (timeout_cnt>TIMEOUT) break; } |
|
48 |
timeout_cnt= 0; while (LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET) { timeout_cnt++; if (timeout_cnt>TIMEOUT) break;} |
|
49 |
value = LL_SPI_ReceiveData8( SPI1); |
|
50 |
|
|
51 |
return value; |
|
52 |
} |
|
53 |
|
|
54 |
void SpiIn( uint8_t *txBuffer, uint16_t size ) |
|
55 |
{ |
|
56 |
|
|
57 |
uint16_t i; |
|
58 |
|
|
59 |
for(i=0;i<size;i++) |
|
60 |
{ |
|
61 |
while( LL_SPI_IsActiveFlag_TXE(SPI1) == RESET);//当发送buffer为空时(说明上一次数据已复制到移位寄存器中)退出,这时可以往buffer里面写数据 |
|
62 |
LL_SPI_TransmitData8(SPI1,txBuffer[i]); |
|
63 |
while( LL_SPI_IsActiveFlag_RXNE(SPI1) == RESET);//当接收buffer为非空时退出 |
|
64 |
LL_SPI_ReceiveData8( SPI1); |
|
65 |
} |
|
66 |
|
|
67 |
} |
|
68 |
|
|
69 |
|