📄 lcd1602.h
字号:
/**************************************************************************
THE 1602 CHAR LCD LIB
COPYRIGHT (c) 2007 BY zyg.
-- ALL RIGHTS RESERVED --
File Name: LCD.h
Author: zhuyiguo
Created: 2007/4/23
Modified: NO
Revision: 1.0
***************************************************************************/
#ifndef LCD_CHAR_1602_2007_4_23
#define LCD_CHAR_1602_2007_4_23
//控制脚定义
#define RS_D asm("sbi 0x15,2")
#define RS_I asm("cbi 0x15,2")
#define RW_R asm("sbi 0x15,3")
#define RW_W asm("cbi 0x15,3")
#define E_1 asm("sbi 0x15,4")
#define E_0 asm("cbi 0x15,4")
//**************************************************************************
//ATMEGA16初始化
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x10; //m103 output only
DDRC = 0x1C;
PORTD = 0x00;
DDRD = 0x00;
}
//******************************************************************
//Watchdog initialize
// prescale: 2048K
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
WDTCR = 0x0f; //WATCHDOG ENABLED - dont forget to issue WDRs
}
//******************************************************************
//TIMER1 initialize - prescale:256
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 500mSec
// actual value: 500.000mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xC2; //setup
TCNT1L = 0xF7;
//OCR1AH = 0x3D;
//OCR1AL = 0x09;
//OCR1BH = 0x3D;
//OCR1BL = 0x09;
//OCR1CH = $OCR1CH$;
//OCR1CL = $OCR1CL$;
//ICR1H = 0x3D;
//ICR1L = 0x09;
TCCR1A = 0x00;
TCCR1B = 0x04; //start Timer
}
//******************************************************************
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
watchdog_init();
timer1_init();
MCUCR = 0x00;
GICR = 0xc0;//interrupt int0,int1 enable
TIMSK = 0x04; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//******************************************************************
//延时函数
void delay_us(int time)//微妙级延时函数
{
WDR();
while(time--)
{
NOP();NOP();NOP();NOP();NOP();NOP();NOP();NOP();
}
}
void delay_ms(int time)//毫秒级延时函数
{
while(time--)
{
delay_us(1000);
}
}
//********************************************************************
//读LCD1602状态字
unsigned char lcd_read_code(void)
{
unsigned char code;
DDRA=0x00;
RS_I;
RW_R;
E_1;
delay_ms(1);
code=PINA;
E_0;
RW_W;
DDRA=0xff;
return code;
}
//*******************************************************************
//判断LCD是否为忙状态
void judgebusy(void)
{
//unsigned char t;
//t=lcd_read_code();
while(((lcd_read_code())&0x80)==0x80)
{
//t=lcd_read_code();
//if((t&0x80)!=0x80) break;
}
}
//写显示数据*******************************************************************
void lcd_write_data(unsigned char data)
{
WDR();
DDRA=0xff;
RS_D;
delay_us(1);
RW_W;
delay_us(1);
E_1;
//judgebusy();
delay_ms(1);
PORTA=data;
E_0;
delay_us(1);
//E_0;
RW_R;
DDRA=0x00;
WDR();
}
//写状态字********************************************************************
void lcd_write_code(unsigned char code)
{
WDR();
DDRA=0xff;
RS_I;
delay_us(1);
RW_W;
delay_us(1);
E_1;
//judgebusy();
delay_ms(1);
PORTA=code;
E_0;
delay_us(1);
//E_0;
RW_R;
DDRA=0x00;
WDR();
}
//显示字符串******************************************************************
void Print(unsigned char *str)
{
while(*str!='\0')
{
WDR();
lcd_write_data(*str);
//delay_ms(100);
str++;
}
}
//显示位置************************************************************************
void GotoXY(unsigned char x,unsigned char y)
{
if(y==0)
lcd_write_code(0x80|x);
if(y==1)
lcd_write_code(0x80|(x-0x40));
}
void lcd_init(void)
{
WDR();
delay_ms(400);//等待LCD稳定
E_1;
//lcd_write_code(0x01);
delay_ms(15);
lcd_write_code(0x38);//显示模式:8位数据端口,2行显示,5*7点阵
delay_ms(5);
lcd_write_code(0x38);
delay_ms(5);
lcd_write_code(0x38);
delay_ms(5);
lcd_write_code(0x0C);//开启显示, 无光标
delay_ms(5);
lcd_write_code(0x06);//读写一个字符后光标加1,地址加1
delay_ms(5);
lcd_write_code(0x01);//清屏:数据指针清0,显示清0
delay_ms(5);
lcd_write_code(0X02);//数据指针清0
WDR();
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -