Click here to Skip to main content
15,885,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm currently working on STM32 and I want to make my speaker work depending on it's PWM generation. I made the clock/pinout configuration and some parts of codes as written below, but I don't get why this code doesn't works. It doesn't make any errors in the building process, but my speaker doesn't make any sounds. I'm really not familiar with this STM32 programming, so if you could explain my problem in detail it would be a big help. Thanks a lot!
pinout configuration[^]
clock configuration[^]

Below these are the codes that I've written,(main.c/it.c/user_codex.h/user_codex.c) and for main.c and it.c I included the codes partially. Please help me solve my problem!

What I have tried:

  /* USER CODE BEGIN 2 */
  HAL_TIM_PWM_Start(&htim1,0);
  HAL_TIM_Base_Start_IT(&htim2);
  Codex_off();
  Codex_on();

  Codex_Speaker_on();
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
      freq++;
      if(freq>=1500) freq = 500;
      HAL_Delay(1);
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/* External variables --------------------------------------------------------*/

    extern TIM_HandleTypeDef htim2;
    extern UART_HandleTypeDef huart1;
    extern TIM_HandleTypeDef htim1;
    extern uint16_t freq;
    void TIM2_IRQHandler(void)
    {
      /* USER CODE BEGIN TIM2_IRQn 0 */
      __HAL_TIM_SET_COMPARE(&htim1,0,(int)500*SinGenerator(freq) + 500);
      /* USER CODE END TIM2_IRQn 0 */
      HAL_TIM_IRQHandler(&htim2);
      /* USER CODE BEGIN TIM2_IRQn 1 */
    
      /* USER CODE END TIM2_IRQn 1 */
    }

#ifndef INC_USER_CODEX_H_
   #define INC_USER_CODEX_H_

   #include "main.h"

   #define CODEX_READ 0x03
   #define CODEX_WRITE 0x02

   #define CODEX_REG_MODE     0x00
   #define CODEX_REG_WRAM     0x06
   #define CODEX_REG_WRAMADDR 0x07

   #define CODEX_MEM_DDR   0xc017
   #define CODEX_MEM_ODATA 0xc019

   void Codex_on();
   void Codex_off();
   void Codex_reg_read(uint8_t addr, uint16_t* data);
   void Codex_reg_write(uint8_t addr, uint16_t data);
   void Codex_Speaker_on();
   void Codex_Speaker_off();
   float SinGenerator(uint16_t freq);

   #endif /* INC_USER_CODEX_H_ */

#include "user_codex.h"
    
    extern SPI_HandleTypeDef hspi1;
    void Codex_on()
    {
        HAL_GPIO_WritePin(VS_RST_GPIO_Port,VS_RST_Pin,GPIO_PIN_SET);
        HAL_Delay(100);
    }
    void Codex_off()
    {
        HAL_GPIO_WritePin(VS_RST_GPIO_Port,VS_RST_Pin,GPIO_PIN_RESET);
        HAL_Delay(100);
    }
    void Codex_reg_read(uint8_t addr, uint16_t* data)
    {
        uint8_t pTxData[4];
        uint8_t pRxData[4];
    
        pTxData[0] = CODEX_READ;
        pTxData[1] = addr;
    
        while(HAL_GPIO_ReadPin(VS_DERQ_GPIO_Port,VS_DERQ_Pin) == GPIO_PIN_RESET);
    
        HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_RESET); //CS low
        HAL_SPI_TransmitReceive(&hspi1,pTxData,pRxData,4,100);
        HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_SET); //CS high
    
        *data = (pRxData[2]<<8) | pRxData[3];
    }
    void Codex_reg_write(uint8_t addr, uint16_t data)
    {
        uint8_t pTxData[4];
        uint8_t pRxData[4];
    
        pTxData[0] = CODEX_READ;
        pTxData[1] = addr;
        pTxData[2] = 0xff & (data >> 8);
        pTxData[3] = 0xff & data;
    
        while(HAL_GPIO_ReadPin(VS_DERQ_GPIO_Port,VS_DERQ_Pin) == GPIO_PIN_RESET);
    
        HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_RESET); //CS low
        HAL_SPI_TransmitReceive(&hspi1,pTxData,pRxData,4,100);
        HAL_GPIO_WritePin(VS_XCS_GPIO_Port,VS_XCS_Pin,GPIO_PIN_SET); //CS high
    }
    void Codex_Speaker_on()
    {
        Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_DDR);
        Codex_reg_write(CODEX_REG_WRAM, 0x10);
    
        Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_ODATA);
        Codex_reg_write(CODEX_REG_WRAM, 0x10);
    }
    
    void Codex_Speaker_off()
    {
        Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_DDR);
        Codex_reg_write(CODEX_REG_WRAM, 0x10);
    
        Codex_reg_write(CODEX_REG_WRAMADDR, CODEX_MEM_ODATA);
        Codex_reg_write(CODEX_REG_WRAM, 0x00);
    }
    
    float SinGenerator(uint16_t freq)
    {
        static float phase = 0;
        phase += 2.0*3.14*(float)freq / 8000.0;
    
        return sin(phase);
    }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900