关闭

LCD5110

312人阅读 评论(0) 收藏 举报
分类:
#ifndef _LCD_H
#define _LCD_H

#include <reg51.h>

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;

#define WIDTH   84
#define HEIGHT  48

sbit LCD_RST =  P1^0;   //RST
sbit LCD_CE =   P1^1;   //CE
sbit LCD_DC =   P1^2;   //DC
sbit LCD_SDA =  P1^3;   //D1
sbit LCD_SCL =  P1^4;   //D0
sbit LCD_VCC =  P1^5;
sbit LCD_BL =   P1^6;
sbit LCD_GND =  P1^7;

/* OLED IO输出宏替换 */
#define LCD_SCLK_OL     LCD_SCL = 0;
#define LCD_SCLK_OH     LCD_SCL = 1;

#define LCD_SDIN_OL     LCD_SDA = 0;
#define LCD_SDIN_OH     LCD_SDA = 1;

#define LCD_RST_OL      LCD_RST = 0;
#define LCD_RST_OH      LCD_RST = 1;

#define LCD_DC_OL       LCD_DC = 0;
#define LCD_DC_OH       LCD_DC = 1;

#define LCD_CE_OL       LCD_CE = 0;
#define LCD_CE_OH       LCD_CE = 1;

#define LCD_BL_OH       LCD_BL = 0;

void LCD5110_Init(void);
void LCD5110_Clear(void);
void LCD5110_Write_Char(uint8_t x, uint8_t y, uint8_t c);
void LCD5110_Write_Num5(uint8_t x, uint8_t y, uint16_t num);
void LCD5110_Write_String(uint8_t x, uint8_t y, uint8_t *s);

#endif
#include "lcd.h"
#include "ziku.h"
#include <intrins.h>

void Delay10us()
{
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
    _nop_();
}

/*
 *  延时函数
 */
void delay_ms(uint16_t ms)
{                         
    uint16_t a;

    while (ms--)
    {
        a = 100;
        while (a--)
        {
            Delay10us();
        }
    }
}

//作用:写一个字节(数据或命令)
//两个参数:dt为要写入的字节,command为数据/命令选择
void LCD5110_WrDat(uint8_t dat)
{
    uint8_t i = 8;
    LCD_CE_OL;      //片选信号拉低,选中芯片,进行下一步操作
    LCD_DC_OH;      //写数据
    _nop_();
    LCD_SCLK_OL;
    _nop_(); 
    while (i --)//每个时钟周期写入一位
    {
        if (dat & 0x80)
        {
            LCD_SDIN_OH;
        }
        else
        {
            LCD_SDIN_OL;
        }
        LCD_SCLK_OH; 
        _nop_();_nop_();
        LCD_SCLK_OL;    
        dat <<= 1;    
    }
    LCD_CE_OH;
}
void LCD5110_WrCmd(uint8_t cmd)
{
    uint8_t i = 8;
    LCD_CE_OL;
    LCD_DC_OL;//写命令
    _nop_();
    LCD_SCLK_OL;
    _nop_(); 
    while (i --)
    {
        if (cmd & 0x80)
        {
            LCD_SDIN_OH;
        }
        else
        {
            LCD_SDIN_OL;
        }
        LCD_SCLK_OH; 
        _nop_();_nop_();
        LCD_SCLK_OL;    
        cmd <<= 1;    
    }
    LCD_CE_OH;  
}
//设置坐标,两个参数:X,Y坐标
void LCD5110_Set_XY(uint8_t X, uint8_t Y)
{
     Y |= 0x40;
     X |= 0x80;
     LCD5110_WrCmd(Y);
     LCD5110_WrCmd(X);

}

//整屏清屏(可以从中扩展出指定行列的清除)
void LCD5110_Clear(void)
{
     uint8_t t,k;
     LCD5110_Set_XY(0,0);
     for(t = 0; t < (HEIGHT >> 3); t++)
     {
        for(k = 0;k < WIDTH; k++)
        {           
            LCD5110_WrDat(0x00);
        }
     }
}

//LCD初始化
void LCD5110_Init(void)
{
     LCD_VCC = 1;
     LCD_GND = 0;
     LCD_BL_OH;
     LCD_CE_OH;
     LCD_RST_OL;
     delay_ms(10);
     LCD_RST_OH;
     delay_ms(50);
     LCD5110_WrCmd(0x21);
     LCD5110_WrCmd(0x13);
     LCD5110_WrCmd(0xc8);//参考电压
     LCD5110_WrCmd(0x20);
     LCD5110_WrCmd(0x0c);
     LCD5110_Clear();
     LCD_CE_OL;
}

void LCD5110_Write_Char(uint8_t x, uint8_t y, uint8_t c) 
{
    uint8_t i = 0;
    if ((c >= 'A') && (c <= 'Z'))
    {
        c = c - 'A' + 10;
    }
    else if ((c >= 'a') && (c <= 'z')) 
    {
        c = c - 'a' + 44;
    }
    else if (c == ' ')
    {
        c = 36;
    }
    else if (c == ':')
    {
        c = 37;
    }
    else if (c == '-')
    {
        c = 38;
    }
    else if (c == '/')
    {
        c = 39;
    }
    else if(c == '.')
    {
        c = 40;
    }
    else if (c == ',')
    {
        c = 41;
    }
    else if (c == '^')
    {
        c = 42;
    }

    if (x > 126) // > 126 当前行不足以写完另一字符
    {
        x = 0;
        y ++;
    }

    LCD5110_Set_XY(x * 6, y);
    for (i = 0; i < 6; i++) 
    {
        LCD5110_WrDat(SmallChars[c * 6 + i]); 
    }                                        
}

void LCD5110_Write_String(uint8_t x, uint8_t y, uint8_t *s)
{
    uint8_t i;

    for (i=0; s[i]!='\0'; i++)
    {
        LCD5110_Write_Char(x + i, y, s[i]);
    }
}
void LCD5110_Write_Num5(uint8_t x, uint8_t y, uint16_t num)
{
    LCD5110_Write_Char(x,     y, num / 10000);
    LCD5110_Write_Char(x + 1, y, num % 10000 / 1000);
    LCD5110_Write_Char(x + 2, y, num % 1000 / 100);
    LCD5110_Write_Char(x + 3, y, num % 100/10);
    LCD5110_Write_Char(x + 4, y, num % 10);
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10547次
    • 积分:488
    • 等级:
    • 排名:千里之外
    • 原创:39篇
    • 转载:9篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论