NOKIA5110屏幕驱动程序

虽然今年的单片机比赛已经结束,但我想我还远不会和单片机说拜拜。它是学习硬件知识的最佳选择,虽然我们学它主要是在写程序,但不得不说学习到不少的新知识。有时你会很无奈地说句“硬件这东西。。。”,我想这也正是硬件的魅力。

虽然今年换了比较大型的板子必比赛,用的事TFT彩屏来演示程序,单学习屏幕还是应该晓得最简单点的,像以前使用的NOKIA5110的屏,驱动如下:

NOKIA5110.H文件:

#ifndef __NOKIA5110_H__
#define __NOKIA5110_H__
#include <reg52.h>
#include <string.h>
typedef unsigned char uchar;
extern uchar Lcd_x,Lcd_y;
extern uchar Datas[6][84];

sbit  N_CS = P1^1;  // chip selected.
sbit  N_RS = P1^0;  // reset.
sbit  N_DC = P1^2;  // write data or write command(1/0).
sbit  N_DI = P1^3;  // data in.
sbit  N_CL = P1^4;  // clock.

void LcdInit(void);					  // NOKIA5110 LCD init.
void Clear(void);					  // clear full screen.
void WriteByte(uchar dat,bit f_dc);   // Write byte, f_dc is flag bit.
void SetPos(uchar _x,uchar _y);
void PutPixel(uchar _x,uchar _y,uchar f_c);
void Line(char x0,char y0,char x1,char y1,uchar f_c);
void Rectangle(char x0,char y0,char x1,char y1,char f_c);
void Circle(char x0,char y0,char r,uchar f_c);
void PutChar(char x0,char y0,char c0);
void PutString(char x0,char y0,char* s);
uchar GetPixel(char x0,char y0);
#endif
NOKIA5110.C文件:
#include "NOKIA5110.H"	//	function's hand define
#include "DELAY.H"		//	delay functions
#include "CONSTANT.H"	//	constants
uchar Lcd_x,Lcd_y;		//	point's position
uchar Datas[6][84];
void LcdInit(void)
{
	N_RS = 0;			//	reset
	delayms(10);
	N_RS = 1;
	
	WriteByte(0x21,0);	//  use expansion directive
	WriteByte(0xC6,0);	//  
	WriteByte(0x06,0);	//  temp
  	WriteByte(0x13,0);  //  1:48
	WriteByte(0x20,0);  //  use base directive
	WriteByte(0x0C,0);	//  normal
	
	Clear();			//	Init Clear
	SetPos(0,0);		//	Init Position
}
void WriteByte(uchar dat,bit f_dc)
{
	uchar  i;
	N_CS = 0;			//	open chip selected
	N_DC = f_dc;		//	data selected or command selected
	for(i=0; i<8; i++)
	{ 
		if(dat&0x80) N_DI = 1;	//	from high bit to low bit
		else         N_DI = 0;	//	
		dat <<= 1;				//	next bit
		N_CL = 0;	 N_CL = 1;	//	rising edge
	}	
	N_DI = N_CS = N_DC = 1;		//	set 1
}

void SetPos(uchar _x,uchar _y)
{
	WriteByte(0x80|_x,0);	//	set x 
	WriteByte(0x40|_y,0);	//  set y
	Lcd_x=_x; Lcd_y=_y;	    //	record x and y
}
void Clear(void)			//	clear screen
{
	int i = 504;			//	504 byte
	SetPos(0,0);
	while(i--) WriteByte(0x00,1);
	memset(Datas,0,sizeof(Datas));	//	record for every point
}
void PutPixel(uchar _x,uchar _y,uchar f_c)
{
	uchar _over = _y%8;		//	over position
	_y /= 8;				//	8 bit for div
	SetPos(_x,_y);
	if(f_c) Datas[_y][_x] |= 1 << _over;	//	judge set or clear
	else Datas[_y][_x] &= ~(1 << _over);
	WriteByte(Datas[_y][_x],1);				//	do it 
}
void Line(char x0,char y0,char x1,char y1,uchar f_c)	// line from (x0,y0) to (x1,y1)
{
	char dx,dy,di,dx_x2,dy_x2,dx_sym,dy_sym;

	dx = x1 - x0;		
	dy = y1 - y0;

	if(dx > 0) dx_sym = 1;
	else 	   dx_sym = -1;

	if(dy > 0) dy_sym = 1;
	else       dy_sym = -1;

	dx = dx_sym * dx;
	dy = dy_sym * dy;

	dx_x2 = dx * 1;
	dy_x2 = dy * 1;

	if(dx >= dy)
	{
		di = dy_x2 - dx;
		while(x0 != x1)
		{
			PutPixel(x0,y0,f_c);
			x0 +=dx_sym;
			if(di < 0) di += dy_x2;
			else
			{
				di += dy_x2 - dx_x2;
				y0 += dy_sym;
			}	
		}
	}
	else 
	{
		di = dx_x2 - dy;
		while(y0 != y1)
		{
			PutPixel(x0,y0,f_c);
			y0 += dy_sym;
			if(di < 0) di += dx_x2;
			else
			{
				di += dx_x2 - dy_x2;
				x0 += dx_sym;
			}
		}
	}
	PutPixel(x0,y0,f_c);
}
void Circle(char x0,char y0,char r,uchar f_c)	
{	//	(x0,y0) for the	center of the circle, r for radius
	char a=0,b=r,di=3-2*r;
	while(a <= b)
	{
		PutPixel(x0-b,y0-a,f_c);
		PutPixel(x0+b,y0-a,f_c);
		PutPixel(x0-a,y0+b,f_c);
		PutPixel(x0-b,y0-a,f_c);
		PutPixel(x0-a,y0-b,f_c);
		PutPixel(x0+b,y0+a,f_c);
		PutPixel(x0+a,y0-b,f_c);
		PutPixel(x0+a,y0+b,f_c);
		PutPixel(x0-b,y0+a,f_c);
		a ++;
		if(di < 0) di += 4 * a + 6;
		else	    
		{
			di += 10 + 4 * (a - b);
			b --;
		}
		PutPixel(x0+a,y0+b,f_c);	
	}
}
void Rectangle(char x0,char y0,char x1,char y1,char f_c)
{
	Line(x0,y0,x1,y0,f_c);  Line(x0,y0,x0,y1,f_c);
    Line(x1,y0,x1,y1,f_c);  Line(x0,y1,x1,y1,f_c);
}

void PutChar(char x0,char y0,char c0)	
{	//	give point of left top and put the char
	char i, j;
	if(c0<ascii_begin||c0>=ascii_end) return;
	for(i=0; i<6; i++)
	{
		uchar tc=Ascii[c0-ascii_begin][i];
		for(j=0; j<8; j++)
		{
			PutPixel(x0+i,y0+j,tc&0x01);
			tc >>= 1;
		}
	}
}
void PutString(char x0,char y0,char* s)
{	//	Put a string, if too long, it will get next line.
	while(*s)
	{
		if(x0>78){ x0=0; y0+=8; }
		PutChar(x0,y0,*s++);
		x0+=6;	
	}
}
uchar GetPixel(char x0,char y0)	//	get give point's message.
{
	return (Datas[y0/8][x0]&(1<<(y0%8)));
}

你可能感兴趣的:(NOKIA5110屏幕驱动程序)