Help to resolve RGB LED DRIVER - Page 1
Forum section: electronics / general

author: alizare368
author_rank: Regular Contributor
timestamp: June 23, 2019, 12:08:51 pm
content: Hi GuysI want to launch RGB_LED with STP1612pw05 and I use this program as a SPI software and does not work.I used stm32f401 to control STP1612pw05.someone know why???pin connect to this.SDI to PB4SCK to PB5LE TO PB9PWCLK TO PC6PWCLK is 2MHZ.My programCode:int main(void){MX_GPIO_Init();MX_TIM3_Init();/* USER CODE BEGIN 2 */HAL_TIM_Base_Init(&htim3);HAL_TIM_PWM_Init(&htim3);HAL_TIM_Base_Start(&htim3);HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);while (1){SPI_1(0x5555);}}// my software spi function#define SPI_SCLK_LOW_TIME 200#define SPI_SCLK_HIGH_TIME 1uint16_t SPI_1(uint16_t byte_out){uint16_t byte_in =0;uint16_t bit;for(i=0;i<16;i++){for (bit = 0x8000; bit; bit >>= 1){/* Shift-out a bit to the MOSI line */HAL_GPIO_WritePin(GPIOB,GPIO_PIN_4,(byte_out & bit) ? GPIO_PIN_SET : GPIO_PIN_RESET);/* Delay for at least the peer's setup time */for(int i=0;i<SPI_SCLK_LOW_TIME;i++){}/* Pull the clock line high */HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_SET);//LE PIN IS LOWHAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);/* Shift-in a bit from the MISO line */if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_8)==1)byte_in |= bit;/* Delay for at least the peer's hold time */for(int i=0;i<SPI_SCLK_HIGH_TIME;i++){}/* Pull the clock line low */HAL_GPIO_WritePin(GPIOB,GPIO_PIN_5,GPIO_PIN_RESET);HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);}}//return byte_in;}

author: reddsta87
author_rank: Newbie
timestamp: June 24, 2019, 12:03:20 pm
content: I must admit that I usually only work with arduinos and raspberri pi's and it has been a few years admittedly. I'm not sure the difference tbh but does your program define what function your assigned pins do like in your description in the code like the arduino? I'm looking in the code and don't see where it defines what the pinout is supposed to do. Am I missing something?

author: tszaboo
author_rank: Super Contributor
timestamp: June 24, 2019, 12:11:27 pm
content: You are shifting the wrong way I think. Or you are shifting it 16 * 16 times. Or something like that. You have two for cycles in each other, and you dont need that, right? Anyway, unless you want your code obfuscated, there are better ways to write bit bang interfaces.

author: alizare368
author_rank: Regular Contributor
timestamp: June 24, 2019, 02:23:40 pm
content: HiThanks for your helpCan you further help write code?

