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

📄 lcd._c

📁 单片机的应用程序
💻 _C
字号:
#include <iom48v.h>
#include <macros.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "CommonDefs.h"
#include "res.h"
#include "lcd.h"


void Comwrite( unsigned char command )
{//
	unsigned char i;
	
	//TIMER1_STOP();
	
	Clr_LCD_CS();
	Clr_LCD_DC();
	Clr_LCD_SCK();
	delay_nus(1);
	
   for(i=8;i>0;i--) // Send command byte
   {//
      if(command&0x80) Set_LCD_SDA();
      else Clr_LCD_SDA();
      delay_1us();
      Set_LCD_SCK(); 
      delay_1us();
      Clr_LCD_SCK();
      delay_1us();
      command<<=1;
   }
	
	Set_LCD_CS();
	Set_LCD_DC();
	delay_nus(1);
	//TIMER1_START();
}

//###########################################################//
void Datwrite(unsigned char dat)
{//
	unsigned char i;
	
	//TIMER1_STOP();  //
	
	Clr_LCD_CS();
	Set_LCD_DC(); 
	Clr_LCD_SCK();
	delay_1us();
	
   for(i=8;i>0;i--) // Send command byte
   {//
      if(dat&0x80) Set_LCD_SDA();
      else Clr_LCD_SDA();
      delay_1us();
      Set_LCD_SCK(); 
      delay_1us();
      Clr_LCD_SCK();
      delay_1us();
      dat<<=1;
   }
	
	Set_LCD_CS();
	Clr_LCD_DC();
	delay_nus(1);
	//TIMER1_START();
}

void LCDM_putchar(unsigned char x, unsigned char y,
					  const unsigned char *uchar_matrix,
					  unsigned char Reverse )
{//
	unsigned char i, uchar_temp;
	
	CLI();
	Comwrite(0x40);  //Initial Display Line
						//Sets the line address of display RAM to determine the Initial Display Line.
						//The RAM display data is displayed at the top row 
						//(COM0 when SHL = L, COM63 when SHL = H) of LCD panel.	
	if(y>7) return;
	else if( y>=4 )	//	y=4,5,6,7; means page b0,b1,b2,b3
	{//
		  //page = 0xb0 + y - 4;
		  Comwrite( 0xac + y );//Set page address, 0b1011( P3 P2 P1 P0 )
	}
	else	   	 	  // y=0,1,2,3; means page b4,b5,b6,b7
	{//
		  //page = 0xb0 + y + 4;
		  Comwrite( 0xb4 + y );//Set page address, 0b1011( P3 P2 P1 P0 )
	}
	
	if(x>120) return; // font 8x8, the x start point should not larger then 120
	else
	{//
		  //column = x;
		  x = x+1;
		  uchar_temp = 0x10|((x&0xF0)>>4);
		  Comwrite( uchar_temp );//Set column address MSB, 0b0001( Y7 Y6 Y5 Y4 )
		  uchar_temp = x&0x0F;
		  Comwrite( uchar_temp );//Set column address LSB, 0b0000( Y3 Y2 Y1 Y0 )
	}
	
	for( i=0; i<8; i++ )
	{//
		  uchar_temp = uchar_matrix[i];
		  if(Reverse==0) uchar_temp = ~uchar_temp;
		  Datwrite(uchar_temp);
	}
	
	delay_nus(1);
	SEI();
}


void LCD_init(void) 
{ 
  	//Comwrite(0xe3); //?? I change this to 0xe2, just as the following instruction
	Comwrite(0xe2);	// software reset
	
	Comwrite(0xA2);  //Select LCD bias
	
	Comwrite(0xA0);  //Select SEG output direction
						//When ADC = 0: normal direction (SEG0 -> SEG131)
						//When ADC = 1: reverse direction (SEG131 -> SEG0)
						
	Comwrite(0xc9);  //Select COM output direction, 0b1100(SHL)xxx
						//When SHL = 0: normal direction (COM0 -> COM63)
						//When SHL = 1: reverse direction (COM63 -> COM0)				
	
	Comwrite(0x2c);  //Control power circuit operation
	Comwrite(0x2e);  //Control power circuit operation
	Comwrite(0x2F);  //Control power circuit operation
	
	
	Comwrite(0x81);  //Set reference voltage mode
	Comwrite(0x28);  //Control power circuit operation
	Comwrite(0x24);	//Select internal resistance ratio of the regulator resistor
	
	Comwrite(0x40);  //Initial Display Line
						//Sets the line address of display RAM to determine the Initial Display Line.
						//The RAM display data is displayed at the top row 
						//(COM0 when SHL = L, COM63 when SHL = H) of LCD panel.	
							
	Comwrite(0XA6);	//normal display, 0xA7 reverse display
	Comwrite(0xAF);	//display ON, 0xAE display OFF

	//Comwrite(0x03);//當I/D = "1", 游標右移,DDRAM 位址計數器(AC)加1
	//delay_ms(10); 
	//Comwrite(0x0c); //令光标,0x0c=不显示,0x0d=显示闪动.
	//delay_1ms();
	//Comwrite(0x01); // 清除显示器  
	//delay_1ms();      
	//Comwrite(0x06); //令LCD每接收到1Byte数据后,AC自动加1 
    delay_1ms();
}

