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

📄 lcd.c

📁 Proteus 仿真练习:LCD3202测试电路
💻 C
字号:
/*            AVR生成代码测试程序                   */
/*--------------------------------------------------*/
/*     程序由AVR辅助开发工具V2.1.1自动生成          */
/*     AVR系统的处理器为:   ATMega16               */
/*     AVR系统的晶振频率:   8.0000 Mhz             */
/*--------------------------------------------------*/

#include <avr/interrupt.h>       //中断函数
#include <avr/sfr_defs.h>       //寄存器
#include <compat/deprecated.h>  //宏指令
#include <avr/pgmspace.h>       //FLASH宏指令
#include <avr/portpins.h>       //PORT定义
#include <avr/io.h>             //IO口描述
#include "lcd.h"
#include "delay.h"

/**********************************************************
光标命令
LCD16xx_write_char(1,0x0e);    //光标开
LCD16xx_write_char(1,0x0d);    //光标所在字符闪烁
LCD16xx_write_char(1,0x0c);    //光标关
**********************************************************/
/**********************************************************
TC16xxB LCD DISPLAY
LCD_write函数功能:当command=0时,向LCD写入数据,否则向LCD写
                   入命令
LCD第一行显示寄存器地址:0x80-0x8F
LCD第二行显示寄存器地址:0xC0-0xCF
**********************************************************/
void LCD16xx_init(void)
{
    LCD_DATA_DDR |=  LCD_DATA;
    LCD_EN_DDR   |=  LCD_EN;
    LCD_RS_DDR   |=  LCD_RS;
    LCD_RW_DDR   |=  LCD_RW;
    RW_L;
	
    LCD_DATA_PORT =  0x20;
    LCD16xx_en_write();
    delay_nus(50);
   
    LCD16xx_write_char(1,0x28);      //4bit test
    delay_nms(5);
    LCD16xx_write_char(1,0x0c);      //显示开
    LCD16xx_write_char(1,0x01);      //显示清屏
    LCD16xx_write_char(1,0x06);      //显示光标移动设置
}

void LCD16xx_en_write(void)          //EN端产生一个高电平脉冲,写LCD
{
    E_H;
    delay_nus(4);                    //如果晶振频率太高,则可相应提高延时
    E_L;
}

/************************************************************************/
/* LCD16xx清屏                                                          */
/************************************************************************/
void LCD16xx_clr(void)
{
    LCD16xx_write_char(1,0x01);
}

/*-----------------------------------------------------------------------
LCD16xx_write_char    : 英文字符串显示函数

输入参数:*s      :英文字符串指针;
          X、Y    : 显示字符串的位置,X:0-15,Y:0-1
                    LCD第一行显示寄存器地址:0x80-0x8F
                    LCD第一行显示寄存器地址:0xC0-0xCF
-----------------------------------------------------------------------*/
void LCD16xx_write_char(unsigned char command,unsigned char data)
{
    unsigned char datah,datal;        

    datah = data;
    datal = data<<4 ;

    //LCD16xx_wait_Ready();
   
    RW_L;                                   //RW=0
    if (command == 0)                       //data
       RS_H;                                //RS=1
    else                                    //command
       RS_L;                                //RS=0
       
    LCD_DATA_PORT &= 0x0F;
    LCD_DATA_PORT |= datah&0xf0;            //send high 4bit
    LCD16xx_en_write();

    LCD_DATA_PORT &= 0x0F;
    LCD_DATA_PORT |= datal&0xf0;            //send low 4bit
    LCD16xx_en_write();
}

void LCD16xx_wait_Ready(void)              //等待LCD空闲
{
    LCD_DATA_DDR &= ~0x80;                 //PD7 I/O口方向设置为输入
    RW_H;                                  //RW=1
    RS_L;                                  //RS=0
    E_H;                                   //EN=1
    while (!( LCD_DATA_PIN&0x80 ) == 0);   //RW=1,读PD7,为0表示空闲;
    E_L;                                   //EN=0
    LCD_DATA_DDR |= LCD_DATA;
}

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

输入参数:x、y    : 显示字符串的位置,X:0-15,Y:0-1
                    LCD第一行显示寄存器地址:0x80-0x8F
                    LCD第一行显示寄存器地址:0xC0-0xCF
-----------------------------------------------------------------------*/
void LCD16xx_set_xy( unsigned char x, unsigned char y )
{
    unsigned char address;
    if (y == 0)
       address = 0x80 + x;
    else if(y == 1)
       address = 0xc0 + x;
    else if(y == 2)
       address = 0x90 + x;
    else
       address = 0xd0 + x;
    LCD16xx_write_char( 1,address );
}
/*-----------------------------------------------------------------------
LCD16xx_write_string  : 英文字符串显示函数

输入参数:*s      :英文字符串指针;
          X、Y    : 显示字符串的位置
-----------------------------------------------------------------------*/
void LCD16xx_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
    LCD16xx_set_xy( X, Y );
    while (*s) 
    {
        delay_nus(5);
        LCD16xx_write_char( 0, *s );
        s++;
    }
}

⌨️ 快捷键说明

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