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

📄 7920_16.c

📁 avr驱动显示屏LCD12864R(7920)
💻 C
字号:
//12864液晶(ST7920驱动)的3线串行驱动 MCU=ATMega16(L)

/***********************************************************************
工    程:ST7920驱动的12864液晶的3线串行驱动模式
引脚定义:
	RS(CS)=====>PB0
    	RW(SID)====>PB1
        EN(SCLK)===>PB2
    	PSB为硬件控制,接高电平为8位或4位的并行模式,接低电平为串行模式
************************************************************************/
#include <iom16v.h>
#include <macros.h>

#define uchar unsigned char
#define uint unsigned int 
#define nop()  NOP()
#define xtal 8

#define Set_CS() DDRB |= (1<<0);PORTB |= (1<<0)
#define Set_SID() DDRB |= (1<<1);PORTB |= (1<<1)
#define Set_SCLK() DDRB |= (1<<2);PORTB |= (1<<2)

#define Clr_CS() DDRB |= (1<<0);PORTB &=~(1<<0)
#define Clr_SID() DDRB |= (1<<1);PORTB &=~(1<<1)
#define Clr_SCLK() DDRB |= (1<<2);PORTB &=~(1<<2)

//====================================================================
//函数声明
void Delay(uint ms);      //延时子程序
void W_1byte(uchar RW, uchar RS, uchar W_data);
void Write_8bits(uint W_bits);
void LCD_Init(void);
/*********************************************************************/ 
const uchar mynew1[]={"7920_3wires_demo"};
const uchar mynew2[]={"Create by:CANY"};
const uchar mynew3[]={"E_mail:CANY_999@"};
const uchar mynew4[]={"163.com"};
/*********************************************************************/
void main()
{
	uchar i = 0;
 	Clr_CS();
 	Clr_SID();
 	Clr_SCLK();
 	LCD_Init();
 	while(1)
 	{ 
  		nop();
  		nop();
  		W_1byte(0,0,0x80);   			//显示的地址0x80
  		nop();
  		for(i=0;mynew1[i]!='\0';i++)
  		{
   			W_1byte(0,1,mynew1[i]);
		  }
  		W_1byte(0,0,0x90);     			//显示的地址0x90
  		for(i=0;mynew2[i]!='\0';i++)
  		{
   			W_1byte(0,1,mynew2[i]); 
  		}
  		W_1byte(0,0,0x88);   			//显示的地址0x88
  		for(i=0;mynew3[i]!='\0';i++)
  		{
   			W_1byte(0,1,mynew3[i]);
  		}
  		W_1byte(0,0,0x98);     			//显示的地址0x98
  		for(i=0;mynew4[i]!='\0';i++)
  		{
   			W_1byte(0,1,mynew4[i]);
  		}
  		nop();
  		for(;;)
  		{
    			continue;
  		} 
 }
}
/******************************************************************/
void LCD_Init(void)
{
	uchar cmd;
  	cmd=0x30;   				//功能设置 8位数据,基本指令
 	W_1byte(0,0,cmd);
 	Delay(2);
 	cmd=0x0C;   				//显示状态 ON,游标OFF,反白OFF
 	W_1byte(0,0,cmd); 			//写指令
 	Delay(2);
 	cmd=0x01;   				//清除显示
 	W_1byte(0,0,cmd); 			//写指令
 	Delay(2);
 	cmd=0x02;   				//地址归位
 	W_1byte(0,0,cmd); 			//写指令
 	Delay(2);
 	cmd=0x80;   				//设置DDRAM地址
 	W_1byte(0,0,cmd); 			//写指令
 	Delay(2);   				//延时
}
/*******************************************************************
函 数 名:W_1byte
入口参数:RW、RS、W_data
函数作用:写一个字节的数据到12864液晶,包括指令和数据
说    明:RW=1,从液晶读数据到MCU;RW=0,写一个数据到液晶;
   	 (一般RW都设为0,即只向液晶写数据,不读数据)
          RS=1,写入的是数据;RS=0,写入的是指令;
    	 一般模式:RW=0,RS=1;写数据
       	 RW=0,RS=0;写指令
********************************************************************/
void W_1byte(uchar RW, uchar RS, uchar W_data)
{
	uint H_data,L_data,S_ID = 0xf8;  	//11111RWRS0
 	if(RW == 0)
 	{
   		S_ID &=~ 0x04;
	}
 	else     //if(RW==1)
 	{
   		S_ID |= 0X04;
 	}
 	if(RS == 0)
 	{
   		S_ID &=~ 0x02;
 	}
 	else     //if(RS==1)
 	{
   		S_ID |= 0X02;
 	}
 	H_data = W_data;
 	H_data &= 0xf0;   			//屏蔽低4位的数据
 	L_data = W_data;     			//xxxx0000格式
 	L_data &= 0x0f;   			//屏蔽高4位的数据
 	L_data <<= 4;   			//xxxx0000格式
 	Set_CS();
 	Write_8bits(S_ID);   			//发送S_ID
 	Write_8bits(H_data); 			//发送H_data
 	Write_8bits(L_data); 			//发送L_data
 	Clr_CS(); 
}
/********************************************************************
函 数 名:Write_8bits
入口参数:W_bits
函数作用:负责串行输出8个bit位
********************************************************************/
void Write_8bits(uint W_bits)
{
	uint i,Temp_data;
 	for(i=0; i<8; i++)
 	{
  		Temp_data = W_bits;
  		Temp_data <<= i;
  		if((Temp_data&0x80)==0)  //bit7 is zero
  		{
   			Clr_SID();
   			nop();
   			Set_SCLK();
   			nop();
   			nop();
   			Clr_SCLK();
   			nop();
   			Clr_SID();
  		}
  		else         //bit7 is one
  		{
   			Set_SID();
   			nop();
   			Set_SCLK();
   			nop();
   			nop();
   			Clr_SCLK();
   			nop();
   			Clr_SID();
  		} 
 	}
}
/********************************************************************
函 数 名:Delay
入口参数:ms
函数作用:毫秒级的延时程序,当晶振为12Mhz时,xtal=12;
********************************************************************/
void Delay(uint ms) 
{ 
	uint i; 
    	while(ms--)    
   	{ 
     		for(i=1;i<(uint)(xtal*143-2);i++) 
         	; 
   	}   
}
//===================================================================*/

⌨️ 快捷键说明

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