Chinaunix首页 | 论坛 | 博客
  • 博客访问: 171543
  • 博文数量: 46
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 224
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-21 09:42
个人简介

菜鸟一枚,请多多关照

文章分类

全部博文(46)

文章存档

2016年(3)

2015年(24)

2014年(19)

我的朋友

分类: C/C++

2014-12-09 19:01:59

原文地址:LCD1602-msp430f249 作者:yyj1028

#include
#include

#define uchar unsigned char

void delay_1ms();
void delay_10us();

void delay_10us()
{
    unsigned int i =100;

    i--;
    i--;
    i--;
}
void delay_1ms()
 {
      uchar i,j;
    for(i=0;i>2;i++)
    {
        for(j=0;j<20;j++)
        ;
    }
 }

bool lcd_1602_busy()
{
    uchar tmp;
    P1DIR = 0xff;
    P1OUT = 0xff;
    //lcd_1602_RW = 1;
    //lcd_1602_RS = 0;
    P2DIR=0xff;
    P2OUT |=BIT1;//BIT1=0x0002
    P2OUT &=~BIT2;//bit2=0x0004 =0000 0100 ~bit2 =1111 1011
    
    P2OUT |=BIT0;
    delay_10us();
    P1DIR =0x00;
    tmp = P1IN;
    P2OUT &=~BIT0;
    //lcd_1602_EN = 0;

return (bool)(0x80 & tmp); 
}

void lcd_1602_write_cmd(uchar cmd)
{
    while(lcd_1602_busy());

    P2DIR=0xff;
    P2OUT &=~BIT1;
    //lcd_1602_RW = 0;
    P2OUT &= ~BIT2;
    //lcd_1602_RS = 0;
    P1DIR =0xff;
    P1OUT =cmd;
    //P0 = cmd;


    P2OUT |=BIT0;
    //lcd_1602_EN = 1;
    delay_10us();
    P2OUT &= ~BIT0;
    //lcd_1602_EN = 0;
}


void lcd_1602_write_data(uchar dat)
{
    while(lcd_1602_busy());
    
    P2DIR =0xff;
    P2OUT &=~BIT1;
    //lcd_1602_RW = 0;
    //lcd_1602_RS = 1;
    //P0 = dat;
    P2OUT |=BIT2;
    P1DIR=0xff;
    P1OUT=dat;
    //lcd_1602_EN =1;
    delay_10us();
    P2OUT &= ~BIT0;
    //lcd_1602_EN = 0;
}

void lcd_1602_init()
{
    lcd_1602_write_cmd(0x01);
    lcd_1602_write_cmd(0x02);
    lcd_1602_write_cmd(0x06);
    lcd_1602_write_cmd(0x38);
    lcd_1602_write_cmd(0x01);
    delay_1ms();
}


void lcd_1602_write_char(uchar x, uchar y, uchar dat)
{
    if(x == 0)
    {
        lcd_1602_write_cmd(0x80 +y%16);
        lcd_1602_write_data(dat);
    }
    else
    {
        lcd_1602_write_cmd(0xc0 + y%16);
        lcd_1602_write_data(dat);
    }
}

void lcd_1602_write_string(uchar x, uchar y, uchar *string)
{
    while(*string != '\0')
    {
    lcd_1602_write_char(x,y++,*(string));
    }
}
void main()
{
    P1SEL=0X00;
    P2SEL=0X00;
    lcd_1602_write_cmd(0x0e);

    lcd_1602_write_cmd(0x80);
    lcd_1602_write_data('1');
    lcd_1602_write_data('2');
    lcd_1602_write_data('3');
    lcd_1602_write_data('4');
    lcd_1602_write_data('5');
}
阅读(1602) | 评论(0) | 转发(0) |
0

上一篇:7SEG-msp430f249

下一篇:atexit

给主人留下些什么吧!~~