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

📄 2274lcd.c

📁 关于信利12864的LCD源码,已通过,单片机MSP430,3.3V供电,
💻 C
字号:
/*
//msp430fe427spi接口程序,驱动SSD1815LCD(串口方式)
//2007.7.4  SPI发送数据至LCD成功.保存文件名:spidebug.c
//2007.7.5  spi 发送字库5*8至LCD成功,保存文件名:spizk.c
//2007.7.15 用FE427SPI发送成功,保存文件名:spi_fe427.c
//2007.7.15 改用FG4619芯片做实验

*/

//#include <msp430xE42x.h>
//#include  "msp430xG46x.h"
#include <c:\msp430\zk.h>
#include "msp430x22x4.h"
//
// LCD.res(2)-----------P7.7
// LCD.cs(3)------------P7.6
// LCD.RS(4)------------P7.5
// LCD.clk(17)----------P7.3
// LCD.SDA(18)----------P7.2
//


//函数描述区//
void Init_Port(void);
void res_lcd(void);
void Init_SPI (void);
void rs_command(void);
void rs_data(void);
void CS_Enable(void);
void CS_Disable(void);
void init_lcd(void);
void writeSPI(char RS,char inBuf);
void disp(char sy_num,char x,char y);
void dot(char x,char y);
void sysinit(void);
void opcompinit(void);//光电编码器测试
void dnum(int x);
void init_key(void);
void disp8_16(char sy_num,char zx,char y);

//系统变量定义区
//
int opvalue;//增量型光电编码现场值
unsigned char opset=0;
char  dnum_e,dnum_h,dnum_t,dnum_o,dnum_0;



#pragma vector=PORT2_VECTOR
__interrupt void opcomp(void)
{
  char p2result;
  char result;
  p2result=P2IN&3;
  switch (p2result)
  {
    case 0:result=0;P2IES&=~BIT0;P2IES&=~BIT1;break;
    case 1:result=1;P2IES|=BIT0;P2IES&=~BIT1;break;
    case 2:result=3;P2IES&=~BIT0;P2IES|=BIT1;break;
    case 3:result=2;P2IES|=BIT0;P2IES|=BIT1;break;
  }
  if ((opset>result)||((opset==0)&&(result==3)))
  {
    opvalue++;
  }
  if ((opset<result)||((opset==3)&&(result==0)))
  {
    opvalue--;
  }
  P2IFG&=~BIT0;
  P2IFG&=~BIT1;
  if ((opvalue%20)==3)
  {
    P1OUT ^=BIT0; 
  }
  opset=result;
} 
 

void init_key(void)
{
    //将P1口所有的管脚在初始化的时候设置为输入方式
    P1DIR = 0;
 
    //将P1口所有的管脚设置为一般I/O口
    P1SEL = 0;
    
    // 将P1.4 P1.5 P1.6 P1.7设置为输出方向
    P1DIR |= BIT4;
    P1DIR |= BIT5;
	P1DIR |= BIT6;
    P1DIR |= BIT7;

	//先输出低电平
	P1OUT = 0x00;

	// 将中断寄存器清零
    P1IE = 0;
    P1IES = 0;
    P1IFG = 0;
    //打开管脚的中断功能
	//对应的管脚由高到低电平跳变使相应的标志置位
    P1IE |= BIT0;	
    P1IES |= BIT0;	
    P1IE |= BIT1;	
    P1IES |= BIT1;
	P1IE |= BIT2;	
    P1IES |= BIT2;	
    P1IE |= BIT3;	
    P1IES |= BIT3;
	_EINT();//打开中断
    return;
}

