atmega16中的1602液晶程序

//ICC-AVR application builder : 2009-10-1 20:33:38
// Target : M16
// Crystal: 1.0000Mhz

#include
#include

/*
1 GND GND
2 VCC VCC
3 VL 对地接1.5KΩ电阻
4 RS PA5
5 RW PA6
6 EP PA7
7 D0 PB0
8 D1 PB1
9 D2 PB2
10 D3 PB3
11 D4 PB4
12 D5 PB5
13 D6 PB6
14 D7 PB7
15 BLA VCC
16 BLK 对地接0Ω电阻
*/

#define RS PA5    //定义数据命令选择
#define RW PA6    //定义读写端
#define EN PA7   //定义使能端
#define uchar unsigned char
#define uint unsigned int

uchar LCD1[]="GAO YAN";

//函数声明
void LCDPort_Init(void); //液晶端口初始化
void LCD_Init(void); //LCD 初始化
void Write_Com(uchar  LCD_Com); //LCD 写指令
void Write_Data_Byte(uchar LCD_Data); //LCD 写字节数据
void Write_Place_xy(uchar x,uchar y); //指定当前写字符的位置
void Write_Data_String(uchar x,uchar y,uchar *data);
//在指定的(x,y)位置上写入字符串数据
void Write_Data_Char(uchar x,uchar y,uchar data);
//在指定的(x,y)位置上写入字符数据
void Check_Busy(void); //读写检测函数,每次对液晶操作前都要进行读写检测
void Delayms(uint c ); //ms延时函数

/************************************************************************/
//延时1ms
/***********************************************************************/

void Delayms(uint c)
{
 uint a,b;
 for(b=c;b>0;b--)
  for(a=158;a>0;a--);
}


/************************************************************************/
//液晶端口初始化
/***********************************************************************/
void LCDPort_Init()
{
 PORTB=0X00;
 DDRB=0XFF;//液晶端口全部为输出口
 
 
 //液晶控制端口设置
 PORTA&=~((1<  DDRA|=(1<  
 
}

/************************************************************************/
//LCD初始化
/***********************************************************************/

void LCD_Init()
{
 Write_Com(0X01); //清屏
 Delayms(15);
 
 Write_Com(0X38); //显示模式设置16x2显示,5x7点阵,8 位数据接口
 Delayms(15);
 //Write_Com(0X0f); //显示开关控制,开显示,光标显示,光标闪烁
 Write_Com(0X0c); //显示开关控制,开显示,光标不显示,光标不闪烁
 Write_Com(0X06); //光标设置,读或写一个字符后,地址指针加一,光标加一,整屏不移动
 Delayms(15);
}

/************************************************************************/
//写指令
/***********************************************************************/
 void Write_Com(uchar LCD_Com)
 {
  Check_Busy();
 
  PORTA&=~(1<   PORTA&=~(1<  
  PORTB=LCD_Com;
  PORTA|=(1<   Delayms(15);
  PORTA&=~(1<   Delayms(15);
 }
 
 /************************************************************************/
//写字节数据
/************************************************************************/
 void Write_Data_Byte(uchar LCD_Data)
 {
   Check_Busy();
 
  PORTA|=(1<   PORTA&=~(1<  
  PORTB=LCD_Data;//数据送数据端口
  PORTA|=(1<   Delayms(15);
  PORTA&=~(1<   Delayms(10);
 }
 
/************************************************************************/
//指定当前写字符的位置
/************************************************************************/
void Write_Place_xy(uchar x,uchar y)
{
 uchar add;
 unsigned char address;
 if(y == 0) //行位置
 {
 address = 0x80 + x; //列位置,
 }
 else
 {
 address = 0x80 + 0x40 + x; //
 }
 Write_Com(address); //写地址
}

/************************************************************************/
//在指定的(x,y)位置上写入字符串数据
/************************************************************************/

void Write_Data_String(uchar x,uchar y,uchar *data)
{
 Write_Place_xy(x,y);  //写地址
 
 while(*data)
 {
  Write_Data_Byte(*data);   //写字符串
  data++;
                    //指针地址加1
  Delayms(15);
 }
}
/************************************************************************/
//在指定的(x,y)位置上写入字符数据
/************************************************************************/

void Write_Data_Char(uchar x,uchar y,uchar data)
{
 Write_Place_xy(x,y);
 Write_Data_Byte(data);
}

/************************************************************************/
///读写检测函数,每次对液晶操作前都要进行读写检测
/************************************************************************/
void Check_Busy()
{
 DDRB=0X00;     //PB口置为输入口,准备读取数据
 
 PORTA&=~(1<  PORTA|=(1<  PORTA|=(1<  
 
 while(0X80 & PINA); //监测忙信号,直到忙信号为0,才能进行读写操作
 PORTA &= ~(1 << EN); //EN=0
 DDRA = 0XFF; //PB口置为输出口,准备向端口发送数据
}


/************************************************************************/
//主程序
/************************************************************************/
void main()
{
 uchar count;
 LCDPort_Init();
 LCD_Init();
 Write_Com(0X80+0X00); //指定写数据的位置,从第一行第0 列开始
//  Write_Data_String(0,1,"gaoyan");
 for(count=0;count>0;count++)
 {
  Write_Data_Byte(LCD1[count]);
  Delayms(15);
 }
}

你可能感兴趣的:(AVR)