⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jhdavr.c

📁 JHD529液晶的驱动程序,AVR单片机中使用GCC-AVRStudio
💻 C
字号:
#include<avr/io.h>

//#include <intrins.h>
//-----------------管脚定义-----------------
/*
#ifndef AVR
#define 	LCD12864_IO		P0		//数据端口
sbit LCD12864_RST=P2^0;			    //复位端口
sbit LCD12864_EN=P2^1;				//使能端口
sbit LCD12864_RW=P2^2; 				//读写选择断口
sbit LCD12864_DIN=P2^3;				//数据指令选择端口


#define clr_LCD12864_RST LCD12864_RST=0;
#define set_LCD12864_RST LCD12864_RST=1;
#define clr_LCD12864_EN  LCD12864_EN=0;
#define set_LCD12864_EN  LCD12864_EN=1;
#define clr_LCD12864_RW  LCD12864_RW=0;
#define set_LCD12864_RW  LCD12864_RW=1;
#define clr_LCD12864_DIN LCD12864_DIN=0;
#define set_LCD12864_DIN LCD12864_DIN=1;
*/
#define LCD12864_IO PORTA
#define LCD12864_PIN PINA
#define LCD12864_IODDR DDRA

#define LCD12864_Ctrl PORTC
#define LCD12864_RST PC0
#define LCD12864_EN PC1
#define LCD12864_RW PC2
#define LCD12864_DIN PC3


#define clr_LCD12864_RST LCD12864_Ctrl &= ~_BV(LCD12864_RST);
#define set_LCD12864_RST LCD12864_Ctrl |= _BV(LCD12864_RST);
#define clr_LCD12864_EN  LCD12864_Ctrl &= ~_BV(LCD12864_EN);
#define set_LCD12864_EN  LCD12864_Ctrl |= _BV(LCD12864_EN);
#define clr_LCD12864_RW  LCD12864_Ctrl &= ~_BV(LCD12864_RW);
#define set_LCD12864_RW  LCD12864_Ctrl |= _BV(LCD12864_RW);
#define clr_LCD12864_DIN LCD12864_Ctrl &= ~_BV(LCD12864_DIN);
#define set_LCD12864_DIN LCD12864_Ctrl |= _BV(LCD12864_DIN);



//-----------------函数声明------------------
void MPU_Init(void);
void MPU_Inter_Init(void);
void LCD12864_Init(void);
void LCD12864_Busy(void);
void LCD12864_Command(unsigned char Comm);
void LCD12864_Data(unsigned char Data);
void Delay(unsigned char N);
void delayus(unsigned char n);
void LCD12864_Address(unsigned char line,unsigned char row);
void LCD12864_String(unsigned char row,unsigned char line,unsigned char *s);
void LCD12864_Point(unsigned char Line,unsigned char Row);
void LCD12864_Read(void);
void LCD12864_GDRAM_Clear(void);
void LCD12864_Point(unsigned char Line,unsigned char Row);
void LCD12864_Line(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2);
void LCD12864_Circle(unsigned char x0,unsigned char y0,unsigned char r);
void Time0_inter();
void LCD12864_Ware_FangBo(unsigned char Y1,unsigned char Y2,unsigned char L1); //矩形波
void LCD12864_Ware_YuXian(unsigned char Y1,unsigned char Y2,unsigned char L1); //余弦波
//----------------全局变量-------------------
unsigned char LCD12864_GDRAM_DATA[2]={0x00,0x00};
unsigned char Num=0,List=0;

//-------------------------------------------
//	名字:		主函数	 
//  功能:调用子函数					            
//	参数:无
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void main(void)
{

	MPU_Init();

	while(1)
	{
		//LCD12864_Circle(32,32,32);
	//	LCD12864_Circle(48,32,32);
	//	LCD12864_Circle(63,32,32);
	//	LCD12864_Circle(79,32,32);
	//	LCD12864_Circle(96,32,32);
		//LCD12864_Ware_FangBo(5,26,3);
		//LCD12864_Ware_FangBo(30,56,30);
		//LCD12864_Point(10,10);
		LCD12864_String(1,1,"丰功伟绩");
 		Delay(10);
	//	LCD12864_GDRAM_Clear();
		//LCD12864_Command(0x01);
		
	}

}