void Delay(void)
{
	int i;
	for(i = 100;i>0;i--) ;//延时一点时间
}
int KeyProcess(void)
{
    int nP10,nP11,nP12,nP13;
	int nRes = 0;
	//P1.4输出低电平
	P1OUT &= ~(BIT4);
	nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 13;
	nP11 = P1IN & BIT1;
	if (nP11 == 0) nRes = 14;
	nP12 = P1IN & BIT2;
	if (nP12 == 0) nRes = 15;
	nP13 = P1IN & BIT3;
	if (nP13 == 0) nRes = 16;
	//P1.5输出低电平
	P1OUT &= ~(BIT4);
	nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 9;
	nP11 = P1IN & BIT1;
	if (nP11 == 0) nRes = 10;
	nP12 = P1IN & BIT2;
	if (nP12 == 0) nRes = 11;
	nP13 = P1IN & BIT3;
	if (nP13 == 0) nRes = 12;
	//P1.6输出低电平
	P1OUT &= ~(BIT4);
	nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 5;
	nP11 = P1IN & BIT1;
	if (nP11 == 0) nRes = 6;
	nP12 = P1IN & BIT2;
	if (nP12 == 0) nRes = 7;
	nP13 = P1IN & BIT3;
	if (nP13 == 0) nRes = 8;
	//P1.7输出低电平
	P1OUT &= ~(BIT4);
	nP10 = P1IN & BIT0;
	if (nP10 == 0) nRes = 1;
	nP11 = P1IN & BIT1;
	if (nP11 == 0) nRes = 2;
	nP12 = P1IN & BIT2;
	if (nP12 == 0) nRes = 3;
	nP13 = P1IN & BIT3;
	if (nP13 == 0) nRes = 4;

	P1OUT = 0x00;//恢复以前值。

	//读取各个管脚的状态
	nP10 = P1IN & BIT0;
	nP11 = P1IN & BIT1;
	nP12 = P1IN & BIT2;
	nP13 = P1IN & BIT3;
	for(;;)
	{
		//if(nP10 == 1 && nP11 == 1 && nP12 == 1 && nP13 == 1)
		if ((P1IN&0X0F)==0X0F)
                {
			//等待松开按键
			break;
		}
	}
//        disp8_16(nRes,5,5);
	return nRes;
}
// 处理来自端口 1 的中断
#pragma vector=PORT1_VECTOR
__interrupt void p1key(void)
{
  int keyresult;
  _DINT();
	Delay();
	keyresult=KeyProcess();
    if(P1IFG & BIT0)
    {    	
    	P1IFG &= ~(BIT0);// 清除中断标志位
    }
    if(P1IFG & BIT1)
    {
    	P1IFG &= ~(BIT1);// 清除中断标志位
    }
    if(P1IFG & BIT2)
    {
    	P1IFG &= ~(BIT2);// 清除中断标志位
    }
	if(P1IFG & BIT3)
    {
    	P1IFG &= ~(BIT3);// 清除中断标志位
    }
    disp8_16(keyresult+1,5,5); 
    _EINT();
    
}

void dnum(int x)
	{
	dnum_e=x/10000;
	dnum_h=(x%10000)/1000;
	dnum_t=((x%10000)%1000)/100;
	dnum_o=(((x%10000)%1000)%100)/10;
	dnum_0=(((x%10000)%1000)%100)%10;

	}



void opcompinit(void)
{
  //init port
  //
  P2DIR&=~BIT0;
  P2DIR&=~BIT1;
  P2IE=0;
  P2IES=0;
  P2IFG=0;
  P2IE|=BIT0+BIT1;
  if (P2IN&BIT0)
  {
    P2IES|=BIT0;
  }
  else
  {
    P2IES&=~BIT0;
  }
  if (P2IN&BIT1)
  {
    P2IES|=BIT1;
  }
  else
  {
    P2IES&=~BIT1;
  }
     
}



void sysinit(void)
{
   unsigned int n;
  WDTCTL=WDTPW+WDTHOLD;                   //关看门狗
  BCSCTL1 =CALBC1_8MHZ;                   //设定DCO为1MHZ
  DCOCTL =CALBC1_8MHZ; 
  do{                                     //等待晶振稳定
  IFG1  &=~OFIFG;                        
  for(n=5000;n>0;n--);
  }while(OFIFG&IFG1);
}


void line(int x0,int x1,int y)
{
  int i;
  for (i=x0;i<=x1;i++)
  {
    dot((char)i,(char)y);
  }
  
  
}


void dot(char x,char y)
{
  char zx0,zx1,zy0,zy1;
  char dispdata=0;
  zx0=x%16;
  zx1=x/16;
  zy0=y%8;
  zy1=y/8;
  writeSPI(0,zx0);
  writeSPI(0,(0x10+zx1));
  writeSPI(0,0xb0+zy1);
  
  switch(zy0)
  {
  case 0:
        dispdata=BIT0;
        break;
  case 1:
        dispdata=BIT1;
        break;
  case 2:
        dispdata=BIT2;
        break;
  case 3:
        dispdata=BIT3;
        break;
  case 4:
        dispdata=BIT4;
        break;
  case 5:
        dispdata=BIT5;
        break;
  case 6:
        dispdata=BIT6;
        break;
  case 7:
        dispdata=BIT7;
        break;
  }
  writeSPI(1,dispdata);
  
}


void lcd_cls(void)
{
  char i,j;
  for(j=0;j<8;j++)
  {
    writeSPI(0,0x00);
    writeSPI(0,0x10);
    writeSPI(0,(0xb0+j));
    for(i=0;i<128;i++)
    {
      writeSPI(1,0x00);
    }
  }
}




void disp(char sy_num,char zx,char y)
{ 
	char k,i,x1,x2;
	const char *str;
	y%=8;
        x1=zx%16;
        x2=zx/16;
        str=ZK5_8;
	str+=5*sy_num;
	k=0xb0+y;
	writeSPI(0,x1);
        writeSPI(0,(0x10+x2));
	writeSPI(0,k);
	for(i=0;i<5;i++) 
	{
	  writeSPI(1,*str);
	  str++; 
	}
			
}

void disp8_16(char sy_num,char zx,char y)
{
const  char *str;
  char i,j,k;
  char x1,x2;
  y%=8;
  x1=zx%16;
  x2=zx/16;
  str=ZK5_8;
  str=str+16*sy_num;
  for(j=0;j<2;j++)
	{
	k=0xb0+y+j;
	writeSPI(0,x1);
        writeSPI(0,(0x10+x2));
	writeSPI(0,k);
	for(i=0;i<8;i++) 
		{
		writeSPI(1,*str);
		str++; 
		}
		  	
	 }

}




void main(void)
{
	
int i,j;



 /////////////////////////////////
 // 初始化
 //  
    sysinit();
     _DINT();
    Init_Port();
    Init_SPI();
    
    init_key();

    res_lcd();
    init_lcd();
    lcd_cls();
    
/*  while(1)
  {
 //    if (P2IN&BIT1) i++;
        
    writeSPI(1,0xaa);
    
 }
    */
//
   for (i=0;i<16;i++)
    {
     // disp((i),i,(i));
      for  (j=0;j<4;j++)
      {
        disp8_16(4*i+j,8*i,2*j);
      }
   
    } 
   for (i=30000;i>0;i--);
   
  opcompinit();
   _EINT();
    
 // P1DIR |=BIT0;
	while(1)
        {
          if (opvalue>=0)
          {
            dnum(opvalue);disp8_16(0,0,3);
          }          
          else 
          {
            dnum(0-opvalue);
            disp8_16(14,0,3);
          }
          disp8_16(dnum_e+1,8,3);
          disp8_16(dnum_h+1,16,3);
          disp8_16(dnum_t+1,24,3);
          disp8_16(dnum_o+1,32,3);
          disp8_16(dnum_0+1,40,3);
        }
          ;
}


void Init_Port(void)
{
 
  //将P1口P2口所有的管脚在初始化的时候设置为输入方式
  //  
   P4DIR |=BIT0+BIT1+BIT2;
   
          
     
          return;
}

void Init_SPI (void)
{
  //P3.3 CLK P3.2 SOMI P.1 SIMO 
  //RS P1.1 0 COMMAND   1 DATA
  //CS P1.0  0 ENABLE 1 DISABLE
  //RES P1.2 0 RESET                         
  P3SEL |=BIT1+BIT2+BIT3;                 //p3.4为TX,P3.5为RX,P3.0为UCA0CLK
  P3DIR |=BIT1;//+BIT3;                           //P3.4为输出方向
  UCB0CTL1 |=UCSSEL0+UCSSEL1;                     //时钟源选择32768晶振
  UCB0CTL0 |=UCMST+UCSYNC+UCCKPL+UCMSB;//;+UCCKPH //  //spi主机模式,spi通讯,时钟极性选为高电平无效,选择最高有效位先发
  UCB0BR1=0;                              //32768/1200=27.3
  UCB0BR0=20;
  //UCB0MCTL=0;
  UCB0CTL1 &=~UCSWRST;                    //进入SPI工作模式
 // IE2 |= UCA0RXIE;                        //使能spi接收中断
 // _BIS_SR(GIE); //使能总中断
  return;
}

void res_lcd(void)
{
  int i;
  CS_Enable();
  P4OUT&=~(BIT2);
  for(i=1000;i>1;i--);
  P4OUT|=BIT2;
  CS_Disable();
  return;
  
}

void writeSPI(char RS,char inBuf)
{
	int n;
        CS_Disable();
        if (RS==0) rs_command();
        else rs_data();
	CS_Enable();
         while(!(UCB0TXIFG&IFG2));
         
	UCB0TXBUF = inBuf;
   //      while((UCB0TXIFG&IFG2));
   //  while((UCB0STAT&UCBBUSY));
       for(n=100;n>0;n--);
        CS_Disable();
	return;
}

void CS_Enable(void)
{
	//P1.0输出低电平
	P4OUT &=~(BIT0);
	return ;
}
void CS_Disable(void)
{
	//P1.0输出高电平
	P4OUT|=BIT0;
	return ;
}

void rs_data(void)
{
  
  P4OUT|=BIT1;
  return;
}

void rs_command(void)
{
  P4OUT&=~(BIT1);
  return;
}

void init_lcd(void)
{
  res_lcd();
  rs_command();
  writeSPI(0,0xe2);
  writeSPI(0,0xc8);
  writeSPI(0,0x2f);
  writeSPI(0,0x23);
  writeSPI(0,0xa2);
//  writeSPI(0,0x81);
//  writeSPI(0,0x0F);
  writeSPI(0,0xa9); 
  writeSPI(0,0x40);
  writeSPI(0,0xa6);
  writeSPI(0,0xAF);
 return; 
  
}

⌨️ 快捷键说明

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