gy1206e6.c

来自「STN ST7920 TEST CODE AND SPEC.」· C语言 代码 · 共 304 行

C
304
字号
// ST7920 128*64 2003.3.25 
#include <reg52.h>
#include <delay.c>
#include <intrins.h>

#define true 1
#define false 0

sbit CS=P3^1;
sbit SID=P1^7;
sbit SCLK=P1^6;
sbit RES=P3^7;

void SendMode( Uchar );
void SendCom( Uchar );
void SendData( Uchar );
void Initial(void);
void Disp( Uchar, Uchar ); 
void DispChar( Uchar, Uchar );
//void DispBmp(Uchar *); 
void DispRow( Uchar, Uchar );

Uchar c1=0xb0;
Uchar c2=0xa0; 

Uchar Com_mode  = 0xf8;
Uchar Data_mode = 0xfa;
bit   Command_mode = true;
//extern char code w[]; 
                                                                           
void main(void)
 {
   SCLK = 0;
   Initial();
  
   while(1)
   { 
     
     SendCom(0x01);  //DDRAM clear
     delay(20);   
     DispRow(0x0ff,0);
     DispRow(0x00,0x0ff);
     Disp(0xaa,0xaa);
     Disp(0x55,0x55);
     DispRow(0xaa,0x55);
     DispRow(0x55,0xaa);
     Disp(0x00,0x00);
     
     DispChar(c1,c2);   

     DispChar(0xc0,0xbc);
     DispChar(0xb7,0xe6);
     DispChar(0xc3,0xf7);
    
   }

}
/* 
void DispBmp(Uchar *p)
{
   Uchar HorAdd=0x80;
   Uchar VerAdd=0x80;
   SendCom(0x04);    // I/D =0
     for(;VerAdd<0x80+32;VerAdd++)
   { 
      SendCom(0x34);    // RE=1;
      SendCom(0x36);    // Graphics=1;

      SendCom(VerAdd);
      SendCom(0x80);
      SendCom(0x32);    // RE=0;                       
    
      for(HorAdd = 0; HorAdd <0x0f; HorAdd++) {SendCom(*p++);SendCom(*p++);}//{SendCom(Updat);SendCom(Dndat);}
   } 

   delay(10000);
   SendCom(0x30);    // RE=0;

 }
*/
void Disp(Uchar dot1,Uchar dot2)
 {
  Uchar HorAdd=0x80;
  Uchar VerAdd=0x80;

  for(;VerAdd<0x80+32;VerAdd++)
   { 
      SendCom(0x34);    // RE=1;
      SendCom(0x36);    // Graphics=1;

      SendCom(VerAdd);
      SendCom(0x80);
      SendCom(0x32);    // RE=0;
   
      for(HorAdd = 0; HorAdd <0x0f; HorAdd++)
        {
        SendData(dot1); SendData(dot1);
        SendData(dot2); SendData(dot2);
        }
   } 

   delay(5000);

   SendCom(0x30);    // RE=0;
}
void DispRow(Uchar dot1,Uchar dot2)
 {
 Uchar HorAdd=0x80;
 Uchar VerAdd=0x80;
 Uchar Temp=0;
 bit flag=1;

  for(;VerAdd<0x80+32;VerAdd++)
   { 
      SendCom(0x34);    // RE=1;
      SendCom(0x36);    // Graphics=1;

      SendCom(VerAdd);
      SendCom(0x80);
      SendCom(0x32);    // RE=0;
     
     if (flag==1)
     { 
       Temp = dot1;
       flag=0;
     }
     else 
     {
       Temp = dot2;
       flag=1;
     }
      for(HorAdd = 0; HorAdd <0x0f; HorAdd++)
        {

        SendData(Temp);SendData(Temp);
        SendData(Temp);SendData(Temp);

        }
   } 

   delay(5000);
   SendCom(0x30);    // RE=0;
 } 
void DispChar(Uchar Updat,Uchar Dndat) 
 {                   
   Uchar cnt=0;
   SendCom(0x04);    // I/D =0
//   SendCom(0x02);
   SendCom(0x9f);     // SET DDRAM  00  TO AC 
   for(c1++,cnt=0;c1<0xf7,cnt<32;cnt++) {SendData(c1);SendData(c2++);}//{SendCom(Updat);SendCom(Dndat);}
   delay(5000);
 } 

void Initial()
{  
  delay(500);
  RES  =0;
  delay(100);
  RES  =1;
  delay(100);

  SendCom(0x30);	//Function set  00 1 DL : x RE G 0 	 
   
  SendCom(0x0c);    // 0000 : 1 D C B ;display on
  
  SendCom(0x34);  // RE=1  Function set
  
  SendCom(0x0c);	// sleep mode off 
  SendCom(0x02);	// enable CGRAM 	 

  SendCom(0x30);  // RE =0;Function set

  delay(150);	
	  
 }
  

void SendCom(Uchar dat)
{
 Uchar cnt = 4;

 if( !Command_mode )
    {
	 SendMode(Com_mode);
     Command_mode = true;
	}
 SendMode(Com_mode);
 while(cnt--)   // Higher  4 bit data
 {
	 SID  = (bit)( 0x80 & dat );
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 dat<<=1;
 }
 cnt = 4;
 while(cnt--)    // fill with 0
 {
	 SID  = 0;
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
    _nop_();
 }
 delay(0);
 cnt = 4;
 while(cnt--)   // Lower 4 bit   data
 {
	 SID  = (bit)( 0x80 & dat );
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 dat<<=1;
 } 
 cnt = 4;
 while(cnt--)   // fill with 0
 {
	 SID  = 0;
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 _nop_();
 }
 delay(0);
}


void SendData(Uchar dat)
{
 Uchar cnt = 4;
 
 if( Command_mode )
 {
  SendMode(Data_mode);
  Command_mode = false;
 }
 SendMode(Data_mode);
 while(cnt--)   // Higher  4 bit data
 {
	 SID  = (bit)( 0x80 & dat );
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 dat<<=1;
 }
 cnt = 4;
 while(cnt--)    // fill with 0
 {
	 SID  = 0;
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 _nop_();
 }
 delay(0);
 cnt = 4;
 while(cnt--)   // Lower 4 bit   data
 {
	 SID  = (bit)( 0x80 & dat );
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 dat<<=1;
 } 
 cnt = 4;
 while(cnt--)   // fill with 0
 {
	 SID  = 0;
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 _nop_();
 }
 delay(0);
}


void SendMode(Uchar com)
{
 Uchar cnt = 8;
 CS = 0;
 _nop_();
 CS = 1;
 delay(0);
 while(cnt--)   // Higher  4 bit data
 {
	 SID  = (bit)( 0x80 & com );
	 _nop_();
	 SCLK = 1;
     _nop_();
	 SCLK = 0;
	 com<<=1;
 }
 delay(0);	
}                    

⌨️ 快捷键说明

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