LCM046是一款4位段码液晶模块。这儿列出的驱动是针对mega168微控制器的。具体的管脚连接可以参考.c文件。驱动包括两个文件,.c和.h文件,分别罗列如下:
.h
/********************************************************************************
* Litlle Humidity&Temp Module
* Copyright (c) 2008 bpesun <bpesun@163.com>
* All rights reserved.
* V0.1
********************************************************************************
文件: lcm046.h
说明: lcm046的驱动头文件
原理:
编写者: bpesun@163.com
版本控制:
----------------------------------------------------------------------------
版本号 日期 修改内容 修改者
----------------------------------------------------------------------------
1.0 2008.12.12 初稿 bpesun
----------------------------------------------------------------------------
********************************************************************************/
#ifndef _LCM046_H
#define _LCM046_H
/* ----------------------- Platform includes --------------------------------*/
#include <avr/io.h>
#include <avr/interrupt.h>
/* ----------------------- Defines ------------------------------------------*/
/* ----------------------- Function prototypes ------------------------------*/
void vLCM046Init(void);
void vLCM046WriteCommand( unsigned int usCommand );
void vLCM046WriteData( unsigned char ucAddr, unsigned char ucData );
void vLCM046BitDisplay( unsigned char ucBit, unsigned char ucData );
#endif
.c
/*
* little Humidit&Temp Module
* Copyright (c) 2008 bpesun <bpesun@163.com>
* All rights reserved.
* V0.1
*/
/********************************************************************************
文件: lcm046.c
说明: 模块驱动
原理:
编写者: bpesun@163.com
版本控制:
----------------------------------------------------------------------------
版本号 日期 修改内容 修改者
----------------------------------------------------------------------------
1.0 2008.12.12 初稿 bpesun
----------------------------------------------------------------------------
********************************************************************************/
/* ----------------------- System includes ----------------------------------*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <string.h>
#include <math.h>
#include <avr/wdt.h>
/* ----------------------- Platform includes --------------------------------*/
#include "lcm046.h"
/* ----------------------- project includes ----------------------------------* /
/* ----------------------- Defines ------------------------------------------*/
/*1-/CS---PC0
2-/RD-----PC1
3-/WR-----PC2
4-DA------PC3
5-GND-----PC4
6-VLCD----PC5
7-VDD-----PC6
*/
#define INIT_LCM( ) DDRC="0xFF";PORTC|=(1<<PC0)|(1<<PC1)|(1<<PC2)|(1<<PC3)|(1<<PC5)|(1<<PC6);PORTC&=~(1<<PC4)
#define LCM_CLK(i) if (i) PORTC|=(1<<PC2); else PORTC &= ~(1<<PC2)
#define LCM_CS(i) if (i) PORTC|=(1<<PC0); else PORTC &= ~(1<<PC0)
#define LCM_DATA(i) if (i) PORTC|=(1<<PC3); else PORTC &= ~(1<<PB3)
#define LCM_COMMAND_INIT 0x29
#define LCM_COMMAND_RC 0x18
#define LCM_COMMAND_RC_ON 0x01
#define LCM_COMMAND_LCD_ON 0x03
#define MAX_BIT 4
/* ----------------------- Type definitions ---------------------------------*/
/* ----------------------- Static variables ---------------------------------*/
const unsigned char ucNumTrueValueTable[11]= //断码的真值表
{// DCGB*EFA
0xD7,// 11010111 0
0x50,// 01010000 1
0xB5,// 10110101 2
0xF1,// 11110001 3
0x72,// 01110010 4
0xE3,// 11100011 5
0xE7,// 11100111 6
0x51,// 01010001 7
0xF7,// 11110111 8
0xF3, //11110011 9
0x00 //00000000 什么都不显示,用于闪烁的实现
};
static unsigned char ucDisplayBuff[MAX_BIT];
/* ----------------------- Start implementation -----------------------------*/
void vLCM046Init(void)
{
_delay_ms(200);
//init the I/O port
INIT_LCM( );
vLCM046WriteCommand(LCM_COMMAND_INIT);
vLCM046WriteCommand(LCM_COMMAND_RC);
vLCM046WriteCommand(LCM_COMMAND_RC_ON);
vLCM046WriteCommand(LCM_COMMAND_LCD_ON);
}
/*cs=p1.5 wr=p1.3 data="p1".2*/
void vLCM046WriteCommand( unsigned int usCommand )
{
unsigned char i;
//CS=0
LCM_CS(1); _delay_us(100);
LCM_CS(0); _delay_us(100);
//write 1
LCM_CLK(0); _delay_us(100);
LCM_DATA(1);_delay_us(100);
LCM_CLK(1); _delay_us(100);
//write 0
LCM_CLK(0); _delay_us(100);
LCM_DATA(0);_delay_us(100);
LCM_CLK(1); _delay_us(100);
//write 0
LCM_CLK(0); _delay_us(100);
LCM_DATA(0);_delay_us(100);
LCM_CLK(1); _delay_us(100);
for(i=9;i>0;i--)
{
LCM_CLK(0); _delay_us(100);
if(usCommand&0x80)
{
LCM_DATA(1);
}
else
{
LCM_DATA(0);
}
_delay_us(100);
LCM_CLK(1); _delay_us(100);
usCommand<<=1;
}
//CS=1
LCM_CS(1);
LCM_DATA(1);
}
void vLCM046WriteData( unsigned char ucAddr, unsigned char ucData )
{ unsigned char i;
//init the I/O port
INIT_LCM( );
//CS=0
LCM_CS(1); _delay_us(100);
LCM_CS(0); _delay_us(100);
//write 1
LCM_CLK(0); _delay_us(100);
LCM_DATA(1);_delay_us(100);
LCM_CLK(1); _delay_us(100);
//write 0
LCM_CLK(0); _delay_us(100);
LCM_DATA(0);_delay_us(100);
LCM_CLK(1); _delay_us(100);
//write 1
LCM_CLK(0); _delay_us(100);
LCM_DATA(1);_delay_us(100);
LCM_CLK(1); _delay_us(100);
//write 0
LCM_CLK(0); _delay_us(100);
LCM_DATA(0);_delay_us(100);
LCM_CLK(1); _delay_us(100);
//write addr
for(i=5;i>0;i--)
{
LCM_CLK(0); _delay_us(100);
if(ucAddr&0x10)
{
LCM_DATA(1);
}
else
{
LCM_DATA(0);
}
_delay_us(100);
LCM_CLK(1); _delay_us(100);
ucAddr<<=1;
}
//write data
for(i=4;i>0;i--)
{
LCM_CLK(0); _delay_us(100);
if(ucData&0x01)
{
LCM_DATA(1);
}
else
{
LCM_DATA(0);
}
_delay_us(100);
LCM_CLK(1); _delay_us(100);
ucData>>=1;
}
//CS=1
LCM_CS(1);
LCM_DATA(1);
}
void vLCM046BitDisplay( unsigned char ucBit, unsigned char ucData )
{
if(ucBit <= MAX_BIT)
{
ucDisplayBuff[ucBit] = 0;
ucDisplayBuff[ucBit] = ucNumTrueValueTable[ucData];
vLCM046WriteData( ( 2*ucBit - 2 ), ucDisplayBuff[ucBit]&0x0F );
vLCM046WriteData( ( 2*ucBit -1 ), ucDisplayBuff[ucBit]>>4 );
}
}
文章评论(0条评论)
登录后参与讨论