您的位置:首页单片机AVR单片机
内容搜索:
阅读内容
背景:#EDF0F5 #FAFBE6 #FFF2E2 #FDE6E0 #F3FFE1 #DAFAF3 #EAEAEF 默认  

LCD1602程序

[日期:2008-01-12 ] [来源:ourAVR 作者:] [字体: (投递新闻)
配置:

 ATMega16L @3.3V @7.3728MHz
 1602B LCD @5.0V @6 lines
 Compiler: WinAVR 20060125

我自己搭的万用板,1602用5V供电,M16用3.3V供电。为了使LCD尽快投入运转,同时也因为懒惰 ... 便参考了网站上很多的1602的帖子,包括网站收集帖,程序下载后稍加修改,编译通过,下载到M16中,总是没有反应。

在这个过程中,我确信我的1602硬件是没有问题的,因为我手头上有个以前用51做的时间温度计(1602+18B20),放到上面都能正常显示。

整个过程中,感觉不管是网站收集的精华帖,还是普通帖子里面的1602程序,冗长繁杂 ... 比如我在一个帖子中,看到的一个1602的初始化函数,居然用了10几行代码!我在51中,不过也就4句而已啊 ...


后来,遇到了下面的程序,简明扼要,一次成功。当然,这也是从论坛中COPY的,我做了一些修改:


#include <avr/io.h>

#define LCD_EN_PORT    PORTC
#define LCD_RW_PORT    PORTC
#define LCD_RS_PORT    PORTC
#define LCD_DATA_PORT  PORTA
#define LCD_DATA_DDR   DDRA
#define LCD_DATA_PIN   PINA

//LCD的 r/w 脚直接接 GND
#define LCD_EN         0x80   //portd7        out
#define LCD_RS         0x40   //portc6        out
#define LCD_DATA       0xF0   //porta 4/5/6/7   out

/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void LCD_init         (void);
void LCD_en_write     (void);
void LCD_write_char   (unsigned command,unsigned data);
void LCD_set_xy       (unsigned char x, unsigned char y);
void LCD_write_string (unsigned char X,unsigned char Y,unsigned char *s);
void delay_nus        (unsigned int n);
void delay_nms        (unsigned int n);


void LCD_init(void)       //液晶初始化
{
 delay_nms(15);
 
 DDRA |= LCD_DATA;   // 数据为输出
 DDRC |= LCD_RS | LCD_EN;  //置位RS.EN
 
 LCD_write_char(0x28,0);  //4位显示
 LCD_write_char(0x0c,0);  //显示开
 LCD_write_char(0x01,0);  //清屏
 
 delay_nms(60);
}

void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
 LCD_set_xy( X, Y ); //写地址
   
 while (*s) {
    LCD_write_char( 0, *s );
   s ++;
 }
}
  
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_char(address, 0 );
}

void LCD_en_write(void)  //液晶使能
{
  LCD_EN_PORT |= LCD_EN;
  delay_nus(1);
  LCD_EN_PORT &= ~LCD_EN;
}
void LCD_write_char(unsigned command,unsigned data) // 写数据
{
 unsigned command_temp,data_temp;
 
 command_temp = command;
 data_temp = data;
 delay_nus(16);
 
 if(command == 0) {
   LCD_RS_PORT |= LCD_RS;  //RS=1
   LCD_DATA_PORT &= 0X0f;
   LCD_DATA_PORT |= data_temp & 0xf0;  //写高四位
   
   LCD_en_write();
   
   data_temp = data_temp << 4;  
   LCD_DATA_PORT &= 0X0f;
   LCD_DATA_PORT |= data_temp & 0xF0;   //写低四位
   
   LCD_en_write();
 }
 else {
   LCD_RS_PORT &= ~LCD_RS;   //RS=0
   LCD_DATA_PORT &= 0X0f;
   LCD_DATA_PORT |= command_temp & 0xF0; //写高四位
   
   LCD_en_write();
   
   command_temp = command_temp << 4;
   LCD_DATA_PORT &= 0x0F;
   LCD_DATA_PORT |= command_temp & 0xF0;  //写低四位
   
   LCD_en_write();
  }
}



int main(void)
{

 LCD_init();
 
 LCD_write_string(0,0,"Hello,AVR WORLD!!!");
 LCD_write_string(0,1,"hitro@tom.com");

 while(1);
 
}
   /*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void delay_1us(void)                 //1us延时函数
  {
   asm("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<1356;i++);
  }
  
void delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  }
阅读:
录入:JETTA

推荐 】 【 打印
本文评论
      全部评论
发表评论


点评: 字数
姓名:
站长推荐