51驱动1.8寸TFT屏,H018IN01V8(NT3915)

作者分享了如何使用51单片机成功驱动并点亮一台老式联想手机的彩屏,详细介绍了代码实现及初始化过程。

闲着没事干,把以前老式联想手机给拆了,取出彩屏用51成功驱动并点亮。

<p>/* \file main.c - Keil C v8.02
 * \brief source file for lcd_h018in01 project
 *   Project id: 00595cf9-8de6-4a57-b940-eb0347ac9e13
 *
 * \details This file is part of the lcd_h018in01 project.
 *
 *
 * Developed by RD BIOS team.
 *   \author perry.peng *
 * History:
 *   Date        Author      Description
 *   -------------------------------------------------------------
 *   2013-01-06  Perry       Initial created.
 *
 * Note:
 * This source code can be used, modified, and redistributed under the
 * terms of the license agreement that is included in the lcd_h018in01 package
 * By continuing to use, modify, or redistributed this code you indicate
 * that you have read the license and understand and accept it fully.
 */</p><p> </p>
#include "main.h"

/**<  Physical specifications for H018IN01
 * Display method:            Active matrix TFT
 * Display mode:              Transmissive type
 * Display resolution(dot):   128 x 3(V) x 160(H)
 * Active area(mm):           35.04(V) x 28.032(H)
 * Screen size(inch):         1.8(Diagonal)
 * Pixel pitch(mm):           0.219(V) x 0.219(H)
 * Color configuration:       R.G.B. strip
 * Display color:             8/16 bits, 65K colors
 * Surface treatment:         Hard Coating
 * Light technology:          3 pcs LED
 * Overall dimension(mm):     34(W) x 46.7(H) x 3.55(D)
 * View Angle:                12 o'clock
 * Weight(g):                 9 +/- 0.2
 * Driver IC:                 NT3915
 */

#define H018IN01_MAX_HR                 128
#define H018IN01_MAX_VR                 160

#define RGB86(c)                        (0x3f - ((c) >> 2))
#define RGB85(c)                        (RGB86(c) >> 1)
#define RGB(r,g,b)                      (uint16)((RGB85(b) << 11) | (RGB86(g) << 5) | RGB85(r))

#define COLOR_BLACK                     RGB(0, 0, 0)
#define COLOR_RED                       RGB(0xff, 0, 0)
#define COLOR_GREEN                     RGB(0, 0xff, 0)
#define COLOR_BLUE                      RGB(0, 0, 0xff)
#define COLOR_WHITE                     RGB(0xff, 0xff, 0xff)
#define COLOR_YELLOW                    RGB(0xff, 0xff, 0)

#define NT3915_GET_DEVICE_CODE          0x00

#define NT3915_SET_ENTRY_MODE           0x05
#define NT3915_SET_DISPLAY_CTRL         0x07

#define NT3915_SET_POWER_CTRL1          0x03
#define NT3915_SET_POWER_CTRL2          0x50
#define NT3915_SET_POWER_CTRL3          0x0c
#define NT3915_SET_POWER_CTRL4          0x0d
#define NT3915_SET_POWER_CTRL5          0x0e

#define NT3915_SET_OSC_START            0x00
#define NT3915_SET_DRIVER_OUT_CTRL      0x01
#define NT3915_SET_WAVEFORM_CTRL        0x02
#define NT3915_SET_COMPARE_REG          0x06
#define NT3915_SET_FRAME_CYCLE_CTRL     0x0b
#define NT3915_SET_GATE_SCAN_POS        0x0f
#define NT3915_SET_VSCROLL_CTRL         0x11

#define NT3915_SET_WIN_HORIZONTAL       0x16
#define NT3915_SET_WIN_VERTICAL         0x17
#define NT3915_SET_DATA_MASK            0x20
#define NT3915_SET_GRAM_ADDR            0x21
#define NT3915_WRITE_GRAM               0x22
#define NT3915_READ_GRAM                0x22

#define NT3915_SET_GAMMA_CTRL1          0x30
#define NT3915_SET_GAMMA_CTRL2          0x31
#define NT3915_SET_GAMMA_CTRL3          0x32
#define NT3915_SET_GAMMA_CTRL4          0x33
#define NT3915_SET_GAMMA_CTRL5          0x34
#define NT3915_SET_GAMMA_CTRL6          0x35
#define NT3915_SET_GAMMA_CTRL7          0x36
#define NT3915_SET_GAMMA_CTRL8          0x37
#define NT3915_SET_GAMMA_CTRL9          0x3a
#define NT3915_SET_GAMMA_CTRL10         0x3b

#define NT3915_SET_PWM_CTRL1            0x3d
#define NT3915_SET_PWM_CTRL2            0x3e
#define NT3915_SET_FSM_CTRL             0x3f

sbit lcd_cs = LCD_CS_PIN; // 片选
sbit lcd_rs = LCD_RS_PIN; //
sbit lcd_rd = LCD_RD_PIN;
sbit lcd_wr = LCD_WR_PIN;
sbit lcd_rst= LCD_RST_PIN;

sbit led_stb = P3^3;
sbit led_clk = P3^2;
sbit led_dio = P1^7;

uint16 text_back_color = COLOR_BLACK; // the default background color.
uint16 text_fore_color = COLOR_WHITE; // the default fore color.


 

/** disassembly code for lcd_ir_set
    8F??        MOV   80H, #IMM     CY = 24
    C2B6        CLR   0B6H          CY = 12
    C2B5        CLR   0B5H          CY = 12
    D2B5        SETB  0B5H          CY = 12
 */
#define lcd_write_cmd(c)  \
  LCD_DATA1_PIN = (c);   /**< 指令仅低8位有效。*/ \  
  /* LCD_DATA2_PIN = 0; */ \
  lcd_rs = 0;  /**< 选择命令模式。*/ \            
  lcd_wr = 0; \
  lcd_wr = 1    /**< NT3915(80 SYSTEM) WR上升沿写入数据。*/

/** disassembly code for lcd_write
    8F80        MOV   80H, #IMM     CY = 24
    8EA0        MOV   0A0H, #IMM    CY = 24
    D2B6        SETB  0B6H          CY = 12
    C2B5        CLR   0B5H          CY = 12
    D2B5        SETB  0B5H          CY = 12
 */
#define lcd_write_data(x)  \
  LCD_DATA1_PIN = (uint8)(x);  \
  LCD_DATA2_PIN = (uint8)((x) >> 8); \
  lcd_rs = 1;  /**< 选择数据模式。*/ \             
  lcd_wr = 0;   \
  lcd_wr = 1    /**< NT3915(80 SYSTEM) WR上升沿写入数据。*/

#define lcd_power_ctrl1(bt, dc, ap, slp, stb)  \
  lcd_send_cmd(NT3915_SET_POWER_CTRL1, (uint16)(((bt) & 7) << 8) | (((dc) & 7) << 5) | (((ap) & 7) << 2) | \
                                        (((slp) & 1) << 1) | ((stb) & 1))

#define lcd_power_ctrl2(opf)  \
  lcd_send_cmd(NT3915_SET_POWER_CTRL2, (uint16)((opf) & 1))

#define lcd_power_ctrl3(vc)  \
  lcd_send_cmd(NT3915_SET_POWER_CTRL3, (uint16)((vc) & 7))

#define lcd_power_ctrl4(pon, vrl, vrh)  \
  lcd_send_cmd(NT3915_SET_POWER_CTRL4, (uint16)(((vrl) & 0x0e) << 8) | (((pon) & 1) << 4) | ((vrh) & 0X0E))

#define lcd_power_ctrl5(vcom, vdv, vcm)  \
  lcd_send_cmd(NT3915_SET_POWER_CTRL5, (uint16)(((vcom) & 1) << 0X0D) | (((vdv) & 0X1F) << 8) | ((vcm) & 0X1F))

#define lcd_set_entry_mode(bgr, hwm, id, am, lg)  \
  lcd_send_cmd(NT3915_SET_ENTRY_MODE, (uint16)(((bgr) & 1) << 0x0c) | (((hwm) & 1) << 9) | (((id) & 3) << 4) | \
                                        (((am) & 1) << 3) | ((lg) & 0x7))

#define lcd_display_ctrl(pt, vle, spt, gon, dte, cl, rev, d)  \
  lcd_send_cmd(NT3915_SET_DISPLAY_CTRL, (uint16)(((pt) & 2) << 0x0b) | (((vle) & 3) << 9) | (((spt) & 1) << 8) | \
                                        (((gon) & 1) << 5) | (((dte) & 1) << 4) | (((cl) & 1) << 3) | \
                                        (((rev) & 1) << 2) | ((d) & 3))

#define lcd_driver_out_ctrl(sm, gs, ss, nl) \
  lcd_send_cmd(NT3915_SET_DRIVER_OUT_CTRL, (uint16)(((sm) & 1) << 0x0a) | (((gs) & 1) << 9) | (((ss) & 1) << 8) | \
                                        ((lg) & 0x1f))

#define set_gamma_ctrl(id, b, x1, x2)  lcd_send_cmd((id), (uint16)(((x1) & (b)) << 8) | ((x2) & (b)))

#define lcd_gamma_ctrl1(pkp1, pkp0)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL1, 7, (pkp1), (pkp0))

#define lcd_gamma_ctrl2(pkp3, pkp2)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL2, 7, (pkp3), (pkp2))

#define lcd_gamma_ctrl3(pkp5, pkp4)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL3, 7, (pkp5), (pkp4))

#define lcd_gamma_ctrl4(prp1, prp0)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL4, 7, (prp1), (prp0))

#define lcd_gamma_ctrl5(pkn1, pkn0)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL5, 7, (pkn1), (pkn0))

#define lcd_gamma_ctrl6(pkn3, pkn2)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL6, 7, (pkn3), (pkn2))

#define lcd_gamma_ctrl7(pkn5, pkn4)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL7, 7, (pkn5), (pkn4))

#define lcd_gamma_ctrl8(prn1, prn0)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL8, 7, (prn1), (prn0))

#define lcd_gamma_ctrl9(vrp1, vrp0)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL9, 0x1f, (vrp1), (vrp0))

#define lcd_gamma_ctrl10(vrn1, vrn0)  \
  set_gamma_ctrl(NT3915_SET_GAMMA_CTRL10, 0x1f, (vrn1), (vrn0))

#define set_back_color(c)   text_back_clr = (c)
#define set_fore_color(c)   text_fore_clr = (c)


复位TFT。

 /**<Bits for reset.
   * Driver output control
   * NL[4-0]    = 1010      -->
   * SS         = 0
   * GS         = 0
   *
   * LCD driving AC control
   * FLD[1-0]   = 01
   * B/C        = 0
   * EOR        = 0
   * NW[5-0]    = 00000
   *
   * Power control 1
   * BT[2-0]    = 000
   * DC[2-0]    = 000
   * AP[2-0]    = 000
   * SLP        = 0
   * STB        = 0
   *
   * Power control 2
   * OPF        = 0
   *
   * Entry mode
   * HWM        = 0
   * I/D[1-0]   = 11
   * AM         = 0
   * LG[2-0]    = 000
   * BGR        = 0
   *
   * Compare register
   * CP[15-0]   = 0
   *
   * Display control 1
   * PT[1-0]    = 00
   * VLE[2-1]   = 00
   * SPT        = 0
   * GON        = 0
   * DTE        = 0
   * CL         = 0
   * REV        = 0
   * D[1-0]     = 00        --> R07H[0-1]
   *
   * Frame cycle control
   * NO[1-0]    = 00
   * SDT[1-0]   = 00
   * EQ[1-0]    = 00
   * DIV[1-0]   = 00
   * RTN[3-0]   = 0000
   *
   * Power control 3
   * VC[2-0]    = 000       --> R11H[0-3]
   *
   * Power control 4
   * VRL[3-0]   = 0000
   * PON        = 0         --> R13H[2]
   * VRH[3-0]   = 0000
   *
   * Power control 5
   * VCOMG      = 0
   * VDV[4-0]   = 00000
   * VCM[4-0]   = 00000
   *
   * RAM address set
   * AD[15-0]   = 0
   *
   * RAM write data mask
   * WM[15-0]   = 0
   *
   * Gamma control
   * PKP0[2-0]  = 0
   * PKP1[2-0]  = 0
   * PKP2[2-0]  = 0
   * PKP3[2-0]  = 0
   * PKP4[2-0]  = 0
   * PKP5[2-0]  = 0
   * PRP0[2-0]  = 0
   * PRP1[2-0]  = 0
   * PKN0[2-0]  = 0
   * PKN1[2-0]  = 0
   * PKN2[2-0]  = 0
   * PKN3[2-0]  = 0
   * PKN4[2-0]  = 0
   * PRN0[2-0]  = 0
   * PRN1[2-0]  = 0
   * VRP0[4-0]  = 00000
   * VRP1[4-0]  = 00000
   * VRN0[4-0]  = 00000
   * VRN1[4-0]  = 00000
   *
   * Gate scan start position
   * SCN[4-0]   = 00000
   *
   * Vertical scroll
   * VL[7-0]    = 00000000
   *
   * 1st screen division
   * SE[17-10]  = 11111111
   * SS[17-10]  = 00000000
   *
   * 2nd screen division
   * SE[27-20]  = 11111111
   * SS[27-20]  = 00000000
   *
   * Horizontal RAM addr
   * HEA[7-0]   = 10000011
   * HSA[7-0]   = 00000000
   *
   * Vertical RAM addr
   * VEA[7-0]   = 10101111
   * VSA[7-0]   = 00000000
   *
   * PWM control 1
   * VFB[2-0]   = 000
   *
   * PWM control 2
   * FPWM[2-0]  = 010
   * DPWM[2-0]  = 110
   *
   * FSM control
   * CFSM       = 0000
   * IBZ        = 1
   * EFSM       = 1
   * VR1C[3-0]  = 1100      --> R13H[8-11]
   */
  lcd_reset();


