Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I\'m writing Knight Rider in C program for STM32L152RC , but I\'m not able to al

ID: 667458 • Letter: I

Question

I'm writing Knight Rider in C program for STM32L152RC , but I'm not able to allow it to the trailing LEDs feed into the leading LED before reversing the direction and have at least three trailing LEDs behind the leading LED as shown on this demo video: https://www.youtube.com/watch?v=TWtTreX45Ts

How can I achived the intensity of the light when the light is traveling from one side to other?

Here is the code I wrote so far:

#include <stdio.h>
#include "stm32l1xx.h"

uint32_t SystemCoreClock = 2097000;

void PB4_Init(void){
   RCC->AHBENR |= (1UL<<1);
  
   GPIOB->MODER &= ~(0XFFFFF00);           //0000 1111 1111 1111 1111 1111 0000 0000
   GPIOB->MODER |= (0X5555500); //0000 0101 0101 0101 0101 0101 0000 0000
  
   GPIOB->OTYPER &= ~(0X000000C0);      
  
   GPIOB->OSPEEDR &= ~(0XFFFFF00);      
   GPIOB->OSPEEDR &= (0X5555500);      
  
   GPIOB->PUPDR &= ~(0XFFFFF00);      
}

uint32_t msTicks = 0;
void SysTick_Handler(void){
   msTicks++;
}

void Delay(uint32_t dlyTicks){
   uint32_t curTicks;
  
   curTicks = msTicks;
   while((msTicks - curTicks)<dlyTicks);
}

uint8_t o=1;

uint32_t a=0x10;
uint32_t b=0x40;
uint32_t c= 0x800;
uint32_t d= 0x1000;
uint32_t e= 0x2000;

uint32_t i,j;

int main(void)
{
   if(SysTick_Config(SystemCoreClock / 1000))
   {
       while(1);
   }
   PB4_Init();
  
  
   while(1)
   {
      
       GPIOB->ODR=a;
       Delay(200);
       GPIOB->ODR=b;
       Delay(200);
      
       if(o)
       {
           t<<=1;
       }
       else
       {  
           t>>=1;
       }
       if(t==0x2000) o = 0;
       else if(t==0x10) o=1;
      
   }
  
}

Explanation / Answer

you haven't added the delay for the intensity.

#include <stdio.h>
#include "stm32l1xx.h"

uint32_t SystemCoreClock = 2097000;

void PB4_Init(void){
   RCC->AHBENR |= (1UL<<1);
  
   GPIOB->MODER &= ~(0XFFFFF00);           //0000 1111 1111 1111 1111 1111 0000 0000
   GPIOB->MODER |= (0X5555500); //0000 0101 0101 0101 0101 0101 0000 0000
  
   GPIOB->OTYPER &= ~(0X000000C0);      
  
   GPIOB->OSPEEDR &= ~(0XFFFFF00);      
   GPIOB->OSPEEDR &= (0X5555500);      
  
   GPIOB->PUPDR &= ~(0XFFFFF00);      
}

uint32_t msTicks = 0;
void SysTick_Handler(void){
   msTicks++;
}

void Delay(uint32_t dlyTicks){
   uint32_t curTicks;
  
   curTicks = msTicks;
   while((msTicks - curTicks)<dlyTicks);
}

uint8_t o=1;

uint32_t a=0x10;
uint32_t b=0x40;
uint32_t c= 0x800;
uint32_t d= 0x1000;
uint32_t e= 0x2000;

uint32_t i,j;

int main(void)
{
   if(SysTick_Config(SystemCoreClock / 1000))
   {
       while(1);
   }
   PB4_Init();
  
  
   while(1)
   {
      
       GPIOB->ODR=a;
       Delay(200);
       GPIOB->ODR=b;
       Delay(200);
      
       if(o)
       {
           t<<=1;

           delay_ms(50);
       }
       else
       {  
           t>>=1;

         delay_ms(50);
       }
       if(t==0x2000) o = 0;
       else if(t==0x10) o=1;
      
   }
  
}