|
源文件
/**************************************************/
#include "lcd12864.h"
#include "font.h"
#include "photo.h"
void GPIO_init_LCD(void)
{
RCC->AHB1ENR |= 1<<4;//GPIOE时钟使能
RCC->AHB1ENR |= 1<<5;//GPIOE时钟使能
RCC->AHB1ENR |= 1<<6;//GPIOG时钟使能
//cs/rest
GPIOG->MODER&=~(3<<(0*2));
GPIOG->MODER&=~(3<<(1*2));
GPIOG->MODER|=(1<<0);
GPIOG->MODER|=(1<<1); //输出模式
//开漏输出
GPIOG->OTYPER |=1<<0;
GPIOG->OTYPER |=1<<1;
//50MHZ
GPIOG->OSPEEDR&=~(3<<(0*2));
GPIOG->OSPEEDR&=~(3<<(1*2));
GPIOG->OSPEEDR|=(2<<(0*2));
GPIOG->OSPEEDR|=(2<<(1*2));
//data
// GPIOE->MODER&=~(3<<(0*2));
// GPIOE->MODER&=~(3<<(1*2));
// GPIOE->MODER&=~(3<<(2*2));
// GPIOE->MODER&=~(3<<(3*2));
// GPIOE->MODER&=~(3<<(4*2));
// GPIOE->MODER&=~(3<<(5*2));
// GPIOE->MODER&=~(3<<(6*2));
// GPIOE->MODER&=~(3<<(7*2));
//
// GPIOE->MODER|=(1<<(0*2));
// GPIOE->MODER|=(1<<(1*2));
// GPIOE->MODER|=(1<<(2*2));
// GPIOE->MODER|=(1<<(3*2));
// GPIOE->MODER|=(1<<(4*2));
// GPIOE->MODER|=(1<<(5*2));
// GPIOE->MODER|=(1<<(6*2));
// GPIOE->MODER|=(1<<(7*2));
//
// GPIOE->OTYPER|=(1<<(0));
// GPIOE->OTYPER|=(1<<(1));
// GPIOE->OTYPER|=(1<<(2));
// GPIOE->OTYPER|=(1<<(3));
// GPIOE->OTYPER|=(1<<(4));
// GPIOE->OTYPER|=(1<<(5));
// GPIOE->OTYPER|=(1<<(6));
// GPIOE->OTYPER|=(1<<(7));
//
//
// GPIOE->OSPEEDR&=~(3<<(0*2));
// GPIOE->OSPEEDR&=~(3<<(1*2));
// GPIOE->OSPEEDR&=~(3<<(2*2));
// GPIOE->OSPEEDR&=~(3<<(3*2));
// GPIOE->OSPEEDR&=~(3<<(4*2));
// GPIOE->OSPEEDR&=~(3<<(5*2));
// GPIOE->OSPEEDR&=~(3<<(6*2));
// GPIOE->OSPEEDR&=~(3<<(7*2));
//
// GPIOE->OSPEEDR|=(2<<(0*2));
// GPIOE->OSPEEDR|=(2<<(1*2));
// GPIOE->OSPEEDR|=(2<<(2*2));
// GPIOE->OSPEEDR|=(2<<(3*2));
// GPIOE->OSPEEDR|=(2<<(4*2));
// GPIOE->OSPEEDR|=(2<<(5*2));
// GPIOE->OSPEEDR|=(2<<(6*2));
// GPIOE->OSPEEDR|=(2<<(7*2));
GPIOE->MODER&=~(3<<(8*2));
GPIOE->MODER&=~(3<<(9*2));
GPIOE->MODER&=~(3<<(10*2));
GPIOE->MODER&=~(3<<(11*2));
GPIOE->MODER&=~(3<<(12*2));
GPIOE->MODER&=~(3<<(13*2));
GPIOE->MODER&=~(3<<(14*2));
GPIOE->MODER&=~(3<<(15*2));
GPIOE->MODER|=(1<<(8*2));
GPIOE->MODER|=(1<<(9*2));
GPIOE->MODER|=(1<<(10*2));
GPIOE->MODER|=(1<<(11*2));
GPIOE->MODER|=(1<<(12*2));
GPIOE->MODER|=(1<<(13*2));
GPIOE->MODER|=(1<<(14*2));
GPIOE->MODER|=(1<<(15*2));
/*
GPIOE->OTYPER|=(1<<(8));
GPIOE->OTYPER|=(1<<(9));
GPIOE->OTYPER|=(1<<(10));
GPIOE->OTYPER|=(1<<(11));
GPIOE->OTYPER|=(1<<(12));
GPIOE->OTYPER|=(1<<(13));
GPIOE->OTYPER|=(1<<(14));
GPIOE->OTYPER|=(1<<(15));
*/
GPIOE->OSPEEDR&=~(3<<(8*2));
GPIOE->OSPEEDR&=~(3<<(9*2));
GPIOE->OSPEEDR&=~(3<<(10*2));
GPIOE->OSPEEDR&=~(3<<(11*2));
GPIOE->OSPEEDR&=~(3<<(12*2));
GPIOE->OSPEEDR&=~(3<<(13*2));
GPIOE->OSPEEDR&=~(3<<(14*2));
GPIOE->OSPEEDR&=~(3<<(15*2));
GPIOE->OSPEEDR|=(2<<(8*2));
GPIOE->OSPEEDR|=(2<<(9*2));
GPIOE->OSPEEDR|=(2<<(10*2));
GPIOE->OSPEEDR|=(2<<(11*2));
GPIOE->OSPEEDR|=(2<<(12*2));
GPIOE->OSPEEDR|=(2<<(13*2));
GPIOE->OSPEEDR|=(2<<(14*2));
GPIOE->OSPEEDR|=(2<<(15*2));
GPIOE->PUPDR&=~(3<<(8*2));
GPIOE->PUPDR&=~(3<<(9*2));
GPIOE->PUPDR&=~(3<<(10*2));
GPIOE->PUPDR&=~(3<<(11*2));
GPIOE->PUPDR&=~(3<<(12*2));
GPIOE->PUPDR&=~(3<<(13*2));
GPIOE->PUPDR&=~(3<<(14*2));
GPIOE->PUPDR&=~(3<<(15*2));
GPIOE->PUPDR|=(1<<(8*2));
GPIOE->PUPDR|=(1<<(9*2));
GPIOE->PUPDR|=(1<<(10*2));
GPIOE->PUPDR|=(1<<(11*2));
GPIOE->PUPDR|=(1<<(12*2));
GPIOE->PUPDR|=(1<<(13*2));
GPIOE->PUPDR|=(1<<(14*2));
GPIOE->PUPDR|=(1<<(15*2));
//rs/wr/rd
GPIOF->MODER&=~(3<<(12*2));
GPIOF->MODER&=~(3<<(13*2));
GPIOF->MODER&=~(3<<(14*2));
GPIOF->MODER&=~(3<<(15*2));
GPIOF->MODER|=(1<<(12*2));
GPIOF->MODER|=(1<<(13*2));
GPIOF->MODER|=(1<<(14*2));
GPIOF->MODER|=(1<<(15*2));
GPIOF->OTYPER|=(1<<(12));
GPIOF->OTYPER|=(1<<(13));
GPIOF->OTYPER|=(1<<(14));
GPIOF->OTYPER|=(1<<(15));
GPIOF->OSPEEDR&=~(3<<(12*2));
GPIOF->OSPEEDR&=~(3<<(13*2));
GPIOF->OSPEEDR&=~(3<<(14*2));
GPIOF->OSPEEDR&=~(3<<(15*2));
GPIOF->OSPEEDR|=(2<<(12*2));
GPIOF->OSPEEDR|=(2<<(13*2));
GPIOF->OSPEEDR|=(2<<(14*2));
GPIOF->OSPEEDR|=(2<<(15*2));
}
void LCD12864_Write(u8 data_send,u8 cd)
{
u16 dat1;
LCD_CS = 0; //打开片选 低电平有效
LCD_RD = 1; //读失能
LCD_RS = cd; //选择命令/或者数据 0/命令 1/数据
LCD_WR = 0; //选择写模式
delay_us(2);
/*
dat1=data_send<<8;
GPIOE->BSRRH=dat1;
data_send=~data_send;
dat1=data_send<<8;
GPIOE->BSRRH=dat1;
*/
GPIOE->ODR &=data_send|0x00;;
delay_us(2);
LCD_WR = 1; //写入命令
}
///////////////////////////////
//函数功能:lcd12864的初始化
//函数类型:无
//函数返回值:无
//函数参数:无
///////////////////////////////
void LCD12864_Init(void)
{
u8 i;
GPIO_init_LCD();
delay_us(100);
LCD_RSET = 0; //复位
delay_us(500);
LCD_CS = 0; //选中
delay_us(100);
LCD_RSET = 1; //禁止复位
/*----------------------------------------*/
//软复位
LCD12864_Write(0xE2,0);
delay_us(400);
//左右方向 0XA1 反方向 0XA0 正常方向
LCD12864_Write(0xA0,0);
//上下方向 0XC8反方向 0XC0正常方向
LCD12864_Write(0XC8,0);
//设置字体颜色及背景颜色 0xA6黑字白底 0XAE相反
LCD12864_Write(0XA6,0);
//像素显示 0xA4 正常 0xA5 全开
LCD12864_Write(0xA4,0);
//偏压设置
LCD12864_Write(0xA2,0);
//双字节命令
LCD12864_Write(0xF8,0);
LCD12864_Write(0x01,0);
//设置背景光对比度
LCD12864_Write(0x81,0);
LCD12864_Write(0x21,0);
//调节电阻率
LCD12864_Write(0x25,0);
//电源设置
LCD12864_Write(0x2f,0);
delay_us(400);
//设置显示开始的位置
LCD12864_Write(0x40,0);
//开启显示命令
LCD12864_Write(0xAF,0);
delay_us(400);
LCD12864_Clean_all(0xFF);
}
void LCD12864_Clean_all(u8 data_sent)
{
u8 i,j;
for(i=0;i<8;i++)
{
LCD12864_Write(0xB0+i,0); //设置Y坐标
LCD12864_Write(0x10,0); //设置X坐标 (高位) 写数据自动加1
LCD12864_Write(0x00,0); //低位
for(j=0;j<128;j++)
LCD12864_Write(data_sent,1);
}
}
//列16行 //行4
void LCD_Showchar(u8 x,u8 y,u8 num)
{
u8 i,j;
u8 x0_l;
u8 x0_h;
y=0xb0+y*2;
x*=8;
x0_h=0x10|((x&0xf0)>>4);
x0_l=0x00|(x&0x0f);
////////////////////////
//下面是一般设定法设定光标位置
/*
switch(x)
{
case 0:
x0_h=0x10;
x0_l=0x00;
break;
case 1:
x0_h=0x10;
x0_l=0x08;
break;
case 2:
x0_h=0x11;
x0_l=0x00;
break;
case 3:
x0_h=0x11;
x0_l=0x08;
break;
case 4:
x0_h=0x12;
x0_l=0x00;
break;
case 5:
x0_h=0x12;
x0_l=0x08;
break;
case 6:
x0_h=0x13;
x0_l=0x00;
break;
case 7:
x0_h=0x13;
x0_l=0x08;
break;
case 8:
x0_h=0x14;
x0_l=0x00;
break;
case 9:
x0_h=0x14;
x0_l=0x08;
break;
case 10:
x0_h=0x15;
x0_l=0x00;
break;
case 11:
x0_h=0x15;
x0_l=0x08;
break;
case 12:
x0_h=0x16;
x0_l=0x00;
break;
case 13:
x0_h=0x16;
x0_l=0x08;
break;
case 14:
x0_h=0x17;
x0_l=0x00;
break;
case 15:
x0_h=0x17;
x0_l=0x08;
break;
case 16:
x0_h=0x18;
x0_l=0x00;
break;
} */
num =num-' ';
//--设置Y坐标--//
for (i=0;i<2;i++)
{
//--设置X坐标--//
LCD12864_Write(x0_h,0);
LCD12864_Write(x0_l,0);
LCD12864_Write(y+i,0);
for(j=0; j<8; j++)
{
LCD12864_Write(ascii[num][i*8+j],1);
}
}
}
void Delay10ms(unsigned int c) //误差 0us
{
unsigned char a,b;
for(;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
//显示字符串
//x,y:起点坐标
//width,height:区域大小
//size:字体大小
//*p:字符串起始地址
void LCD_ShowString_12864(u16 x,u16 y,u8 *p)
{
//u8 x0=x;
while((*p<='~')&&(*p>=' '))//判断是不是非法字符!
{ /*
if(x>=width){x=x0;y+=size;}
if(y>=height)break;//退出 */
LCD_Showchar(x,y,*p);
//x+=16/2;
x++;
p++;
//Delay10ms(10);
}
}
头文件
#ifndef _LCD12864_H
#define _LCD12864_H
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#define stm_mcu
#ifndef u8
#define u8 unsigned char
#endif
#ifndef u16
#define u16 unsigned int
#endif
//#ifdef my_mcu
//#include <reg51.h>
//#include "intrins.h"
//#define LCD_DATA P1 //数据接口
////控制端口
//sbit LCD_RS = P2^3; //命令数据控制端口
//sbit LCD_RD = P2^4; //读写控制端口
//sbit LCD_WR = P2^5; //读写模式选择端口 0写 1读 0->1写入数据配合写模式
//sbit LCD_RSET= P2^6; //复位端口
//sbit LCD_CS = P2^7; //片选端口
//#endif
//#ifdef Pz_mcu
//#include "reg51.h"
//#include "intrins.h"
//#define LCD_DATA P0
//sbit LCD_CS = P3^2;
//sbit LCD_RSET = P3^3;
//sbit LCD_RS = P2^6;
//sbit LCD_WR = P2^7;
//sbit LCD_RD = P2^5;
//#endif
#ifdef stm_mcu
//#define LCD_DATA PEout(0)|PEout(1)|PEout(2)|PEout(3)|PEout(4)|PEout(5)|PEout(6)|PEout(7)
#define LCD_CS PGout(1)
#define LCD_RSET PGout(0)
#define LCD_RS PFout(12)
#define LCD_WR PFout(15)
#define LCD_RD PFout(13)
void GPIO_init_LCD(void);
#endif
void LCD12864_Write(u8 data_send,u8 cd); //写函数 cd 0命令 1数据
void LCD12864_Init(void); //初始化
void LCD12864_Clean_all(u8 data_send); //清屏函数 0x00 白 0xff 黑
void LCD_Showchar(u8 x,u8 y,u8 num);
void LCD_ShowString_12864(u16 x,u16 y,u8 *p);
void Delay10ms(unsigned int c); //误差 0us
#endif
|
评分
-
查看全部评分
|