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

📄 tft.c

📁 avr+TFT+SD卡实现数码相框功能
💻 C
字号:
/**************************************************************************************
* 名    称: 
* 功    能:
* 参    数:
* 返 回 值:
**************************************************************************************/
#include <avr/io.h>
#include <avr/pgmspace.h>
//#include<stdio.h>
#include <util/delay.h>
#include "spi.h"
#include "tft.h"
#include "AsciiLib.h"
//=======================================================
void DelayNS(unsigned int time)                
{
    while(time--!=0)
	{
		_delay_ms(1);
	}
}
//=======================================================
/*
void SPI_init(void)
{
	// 设置MOSI和SCK及SS为输出,其他为输入 
	DDR_SPI |= (1<<MOSI)|(1<<SCK);
	
	// 使能SPI,主机模式,设置时钟速率为Fosc/2
	SPCR = (1<<SPE)|(1<<MSTR);
	SPSR |= (1<<SPI2X);	//SPI倍速
}
*/
/********************************************************/
/*
unsigned char spi_send(unsigned char out_data)
 {
    //启动数据传输 //
	SPDR = out_data;
	//等待传输结束 //
	while(!(SPSR & (1<<SPIF)));
	return SPDR;	  
 }
 */
 /*	   
 uchar spi_send(uchar out_data)
 {
 	uchar i,n;
 	for(i=0;i<8;i++)
	{
		n<<=1;
		SCK=0;
		MOSI=(out_data&0x80);
		if(MISO)	n++;
		SCK=1;
		out_data<<=1;
	}
	return n;  
 } */
/**************************************************************************************
* 名    称: 
* 功    能:
* 参    数:
* 返 回 值:
*
**************************************************************************************/
void send_command(unsigned char out_data)              
{
	Clr_CS();
	spiTransferByte(0x70);
	spiTransferByte(0x00);
	spiTransferByte(out_data);
	Set_CS();
}

/**************************************************************************************
* 名    称: 
* 功    能:
* 参    数:
* 返 回 值:
*
**************************************************************************************/
void send_data(unsigned int out_data)           
{
	Clr_CS();
	spiTransferByte(0x72);
	spiTransferByte(out_data>>8);
	spiTransferByte(out_data);
	Set_CS();
}
/**************************************************************************************
* 名    称: 
* 功    能:
* 参    数:
* 返 回 值:
*
**************************************************************************************/
void write_com(unsigned char address,unsigned int num)
{
    send_command(address);
    send_data(num);
}
/**************************************************************************************
* 名    称: 
* 功    能:
* 参    数:
* 返 回 值:
*
**************************************************************************************/
void lcd_init(void)
{
	write_com(0x0001,0x0100);				//Driver Output Contral Register 
   	write_com(0x0002,0x0700);    //0x0701	//LCD Driving Waveform Contral
  	write_com(0x0003,0x1030);				//Entry Mode设置
  												//指针从左至右自上而下的自动增模式
  												//Normal Mode(Window Mode disable)
  												//RGB格式
  												//16位数据2次传输的8总线设置
   	write_com(0x0004,0x0000);				//Scalling Control register
   	write_com(0x0008,0x0207);				//Display Control 2
   	write_com(0x0009,0x0000);				//Display Control 3
   	write_com(0x000A,0x0000);				//Frame Cycle Control
   	write_com(0x000C,0x0000);				//External Display Interface Control 1
    write_com(0x000D,0x0000);				//Frame Maker Position
   	write_com(0x000F,0x0000);				//External Display Interface Control 2
   	DelayNS(100);
   	write_com(0x0007,0x0101);				//Display Control
   	DelayNS(100);
   	write_com(0x0010,0x16B0);    //0x14B0	//Power Control 1
   	write_com(0x0011,0x0001);    //0x0007	//Power Control 2
   	write_com(0x0017,0x0001);    //0x0000	//Power Control 3
   	write_com(0x0012,0x0138);    //0x013B	//Power Control 4
   	write_com(0x0013,0x0800);    //0x0800	//Power Control 5
   	write_com(0x0029,0x0009);				//NVM read data 2
   	write_com(0x002a,0x0009);				//NVM read data 3
   	write_com(0x00a4,0x0000);

   	write_com(0x0050,0x0000);				//设置操作窗口的X轴开始列
   	write_com(0x0051,0x00EF);				//设置操作窗口的X轴结束列
   	write_com(0x0052,0x0000);				//设置操作窗口的Y轴开始行
   	write_com(0x0053,0x013F);				//设置操作窗口的Y轴结束行
   	write_com(0x0060,0x2700);				//Driver Output Control
   												//设置屏幕的点数以及扫描的起始行
   	write_com(0x0061,0x0001);				//Driver Output Control
   	write_com(0x006A,0x0000);				//Vertical Scroll Control
   	//-------------- Partial Display Control ---------//
   	write_com(0x0080,0x0000);				//Display Position – Partial Display 1
   	write_com(0x0081,0x0000);				//RAM Address Start – Partial Display 1
   	write_com(0x0082,0x0000);				//RAM address End - Partial Display 1
   	write_com(0x0083,0x0000);				//Display Position – Partial Display 2
   	write_com(0x0084,0x0000);				//RAM Address Start – Partial Display 2
   	write_com(0x0085,0x0000);				//RAM address End – Partail Display2
	//-------------- Panel Control -------------------//
   	write_com(0x0090,0x0013);				//Frame Cycle Control
   	write_com(0x0092,0x0000);				//Panel Interface Control 2
   	write_com(0x0093,0x0003);				//Panel Interface control 3
   	write_com(0x0095,0x0110);				//Frame Cycle Control
	write_com(0x0097, 0x0000);
    write_com(0x0098, 0x0000);

   	write_com(0x0007,0x0173);
}

