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

📄 lcd_driver.c

📁 实现truly m240128 lcd的驱动,使用 mcu为mega1280
💻 C
字号:

/*************************************************
  Copyright (C), 2007-2008, ptoteam Tech. Co., Ltd.
  File name:      lcd_driver.c
  Author:  张建福     Version:   v1.0     Date: 2007,5,30
  Description:    
                 这个文件是关于lcd操作函数的源代码
  Others:         
  Function List:  
  
  History:   
       
    1. data: 2007,5,30//创建
       Author:张建福
    

*************************************************/
#include <avr/io.h>
#include "char_table.h"
#include "lcd_driver.h"
#define cli() asm("cli\n\t")
#define sei() asm("sei\n\t")
unsigned  char display_temp[34];




void reset_lcd(void)
{
 LCD_RST_LOW();
 delay_us(150);
 LCD_RST_HIGH();

}

void delay_us(unsigned int timers)
{/*
asm volatile(
     "push    r16\n\t"
     "in      r16,0x3f\n\t"
     "push    r16\n\t"
     "push    r26       \n\t"         "push    r27       \n\t"         "ldi     r26,0x0   \n\t"
	 "ldi     r27,0x0   \n\t"         "RJMP    ttst\n\t"      "stwg:WDR         \n\t"      "NOP              \n\t"      "NOP              \n\t"      "NOP              \n\t"      "NOP              \n\t"      "NOP              \n\t"      "NOP              \n\t"      "NOP              \n\t"      "NOP              \n\t"      "NOP              \n\t"      "ADIW    r26,0x01  \n\t"      "ttst: \n\t"      "CP      r26,R24  \n\t"      "CPC     r27,R25  \n\t"      "BRCS    stwg  \n\t" 
	 "pop     r26      \n\t"         "pop     r27       \n\t"   
     "pop     r16\n\t"
     "out     0x3f,r16\n\t"
   	 "pop     r16\n\t"   	 );/**/
unsigned int i;
  for(i=0;i<timers;i++)
  {
  WatchdogReset();
   nop();
   nop();
   nop();
   nop();
   nop();
   nop();
   nop();
   nop();
   nop();
  }

}

void init_lcd(void)
{
  
  reset_lcd();
  commd_write(0Xfe);
  LCD_csa_LOW();
  LCD_csb_LOW();
  delay_ns(2);
  commd_write(0xc0);
  commd_write(0x3f);
  LCD_csa_HIGH();
  LCD_csb_LOW();
  delay_ns(2);
  commd_write(0xc0);
  commd_write(0x3f);
  LCD_csa_LOW();
  LCD_csb_HIGH();
  delay_ns(2);
  commd_write(0xc0);
  commd_write(0x3f);
  LCD_csa_HIGH();
  LCD_csb_HIGH();
  delay_ns(2);
//  commd_write(0X3f);
 }

void shutt_lcd(void)
{
  LCD_csa_LOW();
  LCD_csb_LOW();
  delay_ns(2);
  commd_write(0x3e);
  LCD_csa_HIGH();
  LCD_csb_LOW();
  delay_ns(2);
  commd_write(0x3e);
  LCD_csa_LOW();
  LCD_csb_HIGH();
  delay_ns(2);
  commd_write(0x3e);
}
void open_lcd(void)
{
  LCD_csa_LOW();
  delay_ns(2);
  LCD_csb_LOW();
  delay_ns(2);
  commd_write(0x3f);
  LCD_csa_HIGH();
  	delay_ns(2);
  LCD_csb_LOW();
  delay_ns(2);
  commd_write(0x3f);
  LCD_csa_LOW();
  	delay_ns(2);
  LCD_csb_HIGH();
  delay_ns(2);
  commd_write(0x3f);
}
void commd_write(unsigned char cmd)
{
   //cli();
	if(status_read() == TRUE)
	{
    LCD_WR_LOW();
	delay_ns(2);
    LCDCMD_low();
	delay_ns(2);
    DDRA=0XFF;
	PORTA=cmd;
	delay_ns(2);
    LCD_e_LOW();
	delay_ns(4);
	LCD_e_HIGH(); 
	delay_ns(4);
	LCD_e_LOW();
  	}
 // sei();	
}

/*************************************************
  Function:    void delay_ns(unsigned char temp)
  Description:    纳秒延时程序
  Calls:          
  Called By:      
  Table Accessed: 
  Table Updated:  
  Input:   
              
                 
  Output:         
  Return:       
  Others:        1.2007,6,5 创建
                 2.2007,7,3 修改
				   a:修改初始化方式
  author:   张建福
  			
*************************************************/
void delay_ns(unsigned char temp)
{
unsigned char i;
for(i=0;i<temp; i++)
{
 nop();

}/*
asm volatile(
      "push    r16\n\t"
      "in      r16,0x3f\n\t"
      "push    r16\n\t"
      "ldi    r16,0x0\n\t "
      "RJMP    nsJmp\n\t"
      "gooNs:NOP              \n\t"
      "inc r16\n\t"
      "nsJmp: CP  R16,R24  \n\t"
      "BRCS    gooNs  \n\t"
      "pop     r16\n\t"
      "out     0x3f,r16\n\t"
   	  "pop     r16\n\t"
      );*/
}


void data_write(unsigned char data)
{
   // cli();
	LCD_WR_LOW();
	delay_ns(1);
    LCDCMD_high();
	delay_ns(1);
    DDRA=0XFF;
	PORTA=data;
	delay_ns(3);
    LCD_e_LOW();
	delay_ns(4);
	LCD_e_HIGH();
	delay_ns(4); 
	LCD_e_LOW();
   // sei();
}




unsigned char status_read(void)
{ 
    unsigned char i,lcdregister;
   
	DDRA=0X00;
	for(i=0;i<50;i++)
    {
    LCDCMD_low();
    delay_ns(4);
	LCD_WR_HIGH();
	WatchdogReset();
	LCD_e_HIGH();
	delay_ns(4);
	lcdregister=PINA;
	LCD_e_LOW();
   	if((lcdregister&0x80)==0)
	{
	DDRA=0X0ff;
	return TRUE;
	}
	}
	DDRA=0X0ff;
  	return FALSE;
}

void hz_char(void)
{
//  pgm_read_byte(&char_table[char_code][row_count*2]);

}

unsigned char display_on_hzchar(unsigned char row,unsigned char column,unsigned char char_code,unsigned char negitive)
{
	unsigned char page_int,temp_buf,page_div,temp_row,temp_columnh,temp_columnl;
	unsigned int temp;
	if(row>64)
	return FALSE;
	if(column>192)
	return FALSE;
	cli();
//	commd_write(0X3e);
	temp_buf = column;
	row = 64 -row-16;
	column = (192 - column)-16;
    lcd_cs_screen(column);

	if(negitive==0)
  {
    temp=(char_code-0x5f)*2+0x5f;
    for(page_int=0;page_int<32;page_int++)
	{
	WatchdogReset();
	display_temp[page_int]=pgm_read_byte(&char_table[temp][page_int]);
	}
	temp_row=0;
    page_int=0xb8|(row/8);
	temp_columnl=0x40|(column%64);

    commd_write(page_int++); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);
	temp_columnh=temp_buf;
	for(page_div=0;page_div<16;page_div++)
	{
	  data_write(display_temp[temp_row++]);
	  temp_columnh = 177-temp_buf + page_div;
	  if((temp_columnh%64)==0x0)
	  {
	  nop();
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int-1));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
	}

    lcd_cs_screen(column);
    commd_write(page_int); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);
	temp_columnh=temp_buf;
	for(page_div=0;page_div<16;page_div++)
	{
	  data_write(display_temp[temp_row++]);
	  temp_columnh = 177-temp_buf + page_div;
	  if((temp_columnh%64)==0x0)
	  {
	  nop();
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
	}
   }
   else
   {
   temp=(char_code-0x5f)*2+0x5f;
    for(page_int=0;page_int<32;page_int++)
	{
	WatchdogReset();
	display_temp[page_int]=pgm_read_byte(&char_table[temp][page_int]);
	}
	temp_row=0;
    page_int=0xb8|(row/8);
	temp_columnl=0x40|(column%64);

    commd_write(page_int++); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);
		temp_columnh=temp_buf;
	for(page_div=0;page_div<16;page_div++)
	{
	  data_write(~display_temp[temp_row++]);
	 // data_write(display_temp[temp_row++]);
	  temp_columnh = 177-temp_buf + page_div;
	  if((temp_columnh%64)==0x0)
	  {
	  nop();
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int-1));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
      
	}

    lcd_cs_screen(column);
    commd_write(page_int); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);
	temp_columnh=temp_buf;
	for(page_div=0;page_div<16;page_div++)
	{
	  data_write(~display_temp[temp_row++]);
	// data_write(display_temp[temp_row++]);
	  temp_columnh = 177-temp_buf + page_div;
	  if((temp_columnh%64)==0x0)
	  {
	  nop();
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
	}
   
   }
     //commd_write(0X3f);
	 sei();
	 return TRUE;
}

unsigned char display_on_char(unsigned char row,unsigned char column,unsigned char char_code,unsigned char negitive)
{
	unsigned char temp1,page_int,page_div,temp_row,temp_columnh,temp_columnl;
	unsigned char temp_buf;
    

	if(row>64)
	return FALSE;
	if(column>192)
	return FALSE;
	cli();
//	commd_write(0X3e);
	temp_buf = column;
	row = 64 -row-16;
	column = (192 - column)-8;
    lcd_cs_screen(column);
	if(negitive==0)
  {
    
    for(temp1=0;temp1<16;temp1++)
	{
	display_temp[temp1]=pgm_read_byte(&char_table[char_code][temp1]);
	}
	temp_row=0;
    page_int=0xb8|(row/8);
	temp_columnl=0x40|(column%64);

    commd_write(page_int++); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);
	temp_columnh=temp_buf;
	for(page_div=0;page_div<8;page_div++)
	{
	  data_write(display_temp[temp_row++]);
	  temp_columnh = 185-temp_buf + page_div;
	  if((temp_columnh%64)==0x0)
	  {
	  nop();
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int-1));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
      
	}
	lcd_cs_screen(column);
	commd_write(page_int); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);
	temp_columnh=temp_buf;
	for(page_div=0;page_div<8;page_div++)
	{
	 temp_columnh = 185-temp_buf + page_div;
	  data_write(display_temp[temp_row++]);
	  if((temp_columnh%64)==0x0)
	  {
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
     
	}

   }
   else
   {
       for(temp1=0;temp1<16;temp1++)
	{
	display_temp[temp1]=pgm_read_byte(&char_table[char_code][temp1]);
	}
    temp_row=0;
    page_int=0xb8|(row/8);
	temp_columnl=0x40|(column%64);

    commd_write(page_int++); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);

	temp_columnh=temp_buf;
	for(page_div=0;page_div<8;page_div++)
	{
	
	  data_write(~display_temp[temp_row++]);
	   temp_columnh = 185-temp_buf + page_div;
	  if((temp_columnh%64)==0x0)
	  {
	  nop();
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int-1));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
      
	}
	lcd_cs_screen(column);
	commd_write(page_int); //SET PAGE ADDRESS
    commd_write(0Xc0);
    commd_write(temp_columnl);
	temp_columnh=temp_buf;
	for(page_div=0;page_div<8;page_div++)
	{
	  data_write(~display_temp[temp_row++]);
	  temp_columnh = 185-temp_buf + page_div;
	  if((temp_columnh%64)==0x0)
	  {
	  nop();
	  lcd_cs_screen(temp_columnh);
	  commd_write((page_int));
	  commd_write(0Xc0);
	  temp_columnh=0x40|(0);
	  commd_write(temp_columnh);
	  }
	}
   
   }
     //commd_write(0X3f);
	 sei();
	 return TRUE;
}


void lcd_cs_screen(unsigned char colomn)
{
	
  unsigned char temp;

  temp = colomn;
  if(temp >127)
  {
  
   LCD_csa_HIGH();
  LCD_csb_LOW();
 
  }
  else if( (temp>63)&&(temp<128))
  {
 // LCD_csa_HIGH();
// LCD_csb_LOW();
  LCD_csa_LOW();
   LCD_csb_HIGH(); 
  }
  else if(temp<64)
  {
   LCD_csa_LOW();
   LCD_csb_LOW();
  
  }
  else
  {
    LCD_csa_HIGH();
    LCD_csb_HIGH();
  }
  

}




void Test_character(void)
{
  unsigned char i,j,k,l;
  unsigned char character[]={
                         0x00,0x00,0x00,0x38,0x44,0x5A,0xAA,0xAA,
						 0xAA,0xAA,0xB4,0x42,0x44,0x38,0x00,0x00,
                         0X40,0X3F,0X15,0X15,0X7F,0X00,0X00,0X00,//要显示的字符代码
                         0X00,0X24,0X2A,0X7F,0X2A,0X12,0X00,0X00,//要显示的字符代码
                         0X00,0X00,0X32,0X49,0X79,0X41,0X3E,0X00
                       };
  for(i=0;i<3;i++) 
  {
    commd_write(0XB0); /*SET PAGE ADDRESS*/
    commd_write(0X10); /*SET COLUMN ADDRESS MSB(00000000)*/
    commd_write(0X00); /*SET COLUMN ADDRESS LSB(00000000)*/
    commd_write(0X89); /*SET RAM ADDRESS CONTROL*/
    for(j=0;j<16;j++)
      for(k=0;k<30;k++)
        for(l=0;l<8;l++)
          data_write(character[i*8+l]);
    
 
    //Delay(1000);
  }
}


void clr_screean(void)
{
unsigned char i,j,k,l;
cli();
  for(i = 0xb8; i<0xc0; i++) 
  {
    LCD_csa_LOW();
    LCD_csb_LOW();
    commd_write(0Xc0);
    commd_write(i);
    commd_write(0X40); 
   
	
     for(j=0;j<0x40;j++)
     {
	  data_write(0);
	 }
	  LCD_csa_HIGH();
      LCD_csb_LOW();
     commd_write(0Xc0);
     commd_write(i);
     commd_write(0X40); 
    
     for(j=0;j<0x40;j++)
     {
	  data_write(0);
	 }
	 LCD_csa_LOW();
     LCD_csb_HIGH();
     commd_write(0Xc0);
     commd_write(i);
     commd_write(0X40); 
      for(j=0;j<0x40;j++)
     {
	  data_write(0);
	 }
  
  }
sei();
}
/*****************************/













⌨️ 快捷键说明

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