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

📄 lcd.c

📁 【液晶显示器的C语言程序设计 Freescale 8位微控制器】一书的光盘
💻 C
📖 第 1 页 / 共 2 页
字号:
		{
			for(;x != x1;)
			{
				if(err < 0)
				{
					DrawDot(x + 1,y);
					x += 1;
					err += dy;
				}
				else
				{
					DrawDot(x + 1,y + 1);
					x += 1;
					y += 1;
					err += (dy - dx);
				}
			}
		}
		else
			if((x1 >= x)&&((y1 - y) > (x1 - x)))
			{
				for(;y != y1;)
				{
					if(err < 0)
					{
						DrawDot(x + 1,y + 1);
						x += 1;
						y += 1;
						err += (dy - dx);
					}
					else
					{
						DrawDot(x,y + 1);
						y += 1;
						err -= dx;
					}
				}
			}
			else
				if((x1 < x)&&((y1 - y) <= (x - x1)))
				{
					for(;x != x1;)
					{
						if(err < 0)
						{
							DrawDot(x - 1,y);
							x -= 1;
							err += dy;
						}
						else
						{
							DrawDot(x - 1,y + 1);
							x -= 1;
							y += 1;
							err += (dy + dx);
						}
					}
				}
				else
					if((x1 < x)&&((y1 - y) > (x1 - x)))
					{
						for(;y != y1;)
						{
							if(err < 0)
							{
								DrawDot(x - 1,y + 1);
								x -= 1;
								y += 1;
								err += (dy + dx);
							}
							else
							{
								DrawDot(x,y + 1);
								y += 1;
								err += dx;
							}
						}
					}
	}	
	else
	{
		dx = x; dy = x1;
		if(x < x1)
		{
			dx = x1;
			dy = x;
		}
		for(;dy < dx;dy++)
			DrawDot(dy,y);
	}
}
/*画坐标轴,x0(行)y0(列)为原点坐标,length为坐标长度*/
void DrawCoordinate(Uint x0,Uint y0,Uint length)
{
	DrawLines(x0,y0-4,x0,y0+length);	//横轴
	DrawLines(x0+4,y0,x0-length,y0);	//纵轴	
	DrawLines(x0,y0+length,x0-4,y0+length-4);	//横轴箭头
	DrawLines(x0,y0+length,x0+4,y0+length-4);	
	DrawLines(x0-length,y0,x0-length+4,y0+4);	//纵轴箭头
	DrawLines(x0-length,y0,x0-length+4,y0-4);	
}
//获得数组中的最大值
Uint GetMaxNum(Uint *ptr,Uint cnt)
{
	Uint i,max = 0;
	for(i = 0;i < cnt;i++)
	{
		if(max < ptr[i])
			max = ptr[i];
	}
	return max;
}
Uchar CanDraw(Uint x,Uint y,Uchar fillstyle)
{
	if(fillstyle == 0)
	{
		if((x + y) / 4 * 4 == x + y)
			return 1;	/*是偶数的话返回1,画该点*/
		else
			return 0;
	}
	else
	{
		if(((x - y) >= 0 ? (x - y) : (y - x)) / 4 * 4 == ((x - y) >= 0 ? (x - y) : (y - x)))
			return 1;	/*是偶数的话返回1,画该点*/
		else
			return 0;
	}
}
/*把屏幕某一部分涂黑:(x,y)是区域左上角坐标,(x1,y1)是区域右下角坐标,最后一个参数是填充方式*/
void BarFill(Uint x,Uint y,Uint x1,Uint y1,Uchar fillstyle)
{
	Uint i,j;
	for(i = y;i <= y1;i++)
		for(j = x;j <= x1;j++)
			if((CanDraw(j,i,fillstyle))||((i == y)||(i == y1)||(j == x1)||(j == x)))
				DrawDot(i,j);
}
/*图像显示程序:根据一个整数数组(里面有cnt个元素)显示一个柱状的显示图,
参数x,y,x1,y1代表一个矩形区域的左上角和右下角的坐标*/
void DrawBars(Uint *ptr,Uint cnt,Uint x,Uint y,Uint x1,Uint y1)
{	
	Uint k;
	Uint yitem = ((y1-y+1)/GetMaxNum(ptr,cnt));
	Uint xitem = ((x1-x+1)/cnt);
	DrawCoordinate(y1,x,x1-x+1);
	if(yitem == 0)
		yitem = 1;
	for(k = 0;k < cnt;k++)
	{
		if(k / 2 * 2 == k) /*k为偶数的话*/
		{
			BarFill(x+4+xitem*k,y1-yitem*ptr[k],x+xitem*(k+1),y1,0);
		}
		else
			BarFill(x+4+xitem*k,y1-yitem*ptr[k],x+xitem*(k+1),y1,1);
	}	
}
/*该例程中采用的是4*4键盘。
  0xEE,"F1",0xDE,"1",0xBE,"2",0x7E,"3",
  0xED,"F2",0xDD,"4",0xBD,"5",0x7D,"6",
  0xEB,"F3",0xDB,"7",0xBB,"8",0x7B,"9",
  0xE7,"F4",0xD7,"*",0xB7,"0",0x77,"#"
  KeyValue[4]={0xfe,0xfd,0xfb,0xf7};
  KeyTable[16]={0xee,0xde,0xbe,0x7e,
  				0xed,0xdd,0xbd,0x7d,
  				0xeb,0xdb,0xbb,0x7b,
  				0xe7,0xd7,0xb7,0x77};

由于GP32的PTA口兼有键盘中断功能,因此将键盘的行、列共8条线连接到PTA口上,
其中行1-4连接到PTA0-3上,列1-4连接到PTA4-7上。这里提供一个键盘编程实例,
涉及如何扫描键盘获得键值,中断获取键值,键盘编码等问题。在编程时将PTA0-3定义为输出,
将PTA4-7定义为输入并具有内部上拉电阻,那么"F1"键对应于PTA7~PTA0=11101110,即$EE;
"1"键对应于PTA7~PTA0=11011110,即$DE;...;"#"键对应于PTA7~PTA0=01110111,即$77。*/
/*键盘初始化*/
void KeyInit(void){
	PTA=0x00;
	DDRA=0x0f;
	PTAPUE=0xf0;
	INTKBIER=0xf0;
}
/*读按键值*/
unsigned char ReadKey(void){
	unsigned char temp,tempdata,key;
	unsigned int k;
	for(k=0;k<4;k++){			//依次扫描四行
		temp=KeyValue[k];		//使第k根行扫描线为低电平
		tempdata=PTA;			//
		tempdata|=0x0F;			//PTA4-7保留原态,令PTA0-3为1
		tempdata&=temp;
		PTA=tempdata;
		nop();
		nop();
		
		tempdata=PTA;
		tempdata&=0xF0;
		if (tempdata!=0xf0) {
			key=PTA;
			goto readkey1;
		}
	}
readkey1: return key;
}		
/*键盘中断*/
@interrupt void KeyISR(void){
	unsigned char KeyValue;
	unsigned int OutValue;
	unsigned char k;
		
	INTKBSCR|=0x02;
	KeyValue=ReadKey();
	for(k=0;k<16;k++){
		if(KeyValue==KeyTable[k]){
			OutValue=k;			
		}
//		break;
	}
/*F1显示西文,F2文本方式下显示汉字,F3图形方式下显示汉字,F4*/
	switch(OutValue){
	case 0:						//"F1"显示西文字符串
		Init_LCD_Text();
		CGDisplay(8,5,String);
		break;
	case 1:						//"1"
		DrawDot(10,10);			//先画点再消点
		WaitNms(2000);
		ClrDot(10,10);	
		break;
	case 2:						//"2"			
		break;
	case 3: 					//"3"
		
		DrawCoordinate(8,30,8); //x0(行)y0(列)为原点坐标,length为坐标长度			
		break;
	case 4:						//"F2"		
		Init_LCD_Grap();
		DrawCircle(5,5,5);  	//画圆			
		break;
	case 5:						//"4"			
		break; 
	case 6:						//"5"
		break;
	case 7:					   //"6"			
		break;
	case 8:						//"F3"图形方式下显示汉字
		Init_LCD_Grap();
		DisplayChina(16,5);
		break;
	case 9:						//"7"				
		break;
	case 10:					//"8"							
		break;				
	case 11:					//"9"
		break;
	case 12:					//"F4"
		Init_LCD_Grap();
		DrawBars(ptr,8,10,10,200,200);	//画柱状图
		break;
	case 13:					//"Clear"
		Init_LCD_Grap();
		break;
	case 14:					//"0"		
		break;
	case 15:					//"Enter"
		
		break;
	} 	
	KeyInit();
	INTKBSCR|=0x04;	
	INTKBSCR&=~0x02;	
}
//PLL初始化
void PLLInit(void){
	PCTL=0;				//关闭PLL,准备进行设置
    	PBWC=0x80;			//自动带宽模式
   	PCTL=1;				//R=1
    	PMSH=1;    			
   	PMSL=0x2c;			//N=012c
    	PMRS=0x80;			//L=80
    	PCTL=0x22;			//E=2,PLLON=1,启动PLL
    	while(!(PBWC&0x40));		//等待稳定输出
   	PCTL|=0x10;			//频率锁定,选PLL作为时钟源
}
/* 系统集成模块(SIM)初始化,禁止COP(看门狗)*/
void SIMInit(void) {
	CONFIG2=0x01; /* 内部总线时钟用作SCI的时钟*/
	CONFIG1=0x3d; /* 允许LVE复位信号,禁止LVI的电源,LVI工作在5V,*/
                  /* 经过32个CGMXCLK周期退出STOP模式,禁止COP模块 */ 
}
void main(void)
{   
  	sei();      		//关中断
	SIMInit();  		//SIM初始化
	PLLInit();  		//PLL初始化		
	DDRC=0x0F;
	PTCPUE=0xFF;		//上拉电阻允许
   	PTC.byte=0xff;
   	DDRC=0xFF;
   	PTB=0xff;
   	DDRB=0xff;
  	
	Init_LCD_Text();
	CGDisplay(1,12,String);
	Init_LCD_Grap();	//屏幕初始化
	DisplayChina(10,5);	//显示汉字
	DrawCircle(64,120,30);	//画圆
	KeyInit();		//键盘初始化
 	cli();			//开中断
   	for(;;);		//等待键盘中断,并进行处理
 }

⌨️ 快捷键说明

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