//-------------------------------------------
//	名字:		MPU初始化
//  功能:初始化MPU
//	参数:无
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void MPU_Init(void)
{
	LCD12864_Init();
	//LCD12864_GDRAM_Clear();
	//MPU_Inter_Init();
	DDRA=0xff;
	PORTA=0xff;
	DDRC=0xff;
	PORTC=0xff;
}


//-------------------------------------------
//	名字:		LCD12864初始化
//  功能:初始化LCD12864
//	参数:无
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Init(void)
{
    LCD12864_Command(0x01);      // 清 DDRAM
    LCD12864_Command(0x30);     // 8 bits 数据模式
    LCD12864_Command(0x02);     // 指针复位
    LCD12864_Command(0x0c);     // 显示开
    LCD12864_Command(0x03);   
    LCD12864_Command(0x06);
    LCD12864_Command(0x01);     //  清 DDRAM
}
//-------------------------------------------
//	名字:	      清GDRAM
//  功能: 清GDRAM(行 0-64,列 0-127) 
//	参数:void
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_GDRAM_Clear(void)
{
	unsigned char X = 0x80,Y = 0x80;
    unsigned char i,j;
    for(i=0;i<64;i++) 
    { 
        for(j=0;j<16;j++) 
        { 
            LCD12864_Command(0x34);
            LCD12864_Command(Y);
            LCD12864_Command(X);
            LCD12864_Data(0x00);      //  low 8 bits
            LCD12864_Data(0x00);    //  high 8 bits
            LCD12864_Command(0x36);
            X++;
            j++;
        } 
        Y++; 
        if(Y>0x9f) 
           Y=0x80; 
        X=0x80+((i+1)/32)*8; 
    } 
    LCD12864_Command(0x30);
}
	
