|
开发环境:WinAVR-20081205
相关电路:
(原文件名:lcdcircuit.gif)
驱动和演示源码:
/*
* 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 <http://www.gnu.org/licenses/>.
*
*/
#include <avr/io.h>
#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 <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#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<row; i++)
{
LcdSetLocation(x, y);
for(uint8_t j=0; j<col; j++)
LcdWriteData(pgm_read_byte(pBmp+i*col+j));
y++;
}
}
// 清除位图
// x : x位置
// y : y位置
// row : 位图行数
// col : 位图列数
void LcdEraseBitmap(uint8_t x, uint8_t y, uint8_t col, uint8_t row)
{
if((col > 84) || (row > 6))
return;
for (uint8_t i=0; i<row; i++)
{
LcdSetLocation(x, y);
for(uint8_t j=0; j<col; j++)
LcdWriteData(0);
y++;
}
}
// 输出英文字符
// x : x位置
// y : y位置
// c : 英文字符
static void LcdPutEnChar(uint8_t x, uint8_t y, uint8_t c)
{
if((c < 32) || (c >= (32+NUMBER_OF_CHARACTER)))
return;
LcdSetLocation(x, y);
for (uint8_t n=0; n<LCD_ROW; n++)
LcdWriteData(pgm_read_byte((uint8_t *)EnPix+(c-32)*LCD_ROW+n));
}
// 输出汉语字符
// x : x位置
// y : y位置
// c : 汉语字符
static void LcdPutCnChar(uint8_t x, uint8_t y, uint16_t c)
{
uint8_t k;
for(k=0; k<sizeof(CnPix)/34; k++)
if(pgm_read_word((uint16_t *)(CnPix+k*34)) == c)
break;
if(k == sizeof(CnPix)/34)
return;
for(uint8_t i=0; i<2; i++)
{
LcdSetLocation(x, y++);
for(uint8_t j=0; j<16; j++)
LcdWriteData(pgm_read_byte((uint8_t *)(CnPix+k*34+2+16*i+j)));
}
}
// 输出字符串
// x : x位置
// y : y位置
// str : 字符串
void LcdPutString(uint8_t x, uint8_t y, const char *str)
{
uint8_t *p = (uint8_t *)str;
while(*p != 0)
{
if(*p < 128)
{
LcdPutEnChar(x, y, *p);
x += 6;
p++;
}
else
{
LcdPutCnChar(x, y, *(uint16_t *)p);
x += 16;
p += 2;
}
}
}
显示效果:
(原文件名:lcd.JPG)
固件源码下载:
ourdev_441085.zip(文件大小:13K) (原文件名:10-lcd.zip)
汉字点阵生成软件CnPix
说明:
CnPix原为天衡工作室USAVRDKM16开发套件液晶专用汉字点阵生成软件,作为通用软件还需有所修改。
本软件使用QT开发,可跨平台编译使用,支持Windows、Linux/X11和Mac OS X操作系统。
使用方法:
运行软件在编辑栏中输入或粘贴工程需要的所有汉字,回车即可在当前文件夹下产生cnpix.h文件,复制到工程目录即可使用。
预览:
(原文件名:CnPix.png)
CnPix源码+Windows版下载:
ourdev_441086.zip(文件大小:5.32M) (原文件名:CnPix.zip)
位图点阵生成软件BmPix
说明:
BmPix原为天衡工作室USAVRDKM16开发套件液晶专用位图点阵生成软件,作为通用软件还需有所修改。
本软件使用QT开发,可跨平台编译使用,支持Windows、Linux/X11和Mac OS X操作系统。
本软件目前仅支持单色位图,尺寸不超过84x48像素,高度为8的倍数,如果不满足以上条件,可以先使用画图等程序进行修改转换。
使用方法:
运行软件,添加位图文件,开始转换,可在位图文件同样位置产生同名后缀为h的文件,复制到工程目录即可使用,必要的话可以将得到的文件合并。
预览:
(原文件名:BmPix.png)
BmPix源码+Windows版下载:
ourdev_441087.zip(文件大小:5.33M) (原文件名:BmPix.zip)
---------------------------------------------------------------------------
版权信息:
以上软件开放源码,使用GPL协议发布。
Copyright (C) 2009 Zhao Huabing
www.ursastudio.com.cn
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 <http://www.gnu.org/licenses/>.
---------------------------------------------------------------------------
作者:赵华兵
组织:天衡工作室
网址:www.ursastudio.com.cn
邮箱:ursastudio@gmail.com |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|