|
本帖最后由 woshilee 于 2014-12-16 19:55 编辑 H P+ Q1 v. \* D% |+ } M0的SPI终于调通了,玩儿M3,M4多了现在玩M0就有了固定的思维模式,老路走不通的话就要找它们之间的不同之处了贴上程序吧2 n: V$ g# P& H* E# ~ #include "stm32f0xx.h" #include "stdio.h"& A( c5 c$ a$ W0 J uint8_t getbyte=0;% t; B& S; }6 N5 | /** * @brief Delay * @param None * @retval None# `7 p3 s4 E. R" k */) x/ O- c* Q' y void delay1ms(unsigned int ms) {' ^$ N0 [- Y# O8 J5 K" t unsigned int i,j;) D! M* A1 {8 c' r/ Y for(i=0; i<ms; i++). J0 r$ [ p; N for(j=0; j<100; j++); }6 \3 b+ _0 B+ n" S0 V% Q& q: F /** * @brief GPIO Config * @param None9 g. s2 @5 w# ` g; \5 O' f I( I * @retval None" e1 j; E9 O. h- X */9 n5 r7 g$ C R void GPIO_Configuration(void) {( B* u' C$ J" c GPIO_InitTypeDef GPIO_InitStructure; $ v5 Q4 W! `$ C1 q$ F2 N. ~ /* Configure PA5£¬PA7 in output nopull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;' |8 G- m% J* {8 q GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;0 R }* u( _: W# F GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;" A6 p* m, B" S3 m' l GPIO_Init(GPIOA, &GPIO_InitStructure);0 G" x: [, C) O 1 [5 D1 U: p( a; `$ d+ T' a 8 h$ Z' ]5 ]0 o2 m. q# W GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍ츴ÓÃÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;" m" i5 e0 ]! E& T ^, v8 s! y GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//Ðü¿Õ»òÉÏÀÊäÈë+ J0 Q& ~5 o1 u. S3 z# B! L' }6 h GPIO_Init(GPIOA, &GPIO_InitStructure); 5 [2 |3 N& [- T6 V7 A8 B GPIO_PinAFConfig(GPIOA,GPIO_PinSource4, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_0); GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_0);8 ~! I$ n: Y$ ~) W GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_0); } void RCC_Configuration(void) {- \/ A& ?% s9 ^2 ? U RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);# I# j& I9 s" i" ` RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); }; @8 T) T; k: \/ B) N void SPI_Configuration(void) { SPI_InitTypeDef SPI_InitStructure;1 v1 E' L: q# Q0 n SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; / f. w* S: g$ ` SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;( t% p1 A+ O5 S+ a) J' I SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPI1, &SPI_InitStructure); 4 @4 U" }5 o% R% U SPI_NSSInternalSoftwareConfig(SPI1, SPI_NSSInternalSoft_Set); $ C: O8 b/ |+ T- o5 g4 j SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF);1 J2 k( m: Z5 g' E/ `( J8 { SPI_Cmd(SPI1, ENABLE); /* SPI enable */' s, k( s# r1 c& [+ p W } uint8_t SPI1_ReadWriteByte(uint8_t TxData)* e Y9 ]1 a) v u4 J F/ @ { uint8_t RxData = 0;; L0 b% d7 |0 O" A% m% J) E5 C# z while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //µÈ´ý·¢ËÍ»º³åÇø¿Õ . l7 _* n! K. R! B SPI_SendData8(SPI1, TxData); //·¢Ò»¸ö×Ö½Ú2 u! u4 w: B+ w; I& ]+ G ; c$ H1 V: y- G, Z while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);& H2 ^ d- h. e: q2 L! j' @* ~' E RxData = SPI_ReceiveData8(SPI1);//SPI2 //·µ»Ø½ÓÊÕµ½µÄÊý¾Ý*/ return (uint8_t)RxData; 3 R7 ~; E1 H8 _% R } 2 w9 C: A: J; W d: t2 } 2 S- g0 D$ X4 a# ^2 g$ l) g 6 ^/ U& I% t' H% K/ B6 m int main(void) { RCC_Configuration();- G% L( u' V4 s/ F- I# l GPIO_Configuration(); SPI_Configuration(); // NVIC_Configuration(); 5 m( z2 o3 c! \8 M0 n while (1)7 @5 l* t4 J) f ^: Y8 O' ?3 r {5 Q; A8 w* ]. y- a getbyte=SPI1_ReadWriteByte(0x55);+ r. {) ]" m( _+ a, f& _ delay1ms(100); }" _; D/ E+ O( O$ g6 D } 红色的语句在M3中是没有的,在M0中这个函数的定义如下:% A3 N, ?6 N3 `7 s: {% J& I / I5 L: e2 [* \8 v2 d2 a7 e( g /** * @brief Configures the FIFO reception threshold for the selected SPI. * @param SPIx: where x can be 1 or 2 to select the SPI peripheral.1 A5 ~8 l/ f: L* v; O) ` * @param SPI_RxFIFOThreshold: specifies the FIFO reception threshold.0 \# o: m& K8 d. h0 q2 B * 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. / m4 M% d9 m% G2 {( Y& B. @! W2 E * @arg SPI_RxFIFOThreshold_QF: RXNE event is generated if the FIFO / Z2 v5 p2 t, _' p2 p0 y( w * level is greater or equal to 1/4. * @retval None0 ^, q* q" ^% M9 { */1 A5 ~0 ^3 Q& c void SPI_RxFIFOThresholdConfig(SPI_TypeDef* SPIx, uint16_t SPI_RxFIFOThreshold)8 Z; \% l6 R F3 I; e! D5 f { /* Check the parameters */* r: p p, t7 _: ], S9 J assert_param(IS_SPI_ALL_PERIPH(SPIx));7 S1 d1 m7 j- t/ Z5 q. ? assert_param(IS_SPI_RX_FIFO_THRESHOLD(SPI_RxFIFOThreshold));, u( e# R( ^# B8 x' ~* _. C /* Clear FRXTH bit */ SPIx->CR2 &= (uint16_t)~((uint16_t)SPI_CR2_FRXTH); /* Set new FRXTH bit value */ SPIx->CR2 |= SPI_RxFIFOThreshold;& T' @& L) V1 i) H }* r W: j7 a9 T. ?+ e' e 如果不加这句话的话,肯定会卡在判断RXNE位的地方,因为没有产生这个标志,如果用延时的代替的话是可以发送出去的,但是时序就会有问题,因为发送语句执行后不是立即就完成的。希望大家注意到这一点) l' B [6 G0 w7 d; Q 8 [: J; e6 r1 C0 E 所有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个字节 |
微信公众号
手机版
我这是应该做的,跟你比就小巫见大巫啦