Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1597744
  • 博文数量: 354
  • 博客积分: 8137
  • 博客等级: 中将
  • 技术积分: 5137
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-26 15:40
文章分类

全部博文(354)

文章存档

2010年(300)

2009年(54)

分类: 嵌入式

2010-08-12 16:33:19

#include <avr/io.h>
#include <util/delay.h>
#include "lcd1602.h"


#define Set_Lcd1602_RS Contrl_Port |= (1 << RS)
#define Clr_Lcd1602_RS Contrl_Port &= ~(1 << RS)
#define Set_Lcd1602_RW Contrl_Port |= (1 << RW)
#define Clr_Lcd1602_RW Contrl_Port &= ~(1 << RW)
#define Set_Lcd1602_EN Contrl_Port |= (1 << EN)
#define Clr_Lcd1602_EN Contrl_Port &= ~(1 << EN)

void LCD1602BusyCheck(void)
{
    uint8_t i = 254;
    Data_DDR = 0x00;
    Clr_Lcd1602_RS;
    Set_Lcd1602_RW;
    Set_Lcd1602_EN;
    _delay_us(5);
    while((i--) && (Data_PIN & 0x80));
    Data_DDR = 0xff;
    _delay_us(5);
    Clr_Lcd1602_EN;
    _delay_us(5);
    
}

void LCD1602WriteOperation(uint8_t Data,uint8_t ComOrData)
{
    LCD1602BusyCheck();
    if(0 == ComOrData)
        Clr_Lcd1602_RS;
    else if(1 == ComOrData)
        Set_Lcd1602_RS;
    Clr_Lcd1602_RW;
    Set_Lcd1602_EN;
    _delay_us(5);
    Data_Port = Data;
    _delay_us(5);
    Clr_Lcd1602_EN;
    _delay_us(100);
}

void LCD1602Init(void)
{
    LCD1602WriteOperation(0x38,0);
    _delay_ms(15);
    LCD1602WriteOperation(0x38,0);
    _delay_ms(5);
    LCD1602WriteOperation(0x38,0);
    _delay_ms(5);
    LCD1602WriteOperation(0x38,0);
    _delay_ms(5);

    LCD1602WriteOperation(0x0c,0);
    _delay_ms(1);
    LCD1602WriteOperation(0x06,0);
    _delay_ms(1);
    LCD1602WriteOperation(0x01,0);
    _delay_ms(1);
}

void LCD1602MoveToPosition(uint8_t ucx,uint8_t ucy)
{
    if(0 == ucx)
        {
    LCD1602WriteOperation(0x80 + ucy,0);
    }
    if(1 == ucx)
        {
    LCD1602WriteOperation(0xC0 + ucy,0);
    }
}

void LCD1602PrintString(uint8_t const * str)
{
    while(*str != '\0')
        {
    LCD1602WriteOperation(*str,1);
    str++;
    }
}



#ifndef __LCD1602_H__
#define __LCD1602_H__

#define RS PA4
#define RW PA5
#define EN PA6
#define Contrl_Port PORTA
#define Contrl_DDR DDRA
#define Contrl_PIN PINA

#define Data_Port PORTB
#define Data_DDR DDRB
#define Data_PIN PINB

//void LCD1602BusyCheck(void);

void LCD1602WriteOperation(uint8_t Data,uint8_t ComOrData);
void LCD1602Init(void);
void LCD1602MoveToPosition(uint8_t ucx,uint8_t ucy);
void LCD1602PrintString(uint8_t const * str);

#endif



#include <avr/io.h>
#include <util/delay.h>
#include "lcd1602"

uint8_t ucString1[] = {"This a LCD demo"};
uint8_t ucString2[] = {""};
uint8_t ucString3[] = {" KEEP MOVING! "};
uint8_t ucString4[] = {"BY: kevin chin"};

int main(void)
{
    PORTA = 0xff;
    DDRA = 0xff;
    PORTB = 0xff;
    DDRB = 0xff;

    LCD1602Init();

    while(1)
        {
    LCD1602MoveToPosition(0,0);
        LCD1602PrintString(ucString1);
        LCD1602MoveToPosition(1,0);
        LCD1602PrintString(ucString2);
        _delay_ms(5000);    //延时5毫秒

        LCD1602MoveToPosition(0,0);
        LCD1602PrintString(ucString3);
        LCD1602MoveToPosition(1,0);        //延时5毫秒

        LCD1602PrintString(ucString4);
        _delay_ms(5000);
    }
}


文件:LCD1602.zip
大小:1KB
下载:下载

阅读(1430) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~