/**************************************************************************************
* 名    称: DispOneColor
* 功    能: 全屏显示某种颜色
* 参    数: Color   : 颜色值
* 返 回 值: 无
*
**************************************************************************************/
void DispOneColor(unsigned int Color)
{
    int i,j;
    write_com(0x0021,0);        //行首址0
    write_com(0x0020,0);        //列首址0
    send_command(0x22);
    for(j=0;j<320;j++)
        for(i=0;i<240;i++)
            send_data(Color);
}
/**************************************************************************************
* 名    称: DispClear
* 功    能: 清除显示屏的显示内容
* 参    数: 无
* 返 回 值: 无
*
**************************************************************************************/
void DispClear(void)
{
    DispOneColor(BLACK);
}
//==============================================

void Set_ramaddr(unsigned int x,unsigned int y)
{
    write_com(0x0021,y);        //行首址
	write_com(0x0020,x);        //列首址
}

/**************************************************************************************
* 名    称: DrawSingleAscii
* 功    能: 在指定的位置单个字符
* 参    数: x           : x坐标
*           y         	: y坐标
*           LineColor 	: 字符的颜色
*           FillColor   : 字符背景颜色
* 返 回 值: 无
*
**************************************************************************************/
/*
void DrawSingleAscii(unsigned int x, unsigned int y, char *pAscii, 
                     unsigned int LineColor,unsigned int FillColor, unsigned char Mod)
{
    unsigned char i, j;
    unsigned char str;
    unsigned int OffSet;

    OffSet = (*pAscii - 32)*16;

    for (i=0;i<16;i++)
    {
        Set_ramaddr(x,y+i);
        send_command(0x22);
        //str = *(AsciiLib + OffSet + i);  
		str = pgm_read_byte((AsciiLib + OffSet + i));
        for (j=0;j<8;j++)
        {
            if ( str & (0x80>>j) )     //0x80>>j
            {
                send_data((unsigned int)(LineColor&0xffff));
            }
            else
            {
                if (NORMAL == Mod) 
                    send_data((unsigned int)(FillColor&0xffff));
                else
                {
                    Set_ramaddr(x+j+1,y+i);
                    send_command(0x22);  
                }
            }               
        } 
    }
}
*/
/**************************************************************************************
* 名    称: DrawSingleHz
* 功    能: 在指定的位置显示汉字
* 参    数: x           : x坐标
*           y       	: y坐标
*           LineColor 	: 汉字的颜色
*           FillColor   : 汉字背景颜色
* 返 回 值: 无
**************************************************************************************/
/*
void DrawSingleHz(unsigned int x, unsigned int y, const unsigned char *pHz, 
				  unsigned int LineColor,unsigned int FillColor, unsigned char Mod)
{
	unsigned int j=0,i=0,k=0;
	unsigned int str,len;
//---------------------------统计是否在字库中出现 
	len=sizeof(ChineseLib)/sizeof(ChineseLib[0]);
	for(j=0;j<sizeof(ChineseLib)/sizeof(ChineseLib[0]);j++)   
    	if((unsigned char)ChineseLib[j].Index[0]==pHz[0]&&(unsigned char)ChineseLib[j].Index[1]==pHz[1])break;
//--------------------------      
    if(j<len)
    {
        for (i=0;i<HZ_column;i++)
        {
            Set_ramaddr(x,y+i);
            send_command(0x22);

            str = (unsigned char)ChineseLib[j].Msk[i*2]<<8|(unsigned char)ChineseLib[j].Msk[i*2+1]; 

            for (k=0;k<HZ_column;k++)
            {
                if ( str & (0x8000>>k) )     //0x8000>>k
                {
                    send_data((unsigned int)(LineColor&0xffff));
                }
                else
                {
                    if (NORMAL == Mod) 
                        send_data((unsigned int)(FillColor&0xffff));
                    else
                    {
                        Set_ramaddr(x+k+1,y+i);
                        send_command(0x22);  
                    }
                }               
            } 
        }
    }
}
*/
/**************************************************************************************
* 名    称: DrawString
* 功    能: 在指定的位置显示多个字符
* 参    数: x           : x坐标
*           y         	: y坐标
*           LineColor 	: 字符的颜色
*           FillColor   : 字符背景颜色
* 返 回 值: 无
**************************************************************************************/