//-------------------------------------------
//	名字:	     矩形波
//  功能:矩形波 
//	参数:unsigned char Y1,unsigned char Y2,unsigned char L1
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Ware_FangBo(unsigned char Y1,unsigned char Y2,unsigned char L1)
{	/*
	LCD12864_Line(0,Y1,L1,Y1);

	LCD12864_Line(L1,Y1,L1,Y2);	
	LCD12864_Line(L1,Y2,2*L1,Y2);

	LCD12864_Line(2*L1,Y2,2*L1,Y1);
	LCD12864_Line(2*L1,Y1,3*L1,Y1);
	LCD12864_Line(3*L1,Y1,3*L1,Y2);
	LCD12864_Line(3*L1,Y2,4*L1,Y2);
	LCD12864_Line(4*L1,Y2,4*L1,Y1);
	LCD12864_Line(4*L1,Y1,5*L1,Y1);
	LCD12864_Line(5*L1,Y1,5*L1,Y2);
	LCD12864_Line(5*L1,Y2,6*L1,Y2);
	LCD12864_Line(6*L1,Y2,6*L1,Y1);
	LCD12864_Line(6*L1,Y1,7*L1,Y1);
	LCD12864_Line(7*L1,Y1,7*L1,Y2);
	LCD12864_Line(7*L1,Y2,8*L1,Y2);	*/

	unsigned char A,B,C,D,N;
	N=128/L1;
	A=0;
	B=Y1;
	C=L1;
	D=Y1;
	LCD12864_Line(A,B,C,D);
	for(;N>0;N--)
	{  A=C;
	   B=D;
	   if(D==Y1)
	     D=Y2;
	   else 
		 D=Y1;
	   LCD12864_Line(A,B,C,D);

	   A=C;
	   B=D;
	   C=C+L1;
	   LCD12864_Line(A,B,C,D);
	}
}
//-------------------------------------------
//	名字:	     画圆
//  功能:画圆  
//	参数:unsigned char x0,unsigned char y0,unsigned char r
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Circle(unsigned char x0,unsigned char y0,unsigned char r)
   {
	unsigned char x,y;
	unsigned int xx,rr,xt,yt,rs;
	yt=r;
	rr=r*r+1;			//补偿 1 修正方形
	rs=yt*3/4;      //画8分之一圆弧
	for (xt=0;xt<=rs;xt++)
	{
		xx=xt*xt;
		while ((yt*yt)>(rr-xx))
		      yt--;
		x=x0+xt;		//第一象限
		y=y0-yt;
		LCD12864_Point(x,y);
		x=x0-xt;		//第二象限
		LCD12864_Point(x,y);
		y=y0+yt;		//第三象限
		LCD12864_Point(x,y);
		x=x0+xt;		//第四象限
		LCD12864_Point(x,y);
//----45度镜象画另外8分之一圆弧--------
		x=x0+yt;		//第一象限
		y=y0-xt;
		LCD12864_Point(x,y);
		x=x0-yt;		//第二象限
		LCD12864_Point(x,y);
		y=y0+xt;		//第三象限
		LCD12864_Point(x,y);
		x=x0+yt;		//第四象限
		LCD12864_Point(x,y);
	}
}  
//-------------------------------------------
//	名字:	     画线
//  功能:画线 (行 0-64,列 0-127) 
//	参数:unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Line(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2)
{
	unsigned char t;
	signed int xerr=0,yerr=0;
	signed int delta_x,delta_y,distance;
	signed char incx,incy;
//--------------计算两个方向的长度--------------
	delta_x=x2-x1;
	delta_y=y2-y1;

//---计算增量的方向,增量为"0"表示为垂直或水平线
	if(delta_x>0) 
	     incx=1;
	else 
	    {
		 if( delta_x==0 )
		      incx=0;
		 else 
		      {
			   incx=-1;
			   delta_x =-delta_x;
			  }
		}
	if(delta_y>0) 
	      incy=1;
	else 
	    {
		 if( delta_y==0 )
		     incy=0;
		 else 
		     {
			  incy=-1;
			  delta_y =-delta_y;
			 }
		}
//-----------确定画线的范围-----------------
	if( delta_x > delta_y ) distance=delta_x;
	else distance=delta_y;

//--------------画线-----------------------

		for(t=0;t<=distance+1;t++) 
		   {
			
			LCD12864_Point(x1,y1);
	 	    xerr+=delta_x;
			yerr+=delta_y;
			/**/
			if(xerr>distance ) 
			    {
					xerr-=distance;
		 		    x1+=incx;
			    }
			if(yerr>distance ) 
			    {
					yerr-=distance;
		 		    y1+=incy;
	    		} 
	  	  }
	  	
}
//-------------------------------------------
//	名字:	     写点
//  功能:在任何位置上写点 (行 0-64,列 0-127) 
//	参数:unsigned char Line,unsigned char Row (列,行)
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Point(unsigned char Line,unsigned char Row)
{
   	unsigned char X=0x80,Y=0x80,Low=0x00,High=0x00,X1;

   //-------应该先判断在那个字节------------
   if(Row>31)	  //将其定位于下半区
	   {
	   	  X=X+0x08;
		  Y=Y+Row-32;	
	   }
   else 	    //将其定位于上半区
	   {
	   	  Y=Y+Row;	
	   }
	switch(Line/16)
	{
		case 0:{X=X+0x00;}; break;
		case 1:{X=X+0x01;}; break;
		case 2:{X=X+0x02;}; break;
		case 3:{X=X+0x03;}; break;
		case 4:{X=X+0x04;}; break;
		case 5:{X=X+0x05;}; break;
		case 6:{X=X+0x06;}; break;
		case 7:{X=X+0x07;}; break;
		default:break;
	}
	//---------读数据和数据处理-------------
   
    LCD12864_Command(0x34);//设置为扩张指令集
	LCD12864_Command(Y);   //写行
    LCD12864_Command(X);   //写列
    LCD12864_Read();
	
	X1=Line-(Line/16)*16;
	if(X1>7)
	{
		X1=X1-8;
		High=LCD12864_GDRAM_DATA[1]|(0x01<<(7-X1));
		Low=LCD12864_GDRAM_DATA[0];
	}
	else
	{
		High=LCD12864_GDRAM_DATA[1];
		Low=LCD12864_GDRAM_DATA[0]|(0x01<<(7-X1));	
	}	
	//-------------写数据--------------------
	LCD12864_Command(0x34);//设置为扩张指令集
	LCD12864_Command(Y);   //写行
    LCD12864_Command(X);   //写列
	LCD12864_Data(Low);       //  low 8 bits
    LCD12864_Data(High);       //  high 8 bits
	LCD12864_Command(0x36);
	LCD12864_Command(0x30); 




}