void LCD_hard_init(void)
{//
  LCD_SDA_DDR |= LCD_SDA_PINNum;
  LCD_SDA_PORT |= LCD_SDA_PINNum;
      
  LCD_SCK_DDR |= LCD_SCK_PINNum;     
  LCD_SCK_PORT |= LCD_SCK_PINNum;
    
  LCD_DC_DDR |= LCD_DC_PINNum;	   /* 设置液晶的寄存器选择控制端口为输出引脚 */ 
  LCD_DC_PORT |= LCD_DC_PINNum;
    
  LCD_RES_DDR |= LCD_RES_PINNum;	/* 设置液晶的复位信号控制端口为输出引脚 */
  LCD_RES_PORT |= LCD_RES_PINNum;
  
  LCD_CS_DDR |= LCD_CS_PINNum;    /* 设置液晶的片选控制端口为输出引脚 */ 
  LCD_CS_PORT |= LCD_CS_PINNum;
  
  LCD_BL_DDR |= LCD_BL_PINNum;
  LCD_BL_PORT |= LCD_BL_PINNum;
  
  //prs=1;
  
  Clr_LCD_RES();
  delay_nms(80); 
  Set_LCD_RES();
  
  LCD_init();
}
/*-----------------------------------------------------------------------
LCD_draw_map      : 位图绘制函数

输入参数:X、Y    :位图绘制的起始X、Y坐标;
          *map    :位图点阵数据;
          Pix_x   :位图像素(长)
          Pix_y   :位图像素(宽)
-----------------------------------------------------------------------*/
void LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,
		   						unsigned char Pix_x,unsigned char Pix_y,
								const unsigned char *map )
  {
    unsigned int i,n;
    unsigned char row;
		
    CLI();
    if (Pix_y%8==0) row=Pix_y/8;      //计算位图所占行数
		else
        row=Pix_y/8+1;
			
    for (n=0;n<row;n++)
      {
        for(i=0; i<Pix_x; i+=8)
          {
            LCDM_putchar( X+i, Y+n, &map[i+n*Pix_x], 1);
				//LCD_write_byte(map[i+n*Pix_x], 1);
          }
        //Y++;                         //换行
      }
		SEI();      
  }

void LCD_backlight(unsigned char on_off) 
{ 
  if (on_off == ON) 
  { 
//    LCDM_BL_DDR |= LCDM_BL_PINNum;    /* 设置液晶的背景灯控制端口为输出引脚 */ 
//    LCDM_BL_PORT |= LCDM_BL_PINNum;   /* 打开液晶的背景灯 */   
  } 
  if (on_off == OFF) 
  { 
//    LCDM_BL_DDR |= LCDM_BL_PINNum;    /* 设置液晶的背景灯控制端口为输出引脚 */ 
//    LCDM_BL_PORT &= ~LCDM_BL_PINNum;  /* 关闭液晶的背景灯 */   
  }
  delay_nus(2);
}

void LCD_power(unsigned char on_off)
{ 
  if (on_off == ON) 
  { 
  		Comwrite(0xAF);	//display ON, 0xAE display OFF    
    //S_LCD_PS();
  } 
  
  if (on_off == OFF) 
  { 
  		Comwrite(0xAE);	//display ON, 0xAE display OFF
    //R_LCD_PS();
  }
  delay_1ms();
}


void LCD_putchar(unsigned char x,unsigned char y, unsigned char c,unsigned char flag)
{  

    LCDM_putchar( x, y, &Echar[c-32][0], flag);
}


void LCD_puts(unsigned char x,unsigned char y, unsigned char *s, unsigned char flag)
{   
	unsigned char j=0;
	while(*s!='\0') 
    	{
	    LCD_putchar( x+j*8, y, *s, flag);
	    s++;
	    j++;
      }    
}

void LCD_cputchar(unsigned char x,unsigned char y, const unsigned char c,unsigned char flag)
{  

    LCDM_putchar( x, y, &Echar[c-32][0], flag);
}

void LCD_clearRow(unsigned char y)
{//
   unsigned char x;
   for(x=0;x<16;x++)
   {//
      LCD_putchar( x*8, y, ' ', 1 );
   }
}

void LCD_cputs(unsigned char x,unsigned char y, const unsigned char *s, unsigned char flag)
{   
	unsigned char j=0;
	while(*s!='\0') 
    	{
	    LCD_cputchar( x+j*8, y, *s, flag);
	    s++;
	    j++;
      }    
}

void LCD_MsgBox( unsigned char *s )
{//
   unsigned char x=0,y=0;
   
   LCD_cls();
   while(*s!='\0') 
   {//
      if(*s=='\r') x=0;
      else if(*s=='\n') y++;
      else
      {
      LCD_putchar( x*8, y, *s, 1);      
      x++;
      }      
      if(x>15)
      {//
      		 x=0;
		 y++;
      }
      if(y>7)
      {//
      		 delay_nms(2000);
		 y = 0;
      }
      s++;
   } 
}


void LCD_cls(void)     /* 最大列点数 */ 
{ 
   unsigned char  i;
   unsigned char  j;
	CLI();
	for(i=0;i<8;i++)
	{//
		  Comwrite(0xb0+i); // page
		  Comwrite(0x10);	// column MSB
		  Comwrite(0x01);	// column LSB
		  for(j=0;j<128;j++)
		  {//
	   	 Datwrite(0);
	   }
	}
  SEI();
}

void LCD_putchardec(unsigned char x,unsigned char y, const unsigned char c,unsigned char flag)
{
 	unsigned char a0,b0,c0;
	a0 = c % 100;
	b0 = (c-a0*100) % 10;
	c0 = c-a0*100-b0*10; 
    LCD_putchar( x, y, '0'+a0, flag);
    LCD_putchar( x+8, y, '0'+b0, flag);
    LCD_putchar( x+16, y, '0'+c0, flag);
}

⌨️ 快捷键说明

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