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

📄 avr320240.c

📁 AVR单片机驱动C320240单色液晶模块的C语言程序
💻 C
字号:
/******************************************************************************
**
**      西安博控电子科技有限公司.
**
**  文件名:  C320240.C     
**
**  用途:    C320240 LCM 底层驱动函数库
**
**  编译环境:ICCAVR 7.0
**  修改时间:  $  2007 年 10月  11日       shw$
******************************************************************************/

#include "avr320240.h"
//#include "ascii.h"
//#include "HZK.h"
unsigned char const hzdot[];
unsigned char const ASC_MSK[];
unsigned int const hzIndex[];
//--------------------------------------------------------------------------
//控制器SED1335 相关命令和参数定义
#define SystemSet 0x40 
Uchar const ParaSysTable8[]= {
0x30,0x87,0x07,0x27,0x42,0xf0,0x28,0x00 // P1-P8参数
};
#define Scroll 0x44 
#define BasePart1 0x00
#define BasePart2 0x40
#define BasePart3 0x80
#define BasePart4 0x00
Uchar const ParaScrTableA[]= {
0x00,BasePart1,0xF0,0x00,BasePart2,0xF0,0x00,BasePart3,0x00,BasePart4
};
#define SleepIn 0x53
#define DispOn 0x59
#define DispOff 0x58
#define Ovlay 0x5b
#define HdotScr 0x5a
#define CsrForm 0x5d
#define CgramAdr 0x5c
#define CsrDirR 0x4c
#define CsrDirL 0x4d
#define CsrDirU 0x4e
#define CsrDirD 0x4f
#define CsrW 0x46
#define CsrR 0x47
#define mWrite 0x42
#define mRead 0x43
//bit wAttrib; 
#define Busy 0x40
#define paraP9 0x28
//--------------------------------------------------------------------------


//extern const Uchar code ASC_MSK[];


//信号引脚连接定义
//--------------------------------------------------------------------------

#define databus PORTA
/*
sbit a0=P1^3; 
sbit read=P1^1; 
sbit write=P1^0;

sbit cs=P1^2; 
sbit rst=P1^4; 
*/

//---------------------------------------------------------------------------
/******************************************************************************
**  函数名: 		  void userinit(void)

**  描述: 			  用户初始化

**  修改的全局变量: 

**  输入参数:

**  返回值: 
*******************************************************************************/
void userinit(void)
{
Delay(30);
LcmInition();
WriteCommand(DispOff);
LcmClear();
WriteCommand(DispOn);
WriteCommand( Ovlay ); 
WriteData( 0x0C );    
}

/******************************************************************************
**  函数名: 		void Displayonebmp (Uchar *puts)

**  描述: 		    显示一幅320*240点阵图片

**  修改的全局变量: 

**  输入参数:	    Uchar *puts :图片所在地址

**  返回值: 
*******************************************************************************/
void Displayonebmp (Uchar *puts)   //显示320*240图片
{
Uint X;
Uchar i,j;
X=0;
WriteCommand( CsrDirR );
WriteCommand( CsrW ); 
WriteData(0); 
WriteData(0);
WriteCommand( mWrite ); 
for(i=0;i<240;i++)
{
for(j=0;j<40;j++)
{
WriteData(puts[X]);
X++;
}
} 

}

/******************************************************************************
**  函数名: 		 void LCD_putpixel(Uint Rx,Uint Ry)

**  描述: 			 在指定位置显示一个点

**  修改的全局变量: 

**  输入参数:		Rx:0-319;Ry:0-239

**  返回值: 
*******************************************************************************/
void LCD_putpixel(Uint Rx,Uint Ry)   //
{
Uchar shift;
unsigned long dot_sum;
Uchar C_L,C_H;
dot_sum=Ry*40+(Rx/8);
C_L=(Uchar)dot_sum;
C_H=(Uchar)(dot_sum>>8);
shift=Rx%8;
shift=8-shift-1;
WriteCommand( CsrDirR );
WriteCommand( CsrW ); 
WriteData(C_L);   //CSRL
WriteData(C_H);   //CSRH
WriteCommand( mWrite ); 
WriteData( (1<<shift) );
}


void LCD_put8pixel(Uint Rx,Uint Ry,Uchar dat)   //水平8点 x:0-312;y:0-239
{
unsigned long dot_sum;
Uchar C_L,C_H;
dot_sum=Ry*40+(Rx/8);
C_L=(Uchar)dot_sum;
C_H=(Uchar)(dot_sum>>8);
WriteCommand( CsrDirR );
WriteCommand( CsrW ); 
WriteData(C_L);   //CSRL
WriteData(C_H);   //CSRH
WriteCommand( mWrite ); 
WriteData(dat);
}

