微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32单片机12864驱动

stm32单片机12864驱动

时间:11-29 来源:互联网 点击:
/****************************************************************
lcd12864.hlcd12864 头文件shifu*****************************************************************/#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.clcd12864 驱动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: 指向待显示字符串的字符指针,字符串以结尾注:CGRAM 与中文字型的编码只能出现在adress counter 的起始位DDRAM 8*16 字节的空间Line1 80H 81H 82H 83H 84H 85H 86H 87HLine2 90H 91H 92H 93H 94H 95H 96H 97HLine3 88H 89H 8AH 8BH 8CH 8DH 8EH 8FHLine4 98H 99H 9AH 9BH 9CH 9DH 9EH 9FHLine5 A0H A1H A2H A3H A4H A5H A6H A7HLine6 B0H B1H B2H B3H B4H B5H B6H B7HLine7 A8H A9H AAH ABH ACH ADH AEH AFHLine8 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)加1LCD_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清除绘图RAMLCD_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 151****************2***********************************6263****************************************************************************/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);}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top