|
这次调试SPI, 在网上查了不少资料, 特别是论坛上的, 现在硬件3线SPI已调通, 跟大家分享下.( F4 h( L) _5 @8 `0 v 由于外设是3线spi,MCU不得不也跟着使用3线SPI. MCU: STM8L051F3$ b3 s0 [( E1 o4 _3 C 外设: A7139/ S& z' c0 x2 j4 f: A 操作寄存器方式: 官方库函数 SPI: 3线 # d% e& s. @- {2 k7 p 主要步骤: a2 n S; o, P- X; T 1 初始化
/** * @brief Initializes the SPI and CS pins. * @param None# p D. R; Z# R: R& E" O * @retval None */- J3 _/ J1 w0 y6 K' d( q6 Z$ F void SPI_LowLevel_Init(void)) W& e8 @ o+ v1 r: a d- p! U { n6 p& C$ ?# D3 E$ y /* Enable SPI clock */ CLK_PeripheralClockConfig(SPI_CLK, ENABLE); / y# W4 s" ^- ^1 T4 W" j+ ] /* Set the MOSI,MISO and SCK at high level */1 k0 E4 D2 Z8 E0 k% M GPIO_ExternalPullUpConfig(SPI_SCK_GPIO_PORT, SPI_MISO_PIN | SPI_MOSI_PIN | \ SPI_SCK_PIN, ENABLE);2 e/ X, @; n2 Z9 Q: |' C% J1 u 9 W" s8 V1 S/ U- ^" ~9 J* ] /* SPI Config */ SPI_Init(SPI, SPI_FirstBit_MSB, SPI_BaudRatePrescaler_128, SPI_Mode_Master, SPI_CPOL_Low, SPI_CPHA_1Edge, SPI_Direction_1Line_Tx /*SPI_Direction_2Lines_FullDuplex*/, SPI_NSS_Soft, 0x07); / V; b4 n0 Q5 m: n! j- x /* SPI enable */ SPI_Cmd(SPI, ENABLE); % q" O& S! v$ } /* Set MSD ChipSelect pin in Output push-pull high level */2 S% r0 s7 C: K6 R! N2 T GPIO_Init(CS_GPIO_PORT, CS_PIN, GPIO_Mode_Out_PP_High_Slow); }/ m7 [- X5 F" Q7 d) o 2 读写函数# k+ I0 q5 E" @0 \. o* u
/**: J; C" j* n3 n2 V$ H * @brief Write a byte . * @param Data: byte to send. * @retval None */ uint8_t SPIx_WriteByte(uint8_t Data) {5 U0 d! B& b z$ O0 T0 O) G; [+ C //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); //设置单线为发送: u* n4 ?3 ^9 G; N3 i( ` /*!< Wait until SPI is not busy */ 4 b/ _9 I9 P5 s, T# l# h while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET) {}+ G' {% X$ d0 N: t 3 ^$ x- P# P, g1 r. w /*!< Send the byte */ SPI_SendData(SPI, Data); /*!< Wait until SPI is not busy */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) != RESET)& S- F/ |6 [8 k# _: g {} //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收4 }+ |% x! W K+ Q return 1; }" B& J. [$ v. C9 G' E! G /**. S E$ c% u" k2 X4 D * @brief Read a byte .# I8 v5 T1 @0 C+ t$ b$ T * @param None6 o, T# V! O3 {" H * @retval The received byte.2 ~ J V6 U& A! c V j: ~ */ uint8_t SPIx_ReadByte(void)! [- Y9 h& S* M* U# A { uint8_t Data = 0; r2 b F# W* `* `5 T: o2 z5 G //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收 //SPI_Cmd(SPI, ENABLE);9 U% u3 C4 s( G( _' o9 Z /* Wait untils SPI is working */- _' g. Y0 {% c1 M( Q/ z while (SPI_GetFlagStatus(SPI, SPI_FLAG_BSY) == SET); " s8 i3 B+ r# W /*!< Wait until a data is received */ while (SPI_GetFlagStatus(SPI, SPI_FLAG_RXNE) == RESET) {} /*!< Get the received data */" G. ]: w& a+ K Data = SPI_ReceiveData(SPI);% ]) |2 \! \' T* N //SPI_Cmd(SPI, DISABLE);; E# {; g: b. G: _ //SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx);% [# s8 B( \2 u# A3 w0 n+ C0 a5 g- J) p /*!< Return the shifted data */2 C: _4 J+ z. h# D4 ? return Data;8 z ^7 P) a5 H* q8 E% a } void A7139_SetCID(Uint32 id). C, P1 y7 k5 F8 H { SPI_CS_LOW(); //SCS=0;+ m; @2 v$ G: Z0 H7 j SPIx_WriteByte(CMD_CID_W);/ D/ I% R; ?2 ~6 ?4 S2 n SPIx_WriteByte((Uint8)(id>>24));' I- I" L0 l: p* s SPIx_WriteByte((Uint8)(id>>16));% B# ~5 N1 N' W% I+ I/ L) u SPIx_WriteByte((Uint8)(id>>8));) ~4 s4 X" N3 \6 b SPIx_WriteByte((Uint8)id); SPI_CS_HIGH(); //SCS=1; }' W& u7 d5 t! Q7 x- w% u Uint32 A7139_ReadCID(void): p5 m. P2 h$ e: h. J, ? { Uint32 data = 0; Uint8 i = 0;4 x( e( D: U [9 B+ u- D Uint32 byte[4] = {0};! w! P% P( t0 i5 N* L SPI_CS_LOW();& H& d1 x d' U SPIx_WriteByte(CMD_CID_R);/ H4 G! O) R5 l7 g4 l SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Rx); //设置单线为接收 // for (i = 0; i < 4; i++) // {, A4 o1 p9 O" l // data = data << 8;2 i4 U% A( s" v- { // data |= SPIx_ReadByte(); // } byte[0] = SPIx_ReadByte();6 h: L0 e+ v4 a; a. y byte[1] = SPIx_ReadByte(); byte[2] = SPIx_ReadByte(); byte[3] = SPIx_ReadByte(); {3 ^) J6 a. S0 C f9 A) c' _ data = (byte[0] << 24) | (byte[1] << 16) | (byte[2] << 8) | byte[3]; SPI_BiDirectionalLineConfig(SPI, SPI_Direction_Tx); SPI_CS_HIGH(); 0 c0 J, Y3 t2 f4 J8 g$ v3 p return data; } : X; I% E3 \% L( z |
STM8自学笔记(推荐STM8很好的入门电子书)
无刷直流电机控制应用+基于STM8S系列单片机---电子书
【精华资料】STM8Sxx固件库FOR IAR
基于STM8的DALI (数字可寻址调光协议)
分享STM8 风驰光盘的资料,是完整的(包括原理图+例程+PDF注释)
《无刷直流电机控制应用 基于STM8S系列单片机》
STM8S库函数中文参考 小软件
【资料分享】STM8L的智能手持血糖监测设备的源码
基于STM8S207工程模板
【培训资料】STM8系列PPT培训资料
微信公众号
手机版
https://www.stmcu.org.cn/module/forum/thread-612887-1-1.html