/*
MSP430F4XX系列LCD显示通用驱动程序
说明:该驱动程序库包含了常用的LCD显示功能,如显示数字、字母等
可以作为各种程序的底层驱动使用。
要使用该库函数,需要将本文件(LCD_Display.c)添加进工程,并在
需要调用显示函数的文件开头处包含"LCD_Display.h"
(C)西安电子科技大学 测控技术与仪器教研中心 编写:谢楷 2008/02/02
*/
//
//
// MSP430F4XX
// +---------------+ Digital Number
// | | 左8+ 7654321右
// | | +----------------------+
// |A0+ S0-S15|--->| SoftBaugh LCD_048 |
// |A0- COM0-COM3|--->| 7.1Digit,4-Mux LCD |
// |P2.0 | +----------------------+
// |P2.1 |
// |VRef R03-R33|<---LCD Voltage Ladder Rs
// | |
// | XIN/XOUT|<---32.768KHz Watch Crystal
// | |
// +---------------+
#include "msp430x42x.h"
/*宏定义,数码管a-g各段对应的比特,更换硬件只用改动以下8行*/
#define d 0x01 // AAAA
#define g 0x02 // F B
#define b 0x04 // F B
#define a 0x08 // GGGG
#define DOTSEG 0x10 // E C
#define e 0x20 // E C
#define f 0x40 // DDDD
#define c 0x80
#define NEGSEG 0x02
/*用宏定义自动生成段码表,很好的写法,值得学习*/
/*更换硬件无需重写段码表*/
const char LCD_Tab[] = {
a + b + c + d + e + f, // Displays "0"
b + c, // Displays "1"
a + b + d + e + g, // Displays "2"
a + b + c + d + g, // Displays "3"
b + c + f + g, // Displays "4"
a + c + d + f +g, // Displays "5"
a + c + d + e + f + g, // Displays "6"
a + b + c, // Displays "7"
a + b + c + d + e + f + g, // Displays "8"
a + b + c + d + f + g, // Displays "9"
a + b + c + e + f + g, // Displays "A"
c + d + e + f + g, // Displays "B"
a + d + e + f, // Displays "C"
b + c + d + e + g, // Displays "D"
a + d + e + f + g, // Displays "E"
a + e + f + g, // Displays "F"
a + c + d + e + f, // Displays "G"
b + c + e + f + g, // Displays "H"
e + f, // Displays "I"
b + c + d + e, // Displays "J"
b + d + e + f + g, // Displays "K"
d + e + f, // Displays "L"
a + c + e + g, // Displays "M"
a + b + c + e + f, // Displays "N"
c + e + g, // Displays "n"
c + d + e + g, // Displays "o"
a + b + c + d + e + f, // Displays "O"
a + b + e + f + g, // Displays "P"
a + b + c + f + g, // Displays "Q"
e + g, // Displays "r"
a + c + d + f +g, // Displays "S"
d + e + f + g, // Displays "t"
a + e + f , // Displays "T"
b + c + d + e + f, // Displays "U"
c + d + e, // Displays "v"
b + d + f + g, // Displays "W"
b + c + d + f + g, // Displays "Y"
a + b + d + e + g, // Displays "Z"
g, // Displays "-"
b, // Displays "'"
0 // Displays " "
};
#undef a
#undef b
#undef c
#undef d
#undef e
#undef f
#undef g
#define AA 10
#define BB AA+1
#define CC BB+1
#define DD CC+1
#define EE DD+1
#define FF EE+1
#define GG FF+1
#define HH GG+1
#define II HH+1
#define JJ II+1
#define KK JJ+1
#define LL KK+1
#define mm LL+1
#define NN mm+1
#define nn NN+1
#define oo nn+1
#define OO oo+1
#define PP OO+1
#define QQ PP+1
#define rr QQ+1
#define SS rr+1
#define tt SS+1
#define TT tt+1
#define UU TT+1
#define VV UU+1
#define WW VV+1
#define YY WW+1
#define ZZ YY+1
#define BR ZZ+1 /* - */
#define DT BR+1 /* ' */
#define SP DT+1 /* 空白 */
/****************************************************************************
* 名 称:LCD_Init()
* 功 能:初始化LCD显示屏。
* 入口参数:无
* 出口参数:无
* 说 明: 在主程序LCD操作之前,需要调用该函数设置LCD参数。由于LCD刷新率发生
器复用了BasicTimer,因此调用LCD_Init()之前需要先初始化BasicTimer。
可以调用BasicTimer程序库中的BT_Init()函数或直接对BTCTL赋值之后再
调用LCD_Init()函数。若程序中不使用BasicTimer,在调用LCD_Init()之
前需要加一句BTCTL=0;用来复位BTCTL寄存器(第一次通电,BTCTL的值是随
机的)。
****************************************************************************/
void LCD_Init()
{ char i;
char *pLCD = (char *)&LCDM1; // 取LCDM1寄存器(最低位)的地址
for (i = 0; i < 8; i++) // Clear LCD memory
*pLCD++ = 0; // 清屏
LCDCTL = LCDSG0_1 + LCD4MUX + LCDON; // LCD模式:4mux LCD, segs0-15
BTCTL |= BT_fLCD_DIV64; // 设置 LCD 刷新率
/*刷新率越慢功耗越低,但速度太慢LCD会闪烁*/
}
/****************************************************************************
* 名 称:LCD_DisplayLongDecimal()
* 功 能:在LCD上显示一个带有小数点的长数据。
* 入口参数:Number:显示数值 (-999999~9999999)
DOT :小数点位数(0~3)
* 出口参数:无
* 范 例: LCD_DisplayDecimal( 123456,2); 显示结果: 1234.56 (2位小数)
LCD_DisplayDecimal(-123456,1); 显示结果:-12345.6 (1位小数)
* 说 明: 该函数能够显示满屏7位数字,但执行时间较长,耗电大。
****************************************************************************/
void LCD_DisplayLongDecimal( long int Number, char DOT)
{
char Neg;
char i;unsigned char temp;
char *pLCD = (char *)&LCDM1;
char PolarLocate;
char DispBuff[8];
if(Number<0) {Number=-Number; Neg=1;} //处理负数
else Neg=0;
for(i=0;i<7;i++) //拆分数字
{
DispBuff[i]=Number%10;
Number/=10;
}
for(i=6;i>DOT;i--) //消隐无效"0"
{
if (DispBuff[i]==0) DispBuff[i]=SP;
else break;
}
PolarLocate=i+1; // 负号显示在第一个有效数字左边
if(DOT>3) DOT=255; // 无效的小数点不显示
if(DOT<1) DOT=255; // LCD048段码中只有123位数字有小数点
for(i=0;i<7;i++)
{
temp=LCD_Tab[DispBuff[i]]; //查表
if (DOT==i) temp|=DOTSEG;//显示小数点
if ((PolarLocate==i)&&(Neg)) temp|=NEGSEG;//负号
pLCD[i]=temp; //写入显存
}
}
/****************************************************************************
* 名 称:LCD_DisplayDecimal()
* 功 能:在LCD上显示一个带有小数点的短整型数据。
* 入口参数:Number:显示数值 (-32768~32767)
DOT :小数点位数(0~3)
* 出口参数:无
* 范 例: LCD_DisplayDecimal( 12345,2); 显示结果: 123.45 (2位小数)
LCD_DisplayDecimal(-12345,1); 显示结果:-1234.5 (1位小数)
* 说 明: 该函数显示数值范围小,但执行时间短,耗电小。
****************************************************************************/
void LCD_DisplayDecimal( int Number, char DOT)
{
char Neg;
char i;unsigned char temp;
char *pL