Hello,
I'm currently trying to understand how to use a 74hc165 shift register (PISO) through SERCOM SPI bus for a project.
I'm working with the SAMD21XPLAINED PRO board on Microchip Studio with ASF.
I'm quite used to work with SERCOM SPI bus for SIPO shift register (74hc595) but it seems to be quite different with a PISO shift register...
For my test I connect 8 buttons to the register and I try to control LEDs with it (Each led is connected to a different pin on the board and is supposed to be switched on by clicking on the associated button.).
The problem I have is that no mater what I can do with the 7 first buttons, the associated LEDs won't turn on.
It's only when I press the 8th button that all LEDs turn on.
I think the problem comes from the way I read the data that are coming from the shift register.
It looks like i don't shift between each read on my shift register but I don't manage to fix the problem...
My code bellow :
spi_config.h :
#ifndef SPI_CONFIG_H_ #define SPI_CONFIG_H_ #include <asf.h> /* * With mux_setting is SPI_SIGNAL_MUX_SETTING_E * * Based on samd21 datasheet 26.6 "Data In Pinout" : * DIPO 0x0 : * - DI on SERCOM0 PAD[0] (PA04) * * Based on samd21 datasheet 26.7 "Data Out Pinout" : * DOP0 0x1 : * - DO on SERCOM0 PAD[2] (PA06) * - SCK on SERCOM0 PAD[3] (PA07) * - SS on SERCOM0 PAD[1] (PA05) */ #define SERCOM_PORT EXT1_SPI_MODULE // SERCOM0 #define MUX_SETTING SPI_SIGNAL_MUX_SETTING_E // DIPO 0x0 / DOPO 0x1 #define MUX_PAD0 EXT1_SPI_SERCOM_PINMUX_PAD0 // MISO PA04 #define MUX_PAD1 EXT1_SPI_SERCOM_PINMUX_PAD1 // Slave Selection PA05 #define MUX_PAD2 EXT1_SPI_SERCOM_PINMUX_PAD2 // MOSI PA06 #define MUX_PAD3 EXT1_SPI_SERCOM_PINMUX_PAD3 // SCK PA07 #define SLAVE_SELECT_PIN EXT1_PIN_SPI_SS_0 // SPI slave selection pin struct spi_module spi_master_instance; struct spi_slave_inst slave; void configure_spi(void); #endif /* SPI_CONFIG_H_ */
spi_config.c :
#include "spi_config.h" void configure_spi(void){ struct spi_config master_config; struct spi_slave_inst_config slave_config; spi_get_config_defaults(&master_config); master_config.mux_setting = MUX_SETTING; master_config.pinmux_pad0 = MUX_PAD0; // MISO PA04 master_config.pinmux_pad1 = MUX_PAD1; // Slave Selection PA05 master_config.pinmux_pad2 = MUX_PAD2; // MOSI PA06 master_config.pinmux_pad3 = MUX_PAD3; // SCK PA07 master_config.data_order = SPI_DATA_ORDER_MSB; master_config.mode_specific.master.baudrate = 4000000; spi_slave_inst_get_config_defaults(&slave_config); slave_config.ss_pin = SLAVE_SELECT_PIN; spi_init(&spi_master_instance, SERCOM_PORT, &master_config); spi_attach_slave(&slave, &slave_config); spi_enable(&spi_master_instance); }
main.c :
#include <asf.h> #include "spi_config.h" #define NUMBER_OF_LED 8 #define LED_1 IOPORT_CREATE_PIN(1, 12) #define LED_2 IOPORT_CREATE_PIN(1, 13) #define LED_3 IOPORT_CREATE_PIN(1, 14) #define LED_4 IOPORT_CREATE_PIN(1, 15) #define LED_5 IOPORT_CREATE_PIN(1, 16) #define LED_6 IOPORT_CREATE_PIN(1, 17) #define LED_7 IOPORT_CREATE_PIN(1, 10) #define LED_8 IOPORT_CREATE_PIN(1, 11) static uint8_t ledValue[NUMBER_OF_LED]; uint8_t *LED[NUMBER_OF_LED] = { (uint8_t*)LED_1, (uint8_t*)LED_2, (uint8_t*)LED_3, (uint8_t*)LED_4, (uint8_t*)LED_5, (uint8_t*)LED_6, (uint8_t*)LED_7, (uint8_t*)LED_8, }; void btn_scan(void); void refresh_light(void); int main (void) { system_init(); ioport_init(); delay_init(); configure_spi(); for (uint8_t i = 0; i < NUMBER_OF_LED; i++){ ioport_set_pin_dir((ioport_pin_t)LED[i], IOPORT_DIR_OUTPUT); } while (1) { btn_scan(); refresh_light(); } } void refresh_light(void){ uint8_t *ptrLedVal = ledValue; for (uint8_t i = 0; i < NUMBER_OF_LED; i++){ *(ptrLedVal + i) ? ioport_set_pin_level((ioport_pin_t)LED[i], true) : ioport_set_pin_level((ioport_pin_t)LED[i], false); } } void btn_scan(void){ uint8_t *ptrLedVal = ledValue; spi_select_slave(&spi_master_instance, &slave, true); // Read 8 value and put each in a box of "ledValue" table spi_read_buffer_wait(&spi_master_instance, ptrLedVal, 8, 0b00); spi_select_slave(&spi_master_instance, &slave, false); }
74hc165 pin configuration :
Shift register's connections :
- 1 : PA05 (SPI SS Pin)
- 2 : PA07 (SPI SCK Pin)
- 3-6 : Buttons
- 7 : Nothing
- 8 : GND
- 9 : PA04 (SPI MISO)
- 10 : Nothing (No cascade)
- 11-14 : Buttons
- 15 : Nothing
- 16 : 3.3V
(Edit) Schematic :