初始化屏,完成以下初始化步骤之后就可以看到屏幕亮了,只是显示的是乱七八糟的,这是因为此屏在上电复位时不会自动初始化GRAM中的内容。所以需要自己在显示打开前清屏。

  /**< 2, Sets bits for Power supply setting(1)
   * Bits for power-supply initial setting:
   * VCOM     = 0           --> R0EH[13]
   * VCM[4-0] = 0001        --> R0EH[0-4]
   * VDV[4-0] = 1001        --> R0EH[8-12]
   * VC[2-0]  = 000         --> R0CH[0-2]
   * VRH[3-0] = 1000        --> R0DH[0-3]
   * VRN[4-0] = 0000        --> R3BH[0-4], [8-12]
   * VRP[4-0] = 0000        --> R3AH[0-4], [8-12]
   * setting of the source-driver grayscale voltage.
   */
  lcd_power_ctrl3(/*VC=*/0);
  lcd_power_ctrl4(/*PON=*/0, /*VRL=*/0, /*VRH=*/8);
  lcd_power_ctrl5(/*VCOM=*/0, /*VDV=*/9, /*VCM=*/1);

  lcd_gamma_ctrl1(/*PKP1=*/0, /*PKP0=*/0);
  lcd_gamma_ctrl2(/*PKP3=*/0, /*PKP2=*/0);
  lcd_gamma_ctrl3(/*PKP5=*/0, /*PKP4=*/0);
  lcd_gamma_ctrl4(/*PRP1=*/0, /*PRP0=*/0);
  lcd_gamma_ctrl7(/*PKN1=*/0, /*PKN0=*/0);
  lcd_gamma_ctrl5(/*PKN3=*/0, /*PKN2=*/0);
  lcd_gamma_ctrl6(/*PKN5=*/0, /*PKN4=*/0);
  lcd_gamma_ctrl8(/*PRN1=*/0, /*PRN0=*/0);
  lcd_gamma_ctrl9(/*VRP1=*/0, /*VRP0=*/0);
  lcd_gamma_ctrl10(/*VRN1=*/0, /*VRN0=*/0);

  /**< 3, Issues instruction for power supply setting(2)
   * Bits for power-supply operation start setting:
   * BT[2-0] = 000          --> R03H[8-10]
   * DC[2-0] = 000          --> R03H[5-7]
   * AP[2-0] = 011          --> R03H[2-4]
   */
  lcd_power_ctrl1(/*BT=*/0, /*DC=*/0, /*AP=*/3, /*SLP=*/0, /*STB=*/0);

  // Wait about 50ms.
  delay_xms(50);

  /**< 4, Issues instruction for power supply setting(3)
   * Bits for charge-pump circuir 3 operation start
   * PON = 1                --> R0DH[4]
   */
  lcd_power_ctrl4(/*PON=*/1, /*VRL=*/0, /*VRH=*/8);

  // Wait about 200ms.
  delay_xms(200);

  /**< 5, Issues instruction for other mode setting. */
  lcd_set_entry_mode(/*BGR=*/0, /*HWM=*/0, /*ID=*/3, /*AM=*/1, /*LG=*/0);//AM等于1横着显示。

  /**< 6, Display on(1)
   * GON = 0                --> R07H[5]
   * DTE = 0                --> R07H[4]
   * D[1-0] = 01            --> R07H[0-1]
   */
  lcd_display_ctrl(/*PT=*/0, /*VLE=*/0, /*SPT=*/0, /*GON=*/0, /*DTE=*/0, /*CL=*/0, /*REV=*/0, /*D=*/1);

  // Wait about 20ms.
  delay_xms(20);

  /**< 7, Display on(2)
   * GON = 1                --> R07H[5]
   * DTE = 0                --> R07H[4]
   * D[1-0] = 01            --> R07H[0-1]
   */
  lcd_display_ctrl(/*PT=*/0, /*VLE=*/0, /*SPT=*/0, /*GON=*/1, /*DTE=*/0, /*CL=*/0, /*REV=*/0, /*D=*/1);

  /**< 8, Display on(3)
   * GON = 1                --> R07H[5]
   * DTE = 0                --> R07H[4]
   * D[1-0] = 11            --> R07H[0-1]
   */
  lcd_display_ctrl(/*PT=*/0, /*VLE=*/0, /*SPT=*/0, /*GON=*/1, /*DTE=*/0, /*CL=*/0, /*REV=*/0, /*D=*/3);

  // Wait about 20ms.
  delay_xms(20);

  /**< x, Display on.
   * Bits for display on:
   * GON = 1                --> R07H[5]
   * DTE = 1,               --> R07H[4]
   * D[1-0] = 11,           --> R07H[0-1]
   */
  lcd_display_ctrl(/*PT=*/0, /*VLE=*/0, /*SPT=*/0, /*GON=*/1, /*DTE=*/1, /*CL=*/0, /*REV=*/0, /*D=*/3);

  // Wait about 100ms.
  delay_xms(100);


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值