/******************************************************************************
**  函数名: 		  void line(Uint S_X,Uint S_Y,Uint E_X,Uint E_Y)

**  描述: 			  指定起点和终点画一条直线,

**  修改的全局变量: 

**  输入参数:		  Uint S_X,Uint S_Y   :起点坐标

**					  Uint E_X,Uint E_Y   :终点坐标

**  返回值: 
*******************************************************************************/
void line(int S_X,int S_Y,int E_X,int E_Y)
{
/*
float K;
Uint i,LEN;
int B;
Uchar dat;
long X,Y;
//--------------------------------------------------------------
//斜率无穷大
if(E_X==S_X)
 {
  if(S_Y<E_Y)LEN=E_Y-S_Y;
    else LEN=S_Y-E_Y;
    X=S_X;
    for(i=0;i<LEN;i++)
    {
     if(S_Y<E_Y)
      Y=S_Y+i;
     else  Y=E_Y+i; 
     LCD_putpixel(X,Y);
    }
   return ;
  }
//----------------------------------------------------------------
//斜率为0
if(S_Y==E_Y)  
    {
     if(S_X<E_X)LEN=E_X-S_X;
     else LEN=S_X-E_X;
     Y=S_Y;
     LEN=LEN-( (LEN+1)%8);
     for(i=0;i<LEN;i=i+8)
      {
     if(S_X<S_Y)
       X=S_X+i;
      else X=E_X+i;
     LCD_put8pixel(X,Y,0xff);
      }
      if( (LEN+1)%8 )
       {
         dat=0xff;
         for(i=0;i<(8-LEN%8);i++)
          {
          dat=dat-i*2;
          LCD_put8pixel(X+8,Y,dat);
          }
       } 
   return;   
  }
//----------------------------------------------------------------  
K=(float)(E_Y-S_Y);
K=K/(float)(E_X-S_X);
B=S_Y-S_X*K;
if(S_X<E_X)LEN=E_X-S_X;
else LEN=S_X-E_X;
for(i=0;i<LEN;i++)
{
X=(S_X)+i;
Y=(K*((S_X)+i)+B);
LCD_putpixel(X,Y);
}
*/
}


void Delay(Uint MS)
{
Uchar us,usn;
while(MS!=0)
{ usn = 4;
while(usn!=0)
{
us=0xf0;
while (us!=0){us--;};
usn--;
}
MS--;
}
}

/******************************************************************************
**  函数名: 			  void WriteCommand( Uchar CommandByte )

**  描述: 				  向控制器写命令

**  修改的全局变量: 

**  输入参数:

**  返回值: 
*******************************************************************************/
void WriteCommand( Uchar CommandByte ) 
{
    PORTB|=(1<<3);    //rs = 1
    PORTB|=(1<<1);    //read = 1
    PORTB|=(1<<0);    //write = 1
    PORTA=0xff;
    PORTA=CommandByte;
//	Delay(10);
    PORTB &= ~(1<<2);  //cs = 0
//	Delay(10);
    PORTB &= ~(1<<0);  //write = 0
//	Delay(20);       
    PORTB |= (1<<0);   //write = 1
//	Delay(10);   
    PORTB |= (1<<2);   //cs = 1
}

/******************************************************************************
**  函数名: 		  void WriteData( Uchar dataW )

**  描述: 			  向控制器写数据

**  修改的全局变量: 

**  输入参数:

**  返回值: 
*******************************************************************************/
void WriteData( Uchar dataW ) 
{
    PORTB &=~(1<<3);    //rs = 0 
    PORTB|=(1<<1);     //read = 1
    PORTB|=(1<<0);     //write = 1 
    PORTA=0xff;
    PORTA=dataW; 
//	Delay(10);
    PORTB &= ~(1<<2);   //cs = 0
//	Delay(10);   
    PORTB &= ~(1<<0);   //write = 0 	
//	Delay(20);
    PORTB |= (1<<0);    //write = 1
//	Delay(10);
    PORTB |= (1<<2);    //cs = 1
}

/******************************************************************************
**  函数名: 		void LcmInition( void )	  

**  描述: 			LCM 控制器初始化

**  修改的全局变量: 

**  输入参数:

**  返回值: 
*******************************************************************************/
void LcmInition( void ) 
{

Uchar i;

WriteCommand( SystemSet ); 
for (i=0;i<8;i++) {
WriteData( ParaSysTable8[i] ); 
}

WriteCommand( Scroll ); 
for (i=0;i<10;i++) {
WriteData( ParaScrTableA[i] );
}

WriteCommand( HdotScr ); 
WriteData( 0 ); 
WriteCommand( Ovlay ); 
WriteData( 4 ); 
WriteCommand( DispOn ); 
WriteData( 0x54 ); 
}

