2424#define DBG_LVL DBG_INFO
2525#include <rtdbg.h>
2626
27- #define LCD_PWR_PIN GET_PIN(B, 7)
2827#define LCD_DC_PIN GET_PIN(B, 4)
2928#define LCD_RES_PIN GET_PIN(B, 6)
3029
@@ -120,9 +119,6 @@ static void lcd_gpio_init(void)
120119 rt_pin_mode (LCD_DC_PIN , PIN_MODE_OUTPUT );
121120 rt_pin_mode (LCD_RES_PIN , PIN_MODE_OUTPUT );
122121
123- rt_pin_mode (LCD_PWR_PIN , PIN_MODE_OUTPUT );
124- rt_pin_write (LCD_PWR_PIN , PIN_LOW );
125-
126122 rt_pin_write (LCD_RES_PIN , PIN_LOW );
127123
128124 //wait at least 100ms for reset
@@ -133,8 +129,10 @@ static void lcd_gpio_init(void)
133129static int rt_hw_lcd_init (void )
134130{
135131 __HAL_RCC_GPIOD_CLK_ENABLE ();
132+
136133 rt_hw_spi_device_attach ("spi3" , "spi30" , GPIOD , GPIO_PIN_7 );
137134 lcd_gpio_init ();
135+
138136 /* Memory Data Access Control */
139137 lcd_write_cmd (0x36 );
140138 lcd_write_data (0x00 );
@@ -213,7 +211,7 @@ static int rt_hw_lcd_init(void)
213211 rt_thread_mdelay (100 );
214212
215213 /* display on */
216- rt_pin_write ( LCD_PWR_PIN , PIN_HIGH );
214+ lcd_display_on ( );
217215 lcd_write_cmd (0x29 );
218216
219217 return RT_EOK ;
@@ -236,27 +234,39 @@ void lcd_set_color(rt_uint16_t back, rt_uint16_t fore)
236234}
237235#endif /* BSP_USING_LVGL */
238236
237+ void lcd_display_brightness (rt_uint8_t percent )
238+ {
239+ struct rt_device_pwm * pwm_dev ;
240+
241+ pwm_dev = (struct rt_device_pwm * )rt_device_find ("pwm4" );
242+ if (pwm_dev != RT_NULL )
243+ {
244+ rt_pwm_set (pwm_dev , 2 , 1000000 , percent * 10000 ); /* PWM4 CH2 with 1000Hz */
245+ rt_pwm_enable (pwm_dev , 2 );
246+ }
247+ }
248+
239249void lcd_display_on (void )
240250{
241- rt_pin_write ( LCD_PWR_PIN , PIN_HIGH );
251+ lcd_display_brightness ( 100 );
242252}
243253
244254void lcd_display_off (void )
245255{
246- rt_pin_write ( LCD_PWR_PIN , PIN_LOW );
256+ lcd_display_brightness ( 0 );
247257}
248258
249259/* lcd enter the minimum power consumption mode and backlight off. */
250260void lcd_enter_sleep (void )
251261{
252- rt_pin_write ( LCD_PWR_PIN , PIN_LOW );
262+ lcd_display_off ( );
253263 rt_thread_mdelay (5 );
254264 lcd_write_cmd (0x10 );
255265}
256266/* lcd turn off sleep mode and backlight on. */
257267void lcd_exit_sleep (void )
258268{
259- rt_pin_write ( LCD_PWR_PIN , PIN_HIGH );
269+ lcd_display_on ( );
260270 rt_thread_mdelay (5 );
261271 lcd_write_cmd (0x11 );
262272 rt_thread_mdelay (120 );
0 commit comments