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

📄 lcd1602.c

📁 AVR Devolpment Board
💻 C
字号:
#include<avr/io.h> 
#include<util/delay.h> 
 

#define CLR_RS()   (PORTD &= ~(1<<PD4))   /* RS=0 , PD4---RS */
#define SET_RS()   (PORTD |=  (1<<PD4))   /* RS=1            */
#define CLR_RW()   (PORTD &= ~(1<<PD5))   /* RW=0 , PD5---RW */
#define SET_RW()   (PORTD |=  (1<<PD5))   /* RW=1            */
#define CLR_EN()   (PORTD &= ~(1<<PD6))   /* EN=0 , PD6---EN */
#define SET_EN()   (PORTD |=  (1<<PD6))   /* EN=1            */
#define DATA_IN()  (DDRB   = 0x00)        /* Set the data bus as input  */
#define DATA_OUT() (DDRB   = 0xFF)        /* Set the data bus as output */

#define OUTDATA     PORTB                 /* Output data bus---PORTB    */
#define INDATA      PINB                  /*  Input data bus---PINB     */


void delay_ms(unsigned int ms)
{                           
	while(ms--)
		_delay_ms(1);
}

unsigned char lcd_busy()
{                          
	unsigned char result;	
    CLR_RS();   /* RS = 0 , RW = 1 , read data from lcd */
    SET_RW();
	DATA_IN();  /* Set the data bus as input */
	asm("nop"); /* After change the data bus direction , you must wait aT least one clock */
	SET_EN();
    CLR_EN();
	result = INDATA&0x80;	/* Check the MSB and */
	DATA_OUT();	            /* Set the data bus as output */
    return result;
} 

void WriteCommand(unsigned char Command)
{
	while(lcd_busy());   /* Wait while the lcd is not busy */
	CLR_RS();            /* RS = 0 , RW = 0 , write command to lcd */
	CLR_RW();
	OUTDATA=Command;
	SET_EN();
	CLR_EN();		
}

void WriteData(unsigned char Data)
{
	while(lcd_busy());   /* Wait while the lcd is not busy */
	SET_RS();            /* RS = 1 , RW = 0 , write data to lcd */
	CLR_RW();
	OUTDATA=Data;
	SET_EN();
	CLR_EN();
}

void Display(unsigned char x,unsigned char y,unsigned char *Data)
{
	if(x==0)
		WriteCommand(0x80+y);
	if(x==1)
		WriteCommand(0xC0+y);
	while(*Data!='\0')
	{
		WriteData(*Data);
		Data++;
	}
}

void LCM_Init()
{
    WriteCommand(0x38);
	WriteCommand(0x0C);  
    WriteCommand(0x06);
	WriteCommand(0x01);
}

int main(void)
{
	DDRD|=(1<<PD4)|(1<<PD5)|(1<<PD6);  /* Set RS , RW ,EN pin as output */
	LCM_Init();                        /* Config the lcd */
	while(1)
	{
		Display(0,0,"Welcome To HUST!");
		delay_ms(2000);
		WriteCommand(0x01);
		Display(1,0,"0123456789ABCDEF");
		delay_ms(2000);
		WriteCommand(0x01);
	}
	return 0;
}

⌨️ 快捷键说明

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