#include "stm32c0xx_hal.h"
volatile int mode = 0;
volatile int brightness = 0;
volatile int direction = 1;
void delay(volatile uint32_t t)
{
while(t--);
}
// Software PWM
void pwm_rgb(int r, int g, int b, int bright)
{
for(int i=0; i<100; i++)
{
if(i < bright)
{
if(r) GPIOA->BSRR = (1 << 0);
else GPIOA->BRR = (1 << 0);
if(g) GPIOA->BSRR = (1 << 1);
else GPIOA->BRR = (1 << 1);
if(b) GPIOA->BSRR = (1 << 2);
else GPIOA->BRR = (1 << 2);
}
else
{
GPIOA->BRR = (1 << 0);
GPIOA->BRR = (1 << 1);
GPIOA->BRR = (1 << 2);
}
delay(50);
}
}
int main(void)
{
// GPIOA clock
RCC->IOPENR |= RCC_IOPENR_GPIOAEN;
// PA0 PA1 PA2 output
GPIOA->MODER &= ~(0x3F);
GPIOA->MODER |= (0x15);
// PA3 input
GPIOA->MODER &= ~(3 << (3 * 2));
// pull-up
GPIOA->PUPDR &= ~(3 << (3 * 2));
GPIOA->PUPDR |= (1 << (3 * 2));
int old_button = 1;
int rainbow_step = 0;
while(1)
{
// knopka o‘qish
int button = (GPIOA->IDR & (1 << 3));
// bosildi
if(!button && old_button)
{
mode++;
if(mode > 3)
mode = 0;
delay(300000);
}
old_button = button;
// brightness o‘zgarishi
brightness += direction;
if(brightness >= 100)
direction = -1;
if(brightness <= 0)
direction = 1;
// mode lar
switch(mode)
{
case 0:
pwm_rgb(1,0,0,brightness);
break;
case 1:
pwm_rgb(0,1,0,brightness);
break;
case 2:
pwm_rgb(0,0,1,brightness);
break;
case 3:
switch(rainbow_step)
{
case 0:
pwm_rgb(1,0,0,brightness);
break;
case 1:
pwm_rgb(0,1,0,brightness);
break;
case 2:
pwm_rgb(0,0,1,brightness);
break;
case 3:
pwm_rgb(1,1,0,brightness);
break;
case 4:
pwm_rgb(1,0,1,brightness);
break;
case 5:
pwm_rgb(0,1,1,brightness);
break;
}
// brightness tugaganda keyingi rang
if(brightness == 0)
{
rainbow_step++;
if(rainbow_step > 5)
rainbow_step = 0;
}
break;
}
}
}Loading
st-nucleo-c031c6
st-nucleo-c031c6