cancel
Showing results for 
Search instead for 
Did you mean: 

How to apply 74HC165 for STM32

Duy Khang Banh
Associate II
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.
8 REPLIES 8
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 Venmo
Up vote any posts that you find helpful, it shows what's working..
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.

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 Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 27, 2017 at 18:57

I have only 1 high signal at D0. So there is only a single bit for ex:(0x01).  I came across a chinese website and tried its code, however they use a different library than the HAL library used by STM32Cube. Downloading the library(STM32F10x_StdPeriph_Lib_V3.5.0), using the example of a gpio toggle with its including template doesn't even toggle the pin after flashing the prgram(reset and run). No errors, but also doesn't run. However, this is another topic.

Is there no 'global' basic code for the 74HC165 parallel in/serial out?

Posted on December 28, 2017 at 13:39

give me your direct e-mail and I will send you code you need

Posted on December 28, 2017 at 14:49

I'll send you my e-mail via private message. Thank you in advance!

Posted on December 28, 2017 at 16:17

hello. I did get your email as you see. Can you please again explain your

issue with 74hc165 ?

And please clarify who is manufacturer of your 74hc165. TI, Philips and so

on use different names of ports. For example TI has port SI but Philips the

same port naming as DS.

But behavior is identical. Code will be very simple.

--

Sent from Gmail Mobile

Posted on December 28, 2017 at 18:09

NXP 74HC165D. It's a micro variant from the normal 74hc I've soldered it onto a SOP 20 Pin plate so I can pin it into my breadbord. If I am correct then it uses the standard names of ports. Conrad has a documentation of it:

http://www.produktinfo.conrad.com/datenblaetter/1100000-1199999/001116074-da-01-en-IC_SCHIE_REGST_74HC165D_653_SOIC_16_NEX.PDF

My problem is that I can't find any working 74HC165 that works with the stm32f103c8. I've found sources that uses another library or half products which I can't understand since they only provide half of the code.