本帖最后由 woshilee 于 2014-12-16 19:55 编辑 . _; O& K2 b/ b0 Z, R8 { 5 P0 ^ q( L5 j# s5 p M0的SPI终于调通了,玩儿M3,M4多了现在玩M0就有了固定的思维模式,老路走不通的话就要找它们之间的不同之处了贴上程序吧4 [) U& Y8 `0 [3 _1 p; U #include "stm32f0xx.h"- n7 f9 f7 u% q3 f* \% ` #include "stdio.h"$ B# v# w% W: p6 V$ u2 F) O# R uint8_t getbyte=0; /**9 `( {1 p4 h- K7 ? * @brief Delay3 G+ ]5 W$ t: H* y. r * @param None * @retval None */ void delay1ms(unsigned int ms) { M3 O0 }; h. d3 o unsigned int i,j; for(i=0; i<ms; i++) for(j=0; j<100; j++); } /**, r; S" B+ }9 \+ b* p- |0 y4 X * @brief GPIO Config0 A/ v& T- H6 {8 `# J+ `1 ^% s * @param None2 i6 N- ^& R& v- [1 Z$ _ * @retval None2 Z6 N, w- u+ ^2 P. x$ a */ void GPIO_Configuration(void)" T9 G- |2 H9 b( V {: p& T7 f% _+ n4 x/ I% r/ h GPIO_InitTypeDef GPIO_InitStructure; . o! N, [0 _0 V; @9 }" l0 y /* Configure PA5£¬PA7 in output nopull mode */) J2 c6 V" Q* D% i1 \% k GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7;/ a! ?5 m ^: e. e5 ^+ } GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;6 `5 F3 W) J9 E k" Y% Y GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); 9 A! g% k+ P3 X- g! }' }) l+ j& Z # O4 |5 H. R8 [( b _% J, A GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;( @6 `# y' F* _5 o // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;. V5 N( y- K5 x3 A 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;//Ðü¿Õ»òÉÏÀÊäÈë GPIO_Init(GPIOA, &GPIO_InitStructure); 8 e/ ~, T% O6 j* G H5 Y GPIO_PinAFConfig(GPIOA,GPIO_PinSource4, GPIO_AF_0);) {3 k. m) q0 e: J3 [: E7 v GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_0);- B9 _6 P( P" L Z, M8 J GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_0); }- C) q3 b8 D# A( v% m3 i# G void RCC_Configuration(void) {& x; n8 ^$ F C" z RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); g2 `7 r9 v9 ?- x# H } void SPI_Configuration(void) {$ @5 P( Z, J: n# U& [ SPI_InitTypeDef SPI_InitStructure;# V' q2 p9 X! J1 e SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; 1 j0 _6 U/ [: y$ f+ X, E SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; 1 z& H: m8 j5 M3 X, m/ h5 D SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;1 E6 k! q! L$ |% }( Z$ `$ q, T SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;" g" M$ S3 Q( x1 ]" |; ^) b7 B SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPI1, &SPI_InitStructure); ! \# H9 d. M) Z, A. S4 S* a SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set); SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF); SPI_Cmd(SPI1, ENABLE); /* SPI enable */0 h# Y5 w7 c0 T+ J- \7 d+ I + p- T5 F. \3 x- K* l) ]$ n- D }- D( o7 h+ v0 I2 P2 } uint8_t SPI1_ReadWriteByte(uint8_t TxData) { uint8_t RxData = 0;+ a% O2 z. N# z, B9 A% t3 J while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //µÈ´ý·¢ËÍ»º³åÇø¿Õ ) O6 h/ Y+ K5 X2 K' B5 x; e SPI_SendData8(SPI1, TxData); //·¢Ò»¸ö×Ö½Ú( @9 u' j$ m% Y! |* h ( x- O) L! Q2 E% I9 ~# X* x- e9 C1 A/ Z while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);! G2 k' Z. ]+ K) J. T3 ] RxData = SPI_ReceiveData8(SPI1);//SPI2 //·µ»Ø½ÓÊÕµ½µÄÊý¾Ý*/: i8 H3 j8 D3 A, H: n0 V0 B return (uint8_t)RxData; } ; u; w6 A8 L: s4 K9 o . Z$ v) @; o. e6 Z% r& g; J+ F: I 7 b' I$ h! h i% z" m0 M " g6 K( k7 a! T0 y2 U0 Y$ w8 r% Y int main(void) { * Z, s$ E8 ?7 h- z4 n- T RCC_Configuration(); GPIO_Configuration();0 G3 ?6 j i% T J9 |$ { SPI_Configuration(); // NVIC_Configuration(); while (1) {7 } w; E6 T% q, W$ b! i3 I 2 k! v& n+ ^% z1 \- e9 f getbyte=SPI1_ReadWriteByte(0x55);! m- `# V- j- E4 l; S delay1ms(100); } }! W T# ]& e4 @5 F3 T 红色的语句在M3中是没有的,在M0中这个函数的定义如下: /** * @brief Configures the FIFO reception threshold for the selected SPI.4 _0 ?+ M0 B3 x- q+ b* m * @param SPIx: where x can be 1 or 2 to select the SPI peripheral. * @param SPI_RxFIFOThreshold: specifies the FIFO reception threshold.. e5 r6 a W8 E * This parameter can be one of the following values: * @arg SPI_RxFIFOThreshold_HF: RXNE event is generated if the FIFO % @+ Y. H6 y; N1 u* N/ p) {) g- j7 Q* g * level is greater or equal to 1/2. ( @$ u) {$ j$ |+ Q+ j u& ]( c; U * @arg SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO ) ^8 \3 y: I9 c- N" I9 _, K: p. W * level is greater or equal to 1/4. # E! L1 `) M" [ * @retval None */: N1 o% `4 F) ?& z6 o4 j' I8 { void SPI_RxFIFOThresholdConfig(SPI_TypeDef* SPIx, uint16_t SPI_RxFIFOThreshold); ?3 o0 ]3 C. L! C, y a {" I* a' k2 i/ H1 i$ D; j /* Check the parameters */( E( I" \5 }1 P: m7 E assert_param(IS_SPI_ALL_PERIPH(SPIx)); assert_param(IS_SPI_RX_FIFO_THRESHOLD(SPI_RxFIFOThreshold)); 2 v% K/ \8 i: s) h) X /* Clear FRXTH bit */ SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRXTH);% a& r) L( x, L( R- o3 {- N /* Set new FRXTH bit value */ SPIx->CR2 |= SPI_RxFIFOThreshold; }- s8 a( s) b( P6 |* ]& Z9 `0 f4 U 如果不加这句话的话,肯定会卡在判断RXNE位的地方,因为没有产生这个标志,如果用延时的代替的话是可以发送出去的,但是时序就会有问题,因为发送语句执行后不是立即就完成的。希望大家注意到这一点 所有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个字节% K- y. u, g0 p) [ |
我这是应该做的,跟你比就小巫见大巫啦