开发环境:WinAVR-20081205
相关电路:
驱动和演示源码:
/*
* main.c : Demo prog for Nokia3310/5110 LCD driver.
* lcd.h : Driver for Nokia3310/5110 LCD.
* lcd.c : Driver for Nokia3310/5110 LCD.
* Copyright (C) 2005-2009 Zhao Huabing
* www.ursastudio.com.cn
* History: 2005-11-20 Zhao Huabing created
* 2009-03-12 Zhao Huabing modified
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
.
*
*/
#include
#include "lcd.h"
#include "bmpix.h"
int main(void)
{
LcdInit(); //液晶初始化
LcdDrawBitmap(0, 0, (uint8_t *)logo, 80, 6); //画一个接近全屏的位图
LcdPutString(0, 0, "Ursa Studio"); //在位图的左上方覆盖英文字符
LcdPutString(0, 4, "天衡工作室"); //在位图的左下方覆盖中文字符
}
#ifndef _LCD_H
#define _LCD_H
/* 液晶分辨率定义 */
#define LCD_WIDTH 84
#define LCD_HEIGHT 48
#define LCD_ROW (LCD_HEIGHT/8)
#define LCD_COL LCD_WIDTH
/* 液晶控制器端口定义 */
#define LCD_DDR DDRB
#define LCD_PORT PORTB
/* 液晶控制器引脚定义 */
#define LCD_SCLK PB7
#define LCD_SDIN PB5
#define LCD_DC PB4
#define LCD_SCE PB1 //低电平使能
#define LCD_RES PB0 //低电平使能
/* 参数宏 */
#define COMMAND 0
#define DATA 1
#define POWERDOWN 1
#define ACTIVE 0
#define V_ADDR 1
#define H_ADDR 0
#define EXT_INS 1
#define BAS_INS 0
#define DISPLAY_BLANK 0b000
#define DISPLAY_ALL 0b001
#define NORMAL_MODE 0b100
#define INVERSE_MODE 0b101
// 类型 、操作宏定义
typedef uint8_t bool;
#define true 1
#define false 0
#define bit_get(sfr, bit) ((sfr>>(bit)) & 0x01)
#define bit_set(sfr, bit, val) ((val) ? (sfr |= _BV(bit)) : (sfr &= ~_BV(bit)))
/* 函数声明 */
void LcdInit(void);
void LcdClsScr(void);
void LcdDrawBitmap(uint8_t x, uint8_t y, uint8_t *pBmp, uint8_t row, uint8_t col);
void LcdPutString(uint8_t x, uint8_t y, const char *str);
#endif
#include
#include
#include
#include "spi.h"
#include "enpix.h"
#include "cnpix.h"
#include "lcd.h"
// 产生复位脉冲
static void LcdReset(void)
{
bit_set(LCD_PORT, LCD_RES, false);
_delay_us(1);
bit_set(LCD_PORT, LCD_RES, true);
_delay_us(10);
}
// 使能信号传输
// true : 使能 false : 禁止
static void LcdEnable(bool Enable)
{
bit_set(LCD_PORT, LCD_SCE, !Enable);
}
// 发送数据或指令
// DC : 数据或指令
// info : 发送的信息
static void LcdWriteDC(bool DC, uint8_t info)
{
LcdEnable(true);
bit_set(LCD_PORT, LCD_DC, DC);
SpiSendByte(info);
LcdEnable(false);
}
// 发送数据
// Data : 发送的数据
static void LcdWriteData(uint8_t data)
{
LcdWriteDC(DATA, data);
}
// 发送命令
// cmd : 发送的命令
static void LcdWriteCommand(uint8_t cmd)
{
LcdWriteDC(COMMAND, cmd);
}
/*
// 空操作
static void LcdNOP(void)
{
LcdWriteCommand(0);
}
*/
// 功能设置
// Power : 活动或掉电(ACTIVE or POWERDOWN)
// Addr : 水平或垂直寻址(H_ADDR or V_ADDR)
// InsSet : 基本或扩展指令集(BAS_INS or EXT_INS)
static void LcdFunctionSet(bool Power, bool Addr, bool InsSet)
{
LcdWriteCommand(_BV(5) | (Power<<2) | (Addr<<1) | InsSet);
}
/* 基本指令集 */
// 显示控制
// DE : DISPLAY_BLANK(显示空白); DISPLAY_ALL(显示全部);
// NORMAL_MODE(普通模式); INVERSE_MODE(反转模式).
static void LcdDispalyControl(uint8_t DE)
{
if(DE != DISPLAY_BLANK && DE != DISPLAY_ALL
&& DE != NORMAL_MODE && DE != INVERSE_MODE)
return;
LcdWriteCommand(_BV(3) | DE);
}
// 设置 Y地址
// Addr : 显存 Y地址 0~5
static void LcdSetYAddress(uint8_t Addr)
{
if(Addr > 5)
return;
LcdWriteCommand(_BV(6) | Addr);
}
// 设置 X地址
// Addr : 显存 X地址 0~83
static void LcdSetXAddress(uint8_t addr)
{
if(addr > 83)
return;
LcdWriteCommand(_BV(7) | addr);
}
/* 扩展指令集 */
// 设置温度系数
// TC : 温度系数 0~3
static void LcdTempCtrl(uint8_t TC)
{
if(TC > 0x03)
return;
LcdWriteCommand(_BV(2) | TC);
}
// 偏置系统
// BS : 偏置系统 0~7
static void LcdBiasSystem(uint8_t BS)
{
if(BS > 0x07)
return;
LcdWriteCommand(_BV(4) | BS);
}
// 设置操作电压
// Vop : 操作电压 0~0x7f
static void LcdSetVop(uint8_t Vop)
{
if(Vop > 0x7f)
return;
LcdWriteCommand(_BV(7) | Vop);
}
// 设置位置
// x : 显存 X地址 0~83
// y : 显存 Y地址 0~5
static void LcdSetLocation(uint8_t x, uint8_t y)
{
LcdSetYAddress(y) , LcdSetXAddress(x);
}
//清除屏幕
void LcdClsScr(void)
{
LcdSetLocation(0, 0);
for (uint16_t i=0; i<(LCD_ROW*LCD_COL); i++)
LcdWriteData(0);
}
// 初始化
void LcdInit(void)
{
//输出引脚初始化
LCD_DDR |= _BV(LCD_RES) | _BV(LCD_DC) | _BV(LCD_SCE);
SpiInit(); //SPI初始化
LcdReset(); //液晶复位
LcdFunctionSet(ACTIVE, H_ADDR, EXT_INS); //扩展指令集
LcdBiasSystem(0x03); //推荐混合率 1:48
LcdSetVop(0x32); //V(6.06) = 3.06 + 0.06*Vop
LcdTempCtrl(0x00); //温度系数 0~3
LcdFunctionSet(ACTIVE, H_ADDR, BAS_INS); //基本指令集
LcdDispalyControl(NORMAL_MODE); //普通模式显示
LcdClsScr();
}
// 绘画位图
// x : x位置
// y : y位置
// pBmp : 位图
// row : 位图行数
// col : 位图列数
void LcdDrawBitmap(uint8_t x, uint8_t y, uint8_t *pBmp, uint8_t col, uint8_t row)
{
if((col > 84) || (row > 6))
return;
for (uint8_t i=0; i 84) || (row > 6))
return;
for (uint8_t i=0; i= (32+NUMBER_OF_CHARACTER)))
return;
LcdSetLocation(x, y);
for (uint8_t n=0; n.
---------------------------------------------------------------------------
作者:赵华兵
组织:天衡工作室
网址:www.ursastudio.com.cn
邮箱:ursastudio@gmail.com