Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48505
  • 博文数量: 24
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 62
  • 用 户 组: 普通用户
  • 注册时间: 2014-10-14 21:50
个人简介

只有想不到的,没有做不到的

文章分类
文章存档

2014年(24)

我的朋友

分类: C/C++

2014-11-23 10:33:07

#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');
}
阅读(1392) | 评论(0) | 转发(1) |
1

上一篇:嵌入式系统制作

下一篇:7SEG-msp430f249

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