Skip to main content
Duy Khang Banh
Associate II
December 26, 2017
Question

How to apply 74HC165 for STM32

  • December 26, 2017
  • 1 reply
  • 4575 views
Posted on December 26, 2017 at 15:08

Dear community,

I can hardly find any code for the STM32 using a parallel in/serial out 74HC 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.

#74hc165 #74hc595 #stm32f103

Note: this post was migrated and contained many threaded conversations, some content may be missing.
This topic has been closed for replies.

1 reply

Tesla DeLorean
Guru
December 26, 2017
Posted on December 26, 2017 at 17:56

>>

I can hardly find any code for the STM32 using a parallel in/serial out 74HC165

It is pretty much an outlier, as a developer you might have to code your own solutions. Things don't need to be STM32 centric, the concepts at play are fairly basic.

I'm sure there are manuals describing the pin sequencing required for a 74165 part, you have to emulate that with signals you place on GPIO pins. Consider a scope or logic analyzer if you need to visualize what the pins are doing.

Code doesn't really describe the pins used, there doesn't seem to be any code to output bits.

No idea what HC165_OUT equates to. Show enough code to be able to review fully.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Duy Khang Banh
Associate II
December 26, 2017
Posted on December 26, 2017 at 18:21

There is more code below but that is generated by cubeMX and it's about GPIO configure. HC165_OUT is connected to QH pin of the 74HC165. It's a sort of MISO without SPI. The code for output bits was originally

void

main(

void

) {    

while

(

1

)     {       P1

=

HC165();       delay();     } }

However, STM32 doesn't use P1 and isn't defined either by cube. I tried with

void

main(

void

) {    

while

(

1

)     {      

HAL_GPIO_WritePin(GPIOB,HC165(),GPIO_PIN_SET);

      delay();     } }

However, I don't think it means the same, I could be wrong, however none of the leds turn on so I guess there is a fault in the coding since I converted the original source to this situation.

Tesla DeLorean
Guru
December 26, 2017
Posted on December 26, 2017 at 18:57

You're going to need to set the pin high or low depending on the bit you want to send. Driving it High all the time won't get the bit data you want shifted out.

You need to bit-bang the data and clock bits to move the data out.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..