AVR ATmega128 单片机驱动12864LCD显示程序

//内 容:12864LCD显示驱动程序
//编译环境: AVR Studio 4/AVR GCC
//芯 片:ATmega128
//系统时钟: 内部晶振8MHz
//作 者:
//日 期; 2012.02.27

#include
#include
#include

#define F_CPU 8000000


#define DI_CLR PORTC &= ~(1<#define DI_SET PORTC |= 1<
#define RW_CLR PORTC &= ~(1<#define RW_SET PORTC |= 1<
#define EN_CLR PORTC &= ~(1<#define EN_SET PORTC |= 1<
#define CLEAR_SCREEN 0x01 //清屏指令:清屏且AC值为00H
#define FUN_MODE 0x30 //工作模式:8位
#define DISPLAY_ON 0x0c //显示开,游标关
#define DISPLAY_OFF 0x08 //显示关
#define CURSE_ADD 0x06 //游标向右移动,图像不动




//*******************************************************
// 端口初始化函数
//*******************************************************
void port_init()
{
//数据端口
PORTA = 0xFF;
DDRA = 0xFF;

//控制端口
PORTC =0x68; //PC3=1,PC4=0,PC5=1,PC6=1
DDRC = 0xFF; //控制端口:PC0-PC5

}



//*******************************************************
// 忙检测函数
//*******************************************************
void Check_Busy()
{
DDRA = 0x00; //PA口置为输入口,准备读取数据
DI_CLR;
RW_SET;
EN_SET;
while(0x80 & PINA); //监测忙信号,直到忙信号为0,才能进行读写操作
EN_CLR;
DDRA = 0xFF; //PA口置为输出口,准备向端口发送数据
}
//*******************************************************
// 写命令函数
//*******************************************************
void LCD_write_com ( unsigned char com )
{
Check_Busy();

DI_CLR;
RW_CLR;
EN_SET;
PORTA = com;
_delay_ms(5);
EN_CLR;

}


//*******************************************************
// 写数据函数
//*******************************************************
void LCD_write_data(unsigned char data)
{
Check_Busy();

DI_SET;
RW_CLR;
EN_SET;
PORTA = data;
_delay_ms(5);
EN_CLR;
}


//*******************************************************
// 清屏函数
//*******************************************************
void LCD_clear()
{
LCD_write_com( 0x01 );
_delay_ms(5);
}

//*******************************************************
// 显示字库函数
//*******************************************************
void DisplayCgrom(unsigned char addr ,unsigned char *content)
{
Check_Busy();
LCD_write_com(addr);
Check_Busy();

while( *content!='\0' )
{
LCD_write_data( *content );
content++;
_delay_ms(5);
Check_Busy();
}
}



//*******************************************************
// 要显示的内容函数
//*

******************************************************
void LCD_Display()
{
DisplayCgrom(0x80,"第一行");
DisplayCgrom(0x88,"第三行");
DisplayCgrom(0x90,"第二行");
DisplayCgrom(0x98,"第四行");
}

//*******************************************************
// LCD初始化函数
//*******************************************************
void LCD_init()
{
DDRA = 0xFF;
DDRC = 0xFF;
LCD_write_com( FUN_MODE ); //显示模式设置
_delay_ms(5);
LCD_write_com( FUN_MODE ); //显示模式设置
_delay_ms(5);
LCD_write_com( DISPLAY_ON ); //显示开
_delay_ms(5);
LCD_write_com( CLEAR_SCREEN ); //清屏
_delay_ms(20);
}


//*******************************************************
// 主函数
//*******************************************************
int main (void)
{
port_init();
_delay_ms(100);//显示模式设置前延时>40ms
LCD_init();
LCD_clear();
while(1)
{
LCD_Display();
}
}

相关文档
最新文档