AnsweredAssumed Answered

How to apply 74HC165 for STM32

Question asked by Duy Khang Banh on Dec 26, 2017
Latest reply on Dec 30, 2017 by Duy Khang Banh

Dear community,

 

I can hardly find any code for the STM32 using a parallel in/serial out 74HC165. I managed to program the stm32f103c8 to work with a 74hc595 which is the counterpart, so it's a serial in/parallel out. If someone wants the code just pm me and I'll send it to you. However, programming 74HC165 is hard for me since am new into the STM32 world. There are a few chinese sources who has the program but it's rather complicated since they do not provide a schematic or even the main function. Does anyone have a code/example for the 74HC165?

 

This is what I have until to far:

 

/**
******************************************************************************
* File Name : main.c
* Description : Main program body
******************************************************************************

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h"

 

/* Private variables ---------------------------------------------------------*/
#define HC165_nPL HC165_nPL_Pin
#define HC165_CK HC165_CK_Pin
#define HC165_OUT HC165_OUT_Pin

 

void SystemClock_Config(void);
static void MX_GPIO_Init(void);


void delay(void)
{
unsigned char i,j;
for(i = 250;i > 0;i--)
for(j = 200;j > 0;j--);
}

unsigned char HC165 ( void )
{
unsigned char i;
unsigned char Temp;
HAL_GPIO_WritePin(GPIOA,HC165_CK,GPIO_PIN_SET);//HC165_CK = 1;
HAL_GPIO_WritePin(GPIOA,HC165_nPL,GPIO_PIN_RESET);//HC165_nPL = 0; //HC165???
HAL_GPIO_WritePin(GPIOA,HC165_nPL,GPIO_PIN_SET);//HC165_nPL = 1; //??HC165???
Temp = 0;
if(HC165_OUT == 1) Temp |= 0x01;
for(i = 0;i < 7;i++)
{
HAL_GPIO_WritePin(GPIOA,HC165_CK,GPIO_PIN_RESET);//HC165_CK = 0;
HAL_GPIO_WritePin(GPIOA,HC165_CK,GPIO_PIN_SET);//HC165_CK = 1;
Temp <<= 1;
if(HC165_OUT == 1)
{
Temp |= 0x01;
}
}
HAL_GPIO_WritePin(GPIOA,HC165_CK,GPIO_PIN_RESET);//HC165_CK = 0;
return(Temp);
}

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();

/* USER CODE BEGIN 2 */
/* USER CODE END 2 */

/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1)
{
HC165();
if(HC165() == 0x01)
{
HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,GPIO_PIN_SET);
delay();
}
}
/* USER CODE END 3 */

}

 

//GPIO config and others are leftout(Made with CubeMX)

I use GPIOB(PB) to turn on 8 leds directly. What I want is if the bitnumber is 1(0x01) then turn on led1. 

 

Yes, the IC is 5v tolerant and so does my STM32F103C8 since I use a development board which has a voltage regulater and accepts power via microUSB. 

Outcomes