Question about stm32f103c8

recently i been making a device that fulfill the following requirement using stm32f103c8:

1. Use the STM32F103C8T6 (Blue Pill) development board.
2. Connect to an RGB LED using suitable I/O pins. An RGB LED is a combination of 3 LEDs (red, green,
and blue) in one package. You can produce other colours by combining these three colours.
3. Display the following colour sequence continuously: Red, Yellow, Green, Cyan, Blue and Magenta.
4. Each colour should stay on for TWO (2) seconds.
5. Add a switch, SW1. Press SW1 to immediately change the RGB LED colour sequence to flashing white at
2 Hz. White is produced by turning on all 3 LEDs (red, green, and blue) at the same time. Release SW1 to
immediately resume the interrupted colour sequence.
6. Add a second switch, SW2. Press SW2 to immediately speed up colour sequence and flashing white
operations by a factor of TEN (10) i.e. each colour should stay on for 0.2 seconds (for colour sequence),
and flashing white at 20 Hz. Release SW2 to immediately return to the default speed of colour sequence
and flashing white operations.

the program is written using c language in microvision 4. however, my code keep on getting stuck in the delay function, is there any way to solve it?

#include "stm32f10x.h"
#define SW1	0
#define SW2	1

volatile uint8_t Blinky_White = 0;
volatile float time1 = 2;
volatile float time2 = 0.25;
float loop = 0;
int External_F = 0;
int del_loop = 0;
int store = 0;
float i;

void RGB_CHANGING(void);
void WHITE_BLINK(void);
void Delay(float time_del);
void RGB_CONTROL(int red, int green, int blue);
void START_WORK(void);
void GPIO_Init(void);

void SysTick_Handler(void){
	SysTick->CTRL = SysTick->CTRL; //clear SysTick interrupt flag
}

void EXTI0_IRQHandler(void){
	if(GPIOC->IDR & (1uL<<(SW1))){
		Blinky_White = 1;
		del_loop = i+1;
	}
	else{
		Blinky_White = 0;
		loop = del_loop;
	}
	External_F = 1;
	EXTI->PR |= (1uL << 0); //CLEAR PENDING
}

void EXTI1_IRQHandler(void){
	if(GPIOC->IDR & (1uL<<(SW2))){
		time1 = time1/10;
		time2 = time2/10;
		loop = (i+1)/10;
	}
	else{
		time1 = 2;
		time2 = 0.25;
		loop = (i+1)*10;
	}
	store = 1;
	External_F = 1;
	EXTI->PR |= (1uL << 1); //CLEAR PENDING
}


int main(void){
	GPIO_Init();
	START_WORK();
}

void GPIO_Init(void){
	RCC->APB2ENR |= (1UL << 0)|(1UL << 3);
	GPIOB->CRL &= (~0x000FFFFF);
	GPIOB->CRL |= 0x00033388; //2 i/p & 3 o/p
	GPIOB->ODR &= (~(1UL << 0))|(~(1UL << 1)); //input with pull-down
	GPIOB->ODR &= (~(1UL << 4))|(~(1UL << 3))|(~(1UL << 2)); //LEDs initially off
	AFIO->EXTICR[0] = (~0x000000FF);
	AFIO->EXTICR[0] = 0x00000011; // Select PB0 for EXTI0 & Select PB1 for EXTI1
	EXTI->RTSR |= (1UL << 0)|(1UL << 1); // Enable both interrupts on rising edges
	EXTI->FTSR |= (1UL << 0)|(1UL << 1); // Enable both interrupts on falling edges
	EXTI->PR |= (1UL << 0)|(1UL << 1); // Clear pending bits
	EXTI->IMR |= (1UL << 0)|(1UL << 1); // Enable IRQs from Line 0 and 1
	
	NVIC_EnableIRQ(EXTI0_IRQn);
	NVIC_EnableIRQ(EXTI1_IRQn);
	
	__enable_irq();
}

void START_WORK(void){
	while(1){
		RGB_CHANGING();
		WHITE_BLINK();
	}
}

void RGB_CHANGING(void){
	static enum {RED_STATE,YELLOW_STATE,GREEN_STATE,CYAN_STATE,BLUE_STATE,MAGANTA_STATE}
STATE = RED_STATE;
	if(Blinky_White == 0){
		switch(STATE){
			case RED_STATE:
				RGB_CONTROL(1,0,0);
				Delay(time1);
				if((!GPIOB->IDR & (1uL<<(SW1)))&&!store){
					STATE = YELLOW_STATE;
					loop = 0;
				}
				if (store)
					store = 0;
				break;
			case YELLOW_STATE:
				RGB_CONTROL(1,1,0);
				Delay(time1);
				if((!GPIOB->IDR & (1uL<<(SW1)))&&!store){
					STATE = GREEN_STATE;
					loop = 0;
				}
				if(store)
						store = 0;
				break;
			case GREEN_STATE:
				RGB_CONTROL(0,1,0);
				Delay(time1);
				if((!GPIOB->IDR & (1uL<<(SW1)))&&!store){
					STATE = CYAN_STATE;
					loop = 0;
				}
				if(store)
						store = 0;
				break;
			case CYAN_STATE:
				RGB_CONTROL(0,1,1);
				Delay(time1);
				if((!GPIOB->IDR & (1uL<<(SW1)))&&!store){
					STATE = BLUE_STATE;
					loop = 0;
				}
				if(store)
						store = 0;
				break;
			case BLUE_STATE:
				RGB_CONTROL(0,0,1);
				Delay(time1);
				if((!GPIOB->IDR & (1uL<<(SW1)))&&!store){
					STATE = MAGANTA_STATE;
					loop = 0;
				}
				if(store)
						store = 0;
				break;
			case MAGANTA_STATE:
				RGB_CONTROL(1,0,1);
				Delay(time1);
				if((!GPIOB->IDR & (1uL<<(SW1)))&&!store){
					STATE = RED_STATE;
					loop = 0;
				}
				if(store)
						store = 0;
				break;
			default:
				STATE = RED_STATE;
				break;
		}
	}
}

void WHITE_BLINK(void){
	//SWITCH (CONDITION? HOW TO GO BACK TO PREVIOUS COLOR?)
	static enum {WHITE_STATE,BLACK_STATE} 
	STATE = WHITE_STATE;
	if(Blinky_White == 1){
		switch(STATE){
			case WHITE_STATE:
				RGB_CONTROL(1,1,1);
				Delay(time2);
				if(!store){
					STATE = BLACK_STATE;
					loop = 0;
				}
				if(store)
						store = 0;
				break;
			case BLACK_STATE:
				RGB_CONTROL(0,0,0);
				Delay(time2);
				if(!store){
					STATE = WHITE_STATE;
					loop = 0;
				}
				if(store)
						store = 0;
				break;
			default:
				STATE = WHITE_STATE;
				break;
		}
	}
}


void Delay(float time_del){
	float factor = time_del/0.025;
	for(i = loop; i < factor; i++){
		if(External_F){
				break;
		}
		SysTick_Config(0.025*SystemCoreClock); 
		__WFI(); 
	}
	External_F = 0;
}

void RGB_CONTROL(int red, int green, int blue){
	if(red){
		GPIOB->BSRR = (1uL << 2);
	}
	if(!red){
		GPIOB->BRR = (1uL << 2);
	}
	if(green){
		GPIOB->BSRR = (1uL << 3);
	}
	if(!green){
		GPIOB->BRR = (1uL << 3);
	}	
	if(blue){
		GPIOB->BSRR = (1uL << 4);
	}
	if(!blue){
		GPIOB->BRR = (1uL << 4);
	}
}


Parents Reply Children
No data