⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yijing.c

📁 基于AVR单片机的LCD液晶驱动工程源代码文件
💻 C
字号:
          //定义MCU与LCD的接口
#define LCD_EN_PORT    PORTD
#define LCD_RW_PORT    PORTD
#define LCD_RS_PORT    PORTD
#define CONTROL_PORT   DDRD
#define LCD_DATA_PORT  PORTC
#define LCD_DATA_DDR   DDRC
#define LCD_DATA_PIN   PINC


#define LCD_RS         0x10   //portD4         out/in
#define LCD_RW         0x20   //portD5         out
#define LCD_EN         0x40   //portD6 port        out
#define LCD_DATA       0xff   //portC 0~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_wait_Ready   (void);
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);




#include "iom8535v.h"
#include "macros.h"
#include "RT12864_LCD.h"
#include "declare.h"

/**********************************************************
RT12864M LCD DISPLAY
建立时间:2005年2月1号
修改日期:2005年2月1号

**********************************************************/
void LCD_init(void)
  {
    delay_nms(1);
    LCD_write_char(0x01,0);      //显示清屏
    LCD_write_char(0x0C,0);      //显示开
   LCD_write_char(0x80,0);      //显示光标移动设置
  }

void LCD_en_write(void)        //EN端产生一个高电平脉冲,写LCD
  {

    LCD_EN_PORT |= LCD_EN;    //EN=1  ,LCD_EN=1
    delay_nus(1);
    LCD_EN_PORT &= ~LCD_EN;   //EN=0  ,LCD_EN=1
  }

/*-----------------------------------------------------------------------
LCD_write_char    : 中英文字符串显示函数
LCD_write函数功能:当command=0时,向LCD写入数据,否则向LCD写
                   入命令
输入参数:*s      :中英文字符串指针;
          X、Y    : 显示字符串的位置,X:0-15,Y:0-1
                    LCD第一行显示寄存器地址:0X80-0X87
					LCD第二行显示寄存器地址:0X90-0X9F
					LCD第三行显示寄存器地址:0X88-0X8F
                    LCD第四行显示寄存器地址:0X98-0X9F
-----------------------------------------------------------------------*/
void LCD_write_char(unsigned command,unsigned  data)
  {
   unsigned  command_temp;
	unsigned  data_temp;
    command_temp = command;
    data_temp = data;
    LCD_wait_Ready();
     if (command == 0)
      {
	  LCD_EN_PORT &= ~LCD_EN; //EN=0
	  LCD_RS_PORT |= LCD_RS;  //RS=1
   	  LCD_RW_PORT &= ~LCD_RW;      //RW=0
	  LCD_DATA_PORT = data_temp;   //send 8bit
	  LCD_en_write();
      }
    else
      {
	  LCD_EN_PORT &= ~LCD_EN;  //EN=0
	  LCD_RS_PORT &= ~LCD_RS; //RS=0
	  LCD_RW_PORT &= ~LCD_RW; //RW=0
	  LCD_DATA_PORT = command_temp;//send command
	  LCD_en_write();
      }
	  LCD_RW_PORT |= LCD_RW;
      LCD_RS_PORT ^= LCD_RS;
  }

void LCD_wait_Ready(void)                  //等待LCD空闲
  {
    LCD_DATA_DDR &= ~0x80;                 //PD7 I/O口方向设置为输入
    LCD_RS_PORT &= ~LCD_RS;                //RS=0
   delay_1us(1);
  	LCD_RW_PORT |= LCD_RW;                 //RW=1
    LCD_EN_PORT |= LCD_EN;                 //EN=1
    while (!( LCD_DATA_PIN&0x80 ) == 0);	
    LCD_EN_PORT &= ~LCD_EN;                //EN=0
    LCD_DATA_DDR |= 0xFF;
  }

/*-----------------------------------------------------------------------
LCD_set_xy        : 设置LCD显示的起始位置

输入参数:x、y    : 显示字符串的位置,X:0-15,Y:0-1
                    LCD第一行显示寄存器地址:0X80-0X8F
                    LCD第一行显示寄存器地址:0XC0-0XCF

编写日期          :2005
最后修改日期      :2005	
-----------------------------------------------------------------------*/
void LCD_set_xy( unsigned char x, unsigned char y )
  {
    unsigned char address;
	switch(y){
	case 0:
	   address = 0x80 + x;
	    break;
    case 1:
	    address = 0x90 + x;
		break;
	case 2:
	address = 0x88 + x;
	break;
    case 3:
	address = 0x98 + x;
	break;
	default:address = 0x80 + x;}
    LCD_write_char( address, 0 );
	
  }
/*-----------------------------------------------------------------------
LCD_write_string  : 中英文字符串显示函数

输入参数:*s      :英文字符串指针;
          X、Y    : 显示字符串的位置

编写日期          :2005
最后修改日期      :2005	
-----------------------------------------------------------------------*/
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 ++;
		delay_nms(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<1140;i++);
  }

void delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int i=0;
   for (i=0;i<n;i++)
   delay_1ms();
  }
  #include "iom8535v.h"
#include "macros.h"
#include "RT12864_LCD.h"
#include "declare.h"
//E4 D9 FF  配置熔丝参数 @8MHz

char *str1,*str2,*str3,*str4,*str5;
void main(void)
  {
  OSCCAL=0XA1;
  str1="欢度春节";

  str2="RS-PD3 RW-PD4";
  str3="EN-PD5 DAT-PC0~7";
  str4="www.ouravr.com";
  str5="接线表";
   LCD_DATA_DDR|=LCD_DATA; //set output 0xff
   CONTROL_PORT |= LCD_RS | LCD_EN | LCD_RW; //设置控制输出
  // LCD_EN_PORT &= ~LCD_EN;
   delay_nms(10);
     LCD_init();
	  while(1){
	LCD_write_char( 0x01, 0 );   //清屏
	delay_nms(1000);
	LCD_write_string(2,1,str1);
	delay_nms(4000);
	LCD_write_char( 0x01, 0 );   //清屏
	LCD_write_string(2,0,str5);
	LCD_write_string(0,1,str2);
	LCD_write_string(0,2,str3);
	LCD_write_string(0,3,str4);
	delay_nms(4000);
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -