#include"delay.h"
#define LCD_EN_PORT PORTC //以下2个要设为同一个口
#define LCD_EN_DDR DDRC
#define LCD_RS_PORT PORTC //以下2个要设为同一个口
#define LCD_RS_DDR DDRC
#define LCD_RW_PORT PORTC //以下2个要设为同一个口
#define LCD_RW_DDR DDRC
#define LCD_DATA_PORT PORTC //以下3个要设为同一个口
#define LCD_DATA_DDR DDRC //一定要用高4位
#define LCD_DATA_PIN PINC
#define LCD_RS (1<<PC0) //0x04 portC0 out
#define LCD_RW (1<<PC1) //0x02 portC1 out
#define LCD_EN (1<<PC2) //0x08 portC2 out
#define LCD_DATA ((1<<PC4)|(1<<PC5)|(1<<PC6)|(1<<PC7)) //0xf0 portC4/5/6/7 out
/*------------------------------------------------------------------------------
函数说明
------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_write_command(unsigned char command) ;
void LCD_write_data(unsigned char data);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s); //X列Y行显示字符串
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char count,unsigned long int data); //X列Y行显示count位变量的大小
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
//==============================================================================
//#include "LCD.h"
void LCD_init(void) //液晶初始化
{
LCD_DATA_DDR|=LCD_DATA; //数据口方向为输出
LCD_EN_DDR|=LCD_EN; //设置EN方向为输出
LCD_RS_DDR|=LCD_RS; //设置RS方向为输出
LCD_RW_DDR|=LCD_RW; //设置RW方向为输出
LCD_write_command(0x28);
LCD_en_write();
delayus(40);
LCD_write_command(0x28); //4位显示
LCD_write_command(0x0c); //显示开
LCD_write_command(0x01); //清屏
delayms(2);
}
void LCD_en_write(void) //液晶使能
{
LCD_EN_PORT|=LCD_EN;
delayus(1);
LCD_EN_PORT&=~LCD_EN;
}
void LCD_write_command(unsigned char command) //写指令
{
delayus(16);
LCD_RW_PORT&=~LCD_RW; //RW=0
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写高四位
LCD_en_write();
command=command<<4; //低四位移到高四位
LCD_DATA_PORT&=0x0f; //清高四位
LCD_DATA_PORT|=command&0xf0; //写低四位
LCD_en_write();
}
void LCD_write_data(unsigned char data) //写数据
{
delayus(16);
LCD_RW_PORT&=~LCD_RW; //RW=0
LCD_RS_PORT|=LCD_RS; //RS=1
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data&0xf0; //写高四位
LCD_en_write();
data=data<<4; //低四位移到高四位
LCD_DATA_PORT&=0X0f; //清高四位
LCD_DATA_PORT|=data&0xf0; //写低四位
LCD_en_write();
}
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_command( address);
}
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCD_write_data( *s );
s ++;
}
}
/*void LCD_write_change(unsigned char X,unsigned char Y,unsigned char *s) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
unsigned int sxh4;
unsigned int sxh3;
unsigned int sxh2;
unsigned int sxh1;
LCD_DISON();
CPU_DELAYTIME(100);
LCD_WRI(row+col-1);
CPU_DELAYTIME(2000);
sxh4=dats/10;
sxh3=sxh4/10;
sxh2=sxh3/10;
sxh1=sxh2/10;
if(count>=5){LCD_WRD(sxh1+48);}
if(count>=4){LCD_WRD(sxh2-sxh1*10+48);}
if(count>=3){LCD_WRD(sxh3-sxh2*10+48);}
if(count>=2){LCD_WRD(sxh4-sxh3*10+48);}
LCD_WRD(dats-sxh4*10+48);
}
*/
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char count,unsigned long int data) //列x=0~15,行y=0,1
{
unsigned long int sxh10;
unsigned long int sxh9;
unsigned long int sxh8;
unsigned long int sxh7;
unsigned long int sxh6;
unsigned long int sxh5;
unsigned long int sxh4;
unsigned long int sxh3;
unsigned long int sxh2;
unsigned long int sxh1;
LCD_set_xy( X, Y ); //写地址
sxh9=data/10;
sxh8=sxh9/10;
sxh7=sxh8/10;
sxh6=sxh7/10;
sxh5=sxh6/10;
sxh4=sxh5/10;
sxh3=sxh4/10;
sxh2=sxh3/10;
sxh1=sxh2/10;
if(count>=10){LCD_write_data(sxh1+0X30);}
if(count>=9){LCD_write_data(sxh2-sxh1+0X30);}
if(count>=8){LCD_write_data(sxh3-sxh2*10+0X30);}
if(count>=7){LCD_write_data(sxh4-sxh3*10+0X30);}
if(count>=6){LCD_write_data(sxh5-sxh4*10+0X30);}
if(count>=5){LCD_write_data(sxh6-sxh5*10+0X30);}
if(count>=4){LCD_write_data(sxh7-sxh6*10+0X30);}
if(count>=3){LCD_write_data(sxh8-sxh7*10+0X30);}
if(count>=2){LCD_write_data(sxh9-sxh8*10+0X30);}
LCD_write_data(data-sxh9*10+0X30);
//LCD_write_data( data);
}
void Cursors_set(unsigned char mode)
{
switch(mode)
{
case 0: LCD_write_command(0x0c);break;//mode=0(关闭)
case 1: LCD_write_command(0x0d);break;//mode=1(闪动黑块)
case 2: LCD_write_command(0x0e);break;//mode=2(不闪的下划线)
case 3: LCD_write_command(0x0f);break;//mode=3(不闪的下划线+闪动黑块)
default:break;
}
}
//=======================================================================
//=========================================================================
//#include "LCD.h"
void init_devices(void)
{
//CLI(); //disable all interrupts
LCD_init();
//MCUCR = 0x00;
//GICR = 0x00;
//TIMSK = 0x00; //timer interrupt sources
//SEI(); //re-enable interrupts
}