单片机 > 单片机程序设计 > 详情

stm32 12864驱动

发布时间:2020-08-26 发布时间:
|

/****************************************************************
【文 件 名 称】lcd12864.h
【功 能 描 述】lcd12864 头文件
*****************************************************************/

#ifndef __LCD12864_H
#define __LCD12864_H

//****************************************************************
#include "stm32f10x_gpio.h"
#include "systick_delay.h"

//*****************************************************************
//管脚定义

#define GPIO_LCD    GPIOE  
#define RCC_APB2Periph_GPIO_LCD    RCC_APB2Periph_GPIOE
//片选
#define LCD_CS_1    GPIO_LCD->BSRR = GPIO_Pin_8
#define LCD_CS_0    GPIO_LCD->BRR = GPIO_Pin_8
//数据
#define LCD_SID_1   GPIO_LCD->BSRR = GPIO_Pin_9
#define LCD_SID_0   GPIO_LCD->BRR = GPIO_Pin_9
//同步时钟
#define LCD_CLK_1   GPIO_LCD->BSRR = GPIO_Pin_10
#define LCD_CLK_0   GPIO_LCD->BRR = GPIO_Pin_10
//复位 低电平复位
#define LCD_RET_1   GPIO_LCD->BSRR = GPIO_Pin_11
#define LCD_RET_0   GPIO_LCD->BRR = GPIO_Pin_11

#define LCD_Write_Con_Cmd    (uc32)0xf8000000 // 1111 1000 0000 0000 0000 0000 0000 0000
#define LCD_Write_Dis_Data   (uc32)0xfa000000 // 1111 1010 0000 0000 0000 0000 0000 0000

//********************函数声明************************************
void LCD_Port_Config(void);
void LCD_Init(void);
void LCD_Write(u32 inst, u8 ddata);
void LCD_Dis_Str(u8 row, u8 col, u8 *str);
void LCD_Dis_Digital(u8 row, u8 col,u32 Dig_Data);
void LCD_Clear(void);
void LCD_Reset(void);
void LCD_Dis_Frame(void);
void LCD_Clear_GDRAM(void);
void LCD_Clear_Graphics(u8 row, u8 col, u8 row_Pixel, u8 col_Pixel);
void LCD_Dis_Graphics(u8 row, u8 col, u8 row_Pixel, u8 col_Pixel, u8 *Dis_Data);
/*****************************************************************
         液晶模块指令集定义
*****************************************************************
      0x01      //清显示指令
      0x06      //设置输入模式
      0x0c      //设置开显控制
      0x30      //功能设定(基本指令)
      0x34      //功能设定(扩充指令)
      0x36      //打开绘图(扩充指令)
*****************************************************************/

#endif

/****************************************************************************
【文 件 名 称】lcd12864.c
【功 能 描 述】lcd12864 驱动
【 作      者 】shifu
****************************************************************************/

/**************************************************************************/
#include "lcd12864.h"
#include "stm32f10x_lib.h"

/****************************************************************************
【功能说明】I/O端口功能、方向设定
****************************************************************************/
void LCD_Port_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

/* 使能端口时钟*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LCD, ENABLE);

/* 配置所用管脚为推免输出,端口速度为50MHz*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIO_LCD, &GPIO_InitStructure);    
}

/****************************************************************************
【功能说明】 LCD写字节函数
入口参数    inst=cmd/data: 命令/数据标志(data:写显示数据 cmd:写控制指令)
                         x : 命令/数据字节
****************************************************************************/

void LCD_Write(u32 inst, u8 ddata)
{
u32 temp = inst;
u32 i,t;
temp |= ((u32)(ddata & (u8)0xf0) << 16) + ((u32)(ddata & (u8)0x0f) << 12);
LCD_CS_1;    //选中12864
//拉低CLK引脚,准备产生上升沿
LCD_CLK_0;   
//串行传送24个二进制位
for(i=0;i<24;i++)                  
   {     
      if(temp & 0x80000000) LCD_SID_1;   
      else                  LCD_SID_0;
     
      //拉高CLK引脚,产生一个上升沿,最高位被传送到LCD模块中
      LCD_CLK_1;  
      t = 0x10; while(t--);   //延时 lcd读取数据
     
       //拉低CLK引脚,准备产生上升沿
      LCD_CLK_0;                    
    
      //左移一位,准备下一个待传送的位
      temp = temp<<1;
   }
LCD_CS_0; //取消选中12864
}

/****************************************************************************
【功能说明】
   LCD字符串显示函数:void LCD_Dis_Str(u8 x, u8 y, u8 *str);
   入口参数 row:   字符串起始显示位置的行
             col:   字符串起始显示位置的列
             str:   指向待显示字符串的字符指针,字符串以'\0'结尾
注:CGRAM 与中文字型的编码只能出现在adress counter 的起始位
DDRAM 8*16 字节的空间
Line1 80H 81H 82H 83H 84H 85H 86H 87H
Line2 90H 91H 92H 93H 94H 95H 96H 97H
Line3 88H 89H 8AH 8BH 8CH 8DH 8EH 8FH
Line4 98H 99H 9AH 9BH 9CH 9DH 9EH 9FH

Line5 A0H A1H A2H A3H A4H A5H A6H A7H
Line6 B0H B1H B2H B3H B4H B5H B6H B7H
Line7 A8H A9H AAH ABH ACH ADH AEH AFH
Line8 B8H B9H BAH BBH BCH BDH BEH BFH
****************************************************************************/
void LCD_Dis_Str(u8 row, u8 col, u8 *str)
{
    u8 addr, i = 0;
    //防止误操作(调试)
//    if(row > 3) row = 3;
//    if(col > 7) col = 7;
   
    //根据x,y坐标确定显示缓冲区地址
    switch(row)                                    
     {
        case 0: addr = 0x80 + col; break;
        case 1: addr = 0x90 + col; break;
        case 2: addr = 0x88 + col; break;
        case 3: addr = 0x98 + col; break;
     }
   
//设置DDRAM地址
LCD_Write(LCD_Write_Con_Cmd, addr);                                           

while(*str)
   {          
     //下一行:从设置好的DDRAM地址开始写字符并准备好指向下一个字符的指针
      LCD_Write(LCD_Write_Dis_Data, *str++);
      i++;
      if(i==2)
       {
         i = 0;
        
          //每写俩字节DDRAM地址会自动增量,跟踪DDRAM的地址变化,以便换行调整
         addr++;
        
         //当addr=88H,90H,98H,a0H时,说明DRAM地址需要重新指定
         if( (addr&0x07) == 0 )
          {
            switch(addr)
             {
                case 0x88: addr = 0x90;   break;
                case 0x98: addr = 0x88;   break;
                case 0x90: addr = 0x98;   break;
                case 0xa0: addr = 0x80;   break;
             }
            //设置被重新指定了的DDRAM地址
            LCD_Write(LCD_Write_Con_Cmd, addr);                  
          }
       }
   }
}


/****************************************************************************
【功能说明】 LCD 清屏
****************************************************************************/
void LCD_Clear(void)
{
   LCD_Write(LCD_Write_Con_Cmd, 0x01);
   ST_Delay_Ms(5);
}

/****************************************************************************
【功能说明】 LCD 重启
****************************************************************************/
void LCD_Reset(void)
{
   LCD_RET_0;
   ST_Delay_Ms(50);
   LCD_RET_1;
   ST_Delay_Ms(100);
}

