thanks for your response actually i want to transmit the ones and zeros from pdm microphone and for that i need a counter to count the ones a from the pdm data to plot in a python script but its dosent work as expected ,im giving the clock with timer using pwm and read the data with gpio in the left channel and in the output im expecting that i receive bit from 0 to 8 but in htrem i get also other value like 56 ect.. this is my code if you have a look on it thanks
#include "main.h"
#include "usb_device.h"
#include "stdbool.h"
#include "usbd_cdc_if.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
void fillByte(bool bitValue);
void sendvalue(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define CHUNK_SIZE 1000
uint8_t binaryData[] = {0x12, 0x34, 0x56, 0x78};
static uint8_t byteToFill = 0;
//static uint8_t i = 0;
//static uint8_t bitIndex = 0;
static uint8_t counter_variable = 0;
static uint8_t onesCount = 0;
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB_DEVICE_Init();
MX_TIM2_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
while (1)
{
if (counter_variable >= 8)
{
sendvalue();
onesCount=0;
counter_variable=0;
byteToFill=0;
}
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim){
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == GPIO_PIN_7) {
// Handle the falling edge event here
fillByte(HAL_GPIO_ReadPin(PDM_Input_GPIO_Port,PDM_Input_Pin)==GPIO_PIN_SET);
}
}
void fillByte(bool bitValue) {
// Set the current bit of the byte according to the bitValue
if (bitValue) {
// Set the bit at bitIndex to 1
onesCount++;
}
counter_variable++;
}
void sendvalue(void) {
// This function can be left empty since we're sending averages in fillByte
// Transmit the average values as binary data
CDC_Transmit_FS(onesCount, sizeof( onesCount));
}