/*************************************************************************************
项 目: 1602液晶显示实验
编 译 器: AVR Studio_4.12sp4+winAVR412
目 标 芯 片: ATmega16
时 钟: 内部RC 8000000hz
建 立 日 期: 2006-11-12
最 后 修改 日期:
*****************************************************************************************/
/*---------------------------------------------------------------------------------------
LCD引脚定义
------------------------------------------------------------------------------------------
1602pin 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
地 Vcc VL Rs Rw En D0 D1 D2 D3 D4 D5 D6 D7 灯+ 灯-
| | | | | | |
M16 PA1 PA2 PA3 PA4 PA5 PA6 PA7
*****************************************************************************************/
/*------------------------------------------------------------------------------------------
头文件
------------------------------------------------------------------------------------------*/
#include
/*-----------------------------------------------------------------------------------------
管脚宏定义
------------------------------------------------------------------------------------------*/
#define LCD_EN_PORT PORTA
#define LCD_EN_DDR DDRA
#define LCD_RW_PORT PORTA
#define LCD_RW_DDR DDRA
#define LCD_RS_PORT PORTA
#define LCD_RS_DDR DDRA
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_PIN PINA
#define LCD_RS (1<#define LCD_RW (1<#define LCD_EN (1<#define LCD_DATA ((1<#define LCD_BUSY (1</*--------------------------------------------------------------------------------------------
函数说明
---------------------------------------------------------------------------------------------*/
void LCD_init(void);
void LCD_en_write(void);
void LCD_wait_Ready(void);
void LCD_write_command(unsigned char command,unsigned char mang,unsigned char mingdata);
void LCD_set_xy (unsigned char x, unsigned char y);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data);
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
/*-------------------------------------------------------------------------------------------
延时函数
----------------------------------------------------------------------------------------------*/
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i="0";
for (i=0;i delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i="0";
for (i=0;i delay_1ms();
}
/*----------------------------------------------------------------------------------------------
液晶使能
-----------------------------------------------------------------------------------------------*/
void LCD_en_write(void)
{
LCD_EN_PORT|=LCD_EN; //EN=1
delay_nus(1);
LCD_EN_PORT&=~LCD_EN; //EN=0
}
/*---------------------------------------------------------------------------------------------
测忙
---------------------------------------------------------------------------------------------*/
void LCD_wait_Ready(void) //等待LCD空闲
{
LCD_DATA_DDR&=~LCD_BUSY; //忙位设置为输入
LCD_RW_PORT|=LCD_RW; //RW=1
LCD_RS_PORT&=~LCD_RS; //RS=0
LCD_EN_PORT|=LCD_EN; //EN=1
while(!( LCD_DATA_PIN&LCD_BUSY)==0); //读忙位,为0表示空闲;
LCD_EN_PORT&=~LCD_EN; //EN=0
LCD_DATA_DDR|=LCD_BUSY; //数据口方向为输出
}
/*---------------------------------------------------------------------------------------------
写指令或数据
-----------------------------------------------------------------------------------------------*/
void LCD_write_command(unsigned char command,unsigned char mang,unsigned char mingdata)
{
if(mang==1)LCD_wait_Ready(); //检测是否测忙!
if(mingdata==1)LCD_RS_PORT&=~LCD_RS; //RS=0 //判断是数据还是命令
else LCD_RS_PORT|=LCD_RS; //RS=1
LCD_RW_PORT &=~LCD_RW; //RW=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_init(void)
{
LCD_DATA_DDR|=LCD_DATA; //数据口方向为输出
LCD_EN_DDR|=LCD_EN; //设置EN方向为输出
LCD_RS_DDR|=LCD_RS; //设置RS方向为输出
LCD_RW_DDR|=LCD_RW;
delay_nms(15); //初始化三次
LCD_write_command(0x28,0,1);
delay_nms(5);
LCD_write_command(0x28,0,1);
delay_nms(5);
LCD_write_command(0x28,1,1); //4位显示
LCD_write_command(0x0c,1,1); //显示开
LCD_write_command(0x06,1,1);
LCD_write_command(0x01,1,1); //清屏
}
/*------------------------------------------------------------------------------------------------
写地址
-----------------------------------------------------------------------------------------------------*/
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,1,1);
}
/*-----------------------------------------------------------------------------------------------------
英文字符串写入函数
-----------------------------------------------------------------------------------------------------*/
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_command(*s,1,0);
s ++;
}
}
/*-----------------------------------------------------------------------------------------------------
单个字符写入函数
--------------------------------------------------------------------------------------------------------*/
void LCD_write_char(unsigned char X,unsigned char Y,unsigned char data) //列x=0~15,行y=0,1
{
LCD_set_xy( X, Y ); //写地址
LCD_write_command( data,1,0);
}
/****************************************************************************************************
主函数
*********************************************************************************************************/
int main(void)
{
unsigned char j;
LCD_init();
while(1) //循环
{
LCD_write_command(0x01,1,1); //清屏
LCD_write_string(15,0,"WOXIAN "); //第一行显示
LCD_write_string(15,1,"ThankS"); //第二行显示
delay_nms(500);
for(j=0;j<22;j++) //向左移动16 格
{
if(j==10)
{
LCD_write_command(0x08,1,1);
delay_nms(100);
LCD_write_command(0x0c,1,1);
delay_nms(200);
}
LCD_write_command(0x18,1,1); //字符同时左移一格
delay_nms(250); //控制移动时间
}
}
}
文章评论(0条评论)
登录后参与讨论