cancel
Showing results for 
Search instead for 
Did you mean: 

How to write multiple sensor modules with stm32 spi

ark2
Associate III

I don't know how to use several sensor modules with stm32 spi

They told me to use the code I received, but I think it's wrong because even if I connect the sensor and run the code, the same value comes out

Do I have to fix all the codes below? Or do I just have to change it a little bit?

 Please tell me how I can use multiple sensor modules with one spi

// spi -speed  = 2 "=" 64.5MBits => 0x00
  // spi -speed  = 4 "=" 32.25MBits => 0xFF
  // spi -speed  = 8 "=" 16.125MBits => 
  // spi -speed  = 16 "=" 8.0825MBits => 0xFF
  // spi -speed  = 256 "=" 503.906MBits => 
   uint8_t T_buffer1[] = {0xC0, 0x00 };
   uint8_t T_buffer2[] = {0x05, 0x00 };
   uint8_t T_buffer3[] = {0x07, 0x03 };
   uint8_t T_buffer4[] = {0x0B, 0x00};
   uint8_t T_buffer6[] = {0x0D, 0x00};
   uint8_t T_buffer7[] = {0x0F, 0x00};
   uint8_t T_buffer8[] = {0x11, 0x00};
   uint8_t T_buffer9[] = {0x13, 0x00};
   uint8_t T_buffer10[] = {0x15, 0x00};
   uint8_t T_buffer11[] = {0x17, 0x00};
   uint8_t T_buffer12[] = {0x19, 0x00};
   uint8_t T_buffer13[] = {0x00};
   uint8_t T_buffer_Cmd[] = {0xA0, 0x00,0xFF,0xFF};
   uint8_t T_buffer_Capture0[] = {0x00, 0x00,0xFF,0xFF,128};
   uint8_t T_buffer_Capture1[] = {0x00, 0x00,0xFF,0xFF,128};
   uint8_t T_buffer_Capture2[] = {0x00, 0x00,0xFF,0xFF,128};
   uint8_t T_buffer_Capture3[] = {0x00, 0x00,0xFF,0xFF,128};
   uint8_t T_buffer_Capture4[] = {0x00, 0x00,0xFF,0xFF,128};
   uint8_t T_buffer_Capture5[] = {0x00, 0x00,0xFF,0xFF,128};
   uint8_t T_buffer_Capture6[] = {0x00, 0x00,0xFF,0xFF,128};
   uint8_t T_buffer_Capture7[] = {0x00, 0x00,0xFF,0xFF,128};
 
   //   --> ch0
   //   uint8_t T_buffer_C1[] = {0x03,0x01};
   //   uint8_t T_buffer_C2[] = {0x05,0xFE};   
   //   uint8_t T_buffer_C3[] = {0x0B,0x06}; //0B : 0,0D : 1 
   //   --> ch1
   //   uint8_t T_buffer_C1[] = {0x03,0x02};
   //   uint8_t T_buffer_C2[] = {0x05,0xFD};   
   //   uint8_t T_buffer_C3[] = {0x0D,0x06}; //0B : 0,0D : 1 
   
   uint8_t T_buffer_C1[] = {0x03,0x01,0x00}; // 80: 7
   uint8_t T_buffer_C2[] = {0x05,0xFE,0x00}; //7F: 7  
   uint8_t T_buffer_C3[] = {0x0B,0x06,0x00}; //0B : 0,0D : 1  19: 7
   uint8_t T_buffer_C33[] = {0xA0,0x00,0x00,0x00};
   uint8_t T_buffer_C4_3[] = {0x00,0x00,0x00,0x00};
   uint8_t T_buffer_C5[] = {0xFF,0xFF,0x00,0x00};
 
   uint8_t T_buffer_C1_[] = {0x85,0x00}; // 80: 7
   uint8_t T_buffer_C2_0[] = {0x0B,0x00}; //7F: 7  
   uint8_t T_buffer_C2_1[] = {0x0D,0x05}; //7F: 7  
   uint8_t T_buffer_C2_2[] = {0x0F,0x05}; //7F: 7  
   uint8_t T_buffer_C2_3[] = {0x11,0x05}; //7F: 7  
   uint8_t T_buffer_C2_4[] = {0x13,0x05}; //7F: 7  
   uint8_t T_buffer_C2_51[] = {0x15,0x05}; //7F: 7  
   uint8_t T_buffer_C2_6[] = {0x17,0x05}; //7F: 7  
   uint8_t T_buffer_C2_7[] = {0x19,0x0F}; //7F: 7  
   uint8_t T_buffer_C33_[] = {0xA0,0x00,0xFF,0xFF};
   uint8_t T_buffer_C4_[] = {0xFF,0xFF,0xFF,0xFF};
   uint8_t T_buffer_C5_[] = {0x00,0x00,0x00,0x00};   
   
   
   uint8_t T_buffer_C1_5[] = {0x03,0x80};
   uint8_t T_buffer_C2_5[] = {0x05,0x7F};   
   uint8_t T_buffer_C3_5[] = {0x19,0x06}; //0B : 0,0D : 1 
   uint8_t T_buffer_C33_5[] = {0xA0,0x00};
   uint8_t T_buffer_C4_5[] = {0xFF,0xFF};
   uint8_t T_buffer_C5_5[] = {0xC0,0x00,0xFF,0xFF};
   
   HAL_SPI_Transmit(&hspi2, T_buffer_C1_, 2, 5);
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_0, 2, 5);   
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     /*
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_1, 2, 5);   
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_2, 2, 5);   
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_3, 2, 5);   
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_4, 2, 5);   
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_51, 2, 5);   
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_6, 2, 5);   
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);    */ 
   HAL_SPI_Transmit(&hspi2, T_buffer_C2_7, 2, 5);
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);    
   HAL_SPI_Transmit(&hspi2, T_buffer_C5_5, 2, 5);
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi2, T_buffer_C5_5+2, 2, 5);
HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);  
 
   HAL_SPI_Transmit(&hspi5, T_buffer_C1_, 2, 5);
HAL_SPI_Receive(&hspi5,R_buffer_Spi2, 2, 5);     
   HAL_SPI_Transmit(&hspi5, T_buffer_C2_0, 2, 5);   
HAL_SPI_Receive(&hspi5,R_buffer_Spi2, 2, 5);  
   HAL_SPI_Transmit(&hspi5, T_buffer_C2_7, 2, 5);
HAL_SPI_Receive(&hspi5,R_buffer_Spi5, 2, 5);    
   HAL_SPI_Transmit(&hspi5, T_buffer_C5_5, 2, 5);
HAL_SPI_Receive(&hspi5,R_buffer_Spi5, 2, 5);     
   HAL_SPI_Transmit(&hspi5, T_buffer_C5_5+2, 2, 5);
HAL_SPI_Receive(&hspi5,R_buffer_Spi5, 2, 5); 
  
  for(;;)
  { 
    osDelay(1);
    if(timer_state==1)
    {            
      for(int i=0; i<8; i++)
      {
        T_buffer_C5_5[0] = 0xC0 + (4 * i);
      
        HAL_Delay(1);
        HAL_SPI_Transmit(&hspi2, T_buffer_C5_, 2, 5);
        HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);
        HAL_SPI_Transmit(&hspi2, T_buffer_C4_, 2, 5);
        HAL_SPI_Receive(&hspi2,R_buffer_Spi2, 2, 5);
        
        Temp_Ch0 = R_buffer_Spi2[3];
        Temp_Ch0 = Temp_Ch0 << 8;
        Temp_Ch0 += R_buffer_Spi2[2];
 
        R_buffer_Spi2_Temp[0] = R_buffer_Spi2[3];
        R_buffer_Spi2_Temp[1] = R_buffer_Spi2[2];
        R_buffer_Spi2_Temp[2] = 0x0D;
        R_buffer_Spi2_Temp[3] = 0x0A; 
        
        HAL_Delay(5);
        
        if(i==0)
        {
          Temp_Ch0 = 0;
          Temp_Ch0 = R_buffer_Spi2_Temp[1];
          Temp_Ch0 = Temp_Ch0 << 8;
          Temp_Ch0 += R_buffer_Spi2_Temp[0];
          set_sensordata(&Temp_Ch0, 0);
        }
        else if(i==7)
        {
          Co2_2_Ch7 = 0;
          Co2_2_Ch7 = R_buffer_Spi2_Temp[1];
          Co2_2_Ch7 = Co2_2_Ch7 << 8;
          Co2_2_Ch7 += R_buffer_Spi2_Temp[0];
          set_sensordata(&Co2_2_Ch7 , 8);
        }
      }
 
      
      T_buffer_C5_5[0]=0xC0;
 
      memset(R_buffer_Spi5, 0x00, sizeof(R_buffer_Spi5));
      memset(R_buffer_Spi2_Temp, 0x00, sizeof(R_buffer_Spi2_Temp));
      
      timer_state= 0;
    }
  }

0 REPLIES 0