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

📄 st7920.c

📁 ST7920串行方式的源码
💻 C
字号:
/*
******************************************************************************************
*                                     Vehicle Travelling Data Recorder                   *
*                                                                                        *
*                                         Driver For LCD( YM12864R )                     *
*                                                                                        *
*                                            Wenxin Ma, SSPU, 2005                       *
*                                             All Rights Reserved                        *
*                                                                                        *
* File : Lcd.C                                                                           *
* By   : Wenxin Ma, SSPU                                                                 *
* Contact : wxma@ee.sspu.cn                                                              *
******************************************************************************************
*/
#include  "config.h"
#include  "ST7920.h"

uint8 dspP;
/****************************************************************************
* 名称:MSpiIni()
* 功能:初始化SPI接口,设置为主机。
* 入口参数:无
* 出口参数:无
****************************************************************************/
void  MspiIni(void){
  PINSEL0=(PINSEL0&0xFFFF00FF)|(0x55<<8); //设置引脚连接SPI
  S0SPCCR = 0x52;			// 设置SPI时钟分频
  S0SPCR = (0<<2)|(0<<3)|(1<<4)|
  		   (1<<5)|(0<<6)|(0<<7);	// 设置SPI接口模式,MSTR=1,CPOL=1,CPHA=0,LSBF=0
}

/****************************************************************************
* 名称:MSendData()
* 功能:向SPI总线发送数据。
* 入口参数:data        待发送的数据
* 出口参数:返回值为读取的数据
****************************************************************************/
uint8 MSPI_WriteOneByte(uint8 data)
{ 
   
   S0SPDR = data;
   while( 0==(S0SPSR&0x80) );		// 等待SPIF置位,即等待数据发送完毕
   
   return(S0SPDR);
}

/***********************************************************************************************
*                                        Read Instruction Register		
*
* Description: Read Instruction Register
*
* Arguments  : 
*
* Returns    : none
*
* Notes      : 1) MAY have problem while interrupts are being enabled
***********************************************************************************************/
unsigned char ST7920_ReadInstruction(void)
{
	uint8 cData;

	IO0SET = ST7920_CS;

	MSPI_WriteOneByte(ST7920_RD_INST);

	cData=MSPI_WriteOneByte(0x0);

	cData=cData | MSPI_WriteOneByte(0x0)>>4;

	IO0CLR = ST7920_CS;
	return cData;
}
/*
***********************************************************************************************
*                                 Write Instruction Register		
*
* Description: Write Instruction Register
*
* Arguments  : Instruction value written to Instruction Register
*
* Returns    : none
*
* Notes      : 1) MAY have problem while interrupts are being enabled
***********************************************************************************************
*/
void ST7920_WriteInstruction(uint8 Instruc)
{
	uint8 cData;

	IO0SET = ST7920_CS;

	MSPI_WriteOneByte(ST7920_WR_INST);

	cData=Instruc & 0xF0;
	MSPI_WriteOneByte(cData);

	cData=Instruc;
	cData=cData<<4;
	MSPI_WriteOneByte(cData);

	IO0CLR = ST7920_CS;
}

/*
***********************************************************************************************
*                                        Write Data RAM		
*
* Description: Write Data RAM
*
* Arguments  : Data value written to RAM
*
* Returns    : none
*
* Notes      : 1) MAY have problem while interrupts are being enabled
***********************************************************************************************
*/
void ST7920_WriteData(uint8 DataRAM)
{
	unsigned char cData;

	IO0SET = ST7920_CS;

	MSPI_WriteOneByte(ST7920_WR_DATA);

	cData=DataRAM & 0xF0;
	MSPI_WriteOneByte(cData);

	cData=DataRAM;
	cData=cData<<4;
	MSPI_WriteOneByte(cData);

	IO0CLR = ST7920_CS;
}
/*
*********************************************************************************************
*                                       	Write Space Data	
*
* Description: Write Space Data to Line (ST7920)
*
* Arguments  : startAddress		is the start address of the Line (80 ~ 9F)
*			   dataNum			is the number of data to be written to Line 
* Returns    : none
*
* Notes      : 1) MAY have problem while interrupts are being enabled
*			   2) data length is UP to 16 bytes	
*			
**********************************************************************************************
*/
void ST7920_WriteSpaceData(uint8 startAddress,uint8 dataNum)
{
 	ST7920_WriteInstruction(startAddress);

    while(dataNum > 0)
    {
        ST7920_WriteData(0x20);
        dataNum--;
    }
}

void ClearScreen(void)
{
	ST7920_WriteSpaceData(0x80,64);	
}

/*
*********************************************************************************************
*                                       	Write Line Data	
*
* Description: Write Data to Line (ST7920)
*
* Arguments  : startAddress		is the start address of the Line (80 ~ 9F)
*			   dataNum			is the number of data to be written to Line 
*			   dataVector		is a pointer to the header of the data vector
* Returns    : none
*
* Notes      : 1) MAY have problem while interrupts are being enabled
*			   2) data length is UP to 16 bytes	
*			
**********************************************************************************************
*/
void ST7920_WriteLineData(uint8 startAddress, uint8 dataNum,uint8 *dataVector)
{
 	ST7920_WriteInstruction(startAddress);

    while(dataNum > 0)
    {
        ST7920_WriteData(*dataVector);
        dataNum--;
        dataVector++;
    }
}

void WriteOneScreen(uint8 startAddress, uint8 dataNum,uint8 *dataVector)
{
	uint8 cLoop;
	
	cLoop = startAddress;
	
    while(dataNum > 0)
    {
        switch(cLoop)
        {
        	case 0x0:
        		ST7920_WriteInstruction(0x80);
        		break;
        	case 0x10:
        		ST7920_WriteInstruction(0x90);
        		break;
        	case 0x20:
        		ST7920_WriteInstruction(0x88);
        		break;
        	case 0x30:
        		ST7920_WriteInstruction(0x98);
        		break;		
        }
        cLoop=(cLoop+1)&0x3f;
        ST7920_WriteData(*dataVector);
        dataNum--;
        dataVector++; 	
    }
}

/****************************************************************************
* 名称:WriteScreen
* 功能:
* 入口参数:Data value written to LCD
* 出口参数:无
****************************************************************************/
void WriteScreen(uint8 data)
{
	switch(dspP){
        case 0x0:
        	ST7920_WriteInstruction(0x80);
        	break;
        case 0x10:
        	ST7920_WriteInstruction(0x90);
        	break;
        case 0x20:
        	ST7920_WriteInstruction(0x88);
        	break;
        case 0x30:
        	ST7920_WriteInstruction(0x98);
        	break;		
	}
    ST7920_WriteData(data);
   	dspP=(dspP+1)&0x3f; 	
}

/****************************************************************************
* 名称:DspTitle
* 功能:
* 入口参数:无
* 出口参数:无
****************************************************************************/
void  DspTitle(void)
{ 
 uint8 Title[] = {"GSM/GPRS通信测试V1.01 2006年05月"
  				  "上海第二工业大学电子电气工程学院"};
  WriteOneScreen(0x0,64,Title);
}

/****************************************************************************
* 名称:ST7920Init
* 功能:Inition
* 入口参数:无
* 出口参数:无
****************************************************************************/
void ST7920Init(void)
{
   	MspiIni();								// 初始化SPI接口
   	
	ST7920_WriteInstruction(ST7920_FUN_SET);
	ST7920_WriteInstruction(ST7920_DSP_ON);
	ST7920_WriteInstruction(ST7920_MODE_SET);
	DspTitle();
}

⌨️ 快捷键说明

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