LCDGFX LCD display driver  1.1.5
This library is developed to control SSD1306/SSD1325/SSD1327/SSD1331/SSD1351/IL9163/PCD8554 RGB i2c/spi LED displays
lcd_backlight.hpp
Go to the documentation of this file.
1 // Copyright 2020 Kenta IDA
2 // LICENSE: Boost Software License
7 #ifndef LCD_BACKLIGHT_HPP__
8 #define LCD_BACKLIGHT_HPP__
9 
10 //#include <samd51p19a.h>
11 #include <cstdint>
12 
17 {
18 private:
19  std::uint8_t currentBrightness = 100;
20  std::uint8_t maxBrightness = 100;
21 public:
26  std::uint8_t getBrightness() const { return this->currentBrightness; }
31  std::uint8_t getMaxBrightness() const { return this->maxBrightness; }
32 
38  void setBrightness(std::uint8_t brightness)
39  {
40  this->currentBrightness = brightness < this->maxBrightness ? brightness : this->maxBrightness;
41  TC0->COUNT8.CC[0].reg = this->currentBrightness;
42  while(TC0->COUNT8.SYNCBUSY.bit.CC0);
43  }
49  void setMaxBrightness(std::uint8_t maxBrightness)
50  {
51  this->maxBrightness = maxBrightness;
52  if( this->currentBrightness > this->maxBrightness ) {
53  this->currentBrightness = this->maxBrightness;
54  }
55  TC0->COUNT8.PER.reg = this->maxBrightness;
56  while(TC0->COUNT8.SYNCBUSY.bit.PER);
57  TC0->COUNT8.CC[0].reg = this->currentBrightness;
58  while(TC0->COUNT8.SYNCBUSY.bit.CC0);
59  }
60 
65  void initialize()
66  {
67  /* Enable Peripheral Clocks */
68  GCLK->PCHCTRL[9].reg = 0 | (1u<<6); // TC0, TC1
69  while(!GCLK->PCHCTRL[9].bit.CHEN);
70  GCLK->PCHCTRL[11].reg = 0 | (1u<<6); // EVSYS[0]
71  while(!GCLK->PCHCTRL[11].bit.CHEN);
72  GCLK->PCHCTRL[33].reg = 0 | (1u<<6); // CCL
73  while(!GCLK->PCHCTRL[33].bit.CHEN);
74  /* Enable Peropheral APB Clocks */
75  MCLK->APBAMASK.bit.TC0_ = 1;
76  MCLK->APBBMASK.bit.EVSYS_ = 1;
77  MCLK->APBCMASK.bit.CCL_ = 1;
78 
79  /* Configure PORT */
80  PORT->Group[2].DIRSET.reg = (1<<5);
81  PORT->Group[2].EVCTRL.reg = 0x85; // PC05, OUT
82  /* Configure EVSYS */
83  EVSYS->USER[1].reg = 0x01; // Channel0 -> PORT_EV0
84  EVSYS->Channel[0].CHANNEL.reg = 0x74 | (0x02<<8) | (0x00<<10); // CCL_LUTOUT0, ASYNCHRONOUS, NO_EVT_OUTPUT
85  /* Configure CCL */
86  CCL->CTRL.reg = (1<<0); // SWRST
87  CCL->SEQCTRL[0].reg = 0x0; // Disable SEQCTRL
88  CCL->LUTCTRL[0].reg = (0xaau << 24) | (1u<<22) | (0x6<<8) | (1<<1); // TRUTH=0xaa, LUTEO, INSEL0=0x06(TC), ENABLE
89  CCL->CTRL.reg = (1<<1); // ENABLE
90 
91  /* Configure TC0 */
92  TC0->COUNT8.CTRLA.reg = (1u<<0); // SWRST;
93  while( TC0->COUNT8.SYNCBUSY.bit.SWRST );
94 
95  TC0->COUNT8.CTRLA.reg = (0x01 << 2) | (0x01 << 4) | (0x04 << 8); // MODE=COUNT8, PRESCALER=DIV16, PRESCSYNC=PRESC
96  TC0->COUNT8.WAVE.reg = 0x02; // WAVEGEN=NPWM;
97  TC0->COUNT8.CTRLBSET.reg = (1u<<1); // LUPD
98  TC0->COUNT8.PER.reg = this->maxBrightness;
99  TC0->COUNT8.CC[0].reg = this->currentBrightness;
100  TC0->COUNT8.CC[1].reg = 0u;
101  TC0->COUNT8.DBGCTRL.bit.DBGRUN = 1;
102  TC0->COUNT8.INTFLAG.reg = 0x33; // Clear all flags
103  while( TC0->COUNT8.SYNCBUSY.reg );
104 
105  TC0->COUNT8.CTRLA.bit.ENABLE = 1; // ENABLE
106  while( TC0->COUNT8.SYNCBUSY.bit.ENABLE );
107  }
108 };
109 #endif //LCD_BACKLIGHT_HPP__
void setBrightness(std::uint8_t brightness)
Sets brightness.
std::uint8_t getMaxBrightness() const
Gets maximum brightness.
void setMaxBrightness(std::uint8_t maxBrightness)
Sets maximum brightness.
void initialize()
Initialize hardware/peripherals to control back light brightness.
std::uint8_t getBrightness() const
Gets current brightness.
Controls Wio Terminal LCD back light brightness.