void DrawString(unsigned int x, unsigned int y, char *pStr, 
				unsigned int LineColor,unsigned int FillColor, unsigned char Mod)
{

    while(1)
    {
        if (*pStr == 0)
        {
            return;
        }

        if (*pStr > 0x80)           //汉字
        {
            //DrawSingleHz(x, y, pStr, LineColor, FillColor, Mod);
            x += HZ_column;
            pStr += 2;              
        }
        else                        //英文字符
        {
            //DrawSingleAscii(x, y, pStr, LineColor, FillColor, Mod);
			DrawAscii(x, y, *pStr, LineColor, FillColor, Mod);
            x += 8;
            pStr += 1;              
        }
    }   
}

//================================= 

void DispNum(unsigned int x, unsigned int y, unsigned int num)
{
    char str[5];

    str[0] = num/1000+0x30;
    str[1] = (num%1000)/100+0x30;
    str[2] = (num%1000)%100/10+0x30;
    str[3] = (num%1000)%100%10+0x30;
    str[4] = '\0';

    DrawString(x, y, str, RED, YELLOW, NORMAL);
}

/**************************************************************************************
* 名    称: DispSmallPic
* 功    能: 在指定的位置显示一张65K色的图片
* 参    数: str     : 图片数组名
* 返 回 值: 无
*
**************************************************************************************/

void DispSmallPic(unsigned int x, unsigned int y, unsigned int w, 
				  unsigned int h, const unsigned char *str)
{
    unsigned int i,j,temp;
    for(j=0;j<h;j++)
    {
        Set_ramaddr(x,y+j);
        send_command(0x22); 
        for(i=0;i<w;i++)
        {   //send_data(*(unsigned short *)(&str[(j*w+i)*2]));       //高位在前
            //temp=str[(j*w+i)*2+1]<<8;     //低位在前
			temp = pgm_read_byte(str+((j*w+i)*2+1));
			temp <<= 8;
            //temp|=str[(j*w+i)*2];
			temp |= pgm_read_byte(str+((j*w+i)*2));
            send_data(temp);
        }
    }
}

//========================================================================
/**************************************************************************************
* 名    称: DrawAscii
* 功    能: 在指定的位置显示指定的单个字符
* 参    数: x           : x坐标
*           y         	: y坐标
*           LineColor 	: 字符的颜色
*           FillColor   : 字符背景颜色
* 返 回 值: 无
*
**************************************************************************************/
void DrawAscii(unsigned int x, unsigned int y, char Ascii, 
                     unsigned int LineColor,unsigned int FillColor, unsigned char Mod)
{
    unsigned char i, j;
    unsigned char str;
    unsigned int OffSet;

    OffSet = (Ascii - 32)*16;

    for (i=0;i<16;i++)
    {
        Set_ramaddr(x,y+i);
        send_command(0x22);
        //str = *(AsciiLib + OffSet + i);  
		str = pgm_read_byte((AsciiLib + OffSet + i));
        for (j=0;j<8;j++)
        {
            if ( str & (0x80>>j) )     //0x80>>j
            {
                send_data((unsigned int)(LineColor&0xffff));
            }
            else
            {
                if (NORMAL == Mod) 
                    send_data((unsigned int)(FillColor&0xffff));
                else
                {
                    Set_ramaddr(x+j+1,y+i);
                    send_command(0x22);  
                }
            }               
        } 
    }
}

/**************************************************************************************/




⌨️ 快捷键说明

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