//-------------------------------------------
//	名字:	   在指定位置显示字符串
//  功能:在指定位置显示字符串
//	参数:unsigned char row,unsigned char line,unsigned char *s
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_String(unsigned char row,unsigned char line,unsigned char *s)
{ 
    unsigned char LCD12864_temp; 
    LCD12864_Address(row,line); 
    LCD12864_temp=*s;
    while(LCD12864_temp != 0x00) 
    { 
        LCD12864_Data(LCD12864_temp);
        LCD12864_temp=*(++s);
    }  
} 

//-------------------------------------------
//	名字:	   设置显示位置
//  功能:设置显示位置	X(1~8),Y(1~4)
//	参数:unsigned char line,unsigned char row
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Address(unsigned char line,unsigned char row)
{
    switch(row) 
    {
        case 1:LCD12864_Command(0x7f + line);
        break;
        case 2:LCD12864_Command(0x8f + line);
        break;
        case 3:LCD12864_Command(0x87 + line);
        break;
        case 4:LCD12864_Command(0x97 + line);
        default:
        break;
    }
}

//-------------------------------------------
//	名字:LCD12864_busy()
//  功能:读忙标志
//	参数:无
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Busy(void)
{
    unsigned char BF = 0;				//标志位清0
    clr_LCD12864_EN; 			//使能端清0
    clr_LCD12864_DIN;			//设为指令方式
    set_LCD12864_RW;			//设为读方式

	LCD12864_IODDR=0x00;
    LCD12864_IO=0xff;		//单片机读数据之前必须先置高位
    do
    {
    	set_LCD12864_EN;
        BF=LCD12864_PIN & 0x80;
		delayus(10);
        clr_LCD12864_EN;
    } while(BF);

    LCD12864_IODDR=0xff;
}
//-------------------------------------------
//			     写命令
//  功能:写命令
//	参数:unsigned char Comm
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Command(unsigned char Comm)
{
    LCD12864_Busy();
    clr_LCD12864_EN;
    clr_LCD12864_DIN;
    clr_LCD12864_RW;  
    delayus(10);
    set_LCD12864_EN;
    LCD12864_IO=Comm;
	delayus(10);
    clr_LCD12864_EN;
}
//-------------------------------------------
//			   读一个字节数据
//  功能:读一个字节数据
//	参数:void
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Read(void)
{
  int i;
  LCD12864_Busy();
  LCD12864_Command(0x30);  //先将其设为基本模式
  LCD12864_IO=0xFF;   
  set_LCD12864_DIN;  
  set_LCD12864_RW; 
  set_LCD12864_EN;
  for(i=5;i>0;i--);  
  LCD12864_GDRAM_DATA[0]=LCD12864_IO;       //虚读    
  clr_LCD12864_EN; 

  LCD12864_Busy();
  LCD12864_IO=0xFF;     
  set_LCD12864_DIN;  
  set_LCD12864_RW;   
  set_LCD12864_EN;
  for(i=5;i>0;i--);    
  LCD12864_GDRAM_DATA[0]=LCD12864_IO;      //真读高8bit  
  clr_LCD12864_EN;
							    
  LCD12864_Busy();
  LCD12864_IO=0xFF;     
  set_LCD12864_DIN;  
  set_LCD12864_RW;   
  set_LCD12864_EN; 
  for(i=5;i>0;i--);   
  LCD12864_GDRAM_DATA[1]=LCD12864_IO;      //真读低8bit  
  clr_LCD12864_EN;
}
//-------------------------------------------
//			   写一个字节数据
//  功能:写一个字节数据
//	参数:unsigned char Data
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void LCD12864_Data(unsigned char Data)
{
   
    LCD12864_Busy();
    clr_LCD12864_EN;
    set_LCD12864_DIN;
    clr_LCD12864_RW;
    delayus(10);
    set_LCD12864_EN;
    LCD12864_IO=Data;
	delayus(10);
    clr_LCD12864_EN;
}
//-------------------------------------------
//			   延时函数
//  功能:延时
//	参数:unsigned char N
//  设计:罗杰       时间:2007-07-26
//-------------------------------------------
void Delay(unsigned char N)
{
	unsigned char i;
    	unsigned int j;
  for(;N>0;i--)
	for(i=0;i<200;i++)
		for(j=1000;j>0;j--);
}
void delayus(unsigned char n)
{
	unsigned char i;
	for(i=0;i<n;i++);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -