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

📄 t9_v1.c

📁 基于单片机的拼音输入程序
💻 C
📖 第 1 页 / 共 2 页
字号:
#include <REG52.H>
#include <stdio.h>
#include<ziku.h>
#include<string.h>




#define uint  unsigned int
#define uchar unsigned char
#define DISP_ON    0x3f
#define DISP_OFF   0x3e
#define COL        0x40    //列地址
#define PAGE 0xb8          //行地址
#define START_LINE 0xc0
#define LCD_BUS     P2 
#define LCD_CON     P3 
#define key_bus     P1




//==========================================口定义=================================================

sbit CS1=LCD_CON^0;             //  CS1
sbit CS2=LCD_CON^1;             //  CS2
sbit E =LCD_CON^2;              //液晶使能
sbit RW=LCD_CON^3;              //液晶读写
sbit DI=LCD_CON^4;              //液晶数据指
sbit sw0=P3^6;					//OK
sbit sw1=P3^7; 					//Cancel
sbit SCL=P0^0;
sbit SDA=P0^1;

//===========================================END===================================================






//=============================================函数声明============================================

void display_h(uchar page,uchar col,uchar code *str,uchar n);
void display_e(uchar page,uchar col,uchar code *str,uchar n);
void display_num(uchar page,uchar col,uchar code *str,uchar n);
void line_v(uchar x,uchar y,uchar l);
void line_h(uchar x,uchar y,uchar l);
void init();
void cls();
void delay(uint);         //延时程序
void write_cmd(uchar);    //写命令操作字
void write_data(uchar);   //写数据
uchar read_data();        //读数据
uchar key_ii();
unsigned char code *py_v1(unsigned char input_py_val[]);
void point(uchar x,uchar y);
unsigned char *T9PY(char *strInput_t9PY_str,uchar length);
//==============================================END================================================


#include<iic.h>


//========================================全局变量声明=============================================
uchar *p_py;
unsigned char PY_num;
struct t9PY_index code  * T9PY_mb[4];

/**************************************************************************************************
*
*T9 输入法查找函数
*函数声明:unsigned char *T9PY(char *strInput_t9PY_str,uchar length);
*参数说明:strInput_t9PY_str为字符串的地址,length为字符串的长度
*函数功能:把找到的汉字点阵表的首地址放在数组T9PY_mb中
*
**************************************************************************************************/

unsigned char *T9PY(char *strInput_t9PY_str,uchar length)
{
    struct t9PY_index *cpHZ,*cpHZedge;
    unsigned char i;
    PY_num=0;                              
    if(*strInput_t9PY_str=='\0')return(0);         //如果输入空字符返回0

    cpHZ=&(t9PY_index2[0]);                        //查首字母索引
    cpHZedge=t9PY_index2+sizeof(t9PY_index2)/sizeof(t9PY_index2[0]);
    while(cpHZ < cpHZedge)                         //待查询记录条数
    {											
        for(i=0;i<length;i++)
        {
               if(*(strInput_t9PY_str+i)!=*((*cpHZ).t9PY_T9+i))    //检查字符串匹配
               {
                     
					break;                                         //发现字母串不配,退出/
               }
			   
			               
        }
		if(i==length&&PY_num<=3)    
        {
                              
  			   T9PY_mb[PY_num]=cpHZ;
               PY_num++;
			   //break;   
        }

        cpHZ++;
    }
	if (PY_num>0)
    {
          p_py=T9PY_mb[0]->PY;
		  return(T9PY_mb[0]->PY_mb);                
	}
	else
	{
		  return(PY_mb_a);
	}
}


/**************************************************************************************************
*         P17>---0-----1-----2-----3      
*				 |	   |	 |	   |
*		  P16>---4-----5-----6-----7
*				 |	   |	 |	   |
*		  P15>---8-----9-----A-----B
*				 |	   |	 |	   |
*		  P14>---C-----D-----E-----F
*				 |	   |	 |	   |
*				P13   P12	P11	  P10
*4*4键盘输入函数
*函数声明:uchar key_ii();
*函数参数:无
*
*
*
**************************************************************************************************/
uchar key_ii()
{
    uchar keycode=0xff,temp,i;
    key_bus=0x0f;
	temp=key_bus&0x0f; 
	if(temp!=0x0f)              //有键按下
	{
	    key_bus=0x7f;
	    for(i=0;i<4;i++)
		{
			temp=key_bus&0x0f;
            if(temp==0x07)
	        {    
			    keycode=i*4;
				break;
			}
	        else if(temp==0x0b)
			{
	            keycode=i*4+1;
				break;
			}
    	    else if(temp==0x0d)
			{
	            keycode=i*4+2;
				break;
			}
    	    else if(temp==0x0e)
			{
	            keycode=i*4+3;
				break;
			}
			key_bus=(key_bus>>1)|0x80;
		}

	     while((key_bus&0x0f)!=0x0f)   //等待释放
		 {
		     key_bus=0x0f; 
		 }								             
	    
	 }
	 return keycode;
}

/**************************************************************************************************
*
*
*
*
*
*
**************************************************************************************************/
uchar key()
{
      uchar keycode=0,temp;
	  //键盘口,先置1
	  sw0=1; sw1=1;
	  temp=P3&0xD0;
	  if(temp!=0xD0)
	  {
            sw0=1; sw1=1;
			temp=P3&0x40;
	        if(temp==0)keycode=6;
	        temp=P3&0x80;
	        if(temp==0)keycode=7;
			while(temp!=0xD0)
			{
			      temp=P3&0xD0;
			}
	  }
	  return keycode;

	     
}


/**************************************************************************************************
*
*延时函数
*函数声明:void delay(uint t);
*函数参数:t
*
*
**************************************************************************************************/

void delay(uint t)             
{
      uint i,j;
      for(i=0;i<t;i++)
            for(j=0;j<10;j++);
}


/**************************************************************************************************
*
*向LCD写命令函数
*函数声明:void write_cmd(uchar cmd);
*函数参数:cmd(具体命令查看数据手册)
*
*
**************************************************************************************************/

void write_cmd(uchar cmd)  
{

      DI=0; RW=0;
      LCD_BUS=cmd;
      E=1;
      E=0;
}

/**************************************************************************************************
*
*向LCD写数据函数
*函数声明:void write_data(uchar dout);
*函数参数:dout为要向LCD写的数据
*
*
**************************************************************************************************/
void write_data(uchar dout)  
{

      DI=1; RW=0;
      LCD_BUS=dout;
      E=1;
      E=0;
}


/**************************************************************************************************
*
*从LCD的RAM中读数据
*函数声明:uchar read_data();  
*返回参数:uchar型为RAM中的数据
*
*
**************************************************************************************************/
uchar read_data()  
{
	  uchar din;
	  LCD_BUS=0xff;
	  DI=1; RW=1;
      E=0;
	  E=0;
	  E=1;
	  E=1;
	  E=1;
	  din=LCD_BUS;
	  return din;        
}


/**************************************************************************************************
*
*画竖线函数
*函数声明:void line_v(uchar x,uchar y,uchar l);
*函数参数:x,y分别为LCD的横向和纵向的坐标值,l为竖线的长度
*
*
**************************************************************************************************/
void line_v(uchar x,uchar y,uchar l)
{
    uchar i;
	for(i=0;i<l;i++) point(x,y+i);
      
}

/**************************************************************************************************
*
*画横线函数
*函数声明:void line_h(uchar x,uchar y,uchar l);
*函数参数:x,y分别为LCD的横向和纵向的坐标值,l为横线的长度
*
*
**************************************************************************************************/
void line_h(uchar x,uchar y,uchar l)
{
    uchar i;
	for(i=0;i<l;i++) point(x+i,y);
      
}

/**************************************************************************************************
*
*在LCD的任意位置画点的函数
*函数声明:void point(uchar x,uchar y);
*函数参数:x为lcd的列坐标(0-127),y为lcd的纵坐标(0-63)
*利用此函数能在LCD12864的任意位置画一个点
*
**************************************************************************************************/
void point(uchar x,uchar y)
{
  uchar y1,y2,pp;
  uchar datain;
  pp=1;
  y1=y>>3;
  y2=y%8;
  pp=pp<<y2;
  if (x<64)
  {
    CS2=1;
	CS1=0;
	write_cmd(PAGE+y1);
    write_cmd(COL+x);  
    datain=read_data();
    write_cmd(PAGE+y1);
    write_cmd(COL+x);
    delay(10);
    datain=read_data();

    write_cmd(PAGE+y1);
    write_cmd(COL+x);
    datain=datain|pp;
    write_data(datain);
  }
  else
  {

    CS2=0;
	CS1=1;
	write_cmd(PAGE+y1);
	write_cmd(COL+x-64);
    datain=read_data();
    write_cmd(PAGE+y1);
    write_cmd(COL+x-64);
    delay(10);
    datain=read_data();
    write_cmd(PAGE+y1);
    write_cmd(COL+x-64);
    datain=datain|pp;
    write_data(datain);
  }
  

  
}


/**************************************************************************************************
*
*初始化LCD屏函数
*函数声明:void init(); 
*函数参数:无

⌨️ 快捷键说明

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