如图,为啥我SPI的 3个字节写完了 SCK信号不停止呢
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB , ENABLE);
-
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_0);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource14, GPIO_AF_0);
- GPIO_PinAFConfig(GPIOB, GPIO_PinSource15, GPIO_AF_0);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;;
- GPIO_Init(GPIOB,&GPIO_InitStructure);
-
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI2, &SPI_InitStructure);
-
- SPI_RxFIFOThresholdConfig(SPI2,SPI_RxFIFOThreshold_QF);
- SPI_Cmd(SPI2, ENABLE);
复制代码
- uint8_t spi2_read_write(uint16_t a)
- {
- while(RESET==(SPI2->SR & SPI_I2S_FLAG_TXE));
- SPI2->DR = a;
- while(RESET==(SPI2->SR & SPI_I2S_FLAG_RXNE));
- a = SPI2->DR;
- while(RESET==(SPI2->SR & SPI_I2S_FLAG_TXE));
- while(RESET==(SPI2->SR & SPI_I2S_FLAG_RXNE));
-
- SPI_I2S_ClearFlag(SPI2,SPI_I2S_FLAG_OVR);
- return a;
- }
复制代码- void LensWriteReg(uint8_t addr,uint16_t data)
- {
- uint8_t buf[3];
- buf[0]=addr&0x3f;
- buf[1]=data;
- buf[2]=data>>8;
- CS_1;
- spi2_read_write(buf[0]);
- spi2_read_write(buf[1]);
- spi2_read_write(buf[2]);
- CS_0;
- }
复制代码
|
即可
评分
查看全部评分
神奇的操作
将DR地址转换为 uint8_t* 和 uint16_t* 是两种不同的情况,
在根据初始化配置的8bit(包含以下)用uint8_t* 高于8的用uint16_t*