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

📄 lcd.c

📁 这是mega128控制320*240液晶控制器sed1335的程序
💻 C
字号:
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <util/delay.h>   
#include <avr/sleep.h>
#include <avr/wdt.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/twi.h>

prog_char LCM_0808[]=
{
/*--  调入了一幅图像:D:\ubbmp\du.bmp  --*/
/*--  宽度x高度=8x8  --*/
0x3C,0x7E,0xE7,0xC3,0xC3,0xE7,0x7E,0x3C,
};
prog_char LCM_0840[]=
{
/*--  调入了一幅图像:D:\ubbmp\dian.bmp  --*/
/*--  宽度x高度=8x40  --*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1C,0x3E,
0x3E,0x3E,0x1C,0x00,0x00,0x00,0x00,0x00,
};
unsigned char SYSTAB[8]={0x30,0x87,0x07,0x27,0x42,0xF0,0x28,0x00};
unsigned char SCRTAB[10]={0x00,0x00,0xf0,0x80,0x25,0xf0,0x00,0x00,0x00,0x00};
void sed1335_write_command(unsigned char commandcode)
{//写命令//
	bitset(PORTG,__lcd_a0);
	bitset(PORTD,__lcd_rd);
	bitclr(PORTG,__lcd_wr);	
	PORTC=commandcode;
	bitset(PORTG,__lcd_wr);
} 
void sed1335_write_data(unsigned char data)
{//写数据//
	bitclr(PORTG,__lcd_a0);
	bitset(PORTD,__lcd_rd);
	bitclr(PORTG,__lcd_wr);		
	PORTC=data;
	bitset(PORTG,__lcd_wr);
}
unsigned char asc_lcd(unsigned char ch)
{//将实际的字符转换为LCD能显示的字符
	if(ch<10)	ch+=48;
	return ch;
}
void Lcd_Init(void)
{//初始化函数//
    unsigned char i;
	PORTC = 0x00;						//lcd数据口,输出
	DDRC  = 0xFF;						//11111111		
	bitset(DDRD,__lcd_rd);
	bitset(DDRG,__lcd_wr);	
	bitset(DDRG,__lcd_a0);				//以上为液晶屏用输出口

	sed1335_write_command(0x40);   		/* 1SYSTEM SET代码40 */
	
    for(i=0;i<8;i++)
    {		
		sed1335_write_data(SYSTAB[i]);  /* 写入参数 */
    }
	delay_us(10);
	
	sed1335_write_command(0x44);   		/* 2SCROLL代码 44*/
	for(i=0;i<10;i++)
    {
		sed1335_write_data(SCRTAB[i]);	/* 写入参数 */
    }
	delay_us(10);
	
	sed1335_write_command(0x5a);		/* 3HDOT SCR 代码 5a*/
    sed1335_write_data(0X00);  			/* 设置参数P1 */
	delay_us(10);
	
    sed1335_write_command(0x5b);		/* 4OVLAY 代码 5b*/
    sed1335_write_data(0x01);			/* 设置参数:显示一,三区为图形属性,合成方式为异或方式 */
	delay_us(10);
	
	sed1335_write_command(0x4c);   		/* 5CSRDIR 代码 4c*/
	delay_us(10);
	
    Lcd_Clear();       					/* 调清屏函数 */
	delay_us(10);
	
    sed1335_write_command(0x59);  		/* 6显示状态设置 */
    sed1335_write_data(0X14);   		/* 设置参数:显示一二区关光标 */	
	delay_us(10);	
}
void Lcd_Clear(void)
{//清屏函数//
    unsigned int i;
    sed1335_write_command(0x46);  		/* CSRW 代码 */
    sed1335_write_data(0X00);   		/* 设置参数光标指针低8位 */
    sed1335_write_data(0X00);		   	/* 设置参数光标指针高8位 */
    sed1335_write_command(0x42);   		/* MWRITE 代码 42*/
    for(i=0;i<9600;i++)				//
    {
        sed1335_write_data(0x20);		/* 写入数据 */
    }
	for(i=0;i<9600;i++)				//
    {
        sed1335_write_data(0x00);		/* 写入数据 */
    }
}
void Cur_Set(unsigned int Ox,unsigned int Oy, unsigned char attribs)
{//光标设置//ox,oy:液晶屏上的位置,attribs为是否为非一区
	unsigned int temp;
	temp = Oy*DB+Ox;
	if(attribs) temp += BasePart1;			// 如果需要就指向第二区,加上第二区首地址
	sed1335_write_command( CsrW );				// 光标Locate,定位
	sed1335_write_data( (unsigned char)(temp & 0xff) );// 写入参数CSRL设置参数光标指针低8位
	sed1335_write_data( (unsigned char)(temp /256 ) );	// 写入参数CSRH设置参数光标指针高8位
}
void Lcd_Rect(unsigned int Ox,unsigned int Oy,unsigned int Rect_Long,unsigned int Rect_High,unsigned char sign)
{//ox,oy:液晶屏上的位置,Rect_Long:线的长度,Rect_High:线的高度,sign反显标志。
	unsigned int 	Optr,last_tmp,tempCount1,tempCount2;
	unsigned char	tempchar;
	//确定最后一个字节要写的数据
    last_tmp=0x00;
	tempCount2=Rect_Long%8;
	for(tempCount1=7;tempCount1>7-tempCount2;tempCount1--)
	{
		bitset(last_tmp,tempCount1);
	}
	//////////////////////////////////////////////////////////////////////
    Optr = Oy * DB + Ox	+ BasePart1	;						//起始内存位置
	
	tempchar=0;
    for(tempCount1=0;tempCount1<Rect_High;tempCount1++)
    {
		sed1335_write_command(CsrW);						//光标定位指令
		sed1335_write_data((unsigned char)(Optr &0xff));	//设置光标地址CSR
		sed1335_write_data((unsigned char)(Optr /256 ));
		
		sed1335_write_command(mWrite);						//ram写指令
		
		for(tempCount2=0;tempCount2<Rect_Long/8;tempCount2++)
		{
			tempchar=0xff;
			if(sign==1)	tempchar=0xff-tempchar;
			sed1335_write_data(tempchar);
		}  
		if(last_tmp!=0)
		{
			tempchar=last_tmp;
			if(sign==1)  tempchar=0xff-tempchar;
			sed1335_write_data(tempchar);
		}
		Optr+=DB;
    }
}	
void Lcd_Char(unsigned int Ox,unsigned int Oy,unsigned char Count,unsigned char *buf)
{//ox,oy:液晶屏上的位置,Count:要显示的字符数量,*buf:要显示字符所在的数组。
	unsigned int 	tempCount1,Optr;
	unsigned char	tempchar;

	Optr = Oy * DB + Ox;									//起始内存位置
	
	sed1335_write_command(CsrW);							//光标定位指令
	sed1335_write_data((unsigned char)(Optr &0xff));		//设置光标地址CSR
	sed1335_write_data((unsigned char)(Optr /256 ));
	
	sed1335_write_command(mWrite);							//ram写指令
	tempchar=0;
	for (tempCount1=0;tempCount1<Count;tempCount1++)
	{
		tempchar=asc_lcd(buf[tempCount1]);
		sed1335_write_data(tempchar);
	}
}
void Lcd_Graph(unsigned int Ox,unsigned int Oy,unsigned int Cnumber,unsigned char width,unsigned char high,unsigned char sign)
{//ox,oy:液晶屏上的位置,Cnumber:数组中的位置,width,high,图形的宽高,sign反显标志。
	unsigned int 	tempPtr,Optr;
	unsigned int 	tempCount1,tempCount2,nByte;
	unsigned char	tempchar;

	nByte = width/8;										//列数
	
	Optr = Oy * DB + Ox	+ BasePart1	;						//起始内存位置
	
	tempPtr = Cnumber*high*nByte;							//数组起始位置
	tempchar=0;
	for (tempCount1=0;tempCount1<high;tempCount1++)
	{
		sed1335_write_command(CsrW);						//光标定位指令
		sed1335_write_data((unsigned char)(Optr &0xff));	//设置光标地址CSR
		sed1335_write_data((unsigned char)(Optr /256 ));
		
		sed1335_write_command(mWrite);						//ram写指令
		for(tempCount2=0;tempCount2<nByte;tempCount2++)
		{			
			switch(width)
			{
				case 8:
					if(high==8)	  	tempchar=pgm_read_byte_near(LCM_0808+tempPtr);
					if(high==40)	tempchar=pgm_read_byte_near(LCM_0840+tempPtr);
					break;
				case 16:
					tempchar=pgm_read_byte_near(LCM_1640+tempPtr);
					break;
				case 32:
					tempchar=pgm_read_byte_near(LCM_3240+tempPtr);
					break;
				case 40:
					if(high==18)	tempchar=pgm_read_byte_near(LCM_4018+tempPtr);
					if(high==36)	tempchar=pgm_read_byte_near(LCM_4036+tempPtr);
					if(high==8)		tempchar=pgm_read_byte_near(LCM_4008+tempPtr);
					break;
				case 48:
					tempchar=pgm_read_byte_near(LCM_4880+tempPtr);
					break;
				case 56:
					if(high==80)	tempchar=pgm_read_byte_near(LCM_5680+tempPtr);
					if(high==40)	tempchar=pgm_read_byte_near(LCM_5640+tempPtr);
					break;
				case 72:
					tempchar=pgm_read_byte_near(LCM_72120+tempPtr);
					break;
				case 96:
					tempchar=pgm_read_byte_near(LCM_9680+tempPtr);
					break;
				case 112:
					tempchar=pgm_read_byte_near(LCM_11280+tempPtr);
					break;
			}
			if(sign==0)	tempchar=0xff-tempchar;
			sed1335_write_data(tempchar);		//写入数据
			tempPtr ++;
		}
		Optr+=DB;
	}
}
//////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

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