LED blinking using PWM Mini64SER6

Hello, today we continue to explore STM32 microcontroller board via Mini64SE R6. In the last article, we’ll run the demo project that generated to us Eclipse.
Let me remind in this project has been programmed flashing LED frequency of once per second. Today we will control the LED brightness via pulse width modulation.
100_9568

So what is the PWM modulation . Pulse-width modulation (PWM – Eng . PWM), or pulse-width modulation for (English PDM) – to manage the width (duration ) of high-frequency pulses to the law, which sets a low signal. In electronics may be the average value controls the output voltage by changing the length of the closed state electronic ( electro ) key , for example, in the circuit core voltage. In binary digital technology , the outputs of which can only take one of two values ​​, the approximation of the desired average output level using PWM is completely natural. This is from Wikipedia .


As shown in the picture placed above the resultant voltage on the LED, and therefore the brightness depends on the width of Impulse logical 1 on pin that will drive the LED.

Go to our project that Eclepse generated to us in the previous article, and make the changes.

//Blue led
#define SYSTICK_FREQUENCY_HZ       1000

#define BLINK_PORT GPIOA
#define BLINK_PIN 5
#define BLINK_RCC_BIT RCC_APB2Periph_GPIOA

#define BLINK_TICKS SYSTICK_FREQUENCY_HZ/10

//Yellow led
#define PWM_PORT GPIOB
#define PWM_PIN 1
#define PWM_RCC_BIT RCC_APB2Periph_GPIOB

This debug outputs that control LEDs: port, pin, bits include timing port. Also here is set the time interval that sets the rate of change of brightness of luminescence in this case 10times per second.
Перейдемо до основного кода.

int
int
main(void)
{
/* Use SysTick as reference for the timer */
SysTick_Config(SystemCoreClock / SYSTICK_FREQUENCY_HZ);

Setting system timer operation period of 1 second

/* GPIO Periph clock enable */
RCC_APB2PeriphClockCmd(BLINK_RCC_BIT, ENABLE);
RCC_APB2PeriphClockCmd(PWM_RCC_BIT, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

Set timing at Port LEDs and timer TIM3 exactly on its channel CH4 connected yellow LED board Mini64SER6

GPIO_InitTypeDef GPIO_InitStructure;

/* Configure pin in output push/pull mode */
GPIO_InitStructure.GPIO_Pin = (1 << BLINK_PIN);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(BLINK_PORT, &GPIO_InitStructure);

Initializes a blue LED output control

GPIO_InitStructure.GPIO_Pin = (1 << PWM_PIN); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(PWM_PORT, &GPIO_InitStructure);

Initializes the output control yellow LED type specified as the output of an alternative function.

TIM3->CCER |=TIM_CCER_CC4E; //Allow timer use CH4 pin

Allows the use of CH4 channel timer TIM3

TIM3->CCMR2|=( TIM_CCMR2_OC4M_0|TIM_CCMR2_OC4M_1|TIM_CCMR2_OC4M_2);//Select inverce PWM on CH4 pin

Establishes an inverted PWM channel CH4

TIM3->CR1 |= TIM_CR1_CEN;// Starting timer

Start the timer

int seconds = 0;
uint32_t pwm_arr[]={0,65000,60000,55000,50000,45000,40000,35000,20000,1000,500,0};//Array of brightness levels of the led

An array with the levels of brightness LED light

uint8_t i;
/* Infinite loop */
while (1)
{
GPIO_SetBits(BLINK_PORT, (1 << BLINK_PIN));
for (i=1;i<=11;i++) 
{ 
  TIM3->CCR4=pwm_arr[i];
  Delay(BLINK_TICKS);
}
GPIO_ResetBits(BLINK_PORT, (1 << BLINK_PIN));
++seconds;

Load the register TIM3-> CCR4 brightness value from the array in the forvard order blue LED flashes once per second

Delay(1000);
++seconds;

GPIO_SetBits(BLINK_PORT, (1 << BLINK_PIN)); for (i=11;i>=1;i--)
{
  TIM3->CCR4=pwm_arr[i];
  Delay(BLINK_TICKS);
}
GPIO_ResetBits(BLINK_PORT, (1 << BLINK_PIN));
++seconds;

Load the register TIM3-> CCR4 brightness values with the the array in order inversely

Delay(1000);
++seconds;

#if defined(DEBUG)
/*
* Count seconds on the debug channel.
*/
printf("Second %d\n", seconds);
#endif
}
}

That’s all. Here is video of the PWM working

Project in Eclipce Mini64SER6
Here is link that i used to wrote my article.

Leave a Reply