
本帖最后由 woshilee 于 2014-12-16 19:55 编辑 ; A7 \9 Q0 V% k4 T+ y' b- u M0的SPI终于调通了,玩儿M3,M4多了现在玩M0就有了固定的思维模式,老路走不通的话就要找它们之间的不同之处了贴上程序吧 #include "stm32f0xx.h" #include "stdio.h" uint8_t getbyte=0;3 d M O, l6 K7 A$ y/ p /** * @brief Delay0 @% Y- N9 s2 t7 g8 v$ C+ q * @param None * @retval None7 s( Y% E1 t/ \4 m; U */ void delay1ms(unsigned int ms) {. O, f) h4 e$ M4 d/ s' B% B- U unsigned int i,j;: ?) [" d( D% ~: @5 `+ t for(i=0; i<ms; i++)+ x G0 n3 ~4 o3 h$ r0 B for(j=0; j<100; j++); }3 `$ P8 T4 R" f7 H+ i . Y4 N0 R: Z {& M$ |: P q /**$ F+ x6 J# [4 {6 a+ ?* u * @brief GPIO Config * @param None * @retval None! ~0 y- T# f( v4 j+ G# A" a */& }' f$ K# n! i4 u: }6 k void GPIO_Configuration(void)- Y3 X2 H# i. p. c* |1 e { GPIO_InitTypeDef GPIO_InitStructure; 3 G# x; P* M# T& s, e$ b) |9 _ /* Configure PA5£¬PA7 in output nopull mode */9 m2 U1 ?! d1 y3 g GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;! [ W. s2 s1 \$ L- R; u1 L' Y GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); " @. {& G" w# H8 t- c4 m GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;* Z+ Z$ z5 Y0 i! ~1 r // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;( O; |9 h, X" \5 U+ K# _3 j/ b6 s GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍ츴ÓÃÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Ðü¿Õ»òÉÏÀÊäÈë0 B4 ~7 b3 s# x9 }+ X$ J9 n1 w GPIO_Init(GPIOA, &GPIO_InitStructure); 8 P5 ^5 u d7 h. b; r2 @" f GPIO_PinAFConfig(GPIOA,GPIO_PinSource4, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_0);9 ~' F* e/ J& }& m- e GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_0);1 C2 S$ w+ }. r; s5 Q } void RCC_Configuration(void) { RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);, U8 x' c+ Q5 X' U4 V* A+ u. ] }. J8 ?2 f* [+ r |1 r' g void SPI_Configuration(void) { SPI_InitTypeDef SPI_InitStructure;& ?, \. v' t+ \1 j& z SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; 5 x4 h4 S7 i5 l; n0 O SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;/ C6 F' g7 l3 ~( {7 [* v0 ]$ X2 T) W SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;- R, h, \6 Z( t# D. t# [) r/ Y SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; " ?- `2 S% X, c. N5 c; O SPI_InitStructure.SPI_Mode = SPI_Mode_Master;8 h! k1 c! e( j9 Q1 E, [# R( h SPI_Init(SPI1, &SPI_InitStructure); SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set); ' R6 w3 s8 g- s& p* F ] SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);& l- E6 D' _ D$ H6 t $ [4 e7 f/ s8 P j3 S# }: U SPI_Cmd(SPI1, ENABLE); /* SPI enable */ }. b7 i) U, k% r3 t+ j t$ ` : U0 I, ~8 m# p' i1 n7 T- _ # Y( M) t3 t/ {4 a1 V! g0 \+ A uint8_t SPI1_ReadWriteByte(uint8_t TxData)$ H3 g4 K6 ]3 [9 p0 I5 {$ X { # Q) g$ U, F' |0 G7 R uint8_t RxData = 0;: N- }2 z" ^. v2 ~; D# w while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //µÈ´ý·¢ËÍ»º³åÇø¿Õ SPI_SendData8(SPI1, TxData); //·¢Ò»¸ö×Ö½Ú# [2 Q m$ x8 z, |% h# U6 p 7 |8 V! W6 i" v+ U/ c while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); RxData = SPI_ReceiveData8(SPI1);//SPI2 //·µ»Ø½ÓÊÕµ½µÄÊý¾Ý*/7 o( U$ O+ F1 ]5 d: U2 G" D5 f return (uint8_t)RxData; } 2 r+ v5 h/ S' w' O+ f | 3 a3 |; y5 k* x. w3 E / G) j3 }7 T0 g' ]5 t! |! j/ v int main(void)/ i. z% M$ f4 v2 v {9 F/ c* W0 G; g, P3 F4 H$ z RCC_Configuration();0 A* h9 P/ B% J' S* K% b GPIO_Configuration();! a! G; u3 E. O; U SPI_Configuration();: F9 L3 h# U7 Y7 `. J; H7 @# b5 p; F // NVIC_Configuration();4 T5 h" t E7 P' R2 E! M# S , P8 Q" ?# w0 V$ a: G/ D2 C1 y. x while (1)8 w6 `/ J. E" z {# \3 L- P9 T! I* h$ _$ k- L getbyte=SPI1_ReadWriteByte(0x55);* B3 I4 k4 C( r delay1ms(100);/ x8 i0 _# n, X+ ^/ U+ Y K }. j( O) o4 z' k$ y/ h }! z$ ^% F" F) K2 N0 H" }( H 红色的语句在M3中是没有的,在M0中这个函数的定义如下:/ s- E( x1 X$ i, t- G& O / {* d% P C4 V /** * @brief Configures the FIFO reception threshold for the selected SPI. * @param SPIx: where x can be 1 or 2 to select the SPI peripheral. * @param SPI_RxFIFOThreshold: specifies the FIFO reception threshold. * This parameter can be one of the following values: * @arg SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO * level is greater or equal to 1/2. * @arg SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO ! F# i& Q* _$ _* V6 p * level is greater or equal to 1/4. - I6 V' I1 \& M0 @ * @retval None */ void SPI_RxFIFOThresholdConfig(SPI_TypeDef* SPIx, uint16_t SPI_RxFIFOThreshold) {1 F/ Y* U! B6 W& q /* Check the parameters */( ?% B2 w; p* U4 O1 @ assert_param(IS_SPI_ALL_PERIPH(SPIx));( M- U9 ?9 k# |! w( M/ J q assert_param(IS_SPI_RX_FIFO_THRESHOLD(SPI_RxFIFOThreshold)); + ?1 q4 A7 v+ ], q: x /* Clear FRXTH bit */ SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRXTH); /* Set new FRXTH bit value */0 j6 o* b* O& o; t9 I5 f; k SPIx->CR2 |= SPI_RxFIFOThreshold;" v+ C) `5 n. |6 \% N } E- U! d/ d1 [ 如果不加这句话的话,肯定会卡在判断RXNE位的地方,因为没有产生这个标志,如果用延时的代替的话是可以发送出去的,但是时序就会有问题,因为发送语句执行后不是立即就完成的。希望大家注意到这一点& y6 A. C6 V, Y! m" E ] / C3 L; {0 w9 z6 U& g% P 所有SPI 数据换都通过32 位的嵌入式FIFO,设置FRXTH 位。 RXFIFO 的阈值必须和对SPIx_DR 寄存器的读访问的字长对齐比如发送的是8位的数据,就要设置SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO *level is greater or equal to 1/4. 就是32/4=8--》1个字节 如果发送的是16位数据就要设置SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO *level is greater or equal to 1/2.就是 32/2=16--》2个字节+ U- o* W# P! u% k5 K6 f2 \ |
我这是应该做的,跟你比就小巫见大巫啦