原创 MSP430+LCD1602

2007-9-16 10:56 5952 7 7 分类: MCU/ 嵌入式

 



#include  <msp430x14x.h>
#include "lcd1602_h.h"


void delay_nms(void);
 
void main(void)
{
  
  WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer
 LCD_init();
 while(1)    
 { 
         LCD_write_string(0,0,"Wellcomefighting");
                LCD_write_string(0,1,"i am the  one ");
                delay_nms();
                LCD_write_string(0,0,"HELLO      WORLD ");
                LCD_write_string(0,1,"  lianghongzhi  ");
                delay_nms(); 
 }
}


void delay_nms(void)      
{
    unsigned int count,i;
    for(count = 4; count > 0; count--)
   for(i=50000;i>0;i--); 
}


下面是1602的子程序
/******************************************************************/
//  
//  DIY by 梁鸿志
//
//           硬件连接信息(调试液晶型号LCD1602)
//              1--VSS     GND           
//              2--VDD  VCC       
//  3--V0  至地
//  4--RS  数据/命令选择
//  5--R/W         读写控制
//  6--E  使能
//  7--DB0  DB0
//  14-DB7  DB7
//  15-LEDA  背光正  
//  16-LEDK  背光负(接地) 
//
//  硬件上需要修改的话,有两个函数需要改动
//  LCD_Busy() 读忙函数
//  LCD_Init() 初始化函数
/******************************************************************/
#include "lcd1602_h.h"
/********************************************************************/
void LCD_init(void)

 P2DIR |= 0xff;     
        P1DIR |= 0xff; 
 LCD_en_command(0x0C);
 LCD_en_command(0x06);
 LCD_en_command(0x80);
 LCD_en_command(0x38);
 LCD_en_command(0x01);
}
/********************************************************************/


void LCD_Busy(void)
{
    char BF = 0;    //  busy flag
    CLR_LCD_RS;
    SET_LCD_RW;
    P2DIR = 0x00;   //  modify this if you change hardware
    do
    {
     SET_LCD_EN;
        BF = P2IN;
        CLR_LCD_EN;
    } while(BF & 0x80);
    P2DIR = 0xff;   //  modify this if you change hardware
}


void LCD_delay(void)
{
 unsigned int i;
 for(i=500;i>0;i--);     // need modify this time at differert mcu
}
/********************************************************************/ 
void LCD_en_command(unsigned char command)
{
        LCD_Busy();
 CLR_LCD_RS;
 CLR_LCD_RW;
 SET_LCD_EN;
 LCD_I0=command;
 CLR_LCD_EN;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
  LCD_Busy();
 SET_LCD_RS;
 CLR_LCD_RW;
 SET_LCD_EN;
 LCD_I0=dat;
 CLR_LCD_EN;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y ) 
{
     unsigned char address;
     if (y == 0)
      address = 0x80 + x;
     else
         address = 0xc0 + x;
     LCD_en_command(address);
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
 LCD_set_xy( x, y );
 LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
    LCD_set_xy( X, Y ); 
    while (*s)
    {
     LCD_I0=*s;
        LCD_en_dat(*s);  
 s ++;
    }
}


1602自身的头文件
/********************************************************************/
#ifndef __LCD1602_H__
#define __LCD1602_H__


#include "msp430x14x.h"


#define LCDCTL   P1OUT
#define LCD_I0   P2OUT
#define SET_LCD_RS  LCDCTL |= BIT0
#define CLR_LCD_RS  LCDCTL &= ~BIT0
#define SET_LCD_RW;  LCDCTL |= BIT2
#define CLR_LCD_RW;  LCDCTL &= ~BIT2
#define SET_LCD_EN;  LCDCTL |= BIT4
#define CLR_LCD_EN;  LCDCTL &= ~BIT4


void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);
void LCD_set_xy( unsigned char x, unsigned char y );
void LCD_en_dat(unsigned char dat);
void LCD_en_command(unsigned char command);
void LCD_init(void);
void LCD_Busy(void);


#endif

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条