新手上路
- 积分
- 39
- 金钱
- 39
- 注册时间
- 2021-10-6
- 在线时间
- 4 小时
|
4金钱
本帖最后由 xiayuu 于 2021-10-8 10:26 编辑
因为外置传感器是单线的(只能向外发送数据),所以用了stm32的SPI2做单线双向接收。但是现在接收不到数据,不知道为什么,希望有人帮帮我。
时钟信号我设置的APB1是36M,下面是我初始化的程序。- <div class="blockcode"><blockquote>void Rcc_Init(void)
- {
- RCC_DeInit();
- RCC_HSEConfig(RCC_HSE_ON); //OPEN_HSE
- while(!(RCC->CR>>17));
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //SYSTEMCLK:9*8=72M
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB: 72/1=48M
- RCC_PCLK1Config(RCC_HCLK_Div4); //APB1:72/4=18M
- RCC_PCLK2Config(RCC_HCLK_Div1); //APB2:72/1=72MS
- RCC_PLLCmd(ENABLE);
- while(!(RCC->CR>>25));
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //set_pll=sysclk
- while(RCC_GetSYSCLKSource() != 0x08);
- }
- void SPI2_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- SPI_InitTypeDef SPI_InitStructure;
- /*使能时钟信号*/
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOD, ENABLE );//PORTB时钟使能
- RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE );//SPI2时钟使能
- /*SPI2 definated pin*/
-
- /*CLK*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- /*SO*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //上拉输入
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOB
- /*user definated pin*/
- /*CS1*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- GPIO_ResetBits(GPIOB,GPIO_Pin_13); //时钟信号空闲位:低
- GPIO_SetBits(GPIOB,GPIO_Pin_14);
- GPIO_SetBits(GPIOD,GPIO_Pin_3);
- /*SPI2初始化*/
- SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; //设置SPI为单线双向接收
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //设置为主机
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //设置SPI输出大小:接收8位帧数据
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //串口同步时钟空闲位为低电平
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //上升沿采样
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS信号由软件控制
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //波特率预分频值256
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //数据传输从MSB位开始
- SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC计算多项式
- SPI_Init(SPI2, &SPI_InitStructure); //初始化寄存器
-
- }
复制代码
下面这段是读取数据,但是一直没有读到数据
- <div class="blockcode"><blockquote>void SPI2_ReadWriteByte(void)
- {
- while (SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE) == SET)
- {
- switch(count)
- {
- case 0:
- read_byte=SPI_I2S_ReceiveData(SPI2); //读低八位
- rec_data=(uint16_t)read_byte;
- count=1;
- break;
- case 1:
- read_byte=SPI_I2S_ReceiveData(SPI2); //高八位
- rec_data|=((uint16_t)read_byte<<8);
- count=2;
- break;
- }
- if(count==2)
- {
- SPI_CS_SET; //取消片选
- Delay_us(1);
- }
- }
- }
-
- void SPI_ReadSR(void)
- {
- if(count==0)
- {
- SPI_CS_CLR; //拉低片选
- Delay_us(3);
- SPI_Cmd(SPI2, ENABLE);
- }
- if(count<2)
- {
- SPI2_ReadWriteByte(); //获取一个字节
- }
- else
- {
- count=0;
- start_byte=1;
- }
- }
复制代码
下面是传感器的工作时序
SPI Comms Digital Ouptu Pressure Sensors_TN_008202-3-EN_Final_30May12.pdf
(210.39 KB, 下载次数: 1)
|
|