|
发表于 2010-8-30 10:49:38
|
显示全部楼层
/**************************************************************************
1602 四线制驱动程序--基于MSP430F149
File Name: LCD1602x4_mps.h
Author: HuNB
***************************************************************************/
#include<msp430x14x.h>
#define set(x) P6OUT |=x
#define clr(x) P6OUT &=~x
#define rs BIT2 //RA1
#define rw BIT3 //RA2
#define e BIT4 //RA3
/*--------------------------------------------------------------------------------------------------
函数说明
--------------------------------------------------------------------------------------------------*/
void init(); //申明I/O口初始化函数
void delay_nus(unsigned int n); //N nS延时函数
void delay_nms(unsigned int n); //N mS延时函数
void LCD_init(void); //LCD初始化函数
void LCD_en_write(void); //LCD写入使能函数
void LCD_write_command(unsigned char command) ; //LCD写入命令函数
void LCD_write_data(unsigned char Recdata); //LCD写入数据函数
void LCD_set_xy (unsigned char x, unsigned char y); //LCD设置显示位置函数
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata); //LCD显示字符函数
int LCD_PutStr(unsigned char *DData,int pos); //LCD显示字符串函数
//int LCD_PutNum(unsigned long num,int XS,int pos);//LCD显示数字函数
int p=0;
//---------------------------------------
//---------------------------------------
//I/O口初始化函数
void init()
{
//ADCON1=0X07; //设置A口为普通I/O口
//TRISA=0X00; //设置A口为输出
//TRISD=0X00; //设置D口为输出
P4DIR=0xff;
P6DIR=0x1c; //设置A口为输出
WDTCTL = WDTPW + WDTHOLD; //杀狗
}
void delay_1us(void) //1us延时函数
{
_NOP();
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
void LCD_en_write(void) //液晶使能
{
set(e);
delay_nus(1);
clr(e);
}
void LCD_write_command(unsigned char command) //写指令
{
unsigned char temp;
temp=command>>4; //高四位移到低四位
delay_nus(16);
clr(rs); //RS=0 选择指令地址
clr(rw); //RW=0 写操作
P4OUT&=0Xf0; //清低四位
P4OUT|=temp&0x0f; //写高四位
//command&0x0f;
LCD_en_write();
P4OUT&=0xf0; //清高四位
P4OUT|=command&0x0f; //写低四位
LCD_en_write();
set(rw); //RW=1 结束写操作
}
void LCD_write_data(unsigned char Recdata) //写数据
{ unsigned char temp;
temp=Recdata>>4; //高四位移到低四位
delay_nus(16);
set(rs); //RS=1 选择数据地址
clr(rw); //RW=0 写操作
P4OUT&=0Xf0; //清高四位
P4OUT|=temp&0x0f; //Recdata&0xf0写高四位
LCD_en_write();
// Recdata=Recdata<<4; //低四位移到高四位
P4OUT&=0Xf0; //清高四位
P4OUT|=Recdata&0x0f; //写低四位
LCD_en_write();
set(rw); //RW=1 结束写操作
}
void LCD_set_xy( unsigned char x, unsigned char y ) //写地址函数
{
unsigned char address;
if (y == 0) address = 0x80 + x;
else address = 0xc0 + x;
LCD_write_command(address);
}
void LCD_init(void) //液晶初始化
{
LCD_write_command(0x28);
delay_nus(40);
LCD_write_command(0x28);
delay_nus(40);
LCD_write_command(0x28);
delay_nus(40);
LCD_en_write();
delay_nus(40);
LCD_write_command(0x28); //4位显示
LCD_write_command(0x0c); //显示开
LCD_write_command(0x01); //清屏
delay_nms(5);
}
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char Recdata) //列x=0~15,行y=0,1
{
LCD_set_xy(X, Y); //写地址
LCD_write_data(Recdata);
}
int LCD_PutStr(unsigned char *DData,int pos){
unsigned char i;
if(pos==-1){
LCD_write_command(0x01); //清屏
delay_nms(2);
pos=0;
}
while((*DData)!='\0'){
switch(*DData){
case '\n': //如果是\n,则换行
{
if(pos<17){
for(i=pos;i<16;i++)
LCD_write_char(i%16, i/16, ' ');
pos=16;
}
else{
for(i=pos;i<32;i++) LCD_write_char(i%16, i/16, ' ');
pos=32;
}
break;
}
case '\b': //如果是\b,则退格
{
if(pos>0) pos--;
LCD_write_char(pos%16, pos/16, ' ');
break;
}
default:
{
if((*DData)<0x20){
*DData=' ';
}
LCD_write_char(pos%16, pos/16,*DData);
pos++;
break;
}
}
DData++;
}
return(pos);
}
/*----------------------------以下函数用于输出数字---------------------
int LCD_PutNum(unsigned long num,int XS,int pos){ //从右边数,保留几位小数
unsigned long tmp=0;
unsigned char numbits=0;
if(pos==-1){
LCD_write_command(0x01);
delay_nms(2);
pos=0;
}
if(num==0){
LCD_write_char(pos%16, pos/16, '0');
pos++;
}
else{
if(num<0){
LCD_write_char(pos%16, pos/16, '-');
num*=(-1);
pos++;
}
while(num){
tmp=tmp*10+(num%10);
num=num/10;
numbits++;
}
while(tmp){
LCD_write_char(pos%16, pos/16, (tmp%10)+48);
tmp=tmp/10;
pos++;
numbits--;
if(numbits==XS) pos=LCD_PutStr(".",pos); //显示小数点
}
while(numbits--){
LCD_write_char(pos%16, pos/16, '0');
pos++;
}
}
return(pos);
}**/ |
|