|
//M16_8MHZ_20090304
/************************宏定义******************************/
#include <iom16v.h>
#include <macros.h>
#define uint unsigned int
#define uchar unsigned char
#define LCD1602_EN_PORT PORTA
#define LCD1602_RW_PORT PORTA
#define LCD1602_RS_PORT PORTA
#define LCD1602_DATA_PORT PORTA
#define LCD1602_EN (1 << PA0)
#define LCD1602_RW (1 << PA1)
#define LCD1602_RS (1 << PA2)
#define LCD1602_DATA 0xf0
//DATA必须为连续的端口高四位
#define LCD1602_EN_DDR DDRA
#define LCD1602_RW_DDR DDRA
#define LCD1602_RS_DDR DDRA
#define LCD1602_DATA_DDR DDRA
#define LCD1602_DATA_PIN PINA
/***********************函数声明***********************************/
void LCD1602_init ( void );
void LCD1602_en_wait ( void );
void LCD1602_en_write ( void );
void LCD1602_set_xy ( signed char x, uchar y );
void LCD1602_write_ins ( uchar ins );
void LCD1602_write_data ( uchar data );
void LCD1602_write_string ( uchar x,uchar y,uchar *s );
void LCD1602_write_pic ( uchar addr , uchar c[8] );
void delay_nus ( uint n );
void delay_nms ( uint n );
/************************主函数*****************************************/
void main( void )
{
uchar c1[8] = {0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02}, //年
c2[8] = {0x0f,0x09,0x0f,0x09,0x0f,0x09,0x0b,0x11}, //月
c3[8] = {0x0f,0x09,0x09,0x09,0x0f,0x09,0x09,0x0f}; //日
LCD1602_init();
LCD1602_write_pic(1,c1);
LCD1602_write_pic(2,c2);
LCD1602_write_pic(3,c3);
while(1)
{
LCD1602_write_ins(0x01);
LCD1602_write_string(0,0,"TIME:");
LCD1602_write_string(-2,0,"2009");
LCD1602_write_data(0x01);
LCD1602_write_string(-2,0,"03");
LCD1602_write_data(0x02);
LCD1602_write_string(-2,0,"06");
LCD1602_write_data(0x03);
LCD1602_write_string(1,0,"FHimem Hacker");
delay_nms(3000);
LCD1602_write_ins(0x01);
LCD1602_write_string(0,0,"feiniao200201");
LCD1602_write_string(1,0,"@163.com");
delay_nms(3000);
}
}
/************************子函数*****************************************/
void LCD1602_init( void ) //液晶初始化
{
LCD1602_EN_DDR |= LCD1602_EN ;
LCD1602_RW_DDR |= LCD1602_RW ;
LCD1602_RS_DDR |= LCD1602_RS ;
LCD1602_DATA_DDR |= LCD1602_DATA;
delay_nms(15);
LCD1602_write_ins(0x28);//显示模式设置三次(此时不管lcd空闲与否)
delay_nms(5);
LCD1602_write_ins(0x28);
delay_nms(5);
LCD1602_write_ins(0x28);
delay_nms(5);
LCD1602_write_ins(0x28);//4位显示
//以后每次写指令、读/写数据操作均需要检测忙信号
LCD1602_write_ins(0x08); //显示关闭
LCD1602_write_ins(0x01); //显示清屏
LCD1602_write_ins(0x06); //显示光标移动设置
LCD1602_write_ins(0x0c); //显示开及光标设置
}
void LCD1602_en_wait(void)
{
LCD1602_DATA_DDR&=~LCD1602_DATA;//设置busy口为输入
LCD1602_RS_PORT&=~LCD1602_RS;//RS=0
LCD1602_RW_PORT|=LCD1602_RW;//RW=1
delay_nus(1);
LCD1602_EN_PORT|=LCD1602_EN;//E=1
delay_nus(1);
while(LCD1602_DATA_PIN&(1<<7));//等待LCD1602_DB7为0
LCD1602_EN_PORT&=~LCD1602_EN;//重设E=0
LCD1602_DATA_DDR|=LCD1602_DATA;//设置busy口为输出
}
void LCD1602_en_write(void) //液晶使能
{
LCD1602_EN_PORT|=LCD1602_EN;
delay_nus(1);
LCD1602_EN_PORT&=~LCD1602_EN;
}
void LCD1602_set_xy( signed char x, uchar y ) //写地址函数
{
if(x>-1)LCD1602_write_ins( 0x40+(0x40<<x)+y);
else if(x==-1) LCD1602_write_ins( 0x40+(y<<3));
}
void LCD1602_write_data ( uchar data )
{
LCD1602_en_wait();
LCD1602_RW_PORT&=~LCD1602_RW;//RW=0
LCD1602_RS_PORT|=LCD1602_RS; //RS=1
LCD1602_DATA_PORT&=0X0f;
LCD1602_DATA_PORT|=data&0xf0; //写高四位
LCD1602_en_write();
data=data<<4;
LCD1602_DATA_PORT&=0X0f;
LCD1602_DATA_PORT|=data&0xf0; //写低四位
LCD1602_en_write();
}
void LCD1602_write_ins(unsigned char command) // 写数据
{
if(command!=0x38||command!=0x28)LCD1602_en_wait();
LCD1602_RW_PORT&=~LCD1602_RW; //RW=0
LCD1602_RS_PORT&=~LCD1602_RS; //RS=0
LCD1602_DATA_PORT&=0X0f;
LCD1602_DATA_PORT|=command&0xf0; //写高四位
LCD1602_en_write();
command=command<<4;
LCD1602_DATA_PORT&=0x0f;
LCD1602_DATA_PORT|=command&0xf0; //写低四位
LCD1602_en_write();
}
void LCD1602_write_string(uchar x,uchar y,uchar *s)
{
LCD1602_set_xy( x, y );
while (*s)
LCD1602_write_data( *s++ );
}
void LCD1602_write_pic(uchar addr,uchar c[8])
{
uchar i=8;
LCD1602_set_xy( -1, addr );
while (i--)
LCD1602_write_data( *c++ );
}
void delay_nus(unsigned int n) //N us延时函数
{
if(n>3) n=n-3;
else return;
while(n--)
{
NOP();
NOP();
}
}
void delay_nms(unsigned int x)
{
unsigned int i;
while(x--)
{
i=1331;
while(i--);
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|