/****************************************************************************
【功能说明】 初始化LCD子程序
****************************************************************************/
void LCD_Init(void)  
{
   //端口配置
   LCD_Port_Config();
  
   //LCD复位
   LCD_Reset();
  
   //基本指令集 8位数据
   LCD_Write(LCD_Write_Con_Cmd,0x30);
   ST_Delay_Ms(1);
   //基本指令集 8位数据
   LCD_Write(LCD_Write_Con_Cmd,0x30);
   ST_Delay_Ms(1);
  
   //显示打开,光标关,反白关
   LCD_Write(LCD_Write_Con_Cmd,0x0C);
   ST_Delay_Ms(1);
    //清屏
   LCD_Clear();
  
   //DDRAM 的地址计数器(AC)加1
   LCD_Write(LCD_Write_Con_Cmd,0x06);
                        
}
/******************************************************************************
【功能说明】 显示十进制数
   入口参数 row: 行
             col: 列
      Dig_Data:显示的十进制数
******************************************************************************/
void LCD_Dis_Digital(u8 row, u8 col,u32 Dig_Data)
{
u8 dd[11];
u8 i,j=9;
u32 temp;
temp = Dig_Data;
dd[10]=0;
for(i=10;(i>j)&(i>0);i--)
   {
     dd[i-1] = temp%10 + '0';
     temp=temp/10;
     if(temp) j--;
   }
LCD_Dis_Str(row,col,&dd[i]);
}
/****************************************************************************
【功能说明】 LCD显示边框 图形模式
****************************************************************************/
void LCD_Dis_Frame(void)
{
u8 x,y;
//LCD清除绘图RAM
LCD_Clear_GDRAM();
LCD_Write(LCD_Write_Con_Cmd,0x34);
LCD_Write(LCD_Write_Con_Cmd,0x36);
for(x=0;x<9;x += 8)
   {
      for(y=0;y < 32;y++)
          {   
            //左竖
            LCD_Write(LCD_Write_Con_Cmd, y+0x80);//行地址
            LCD_Write(LCD_Write_Con_Cmd, x+0x80);//列地址
            LCD_Write(LCD_Write_Dis_Data,0x80);
            LCD_Write(LCD_Write_Dis_Data,0x00 );
            //右竖
            LCD_Write(LCD_Write_Con_Cmd, y+0x80);//行地址
            LCD_Write(LCD_Write_Con_Cmd, x+0x87);//列地址
            LCD_Write(LCD_Write_Dis_Data,0x00);
            LCD_Write(LCD_Write_Dis_Data,0x01);       
         }
    }
for(y=0;y<2;y++)
    {
       for(x=0;x<8;x++)
           {
            LCD_Write(LCD_Write_Con_Cmd, y*31+0x80);//行地址
            LCD_Write(LCD_Write_Con_Cmd, x+0x80+8*y);//列地址            
            LCD_Write(LCD_Write_Dis_Data,0xff);
            LCD_Write(LCD_Write_Dis_Data,0xff);
           }
     }
LCD_Write(LCD_Write_Con_Cmd,0x30);
}
/****************************************************************************
【功能说明】 LCD清除全部绘图RAM GDRAM   64*32字节空间
              0 1 2****13 14 15
              1****************
              2****************
              *****************
              *
              *
              62
              63
****************************************************************************/
void LCD_Clear_GDRAM(void)
{
   u8 x,y;
   LCD_Write(LCD_Write_Con_Cmd,0x34);
   for(y=0;y<64;y++)
    {
      for(x=0;x<16;x++)
         {
            LCD_Write(LCD_Write_Con_Cmd, y+0x80);//行地址
            LCD_Write(LCD_Write_Con_Cmd, x+0x80);//列地址          
            LCD_Write(LCD_Write_Dis_Data,0x00);
            LCD_Write(LCD_Write_Dis_Data,0x00);
         }
     }
   LCD_Write(LCD_Write_Con_Cmd,0x30);
}
/******************************************************************************
【功能说明】 在指定行列显示自定义图形(定义为 64X8像素)            
【入口参数】 u8 row: 行坐标 取值范围(0-63)
            u8 col: 列坐标 取值范围(0-7)
      u8 row_Pixel: 行像素个数 取值范围(1-64)
      u8 col_Pixel: 列像素个数 取值范围(1-8)
      u8 *Dis_Data: 显示数据指针
【注意事项】      : 行坐标加行偏移不能超过 63
                     列坐标加列偏移不能超过 7
******************************************************************************/
void LCD_Dis_Graphics(u8 row, u8 col, u8 row_Pixel, u8 col_Pixel, u8 *Dis_Data)
{
u8 r,c,r_count,c_count;
//打开绘图模式
LCD_Write(LCD_Write_Con_Cmd,0x34);
LCD_Write(LCD_Write_Con_Cmd,0x36);
for(r_count = row; r_count < row + row_Pixel; r_count++)
   {
       if(r_count > 31) r = r_count - 32;
       else              r = r_count;    
       for(c_count = col; c_count < col + col_Pixel; c_count++)
        {
             if(r_count > 31) c = c_count + 8;
             else              c = c_count;
             LCD_Write(LCD_Write_Con_Cmd, r+0x80);//行地址
             LCD_Write(LCD_Write_Con_Cmd, c+0x80);//列地址        
             LCD_Write(LCD_Write_Dis_Data,*Dis_Data++);
             LCD_Write(LCD_Write_Dis_Data,*Dis_Data++);
            
      }
   }
LCD_Write(LCD_Write_Con_Cmd,0x30);
}
/******************************************************************************
【功能说明】 在指定行列清除自定义图形(定义为 64X8像素)            
【入口参数】 u8 row: 行坐标 取值范围(0-63)
            u8 col: 列坐标 取值范围(0-7)
      u8 row_Pixel: 行像素个数 取值范围(1-64)
      u8 col_Pixel: 列像素个数 取值范围(1-8)
【注意事项】      : 行坐标加行偏移不能超过 63
                     列坐标加列偏移不能超过 7
******************************************************************************/
void LCD_Clear_Graphics(u8 row, u8 col, u8 row_Pixel, u8 col_Pixel)
{
u8 r,c,r_count,c_count;
//扩充指令
LCD_Write(LCD_Write_Con_Cmd,0x34);

for(r_count = row; r_count < row + row_Pixel; r_count++)
   {
       if(r_count > 31) r = r_count - 32;
       else              r = r_count;    
       for(c_count = col; c_count < col + col_Pixel; c_count++)
        {
             if(r_count > 31) c = c_count + 8;
             else              c = c_count;
             LCD_Write(LCD_Write_Con_Cmd, r+0x80);//行地址
             LCD_Write(LCD_Write_Con_Cmd, c+0x80);//列地址        
             LCD_Write(LCD_Write_Dis_Data,0x00);
             LCD_Write(LCD_Write_Dis_Data,0x00);
            
      }
   }
LCD_Write(LCD_Write_Con_Cmd,0x30);
}




『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
PIC单片机基础知识之二