MCBSTM32C/Blinky simple with clock control and backlight dim
From Teknologisk videncenter
Add to "minimum gpio.h"
typedef unsigned short int uint16_t;
//General Purpose Timers
#define APB1PERIPH_BASE PERIPH_BASE
#define TIM3_BASE (APB1PERIPH_BASE + 0x0400)
typedef struct
{
__IO uint16_t CR1;
uint16_t RESERVED0;
__IO uint16_t CR2;
uint16_t RESERVED1;
__IO uint16_t SMCR;
uint16_t RESERVED2;
__IO uint16_t DIER;
uint16_t RESERVED3;
__IO uint16_t SR;
uint16_t RESERVED4;
__IO uint16_t EGR;
uint16_t RESERVED5;
__IO uint16_t CCMR1;
uint16_t RESERVED6;
__IO uint16_t CCMR2;
uint16_t RESERVED7;
__IO uint16_t CCER;
uint16_t RESERVED8;
__IO uint16_t CNT;
uint16_t RESERVED9;
__IO uint16_t PSC;
uint16_t RESERVED10;
__IO uint16_t ARR;
uint16_t RESERVED11[3];
__IO uint16_t CCR1;
uint16_t RESERVED12;
__IO uint16_t CCR2;
uint16_t RESERVED13;
__IO uint16_t CCR3;
uint16_t RESERVED14;
__IO uint16_t CCR4;
uint16_t RESERVED15[3];
__IO uint16_t DCR;
uint16_t RESERVED16;
__IO uint16_t DMAR;
uint16_t RESERVED17;
} TIM_GP_TypeDef;
#define TIM3 ((TIM_GP_TypeDef *) TIM3_BASE )
backlight.c
#include "minimum gpio.h"
/*
Example 2:
==========
Using timer 3 to Dim the Backlight in Display
Mode : Continues PWM mode to PB0. Section 15.3.9 page 370
For details see: http://mars.tekkom.dk/mediawiki/index.php/STM32F107VC/Using_MCBQVGA-TS-Display-v12
Caveats: Assumes TIMxCLK = 72 MHz (See RCC - Reset And Clock Control)
*/
void tim3init( void ) {
volatile int i;
i = GPIOB->CRL & 0xfffffff0;
GPIOB->CRL = i | 0xa;
//GPIOB->CRL |= 0xa; // Set PB0 as Alternate function Push-Pull
RCC->APB1ENR |= 2; //Enable timer 3 clock (RCC->APB1ENR bit 1 = 1)
TIM3->PSC = 72; // Prescaler: With timer 3 input clock 72 Mhz divide by 72 to 1 Mhz
TIM3->ARR = 1000; // Auto Reload Register set to 1000 divide 1Mhz/1000 = 1 Khz
TIM3->CCR3 = 100; //Compare Regisger for channel 3
TIM3->CCMR2 |= 7 << 4; //PWM mode 1
TIM3->CCMR2 |= 1 << 3;
TIM3->EGR |= 1; //Update generate
TIM3->CCER |= 1<<8; //Polarity
TIM3->CCER |= 1<<9;
TIM3->CR1 |= 1;
}
/* Change Display Backlight Intensity
input: 0 - 17 ( 0 = 0% backlight (All dark) to 17 = 100% backlight (Brigth)
Caveats: No errorcontrol
When 0 the timer should be turned off and the backlight turned off (PB0=0)
when 17 the timer should be turned off and the backlight turned on (PB0=1)
*/
void backlight( int intensity ) {
int disvalarr[] = {0,5,10,15,25,35,50,65,80,100,120,150,200,300,400,600,800,1000};
TIM3->CCR3 = disvalarr[intensity];
}
Last
Step 1
Make a backlight.h and include it in main.c
Step 2
- use the user button to increase backlight from 0 to 17 using the backlight() function
- use tamper button to decrease backlight from 17 to 0 using the backlight() function