author: mikerj
author_rank: Super Contributor
timestamp: June 24, 2019, 05:04:24 pm
content: It looks like the data is being shifted out the correct way (MSbit first) and the STP1612pw05 has 16 channels of 16 bit data to load so the outer loop is correct, assuming the OP wants to load the same value to all channels.The main problem I can see is in the handling of the LE latch signal, the OP is toggling the LE pin once per bit, same as the clock. Looking at thedatasheetif all 16 channels are being written using the default configuration, then the LE pin needs to be raised for the last bit (LSbit) on the first 15 channels and for the last two bits on the 16th channel. This makes using an USART/SPI peripheral a bit tricky. It's not a very well written datasheet (ST, nothing new there) but it appears it can be configured to accept all 16 channels with a single latch operation by setting the MSbit in the config register. Also the default configuration only provides 12 bit PWM operation, so the 0x5555 value being written in by the OP would be truncated to 0x0555.I don't know if the setup/hold delays are required, probably not since the datasheet shows single digit nanosecond values which will likely be provided by the bloatware HAL.Something like this should work for the default configuration, though this is completely untested and likely full of typos.Code:void STP1612PW05_write_all_chans (uint16_t regdata );uint16_t STP1612PW05_spi_rw (uint16_t byte_out, uint16_t LE_mask);int main(void){MX_GPIO_Init();MX_TIM3_Init();/* USER CODE BEGIN 2 */HAL_TIM_Base_Init(&htim3);HAL_TIM_PWM_Init(&htim3);HAL_TIM_Base_Start(&htim3);HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);while (1){STP1612PW05_write_all_chans(0x07FF); /* 12 bit PWM by default, need to set config register to get 16 bit */}}// my software spi function#define SPI_SCLK_LOW_TIME 200#define SPI_SCLK_HIGH_TIME 1#define SPI_MOSI_PORT GPIOB#define SPI_MOSI_PIN GPIO_PIN_4#define SPI_MISO_PORT GPIOC#define SPI_MISO_PIN GPIO_PIN_8#define SPI_SCLK_PORT GPIOB#define SPI_SCLK_PIN GPIO_PIN_5#define STP1612PW05_DATA_LATCH 0x0001#define STP1612PW05_GLOBAL_LATCH 0x0002#define STP1612PW05_LE_PORT GPIOB#define STP1612PW05_LE_PIN GPIO_PIN_9/*** Write a single value to all 16 channels of the STP1612PW05.* @param chandata 16 bit value to write to all channels.*/void STP1612PW05_write_all_chans (uint16_t chandata){uint16_t le_set;for(int i=0; i<16; i++){/* When le_set == bit, the LE pin will be set highChannels 0-14 the LE is high for the final clock cycleChannel 15 the LE pin is high for the final two clock cycles */le_set = (i < 15) ? STP1612PW05_DATA_LATCH : STP1612PW05_GLOBAL_LATCH;STP1612PW05_spi_rw(chandata, le_set);}}/*** Read and write 16 bits of data to/from the STP1612PW05.* @param data_out 16 bit data value to send.* @param LE_mask Set data bit for LE rising edge (STP1612PW05_DATA_LATCH or STP1612PW05_GLOBAL_LATCH).* @return 16 bit data value read from STP1612PW05.*/uint16_t STP1612PW05_spi_rw (uint16_t data_out, uint16_t LE_mask){uint16_t data_in =0;uint16_t bit;for (bit = 0x8000; bit; bit >>= 1){/* Set outgoing data bit to MOSI */HAL_GPIO_WritePin(SPI_MOSI_PORT, SPI_MOSI_PIN, (data_out & bit) ? GPIO_PIN_SET : GPIO_PIN_RESET);/* Delay for at least the peer's setup time */for(int i=0;i<SPI_SCLK_LOW_TIME;i++){}/* Set LE pin high if required */if( bit == LE_mask ){HAL_GPIO_WritePin(STP1612PW05_LE_PORT, STP1612PW05_LE_PIN, GPIO_PIN_SET);}/* SCLK high */HAL_GPIO_WritePin(SPI_SCLK_PORT, SPI_SCLK_PIN, GPIO_PIN_SET);/* Read incomming data bit from MISO */if( HAL_GPIO_ReadPin(SPI_MISO_PORT, SPI_MISO_PIN) == GPIO_PIN_SET ){data_in |= bit;}/* Delay for at least the peer's hold time */for(int i=0; i<SPI_SCLK_HIGH_TIME; i++){}/* SCLK low */HAL_GPIO_WritePin(SPI_SCLK_PORT, SPI_SCLK_PIN, GPIO_PIN_RESET);}/* Set the LE pin low after D0 shifted out */HAL_GPIO_WritePin(STP1612PW05_LE_PORT, STP1612PW05_LE_PIN, GPIO_PIN_RESET);return byte_in;}

author: alizare368
author_rank: Regular Contributor
timestamp: June 26, 2019, 01:42:25 pm
content: Thanks