/******************************************************************************
**  函数名: 		 void LcmClear( void )

**  描述: 			 清屏

**  修改的全局变量: 

**  输入参数:

**  返回值: 
*******************************************************************************/
void LcmClear( void ) 
{
Uint i1=32767;
WriteCommand( CsrDirR ); 
WriteCommand( CsrW ); 
WriteData( 0 ); 
WriteData( 0 ); 
WriteCommand( mWrite ); 
while(i1--) {

WriteData( 0x0 ); 
}
}

/******************************************************************************
**  函数名: 	void ShowPicture(Uint s_x,Uint s_y,Uint Width,Uint Hight,
                                                  Uchar code  *address)

**  描述: 	    显示一幅图片

**  修改的全局变量: 

**  输入参数:				      Uint s_x,Uint s_y:  起始坐标

                                  Width:宽度0-320

								  Hight:高度0-240

**  返回值: 
*******************************************************************************/
void ShowPicture(Uint s_x,Uint s_y,Uint Width,Uint Hight,Uchar const *address)
{ 
Uchar i,j;
Uint x,y,POS,dot_sum;
Uchar C_L,C_H;
Width=Width/8;
POS=0;

//------------------------------------------------------------
//这种方式简单
//for(j=0;j<Hight;j++)
//  {
//      x=s_x;
//      y=s_y+j;
//   for(i=0;i<Width;i++)
//     {
//      x=s_x+i*8;
//      LCD_put8pixel(x,y,address[POS]);
//      POS++;
//     }
//  }
 
//------------------------------------------------------------
//这种方式效率高

for(j=0;j<Hight;j++)
  {
      x=s_x;
      y=s_y+j;
      dot_sum=y*40+(x/8);
      C_L=(Uchar)dot_sum;
      C_H=(Uchar)(dot_sum>>8);

      WriteCommand( CsrDirR );
      WriteCommand( CsrW ); 
      WriteData(C_L); 
      WriteData(C_H);
      WriteCommand( mWrite ); 
      
   for(i=0;i<Width;i++)
     {
       WriteData(address[POS]);
       POS++;       
     }
  }

}

//-----------------------------------------------------------------------------
void ShowHZ(Uint lin,Uint column,Uchar const *address)
{

ShowPicture(lin,column,16,16,address);

}

void Display_Chinese(Uint UniCode,Uint X,Uint Y)
{  
   Uchar i;
   Uchar const * ImgData;
   Uint CodeID;

   for(i=0;i<hzNum;i++)
     {
        if((hzIndex[2*i]==(Uchar)(UniCode>>8))&&(hzIndex[2*i+1]==(Uchar)UniCode) )
           {                
                CodeID=i;    break;
           }
     }
      ImgData=&hzdot[CodeID*32];
      ShowHZ(X,Y,ImgData);
  
}
//-------------------------------------------------------------------------------
void Display_ASCII(Uchar Chr,Uchar X,Uint Y)
{  
     Uchar const * ImgData;
    {  
     //ImgData=ASC_MSK+12*((Uchar)*Chr-1);
	 ImgData=ASC_MSK+(12*(Chr-1)); 
        
     ShowPicture(X,Y,8,12,ImgData);  
             
    }
}

void DisTest(void)
{
Display_ASCII(0x30,0,0);  //0
Display_ASCII(0x31,8,12); //1 
Display_ASCII(0x32,16,24);//2
Display_ASCII(0x33,24,36);//3
Display_ASCII(0x34,32,48);//4
Display_ASCII(0x35,40,60);//5

}
//-------------------------------------------------------------------------------
/******************************************************************************
**  函数名: void Display_String(Uchar code *p,Uint X,
                                              Uint Y,Uint lenth)

**  描述: 	     显示一个字符串

**  修改的全局变量: 

**  输入参数:	  Uint X,Uint Y  :显示起始地址
                  
				  lenth  :字符串长度
**  返回值: 
*******************************************************************************/
void Display_String(Uchar const *p,Uint X,Uint Y,Uint lenth)
{ Uchar i;
//  Uchar const * chr;
//  chr=p;
  for(i=0;i<lenth;i++)
   {
	if(X>319){Y=Y+16;X=0;}

    if(p[i]<0x80) {Display_ASCII(p[i],X,Y);X=X+8;/*chr++;*/}    //ASCII
    else 
       {
         Display_Chinese((p[i])*256+p[i+1],X,Y);//中文
//		 chr=chr+2;
         i++;
         X=X+16;
       }     
     
   }
  
}


⌨️ 快捷键说明

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