⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glcd_spi_stm32.c

📁 ARM_CORTEX-M3应用实例开发详解光盘
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************/
/* GLCD_SPI_STM32.c: STM32SPI接口底层液晶显示屏驱动程序(320x240 pixels)   */
/******************************************************************************/


#pragma diag_suppress=550

#include <stm32f10x_cl.h>
#include "GLCD.h"
#include "Font_24x16.h"

/*********************** SPI硬件接口配置 **********************/
/* SPI接口= SPI3      引脚配置: 		 
/*   - CS     = PB10 (GPIO pin)		 
/*   - RS     = GND
/*   - WR/SCK = PC10 (SPI3_SCK)
/*   - RD     = GND
/*   - SDO    = PC11 (SPI3_MISO)
/*   - SDI     = PC12 (SPI3_MOSI)                   
**************************************************************/


#define PIN_CS      (1 << 2)

/* SPI_SR寄存器位定义 */
#define RXNE        0x01
#define TXE         0x02
#define BSY         0x80

/*------------------------- 扫描速度设置 -------------------------*/

/* If processor works on high frequency delay has to be increased, it can be 
   increased by factor 2^N by this constant                                   */
#define DELAY_2N    18

/*---------------------- 图形LCD显示尺寸定义 ------------------------*/

#define WIDTH       320                 /* 显示宽度320*/
#define HEIGHT      240                 /* 显示高度240*/
#define BPP         16                  /* 16位/像素*/
#define BYPP        ((BPP+7)/8)         /* 字节/像素*/

/*--------------- 图形LCD模块硬件接口定义 -----------------*/

/*CS片选引脚 置'1'或'0'*/
#define LCD_CS(x)   ((x) ? (GPIOB->BSRR = PIN_CS) : (GPIOB->BRR = PIN_CS));

#define SPI_START   (0x70)              /* 开始发送 */
#define SPI_RD      (0x01)              /* WR bit 1 within start              */
#define SPI_WR      (0x00)              /* WR bit 0 within start              */
#define SPI_DATA    (0x02)              /* RS bit 1 within start byte         */
#define SPI_INDEX   (0x00)              /* RS bit 0 within start byte         */
 
/******************************************************************************/
static volatile unsigned short TextColor = Black, BackColor = White;

static void delay (int cnt) {

  cnt <<= DELAY_2N;
  while (cnt--);
}


/******************************************************************************
* 函数名  : spi_send()
* 描述    :SPI接口 发送一个字节
**************************************************************************/


static __inline unsigned char spi_send (unsigned char byte) {

  SPI3->DR = byte;
  while (!(SPI3->SR & RXNE));           /* 等待发送完成 */
  return (SPI3->DR);
}


/*******************************************************************************
*函数名:wr_cmd() 														 	*
*功能描述:向LCD控制器写入命令                                   *
*输入  :    c: 待写命令                                         *
*******************************************************************************/

static __inline void wr_cmd (unsigned char c) {

  LCD_CS(0)
  spi_send(SPI_START | SPI_WR | SPI_INDEX);   /* RS = 0, RW = 0       */
  spi_send(0);
  spi_send(c);
  LCD_CS(1)
}


/*******************************************************************************
* 函数名:wr_dat()
*功能描述:向LCD写数据                                               *
*输入: c:待写数据                                                   *
*******************************************************************************/

static __inline void wr_dat (unsigned short c) {

  LCD_CS(0)
  spi_send(SPI_START | SPI_WR | SPI_DATA);    /* 写操作RS = 1, RW = 0  */
  spi_send((c >>   8));                       /* 写高位D8..D15   */
  spi_send((c & 0xFF));                       /* 写低位D0..D7    */
  LCD_CS(1)
}


/*******************************************************************************
*函数名:wr_dat_start ()
*功能描述:开始向LCD写入数据*

*******************************************************************************/

static __inline void wr_dat_start (void) {

  LCD_CS(0)
  spi_send(SPI_START | SPI_WR | SPI_DATA);    /* 写操作 : RS = 1, RW = 0 */
}


/*******************************************************************************
*函数名:wr_dat_stop() 
*功能描述:停止向LCD写数据
*******************************************************************************/

static __inline void wr_dat_stop (void) {

  LCD_CS(1)
}


/*******************************************************************************
* 函数名:wr_dat_only()                                                    *
* 功能描述:写数据操作 ,向LCD写入待写数据                                  *
*******************************************************************************/

static __inline void wr_dat_only (unsigned short c) {

  spi_send((c >>   8));                       /* 写高8位D8..D15  */
  spi_send((c & 0xFF));                       /* 写低8位D0..D7   */
}


/*******************************************************************************
* 函数名:rd_dat()                                                            *
* 功能描述:读数据操作,从LCD读取数据.                                        *
*******************************************************************************/

static __inline unsigned short rd_dat (void) {
  unsigned short val = 0;

  LCD_CS(0)
  spi_send(SPI_START | SPI_RD | SPI_DATA);    /* 读操作: RS = 1, RW = 1  */
  spi_send(0);                                /* 空读*/
  val   = spi_send(0);                        /* 读高8位D8..D15 */
  val <<= 8;
  val  |= spi_send(0);                        /* 读低8位D0..D7  */
  LCD_CS(1)
  return (val);
}

/*******************************************************************************
*函数名:wr_reg()
*功能描述:写LCD寄存器操作                                                     
*输入: 待写寄存器,待写寄存器值    
*******************************************************************************/

