Docs

Micro-controllers, wireless transmission and database

Head

74HC595 8-Bit Shift Registers with Blue Pill using STM32CubeIDE

Prerequisites

This project assumes you have already installed STM32CubeIDE. You need to have previously done a basic blink sketch with blue-pill using STM32CubeIDE. I have made a complete video from installing STM32CubeIDE to LED blink program. You can watch it by clicking this link. https://www.youtube.com/watch?v=kXg467nVd_A

Wiring Diagram

Diagram

STM32CubeIDE Settings

Set PB6, PB8 and PB9 to GPIO_Output

Additional code on top of STM32CubeIDE generated code

/* USER CODE BEGIN 0 */
#define SER_PIN GPIO_PIN_6
#define SER_PORT GPIOB
#define RCLK_PIN GPIO_PIN_8
#define RCLK_PORT GPIOB
#define SRCLK_PIN GPIO_PIN_9
#define SRCLK_PORT GPIOB

uint8_t currentVal =  0b00000000;

void HC595write()
{
    for(int i=0; i<8; i++)
    {
        if(currentVal & (1<<i))
        {
            HAL_GPIO_WritePin(SER_PORT, SER_PIN, GPIO_PIN_SET);
        }
        else
        {
            HAL_GPIO_WritePin(SER_PORT, SER_PIN, GPIO_PIN_RESET);
        }
        HAL_GPIO_WritePin(SRCLK_PORT, SRCLK_PIN, GPIO_PIN_SET);
        HAL_GPIO_WritePin(SRCLK_PORT, SRCLK_PIN, GPIO_PIN_RESET);
    }
    HAL_GPIO_WritePin(RCLK_PORT, RCLK_PIN, GPIO_PIN_SET);
    HAL_GPIO_WritePin(RCLK_PORT, RCLK_PIN, GPIO_PIN_RESET);
}

void HC595writePin(uint8_t pin, uint8_t value)
{
      if (value == 0)
      {
          currentVal &= ~(1 << pin);
      }
      else
      {
          currentVal |= (1 << pin);
      }
      HC595write();
}
/* USER CODE END 0 */

  /* USER CODE BEGIN 2 */
  // Set all pins "ON"
  currentVal =  0b11111111;
  HC595write();
  HAL_Delay(1000);

  // Set selected pins "ON"
  // OFF-OFF-ON-ON-ON-ON-OFF-OFF
  currentVal =  0b00111100;
  HC595write();
  HAL_Delay(1000);

  // Set all pins "OFF"
  currentVal =  0b00000000;
  HC595write();
  HAL_Delay(350);

  HC595writePin(0, 1); // Set pin 0 "ON"
  HAL_Delay(350);

  HC595writePin(1, 1); // Set pin 1 "ON"
  HAL_Delay(350);

  HC595writePin(2, 1); // Set pin 2 "ON"
  HAL_Delay(350);

  HC595writePin(3, 1); // Set pin 3 "ON"
  HAL_Delay(350);

  HC595writePin(4, 1); // Set pin 4 "ON"
  HAL_Delay(350);

  HC595writePin(5, 1); // Set pin 5 "ON"
  HAL_Delay(350);

  HC595writePin(6, 1); // Set pin 6 "ON"
  HAL_Delay(350);

  HC595writePin(7, 1); // Set pin 7 "ON"
  HAL_Delay(350);

  HC595writePin(0, 0); // Set pin 0 "OFF"
  HAL_Delay(350);

  HC595writePin(1, 0); // Set pin 1 "OFF"
  HAL_Delay(350);

  HC595writePin(2, 0); // Set pin 2 "OFF"
  HAL_Delay(350);

  HC595writePin(3, 0); // Set pin 3 "OFF"
  HAL_Delay(350);

  HC595writePin(4, 0); // Set pin 4 "OFF"
  HAL_Delay(350);

  HC595writePin(5, 0); // Set pin 5 "OFF"
  HAL_Delay(350);

  HC595writePin(6, 0); // Set pin 6 "OFF"
  HAL_Delay(350);

  HC595writePin(7, 0); // Set pin 7 "OFF"
  HAL_Delay(350);

  // Binary counter
    for(int counter=0; counter<256; counter++)
    {
      currentVal =  counter;
      HC595write();
      HAL_Delay(100);
    }
  /* USER CODE END 2 */