Question: Using a stm 3 2 nucleo board and a WS 2 8 1 2 led ring trying to get the led to light up from

Using a stm32 nucleo board and a WS2812 led ring trying to get the led to light up from this code but not working please help me
#include "stm32l476xx.h"// Include the appropriate STM32 header file
#define LED_PIN 5// GPIO pin connected to the data input of the LED ring
#define ONBOARD_LED_PIN 5// GPIO pin connected to the onboard LED
#define NUM_LEDS 12// Number of LEDs in the ring
// Function to set a GPIO pin high
void set_pin_high(GPIO_TypeDef *GPIOx, uint32_t pin){
GPIOx->BSRR |=(1<< pin);
}
// Function to set a GPIO pin low
void set_pin_low(GPIO_TypeDef *GPIOx, uint32_t pin){
GPIOx->BRR |=(1<< pin);
}
// Function to send a single bit to the LED ring
void send_bit(GPIO_TypeDef *GPIOx, uint32_t pin, uint8_t bit){
if (bit){
set_pin_high(GPIOx, pin);
// Adjust delay based on the timing requirements of the WS2812 LED
volatile int i;
for (i =0; i <3; i++); // Fine-tune this delay
set_pin_low(GPIOx, pin);
// Adjust delay based on the timing requirements of the WS2812 LED
for (i =0; i <6; i++); // Fine-tune this delay
} else {
set_pin_high(GPIOx, pin);
// Adjust delay based on the timing requirements of the WS2812 LED
volatile int j;
for (j =0; j <1; j++); // Fine-tune this delay
set_pin_low(GPIOx, pin);
// Adjust delay based on the timing requirements of the WS2812 LED
for (j =0; j <8; j++); // Fine-tune this delay
}
}
// Function to send a byte to the LED ring
void send_byte(GPIO_TypeDef *GPIOx, uint32_t pin, uint8_t byte){
// Send each bit of the byte, starting from the MSB
for (int i =7; i >=0; i--){
send_bit(GPIOx, pin, (byte >> i) & 0x01);
}
}
// Function to send RGB color data to a single LED in the ring
void send_color(GPIO_TypeDef *GPIOx, uint32_t pin, uint8_t r, uint8_t g, uint8_t b){
// Send data for green LED first, then red, then blue
send_byte(GPIOx, pin, g);
send_byte(GPIOx, pin, r);
send_byte(GPIOx, pin, b);
}
int main(void){
// Initialize GPIO Port A (or the appropriate GPIO port for your microcontroller)
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
// Configure LED pin as output
GPIOA->MODER &= ~(3UL <<(2* LED_PIN));
GPIOA->MODER |=(1UL <<(2* LED_PIN));
// Configure onboard LED pin as output
GPIOA->MODER &= ~(3UL <<(2* ONBOARD_LED_PIN));
GPIOA->MODER |=(1UL <<(2* ONBOARD_LED_PIN));
// Initialize GPIO Port C for button input
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
GPIOC->MODER &= ~(3UL <<(2*13)); // Set as input mode
GPIOC->PUPDR |=(2UL <<(2*13)); // Set pull-down mode
// Main loop
while (1){
// Check if the button is pressed
if (GPIOC->IDR & (1<<13)){// Assuming the button is connected to pin PC13
// Turn on all LEDs in the ring (assuming white color)
for (int i =0; i < NUM_LEDS; i++){
send_color(GPIOA, LED_PIN, 255,255,255); // White
}
} else {
// Turn on all LEDs in the ring (assuming red color)
for (int i =0; i < NUM_LEDS; i++){
send_color(GPIOA, LED_PIN, 255,0,0); // Red
}
}
// Delay for visualization
for (volatile int k =0; k <500000; k++); // Adjust this delay for your system
// Turn off all LEDs
for (int j =0; j < NUM_LEDS; j++){
send_color(GPIOA, LED_PIN, 0,0,0); // Turn off
}
// Delay for visualization
for (volatile int l =0; l <500000; l++); // Adjust this delay for your system
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!