static __inline void wr_reg (unsigned char reg, unsigned short val) {

  wr_cmd(reg);
  wr_dat(val);
}


/*******************************************************************************
*函数名:rd_reg()
*功能描述:读LCD寄存器                      *
*输入: 待读寄存器                           *
*返回: 寄存器值                             *
*******************************************************************************/

static unsigned short rd_reg (unsigned char reg) {

  wr_cmd(reg);
  return (rd_dat());
}



/*******************************************************************************
*函数名:GLCD_Init()
*功能描述:初始化LCD显示模块控制器ILI9325
*******************************************************************************/

void GLCD_Init (void) { 
  static unsigned short driverCode;

  /* 使能GPIOB/C,及复用I/O与SPI3接口时钟*/
  RCC->APB2ENR |= 0x00000019;
  RCC->APB1ENR |= 0x00008000;

  /* SPI3接口重映射(PC10~PC12)*/
  AFIO->MAPR   |= 0x10000000;

  /* 片选NCS=PB2, 推挽输出,置'1'*/
  GPIOB->CRH   &= 0xFFFFF0FF;
  GPIOB->CRH   |= 0x00000300;
  GPIOB->CRL   &= 0xFFFFF0F0;
  GPIOB->CRL   |= 0x00000303;
  GPIOB->BRR   |= 0x0004;
  GPIOB->BSRR   = 0x00000405;

  /* SPI3接口引脚SPI3_SCK, SPI3_MISO, SPI3_MOSI */
  GPIOC->CRH   &= 0xFFF000FF;
  GPIOC->CRH   |= 0x000B8B00;

  /* 使能SPI主模式, CPOL=1, CPHA=1*/
  SPI3->CR1     = 0x0347;
  SPI3->CR2     = 0x0000;

  delay(5);                             /* 延时50 ms                        */
  driverCode = rd_reg(0x00);
  //add by jing
  /* Start Initial Sequence --------------------------------------------------*/
  //wr_reg(0xE3, 0x3008);  
  //wr_reg(0xE7, 0x0012);  
  //wr_reg(0xEF, 0x1231); 
  /* 启动初始化序列 --------------------------------------------------*/
  wr_reg(0x01, 0x0100); /* 设置SS位*/
  wr_reg(0x02, 0x0700); /* 设置1行反转*/
  wr_reg(0x04, 0x0000); /* 重设计寄存器大小*/
  wr_reg(0x08, 0x0207); /* 2 lines front, 7 back porch        */
  wr_reg(0x09, 0x0000); /* Set non-disp area refresh cyc ISC  */
  wr_reg(0x0A, 0x0000); /* FMARK function                     */
  wr_reg(0x0C, 0x0000); /* RGB interface setting              */
  wr_reg(0x0D, 0x0000); /* Frame marker Position              */
  wr_reg(0x0F, 0x0000); /* RGB interface polarity             */

  /* 上电序列 -------------------------------------------------------*/
  wr_reg(0x10, 0x0000); /* 复位电源控制寄存器1*/
  wr_reg(0x11, 0x0000); /* 复位电源控制寄存器2*/
  wr_reg(0x12, 0x0000); /* 复位电源控制寄存器3*/
  wr_reg(0x13, 0x0000); /* 复位电源控制寄存器4*/
  delay(20);            /* 延时*/
  wr_reg(0x10, 0x12B0); /* SAP, BT[3:0], AP, DSTB, SLP, STB  */
  wr_reg(0x11, 0x0007); /* DC1[2:0], DC0[2:0], VC[2:0] */
  delay(5);             /* 延时 50 ms */
  wr_reg(0x12, 0x01BD); /* VREG1OUT电压*/
  delay(5);             /* Delay 50 ms */
  wr_reg(0x13, 0x1400); /* VDV[4:0] VCOM振幅*/
  wr_reg(0x29, 0x000E); /* VCM[4:0] VCOMH  */
  delay(5);             /* Delay 50 ms */
  wr_reg(0x20, 0x0000); /* 显存横坐标*/
  wr_reg(0x21, 0x0000); /* 显存纵坐标*/

  /* 伽玛曲线调整 */
  if (driverCode == 0x5408) {           
    wr_reg(0x30, 0x0B0D);
    wr_reg(0x31, 0x1923);
    wr_reg(0x32, 0x1C26);
    wr_reg(0x33, 0x261C);
    wr_reg(0x34, 0x2419);
    wr_reg(0x35, 0x0D0B);
    wr_reg(0x36, 0x1006);
    wr_reg(0x37, 0x0610);
    wr_reg(0x38, 0x0706);
    wr_reg(0x39, 0x0304);
    wr_reg(0x3A, 0x0E05);
    wr_reg(0x3B, 0x0E01);
    wr_reg(0x3C, 0x010E);
    wr_reg(0x3D, 0x050E);
    wr_reg(0x3E, 0x0403);
    wr_reg(0x3F, 0x0607);
  }
  if (driverCode == 0xC990) {                    
    wr_reg(0x30, 0x0006);
    wr_reg(0x31, 0x0101);
    wr_reg(0x32, 0x0003);
    wr_reg(0x35, 0x0106);
    wr_reg(0x36, 0x0B02);
    wr_reg(0x37, 0x0302);
    wr_reg(0x38, 0x0707);
    wr_reg(0x39, 0x0007);
    wr_reg(0x3C, 0x0600);
    wr_reg(0x3D, 0x020B);
  }

 /* 设置显存区 -----------------------------------------------------------*/
  wr_reg(0x50, 0x0000);                 /* 水平开始坐标*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -